-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
167 lines (131 loc) · 5.49 KB
/
views.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from app import app
from flask import request, Response
from flask_login import *
from flask.ext.security import login_required, LoginForm
from authentication import SECRET_KEY, load_user_from_request, check_auth
from models import User, Message
from flask.ext.login import current_user
from stream_client import client
import json
from collections import namedtuple
from flask_cors import *
from utils import default
from realtime import send_push
@app.route('/api/activities', methods=['GET'])
def get_activities():
if not check_auth(request):
return Response(status=403)
# get the carer profile
user = load_user_from_request(request)
user_feed = client.feed('notification:'+str(user.id))
result = user_feed.get()
result = json.dumps(result, default=default)
return Response(result, mimetype='application/json', status=200)
@app.route('/api/follow/<int:seeker_id>', methods=['PUT'])
def follow_feed(seeker_id):
if not check_auth(request):
return Response(status=403)
# get the carer profile
user = load_user_from_request(request)
user_feed = client.feed('notification:'+str(user.id))
user_feed.follow('user:'+str(seeker_id))
return Response(status=200)
@app.route('/api/unfollow/<int:seeker_id>', methods=['PUT'])
def unfollow_feed(seeker_id):
if not check_auth(request):
return Response(status=403)
# get the carer profile
user = load_user_from_request(request)
user_feed = client.feed('notification:'+str(user.id))
user_feed.unfollow('user:'+str(seeker_id))
return Response(status=200)
@app.route('/api/following', methods=['GET'])
def following():
if not check_auth(request):
return Response(status=403)
# get the carer profile
user = load_user_from_request(request)
# get the care seekers that I am following
user_feed = client.feed('notification:'+str(user.id))
care_seekers = user_feed.following()
return Response(json.dumps(care_seekers), mimetype='application/json', status=200)
@app.route('/api/subscription', methods=['POST', 'DELETE'])
def follow():
# first authenticate the follower, otherwise raise 401
if not check_auth(request):
return Response(status=403)
care_seeker_username = request.form.get('care_seeker_username')
care_seeker = User.query.filter_by(username=care_seeker_username).first()
care_seeker_id = care_seeker.id
care_giver_id = current_user.id
my_feed = client.feed('notification:'+str(care_giver_id))
if not care_seeker_id:
return Response(status=400)
if request.method == 'POST':
my_feed.follow('user:'+str(care_seeker_id))
return Response(status=201)
else:
my_feed.unfollow('user:'+str(care_seeker_id))
return Response(status=202)
'''
Claiming a request by a carer consists of marking the request claimed on database
as well as sending a message to the other carers that you'll be the one taking
the request
'''
@app.route('/api/claims/<int:message_id>', methods=['POST'])
# @app.route('/post/<int:message_id>')
def claim(message_id):
if not check_auth(request):
return Response(status=403)
# get the carer profile
user = load_user_from_request(request)
# get the message
message = Message.query.filter_by(message_id=message_id).first()
# debug printing
print "User %s is claiming message from %s saying %s!" %(user.full_name, message.author.full_name, message.text)
# TODO: patch the message and set the claimed flag
# Sending a message to every other carer besides self that I claimed the request
user_feed = client.feed('user:'+str(message.author_id))
author_followers = user_feed.followers()
for each_follower in author_followers.get('results'):
follower_feed = each_follower.get('feed_id')
follower_id = follower_feed.split(':')[-1]
notification_feed = client.feed(follower_feed)
if str(follower_id) == str(user.id):
continue
else:
followers = []
followers.append(follower_feed)
activity = {
'actor': user.id,
'verb': 'claimed',
'object':message.text,
'foreign_id':message_id,
'geolocation':user.current_location,
'to':followers
}
# add specific "TO" notifications, one by one
# print "Adding activity to ", followers
activity_response = notification_feed.add_activity(activity)
# send push notifications
send_push(activity, follower_id)
return Response(status=201)
@app.route("/login", methods=["POST", "GET", "OPTIONS"])
@cross_origin()
def login():
if not check_auth(request):
return Response(status=403)
user = load_user_from_request(request)
user_feed = client.feed('user:'+str(user.id))
# user.token = user_feed.token
UserStruct = namedtuple('UserStruct', 'id, username, email, care_giver, care_seeker, token')
u = UserStruct(id=user.id, username=user.username, email=user.email, care_giver=user.care_giver, care_seeker=user.care_seeker, token=user_feed.token)
# print json.dumps(json.dumps(u))
return Response(json.dumps(u), mimetype='application/json', status=200)
@app.route("/logout", methods=["POST"])
@cross_origin()
@login_required
def logout():
logout_user()
# flash("Logged out successfully.")
return Response(status=200)