File: api.py

package info (click to toggle)
flask-peewee 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,100 kB
  • sloc: javascript: 9,322; python: 4,053; makefile: 132; sh: 16
file content (48 lines) | stat: -rw-r--r-- 1,253 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from flask_peewee.rest import RestAPI, RestResource, UserAuthentication, AdminAuthentication, RestrictOwnerResource

from app import app
from auth import auth
from models import User, Message, Note, Relationship


user_auth = UserAuthentication(auth)
admin_auth = AdminAuthentication(auth)

# instantiate our api wrapper
api = RestAPI(app, default_auth=user_auth)


class UserResource(RestResource):
    exclude = ('password', 'email',)


class MessageResource(RestrictOwnerResource):
    owner_field = 'user'
    include_resources = {'user': UserResource}


class RelationshipResource(RestrictOwnerResource):
    owner_field = 'from_user'
    include_resources = {
        'from_user': UserResource,
        'to_user': UserResource,
    }
    paginate_by = None


class NoteResource(RestrictOwnerResource):
    owner_field = 'user'
    include_resources = {
        'user': UserResource,
    }

    def get_query(self):
        query = super(NoteResource, self).get_query()
        return query.where(Note.status == 1)


# register our models so they are exposed via /api/<model>/
api.register(User, UserResource, auth=admin_auth)
api.register(Relationship, RelationshipResource)
api.register(Message, MessageResource)
api.register(Note, NoteResource)