Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Add Sponsors to the API
Browse files Browse the repository at this point in the history
Fixes #308.
  • Loading branch information
jonafato committed Jul 4, 2016
1 parent 7917284 commit 5781d01
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
25 changes: 23 additions & 2 deletions pygotham/api/schema.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Fieldsets rendered via Flask-Restful."""

from pygotham.models import Event, User, Talk
from pygotham.models import Event, Level, Sponsor, Talk, User

from .core import marshmallow

__all__ = ('EventSchema', 'UserSchema', 'TalkSchema')
__all__ = ('EventSchema', 'SponsorSchema', 'TalkSchema', 'UserSchema')


class EventSchema(marshmallow.Schema):
Expand All @@ -27,6 +27,27 @@ class Meta:
twitter_id = marshmallow.Function(lambda user: user.twitter_handle)


class LevelSchema(marshmallow.Schema):
"""Serialization rules for sponsorship Level objects."""

class Meta:
model = Level
# The highest sponsorship tiers have the lowest order.
# TODO: Have a real place to put documentation like the comment
# above.
additional = ('name', 'order')


class SponsorSchema(marshmallow.Schema):
"""Serialization rules for Sponsor objects."""

class Meta:
model = Sponsor
additional = ('description', 'logo', 'name', 'url')

level = marshmallow.Nested(LevelSchema)


class TalkSchema(marshmallow.Schema):
"""Serialization rules for Talk objects."""

Expand Down
32 changes: 32 additions & 0 deletions pygotham/api/sponsors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Sponsor-related API endpoints."""

from flask import Blueprint
from flask_restful import Api, Resource

from pygotham.events.models import Event
from pygotham.sponsors.models import Level, Sponsor

from .schema import SponsorSchema

blueprint = Blueprint(
'sponsors',
__name__,
subdomain='<event_slug>',
url_prefix='/events/<int:event_id>/sponsors',
)
api = Api(blueprint)


@api.resource('/')
class SponsorResource(Resource):
"""Represents event sponsors."""

def get(self, event_id):
"""Return a list of accepted sponsors."""
event = Event.query.get_or_404(event_id)
sponsors = Sponsor.query.join(Level).filter(
Sponsor.accepted == True, # NOQA
Level.event == event,
)
schema = SponsorSchema(many=True)
return schema.jsonify(sponsors)

0 comments on commit 5781d01

Please sign in to comment.