This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Our use of Flask-Restful's marshalling functionality is hackish. There exists significant support for moving Flask-Restful's internal marshalling to Marshmallow [1], so we should switch to that. Flask-Marshmallow is used for convenience functions (e.g. `jsonify`). This changeset is strictly a port from Flask-Restful's builtin marshalling mechanism to Marshmallow. TODOs and FIXMEs still exist and should be addressed in a separate change. Flask-Marshmallow relies on changes to `flask.jsonify` made in Flask 0.11, so our version must be upgraded in order to use it. 0.11.1 is the latest stable release of Flask. [1] flask-restful/flask-restful#335
- Loading branch information
Showing
8 changed files
with
111 additions
and
112 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,5 @@ | ||
"""Core API application components.""" | ||
|
||
from flask_marshmallow import Marshmallow | ||
|
||
marshmallow = Marshmallow() |
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 was deleted.
Oops, something went wrong.
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,87 @@ | ||
"""Fieldsets rendered via Flask-Restful.""" | ||
|
||
from pygotham.models import Event, User, Talk | ||
|
||
from .core import marshmallow | ||
|
||
__all__ = ('EventSchema', 'UserSchema', 'TalkSchema') | ||
|
||
|
||
class EventSchema(marshmallow.Schema): | ||
"""Serialization rules for Event objects.""" | ||
|
||
class Meta: | ||
model = Event | ||
fields = ('id', 'begins', 'ends', 'name', 'registration_url', 'slug') | ||
|
||
|
||
class UserSchema(marshmallow.Schema): | ||
"""Serialization rules for User objects.""" | ||
|
||
class Meta: | ||
model = User | ||
additional = ('id', 'bio', 'name') | ||
|
||
email = marshmallow.Function(lambda user: '<redacted>') | ||
picture_url = marshmallow.Function(lambda user: None) | ||
twitter_id = marshmallow.Function(lambda user: user.twitter_handle) | ||
|
||
|
||
class TalkSchema(marshmallow.Schema): | ||
"""Serialization rules for Talk objects.""" | ||
|
||
class Meta: | ||
model = Talk | ||
additional = ('id', 'description') | ||
|
||
conf_key = marshmallow.Function(lambda talk: talk.id) | ||
duration = marshmallow.Function(lambda talk: talk.duration.duration) | ||
language = marshmallow.Function(lambda talk: 'English') | ||
# FIXME: What version are the talks to be licensed under? | ||
license = marshmallow.Function(lambda talk: 'Creative Commons') | ||
priority = marshmallow.Method('get_recording_priority') | ||
released = marshmallow.Function(lambda talk: talk.recording_release) | ||
# TODO: Replace this with a nested SlotSchema. | ||
room = marshmallow.Method('get_room') | ||
room_alias = marshmallow.Method('get_room') | ||
# TODO: Replace this with a nested SlotSchema. | ||
start = marshmallow.Method('get_start_time') | ||
summary = marshmallow.Function(lambda talk: talk.description) | ||
tags = marshmallow.Function(lambda talk: []) | ||
title = marshmallow.Function(lambda talk: talk.name) | ||
user = marshmallow.Nested(UserSchema) | ||
|
||
def get_recording_priority(self, talk): | ||
"""Get the numerical recording priority for a talk. | ||
Args: | ||
talk (pygotham.talks.models.Talk): The talk. | ||
""" | ||
# HACK: Generate the recording priority based on recording | ||
# release. We probably won't have any 5s, but this is about as | ||
# correct as the mapping can be at the moment. | ||
priority_mapping = {True: 9, False: 0, None: 5} | ||
return priority_mapping[talk.recording_release] | ||
|
||
def get_room(self, talk): | ||
"""Get a one line representation of a talk's scheduled room. | ||
In most cases, talks are in one room. Occasionally, however, a | ||
talk may span multiple rooms. When that happens, return a | ||
descriptive string combining room names. | ||
Args: | ||
talk (pygotham.talks.models.Talk): The talk. | ||
""" | ||
return ' & '.join(room.name for room in talk.presentation.slot.rooms) | ||
|
||
def get_start_time(self, talk): | ||
"""Return an IOS8601 formatted start time. | ||
Args: | ||
talk (pygotham.talks.models.Talk): The talk. | ||
""" | ||
return '{:%Y-%m-%d}T{:%H:%M:%S}'.format( | ||
talk.presentation.slot.day.date, | ||
talk.presentation.slot.start, | ||
) |
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