-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |