diff --git a/app/__init__.py b/app/__init__.py index 42e25f3..2b39417 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -15,9 +15,11 @@ def create_app(config): from app.v1.controllers.libraries import libraries_api from app.v1.controllers.maps import maps_api + from app.v1.controllers.speakers import speakers_api _app.register_blueprint(libraries_api, url_prefix='/api/v1') _app.register_blueprint(maps_api, url_prefix='/api/v1') + _app.register_blueprint(speakers_api, url_prefix='/api/v1') db.init_app(_app) Migrate(_app, db) diff --git a/app/v1/controllers/libraries.py b/app/v1/controllers/libraries.py index 8450eeb..cc73630 100644 --- a/app/v1/controllers/libraries.py +++ b/app/v1/controllers/libraries.py @@ -34,67 +34,67 @@ } -library_reqparse = reqparse.RequestParser() -library_reqparse.add_argument('name', type=str, trim=True, - location=['form', 'json'], - required=True, nullable=False, - help='No library name provided') -library_reqparse.add_argument('location_road', type=str, trim=True, - location=['form', 'json'], - required=True, nullable=False, - help='No library location_road provided') -library_reqparse.add_argument('location_number', type=str, trim=True, - location=['form', 'json'], - required=True, nullable=False, - help='No library location_number provided') -library_reqparse.add_argument('location_detail', type=str, trim=True, - location=['form', 'json'], - required=False) - -library_reqparse.add_argument('manager_name', type=str, trim=True, - location=['form', 'json'], - required=True, nullable=False, - help='No library manager_name provided') -library_reqparse.add_argument('manager_email', type=str, trim=True, - location=['form', 'json'], - required=True, nullable=False, - help='No library manager_email provided') -library_reqparse.add_argument('manager_phone', type=str, trim=True, - location=['form', 'json'], - required=True, nullable=False, - help='No library manager_phone provided') -library_reqparse.add_argument('audiences', type=str, trim=True, - location=['form', 'json'], - required=True, nullable=False, - help='No library audiences provided') - -library_reqparse.add_argument('fac_beam_screen', type=bool, - location=['form', 'json'], - required=True, nullable=False, - help='No library fac_beam_screen provided') -library_reqparse.add_argument('fac_sound', type=bool, - location=['form', 'json'], - required=True, nullable=False, - help='No library fac_sound provided') -library_reqparse.add_argument('fac_record', type=bool, - location=['form', 'json'], - required=True, nullable=False, - help='No library fac_record provided') -library_reqparse.add_argument('fac_placard', type=bool, - location=['form', 'json'], - required=True, nullable=False, - help='No library fac_placard provided') -library_reqparse.add_argument('fac_self_promo', type=bool, - location=['form', 'json'], - required=True, nullable=False, - help='No library fac_self_promo provided') - -library_reqparse.add_argument('fac_other', type=str, trim=True, - location=['form', 'json'], - required=False) -library_reqparse.add_argument('req_speaker', type=str, trim=True, - location=['form', 'json'], - required=False) +library_reqparser = reqparse.RequestParser() +library_reqparser.add_argument('name', type=str, trim=True, + location=['form', 'json'], + required=True, nullable=False, + help='No library name provided') +library_reqparser.add_argument('location_road', type=str, trim=True, + location=['form', 'json'], + required=True, nullable=False, + help='No library location_road provided') +library_reqparser.add_argument('location_number', type=str, trim=True, + location=['form', 'json'], + required=True, nullable=False, + help='No library location_number provided') +library_reqparser.add_argument('location_detail', type=str, trim=True, + location=['form', 'json'], + required=False) + +library_reqparser.add_argument('manager_name', type=str, trim=True, + location=['form', 'json'], + required=True, nullable=False, + help='No library manager_name provided') +library_reqparser.add_argument('manager_email', type=str, trim=True, + location=['form', 'json'], + required=True, nullable=False, + help='No library manager_email provided') +library_reqparser.add_argument('manager_phone', type=str, trim=True, + location=['form', 'json'], + required=True, nullable=False, + help='No library manager_phone provided') +library_reqparser.add_argument('audiences', type=str, trim=True, + location=['form', 'json'], + required=True, nullable=False, + help='No library audiences provided') + +library_reqparser.add_argument('fac_beam_screen', type=bool, + location=['form', 'json'], + required=True, nullable=False, + help='No library fac_beam_screen provided') +library_reqparser.add_argument('fac_sound', type=bool, + location=['form', 'json'], + required=True, nullable=False, + help='No library fac_sound provided') +library_reqparser.add_argument('fac_record', type=bool, + location=['form', 'json'], + required=True, nullable=False, + help='No library fac_record provided') +library_reqparser.add_argument('fac_placard', type=bool, + location=['form', 'json'], + required=True, nullable=False, + help='No library fac_placard provided') +library_reqparser.add_argument('fac_self_promo', type=bool, + location=['form', 'json'], + required=True, nullable=False, + help='No library fac_self_promo provided') + +library_reqparser.add_argument('fac_other', type=str, trim=True, + location=['form', 'json'], + required=False) +library_reqparser.add_argument('req_speaker', type=str, trim=True, + location=['form', 'json'], + required=False) class LibraryListResource(Resource): @@ -112,7 +112,7 @@ def get(self): @marshal_with(library_fields) def post(self): - args = library_reqparse.parse_args() + args = library_reqparser.parse_args() library = Library(**args) @@ -143,7 +143,7 @@ def get(self, pk): @marshal_with(library_fields) def put(self, pk): - args = library_reqparse.parse_args() + args = library_reqparser.parse_args() library = get_or_404(Library, pk) for key, value in args.items(): diff --git a/app/v1/controllers/speakers.py b/app/v1/controllers/speakers.py new file mode 100644 index 0000000..b48cf23 --- /dev/null +++ b/app/v1/controllers/speakers.py @@ -0,0 +1,161 @@ +from flask import Blueprint +from flask_restful import (Resource, reqparse, fields, + marshal_with, Api, marshal) +from sqlalchemy.exc import IntegrityError + +from app.database import db +from app.database.models import Speaker, session_time_choices +from app.utils.errors import DuplicatedDataError +from app.v1.controllers import get_or_404, get_is_admin + + +speaker_fields = { + '_id': fields.Integer, + 'name': fields.String, + + 'is_email_verified': fields.Boolean, + 'email_sended_at': fields.DateTime, + + 'session_time': fields.String, + + 'library_id': fields.Integer, + 'created_at': fields.DateTime, + 'updated_at': fields.DateTime, +} + + +speaker_fields_including_protected = { + **speaker_fields, + 'email': fields.String, + 'phone': fields.String, +} + + +speaker_reqparser = reqparse.RequestParser() +speaker_reqparser.add_argument('name', type=str, trim=True, + location=['form', 'json'], + required=True, nullable=False, + help='No speaker name provided') +speaker_reqparser.add_argument('email', type=str, trim=True, + location=['form', 'json'], + required=True, nullable=False, + help='No speaker email provided') +speaker_reqparser.add_argument('phone', type=str, trim=True, + location=['form', 'json'], + required=True, nullable=False, + help='No speaker phone provided') + +speaker_reqparser.add_argument('password', type=str, + location=['form', 'json'], + required=True, nullable=False, + help='No speaker phone provided') +speaker_reqparser.add_argument('session_time', + choices=session_time_choices, + location=['form', 'json'], + required=True, nullable=False, + help='No speaker session_time provided') +speaker_reqparser.add_argument('library_id', type=int, + location=['form', 'json'], + required=True, nullable=False, + help='No speaker library_id provided') + + +def get_is_identified(): + return True + + +class SpeakerListResource(Resource): + def __init__(self): + super().__init__() + + def get(self): + speakers = Speaker.query.all() + + resp_fields = speaker_fields + if get_is_admin() or get_is_identified(): + resp_fields = speaker_fields_including_protected + + return marshal(speakers, resp_fields) + + def post(self): + args = speaker_reqparser.parse_args() + + speaker = Speaker(**args) + + try: + db.session.add(speaker) + db.session.commit() + except IntegrityError as e: + print(str(e)) + db.session.rollback() + raise DuplicatedDataError(str(e.orig)) + except Exception as e: + print(str(e)) + db.session.rollback() + raise e + + return marshal(speaker, speaker_fields_including_protected) + + +class SpeakerResource(Resource): + def get(self, pk): + speaker = get_or_404(Speaker, pk) + + resp_fields = speaker_fields + if get_is_admin(): + resp_fields = speaker_fields_including_protected + + return marshal(speaker, resp_fields) + + @marshal_with(speaker_fields) + def put(self, pk): + args = speaker_reqparser.parse_args() + speaker = get_or_404(Speaker, pk) + + for key, value in args.items(): + if value is None: + continue + + setattr(speaker, key, value) + + try: + db.session.merge(speaker) + db.session.commit() + except IntegrityError as e: + print(str(e)) + db.session.rollback() + raise DuplicatedDataError(str(e.orig)) + except Exception as e: + print(str(e)) + db.session.rollback() + raise e + + return speaker + + def delete(self, pk): + speaker = get_or_404(Speaker, pk) + + try: + db.session.delete(speaker) + db.session.commit() + except Exception as e: + print(str(e)) + db.session.rollback() + raise e + + return '', 204 + + +speakers_api = Blueprint('resources.speakers', __name__) +api = Api(speakers_api) +api.add_resource( + SpeakerListResource, + '/speaker', + endpoint='speakers' +) + +api.add_resource( + SpeakerResource, + '/speaker/', + endpoint='speaker' +)