Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
Add Speakers CRUD Resource (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
kujyp committed Apr 12, 2018
1 parent 444b6eb commit 33ee920
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 63 deletions.
2 changes: 2 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
126 changes: 63 additions & 63 deletions app/v1/controllers/libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand Down Expand Up @@ -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():
Expand Down
161 changes: 161 additions & 0 deletions app/v1/controllers/speakers.py
Original file line number Diff line number Diff line change
@@ -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/<int:pk>',
endpoint='speaker'
)

0 comments on commit 33ee920

Please sign in to comment.