Skip to content

Commit 474435b

Browse files
committed
List activities working example
1 parent 8e46ce8 commit 474435b

File tree

5 files changed

+97
-51
lines changed

5 files changed

+97
-51
lines changed

activities/__init__.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,19 @@
22
from flask import Flask
33
from mongoalchemy.session import Session
44
from documents import Activity
5-
from flask_mongoengine import MongoEngine
6-
from flask_mongorest import MongoRest
5+
from flask_cors import CORS
76

87
with open('config.yml', 'r') as f:
98
config_file = f.read()
109

1110
config = yaml.load(config_file)
1211

1312
app = Flask(__name__)
14-
app.debug = config.get('app', {}).get('debug', False)
15-
# app.config.update(
16-
# MONGODB_HOST=config.get('mongo', {}).get('hostname'),
17-
# MONGODB_PORT=config.get('mongo', {}).get('port'),
18-
# MONGODB_DB=config.get('mongo', {}).get('database'),
19-
# )
13+
cors = CORS(app)
2014

21-
# db = MongoEngine(app)
22-
# api = MongoRest(app)
15+
app.debug = config.get('app', {}).get('debug', False)
16+
app.config['CORS_HEADERS'] = 'Content-Type'
2317

2418
session = Session.connect(config.get('mongo', {}).get('database'))
2519

26-
27-
@app.route('/activities')
28-
def list_activities():
29-
activities = session.query(Activity).filter(Activity.user_id == 12)
30-
31-
# print activities.one()
32-
33-
for activity in activities:
34-
print activity
35-
36-
return 'test'
37-
20+
import activities

activities/activities.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from flask import jsonify, request
2+
from . import app, session
3+
from documents import Activity
4+
from pymongo import DESCENDING
5+
from flask_cors import cross_origin
6+
7+
PAGE_SIZE = 15
8+
9+
10+
@app.route('/activities')
11+
@cross_origin()
12+
def list_activities():
13+
try:
14+
page = int(request.args.get('page', 0))
15+
assert page >= 0
16+
except (ValueError, AssertionError):
17+
page = 0
18+
19+
activities = session.query(Activity).filter(Activity.user_id == 12).sort((Activity.created_at, DESCENDING)) \
20+
.limit(PAGE_SIZE).skip(page * PAGE_SIZE)
21+
22+
meta = {
23+
'total': activities.count(),
24+
'offset': page * PAGE_SIZE,
25+
'count': activities.count(with_limit_and_skip=True)
26+
}
27+
28+
# res = [activity.to_dict() for activity in activities]
29+
res = map(lambda activity: activity.to_dict(), activities)
30+
31+
return jsonify(results=res, meta=meta)

activities/documents.py

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class Activity(Document):
66
config_collection_name = 'activity'
77
config_polymorphic = 'type'
88
config_polymorphic_collection = True
9-
config_extra_fields = 'ignore'
109

1110
type = StringField()
1211
performed_by = StringField()
@@ -22,8 +21,19 @@ def __str__(self):
2221
class Like(Activity):
2322
config_polymorphic_identity = 'like'
2423

25-
def __str__(self):
26-
return '%s: like by %s' % (self.created_at, self.performed_by)
24+
post_id = IntField()
25+
post_image = StringField()
26+
27+
def to_dict(self):
28+
return {
29+
'type': self.type,
30+
'performed_by': self.performed_by,
31+
'performer_avatar': self.performer_avatar,
32+
'post_id': self.post_id,
33+
'post_image': self.post_image,
34+
'created_at': self.created_at.isoformat(),
35+
'clicked': self.clicked
36+
}
2737

2838

2939
class Comment(Activity):
@@ -32,8 +42,16 @@ class Comment(Activity):
3242
post_id = IntField()
3343
post_image = StringField()
3444

35-
def __str__(self):
36-
return '%s: comment by %s' % (self.created_at, self.performed_by)
45+
def to_dict(self):
46+
return {
47+
'type': self.type,
48+
'performed_by': self.performed_by,
49+
'performer_avatar': self.performer_avatar,
50+
'post_id': self.post_id,
51+
'post_image': self.post_image,
52+
'created_at': self.created_at.isoformat(),
53+
'clicked': self.clicked
54+
}
3755

3856

3957
class Repost(Activity):
@@ -42,15 +60,29 @@ class Repost(Activity):
4260
post_id = IntField()
4361
post_image = StringField()
4462

45-
def __str__(self):
46-
return '%s: repost by %s' % (self.created_at, self.performed_by)
63+
def to_dict(self):
64+
return {
65+
'type': self.type,
66+
'performed_by': self.performed_by,
67+
'performer_avatar': self.performer_avatar,
68+
'post_id': self.post_id,
69+
'post_image': self.post_image,
70+
'created_at': self.created_at.isoformat(),
71+
'clicked': self.clicked
72+
}
4773

4874

4975
class Follow(Activity):
5076
config_polymorphic_identity = 'follow'
5177

52-
def __str__(self):
53-
return '%s: follow by %s' % (self.created_at, self.performed_by)
78+
def to_dict(self):
79+
return {
80+
'type': self.type,
81+
'performed_by': self.performed_by,
82+
'performer_avatar': self.performer_avatar,
83+
'created_at': self.created_at.isoformat(),
84+
'clicked': self.clicked
85+
}
5486

5587

5688
class Mention(Activity):
@@ -59,8 +91,16 @@ class Mention(Activity):
5991
post_id = IntField()
6092
post_image = StringField()
6193

62-
def __str__(self):
63-
return '%s: mention by %s' % (self.created_at, self.performed_by)
94+
def to_dict(self):
95+
return {
96+
'type': self.type,
97+
'performed_by': self.performed_by,
98+
'performer_avatar': self.performer_avatar,
99+
'post_id': self.post_id,
100+
'post_image': self.post_image,
101+
'created_at': self.created_at.isoformat(),
102+
'clicked': self.clicked
103+
}
64104

65105

66106
class Badge(Activity):
@@ -69,5 +109,13 @@ class Badge(Activity):
69109
badge_name = StringField()
70110
badge_image = StringField()
71111

72-
def __str__(self):
73-
return '%s: badge by %s' % (self.created_at, self.performed_by)
112+
def to_dict(self):
113+
return {
114+
'type': self.type,
115+
'performed_by': self.performed_by,
116+
'performer_avatar': self.performer_avatar,
117+
'badge_name': self.badge_name,
118+
'badge_image': self.badge_image,
119+
'created_at': self.created_at.isoformat(),
120+
'clicked': self.clicked
121+
}

activities/resources.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

activities/views.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)