Skip to content

Commit

Permalink
Add POST method to create newdles
Browse files Browse the repository at this point in the history
  • Loading branch information
pferreir committed Aug 5, 2019
1 parent d43257d commit a05dc32
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
20 changes: 19 additions & 1 deletion newdle/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from werkzeug.exceptions import UnprocessableEntity

from .core.auth import user_info_from_app_token
from .core.db import db
from .core.webargs import use_kwargs
from .schemas import UserSchema, UserSearchResultSchema
from .models import Newdle, Participant
from .schemas import NewdleSchema, NewNewdleSchema, UserSchema, UserSearchResultSchema


api = Blueprint('api', __name__, url_prefix='/api')
Expand Down Expand Up @@ -78,3 +80,19 @@ def users(q):
'total': len(data),
'users': UserSearchResultSchema(many=True).dump(data[:10]),
}


@api.route('/newdle/', methods=('POST',))
@use_kwargs(NewNewdleSchema(), locations=('json',))
def newdle(title, duration, timezone, time_slots, participants):
newdle = Newdle(
title=title,
creator_uid=g.user['uid'],
duration=duration,
timezone=timezone,
time_slots=time_slots,
participants={Participant(**p, newdle=newdle) for p in participants},
)
db.session.add(newdle)
db.session.commit()
return NewdleSchema().jsonify(newdle)
27 changes: 27 additions & 0 deletions newdle/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from marshmallow import fields, post_dump
from pytz import common_timezones_set

from .core.marshmallow import mm
from .core.util import DATETIME_FORMAT


class UserSchema(mm.Schema):
Expand All @@ -19,3 +21,28 @@ def sort_users(self, data, many, **kwargs):
if many:
data = sorted(data, key=lambda x: x['name'].lower())
return data


class ParticipantSchema(mm.Schema):
name = fields.String(required=True)
email = fields.String()
auth_uid = fields.String()


class SlotSchema(mm.Schema):
start = fields.DateTime(required=True, format=DATETIME_FORMAT)
end = fields.DateTime(required=True, format=DATETIME_FORMAT)


class NewNewdleSchema(mm.Schema):
title = fields.String(validate=lambda x: len(x) >= 3, required=True)
duration = fields.Int(required=True, validate=lambda x: x % 15 == 0)
timezone = fields.String(
validate=lambda x: x in common_timezones_set, required=True
)
time_slots = fields.List(fields.Nested(SlotSchema), validate=bool, required=True)
participants = fields.List(fields.Nested(ParticipantSchema), missing=[])


class NewdleSchema(NewNewdleSchema):
id = fields.Integer()
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ norecursedirs =
.*
.venv
node_modules
newdle/client
addopts = -rsfEw --cov newdle --cov-report html --no-cov-on-fail -p no:warnings
python_files = *_test.py

0 comments on commit a05dc32

Please sign in to comment.