Skip to content

Commit

Permalink
Merge bfbd17d into 5a11e03
Browse files Browse the repository at this point in the history
  • Loading branch information
mugoh committed Jan 11, 2019
2 parents 5a11e03 + bfbd17d commit 14c8fb1
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from app.v1.views.user import UsersRegistration, UserLogin, UserLogout
from app.v1.views.meetups import Meetups, MeetUp, MeetUpItem
from app.v1.views.questions import Question, Questions, QuestionVote
from app.v1.views.rsvp import Rsvps

auth_blueprint = Blueprint("auth", __name__, url_prefix='/api/v1/auth/')
app_blueprint = Blueprint("app", __name__, url_prefix='/api/v1/')
Expand All @@ -22,3 +23,4 @@
app_api.add_resource(Question, 'questions/<int:id>')
app_api.add_resource(QuestionVote, 'questions/<int:id>/<vote>',
methods=['PATCH'])
app_api.add_resource(Rsvps, 'meetups/<int:id>/<response>')
56 changes: 56 additions & 0 deletions app/v1/models/rsvp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from .abstract_model import AbstractModel


class RsvpModel(AbstractModel):

def __init__(self, **kwargs):

super().__init__(rsvps)
self.user = kwargs['user']
self.meetup = kwargs['meetup']
self.response = kwargs['response']

def save(self):
"""
Saves rsvp instance to the present record
holding all rsvps
"""
rsvps.append(self)

def dictify(self):
"""
Returns a dictionary of the rsvp instance
for readability of the rsvp's fields.
"""

return {
"response": self.response,
"meetup": self.meetup,
"user": self.user,
}

#
# Searches

@classmethod
def get_all_rsvps(cls):
"""
Converts all present rsvp objects to a
dictionary and sends them in a list envelope
"""
return [rsvp.dictify() for rsvp in rsvps]

@classmethod
def verify_unique(cls, rsvp_object):
"""
Helps in ensuring a user does not rsvp for
the meetup twice with the same rsvp data.
"""
return any([rsvp for rsvp in rsvps
if repr(rsvp) == repr(rsvp_object)])

def __repr__(self):
return '{response} {meetup} {user}'.format(**self.dictify())


rsvps = []
71 changes: 71 additions & 0 deletions app/v1/views/rsvp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
This module contains resource views for the
rsvp Resource
"""

from flask_restful import Resource, reqparse
from flask_jwt_extended import jwt_required, get_jwt_identity

from app.v1.models.rsvp import RsvpModel
from ..models.meetups import MeetUpModel
from ..models.users import UserModel


class Rsvps(Resource):

@jwt_required
def post(self, id, response):
"""
Creates an rsvp with refrence to a meetup and the
existing user's
"""

args = {}

# Confirm response is valid

ex = ['yes', 'no', 'maybe']

err_msg = "Your response is not known. Make it: " + \
str(ex[:-1]) + ' or ' + str(ex[-1])

if response not in ex:
return {
"Status": 400,
"Message": err_msg
}, 400

# Confirm existence of the meetup to rsvp

if not MeetUpModel.get_by_id(id):
return {
"Status": 404,
"Message": "That meetup does not exist"
}, 404

user = UserModel.get_by_name(get_jwt_identity())

user_id = getattr(user, 'id')

args.update({
"user": user_id,
"meetup": id,
"response": response
})

# Create rsvp and confirm it's not a duplicate

rsvp = RsvpModel(**args)
if not RsvpModel.verify_unique(rsvp):
rsvp.save()

else:
return {
"Status": 409,
"Message": "You've done that same rsvp already"
}, 409

return {
"Status": 201,
"Data": rsvp.dictify()
}, 201

0 comments on commit 14c8fb1

Please sign in to comment.