-
Notifications
You must be signed in to change notification settings - Fork 2
/
views.py
289 lines (240 loc) · 10.2 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#-*- encoding: utf-8 -*-
#!/usr/bin/env python
#import django libraries
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from models import CheckIn, UserProfile
from datetime import datetime
from django.utils import timezone
from urllib2 import urlopen
import json
#waterlog.us/checkin?userid=matthewguitar89&time=1111111111111&fqloc=0923840923840948fe3&type=shower
#returns {}, or {'status':'ok'}
#waterlog.us/foursquare?lat=23984729834.432&lon=2938742384.234
#returns
# {'possible_locations': [('manhattan center','123123123fe3'), ('dunkin donuts','123123123123fe3')]}
#waterlog.us/recent?
#returns
# {'recent_checkins':[('matthew', 'toilet'), ('ryan', 'shower')]}
#waterlog.us/profile?userid=matthewguitar89
#returns
# {'picture':'www.picture.com', 'data':[(1,123), (2,234), (3,345)]}
# or
# {'status':'no such person :( '}
def home(request):
c = {}
return render_to_response('index.html', c, context_instance=RequestContext(request))
def fs_profile(request):
if 'userid' not in request.GET:
r = HttpResponse(json.dumps({'status':'no such person :( '}), content_type="application/json")
r['Access-Control-Allow-Origin'] = '*'
return r
photo = json.loads(urlopen("https://api.foursquare.com/v2/users/" + request.GET['userid'] + "?oauth_token=JUNFTM24JVJONNRETUVTVAHJHDJQPQYCD3JCCH5EVRLKABZ0&v=20130427").read())
photo = photo['response']['user']['photo']
photo = (photo['prefix'], photo['suffix'])
data = CheckIn.objects.filter(which_user_id=UserProfile.objects.get(foursquare_profile=request.GET['userid']).pk)
usage = []
running_total = 0
for n, x in enumerate(data):
if x.action_type == 'glass':
running_total += 0.06
# usage.append((n, running_total + 0.06))
if x.action_type == 'washingmachine':
running_total += 35
# usage.append((n, running_total + 35))
if x.action_type == 'toilet':
running_total += 6
# usage.append((n, running_total + 6))
if x.action_type == 'dishwasher':
running_total += 15
# usage.append((n, running_total + 15))
if x.action_type == 'shower':
running_total += 7
# usage.append((n, running_total + 7))
if x.action_type == 'bath':
running_total += 30
usage.append({'x':n, 'y':running_total})
r = HttpResponse(json.dumps({'picture':photo, 'data':usage}), content_type="application/json")
r['Access-Control-Allow-Origin'] = '*'
return r
def foursquare(request):
needed = ['lat', 'long']
for x in needed:
if x not in request.GET:
r = HttpResponse(json.dumps({'status':'error', 'message':'LatLong invalid :('}), content_type="application/json")
r['Access-Control-Allow-Origin'] = '*'
return r
latlong = request.GET['lat'] + ',' + request.GET['long']
fsurl = "https://api.foursquare.com/v2/venues/search?ll=" + latlong
fsurl += "&oauth_token=JUNFTM24JVJONNRETUVTVAHJHDJQPQYCD3JCCH5EVRLKABZ0&v=20130427"
#optional search keyword
fsurl += "&query=" + request.GET['kw'] if 'kw' in request.GET else ''
fsdata = json.loads(urlopen(fsurl).read())
locs = []
for loc in fsdata['response']['venues']:
locs.append({'name':loc['name'], 'id':loc['id']})
r = HttpResponse(json.dumps({'possible_locations':locs}), content_type="application/json")
r['Access-Control-Allow-Origin'] = '*'
return r
def building(request):
if 'foursquare_location_id' not in request.GET:
r = HttpResponse(json.dumps({'status':'invalid id'}), content_type="application/json")
r['Access-Control-Allow-Origin'] = '*'
return r
data = CheckIn.objects.filter(foursquare_location=request.GET['foursquare_location_id'])
usage = []
running_total = 0
for n, x in enumerate(data):
if x.action_type == 'glass':
running_total += 0.06
if x.action_type == 'washingmachine':
running_total += 35
if x.action_type == 'toilet':
running_total += 6
if x.action_type == 'dishwasher':
running_total += 15
if x.action_type == 'shower':
running_total += 7
if x.action_type == 'bath':
running_total += 30
usage.append((n, running_total))
r = HttpResponse(json.dumps({'data':usage}), content_type="application/json")
r['Access-Control-Allow-Origin'] = '*'
return r
def checkin(request):
#check that all variables are present
needed = ['userid', 'time', 'fqloc', 'type']
errors = {}
for var in needed:
if var not in request.GET:
if 'empty_fields' not in errors.keys():
errors['empty_fields'] = []
errors['empty_fields'].append(var)
if errors != {}:
errors['status'] = 'error'
r = HttpResponse(json.dumps(errors), content_type="application/json")
r['Access-Control-Allow-Origin'] = '*'
return r
#check that userid exists
user = UserProfile.objects.filter(foursquare_profile=request.GET['userid'])
if len(user) != 1:
user = create_user(request.GET['userid'])
user = UserProfile.objects.get(pk=user[0])
else:
user = user[0]
if errors != {}:
errors['status'] = 'error'
r = HttpResponse(json.dumps(errors), content_type="application/json")
r['Access-Control-Allow-Origin'] = '*'
return r
else:
#everything is ok
#save record to CheckIn
ci = CheckIn.objects.create(which_user_id=user.pk)
ci.foursquare_location = request.GET['fqloc']
ci.action_type = request.GET['type']
ci.time = datetime.fromtimestamp(float(request.GET['time']))
ci.save()
r = HttpResponse(json.dumps({'status':'ok'}), content_type="application/json")
r['Access-Control-Allow-Origin'] = '*'
r['X-FRAME-OPTIONS'] = "ALLOW_FROM http://appery.io/"
return r
def request_auth(request):
redir = "http://appery.io/app/view/cc7604bf-7a24-471f-84f4-750aeb4afb9b/feed.html"
#redir = "http://appery.io/app/view/cc7604bf-7a24-471f-84f4-750aeb4afb9b/feed.html"
return HttpResponseRedirect("https://foursquare.com/oauth2/authenticate?client_id=L43LFSTUPVMYRPKLV0PDIFIFU0IHDHA5PLSX2CFZUYQRH322&response_type=token&redirect_uri=" + redir)
def letsgo(request):
r = HttpResponse(json.dumps({'status':'sounds good'}), content_type="application/json")
r['Access-Control-Allow-Origin'] = '*'
return r
def create_user(fsprofile):
u = UserProfile.objects.create()
u.time_created = datetime.now()
u.foursquare_profile = fsprofile
user_data = "https://api.foursquare.com/v2/users/" + fsprofile + "?oauth_token=JUNFTM24JVJONNRETUVTVAHJHDJQPQYCD3JCCH5EVRLKABZ0&v=20130428"
u.username = json.loads(urlopen(user_data).read())['response']['user']['firstName']
u.save()
return (u.pk, u.foursquare_profile)
def recent(request):
#get 100 most recent items
recent = []
recents = CheckIn.objects.all().order_by("-time")
recentlen = len(recents)
if recentlen > 10:
recentlen = 10
for x in range(recentlen):
message = ""
quantity = ""
time = pretty_date(recents[x].time)
u = UserProfile.objects.get(pk=recents[x].which_user_id).username
if recents[x].action_type == 'glass':
message = u + ' was thirsty. Chug!'
quantity = "0.06 gallons"
if recents[x].action_type == 'shower':
message = u + ' took a shower'
quantity = "7 gallons per minute"
if recents[x].action_type == 'bath':
message = u + ' took a long bath'
quantity = "30 gallons"
if recents[x].action_type == 'washingmachine':
message = u + ' got rid of some stains'
quantity = "35 gallons"
if recents[x].action_type == 'dishwasher':
message = u + ' did the dishes'
quantity = "15 gallons"
if recents[x].action_type == 'toilet':
message = u + ' had a call of nature'
quantity = "6 gallons"
table = "<table><tr><td valign='middle'><img src='http://www.waterlog.us/static/" + recents[x].action_type + ".png" + "'></td>"
table += "<td valign='middle'>"+message+"<br><span style='font-size:smaller;'>" + quantity + "</span></td></tr></table>"
recent.append({
'text': table,
'visible': 'true',
'image': "http://www.waterlog.us/static/" + recents[x].action_type + ".png",
#'quantity': quantity,
#'timesince': time
})
r = HttpResponse(json.dumps({'recent_checkins':recent}), content_type="application/json")
r['Access-Control-Allow-Origin'] = '*'
return r
def pretty_date(time=False):
"""
Get a datetime object or a int() Epoch timestamp and return a
pretty string like 'an hour ago', 'Yesterday', '3 months ago',
'just now', etc
"""
from datetime import datetime
now = timezone.now()
if type(time) is int:
diff = now - datetime.fromtimestamp(time)
elif isinstance(time,datetime):
diff = now - time
elif not time:
diff = now - now
second_diff = diff.seconds
day_diff = diff.days
if day_diff < 0:
return ''
if day_diff == 0:
if second_diff < 10:
return "just now"
if second_diff < 60:
return str(second_diff) + " seconds ago"
if second_diff < 120:
return "a minute ago"
if second_diff < 3600:
return str( second_diff / 60 ) + " minutes ago"
if second_diff < 7200:
return "an hour ago"
if second_diff < 86400:
return str( second_diff / 3600 ) + " hours ago"
if day_diff == 1:
return "Yesterday"
if day_diff < 7:
return str(day_diff) + " days ago"
if day_diff < 31:
return str(day_diff/7) + " weeks ago"
if day_diff < 365:
return str(day_diff/30) + " months ago"
return str(day_diff/365) + " years ago"