-
Notifications
You must be signed in to change notification settings - Fork 87
/
api.py
55 lines (36 loc) · 1.42 KB
/
api.py
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
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from datetime import datetime
from flask import abort
from flask.ext.security import current_user
from udata.api import api, API, marshal
from udata.models import FollowOrg, FollowDataset, Follow, FollowReuse
class FollowAPI(API):
'''
Base Follow Model API.
'''
model = Follow
def post(self, id):
if not current_user.is_authenticated():
abort(401)
follow, created = self.model.objects.get_or_create(follower=current_user.id, following=id)
return '', 201 if created else 200
def delete(self, id):
if not current_user.is_authenticated():
abort(401)
follow = self.model.objects.get_or_404(follower=current_user.id, following=id, until=None)
follow.until = datetime.now()
follow.save()
return '', 204
class FollowUserAPI(FollowAPI):
model = Follow
class FollowOrgAPI(FollowAPI):
model = FollowOrg
class FollowDatasetAPI(FollowAPI):
model = FollowDataset
class FollowReuseAPI(FollowAPI):
model = FollowReuse
api.add_resource(FollowUserAPI, '/follow/user/<id>/', endpoint=b'api.follow_user')
api.add_resource(FollowOrgAPI, '/follow/org/<id>/', endpoint=b'api.follow_org')
api.add_resource(FollowDatasetAPI, '/follow/dataset/<id>/', endpoint=b'api.follow_dataset')
api.add_resource(FollowReuseAPI, '/follow/reuse/<id>/', endpoint=b'api.follow_reuse')