-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandlers.py
951 lines (788 loc) · 32.5 KB
/
handlers.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
# -*- coding: utf-8 -*-
import os
import json
import datetime
import logging
import secrets
import urllib
import collections
import webapp2
from webapp2_extras import auth, sessions, jinja2
from jinja2.runtime import TemplateNotFound
from django.utils.html import strip_tags
from urlparse import urlparse
from simpleauth import SimpleAuthHandler
from google.appengine.ext import ndb
from google.appengine.api import urlfetch
from google.appengine.api import mail
from google.appengine.api import users
from webapp2_extras.appengine.auth.models import User
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.api import images
from google.appengine.api import search
from urlparse import parse_qs
project_key = ndb.Key('Projects', 'default_projects')
firms_key = ndb.Key('Firms', 'default_firms')
comments_key = ndb.Key('Comments', 'default_comments')
user_key = ndb.Key('User', 'default_user')
notification_key = ndb.Key('Notification', 'default_notification')
_INDEX_NAME_FIRMS = 'firms'
custom_search_key = 'search key'
custom_search_cx = 'cx'
class Firms(ndb.Model):
admin = ndb.KeyProperty(kind='User')
admin_id = ndb.IntegerProperty()
members = ndb.StringProperty(repeated=True)
picture = ndb.StringProperty()
link = ndb.StringProperty()
desc = ndb.StringProperty()
logo = ndb.BlobKeyProperty()
title = ndb.StringProperty()
pos = ndb.GeoPtProperty(default=ndb.GeoPt(28.635308, 77.224960))
privacy = ndb.IntegerProperty(default=0)
date = ndb.DateTimeProperty(auto_now_add=True)
class Projects(ndb.Model):
author = ndb.KeyProperty(kind='User')
author_id = ndb.IntegerProperty()
firm = ndb.KeyProperty(kind='Firms')
firm_id = ndb.IntegerProperty()
title = ndb.StringProperty()
content = ndb.StringProperty()
deadline = ndb.DateTimeProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
class Comments(ndb.Model):
author = ndb.KeyProperty(kind='User')
firm = ndb.KeyProperty(kind='Firms')
project = ndb.KeyProperty(kind='Projects')
comment = ndb.StringProperty()
project_id = ndb.IntegerProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
class Notification(ndb.Model):
author = ndb.KeyProperty(kind='User')
body_html = ndb.StringProperty()
id = ndb.IntegerProperty()
href = ndb.StringProperty()
time = ndb.DateTimeProperty(auto_now_add=True)
users = ndb.StringProperty(repeated=True)
class BaseRequestHandler(webapp2.RequestHandler):
def dispatch(self):
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)
@webapp2.cached_property
def jinja2(self):
"""Returns a Jinja2 renderer cached in the app registry"""
return jinja2.get_jinja2(app=self.app)
@webapp2.cached_property
def session(self):
"""Returns a session using the default cookie key"""
return self.session_store.get_session()
@webapp2.cached_property
def auth(self):
return auth.get_auth()
@webapp2.cached_property
def current_user(self):
"""Returns currently logged in user"""
user_dict = self.auth.get_user_by_session()
return self.auth.store.user_model.get_by_id(user_dict['user_id'])
@webapp2.cached_property
def logged_in(self):
"""Returns true if a user is currently logged in, false otherwise"""
return self.auth.get_user_by_session() is not None
def render(self, template_name, template_vars={}):
# Preset values for the template
values = {
'url_for': self.uri_for,
'logged_in': self.logged_in,
'flashes': self.session.get_flashes()
}
# Add manually supplied template values
values.update(template_vars)
# read the template or 404.html
try:
self.response.write(self.jinja2.render_template(template_name, **values))
except TemplateNotFound:
self.abort(404)
def head(self, *args):
"""Head is used by Twitter. If not there the tweet button shows 0"""
pass
class CommentAdd(BaseRequestHandler):
def post(self):
firm_id = int(self.request.get('firm_id'))
project_id = int(self.request.get('project_id'))
firm = Firms.get_by_id(firm_id, parent=firms_key)
project = Projects.get_by_id(project_id, parent=project_key)
comment = Comments(parent=comments_key)
comment.author = self.current_user.key
comment.firm = firm.key
comment.comment = self.request.get('comment')
comment.project = project.key
comment.project_id = project_id
comment.put()
notification = Notification(parent=notification_key)
notification.author = self.current_user.key
notification.body_html = "<a href=\"./profile?pro_id=" + str(self.current_user.key.id()) \
+ "\">" + self.current_user.name + "</a> has commented on your project in " \
+ "<a href=\"./projects?firm_id=" + str(firm.key.id()) \
+ "\">" + firm.title + "</a> .."
notification.href = "./projects?firm_id=" + str(firm.key.id()) + "#row" + str(project.key.id())
notification.id = comment.key.id()
notification.users.append(project.author.get().email)
notification.put()
if firm_id:
self.redirect('/projects?firm_id=' + str(firm_id) + '#row' + str(project_id))
else:
self.redirect('/')
class ProjectAdd(BaseRequestHandler):
def post(self):
firm_id = int(self.request.get('firm_id'))
firm = Firms.get_by_id(firm_id, parent=firms_key)
projects = Projects(parent=project_key)
projects.author = self.current_user.key
projects.author_id = int(self.current_user.key.id())
projects.firm = firm.key
projects.firm_id = firm_id
projects.content = self.request.get('content')
projects.title = self.request.get('title')
projects.deadline = datetime.datetime.strptime(self.request.get('datetime'), '%m/%d/%Y %H:%M %p')
projects.put()
notification = Notification(parent=notification_key)
notification.author = self.current_user.key
notification.body_html = "<a href=\"./profile?pro_id=" + str(self.current_user.key.id()) \
+ "\">" + self.current_user.name + "</a> has created a new project " \
+ self.request.get('title') + " in " \
+ "<a href=\"./projects?firm_id=" + str(firm.key.id()) \
+ "\">" + firm.title + "</a> .."
notification.href = "./projects?firm_id=" + str(firm.key.id())
notification.id = projects.key.id()
for member in firm.members:
notification.users.append(member)
notification.put()
self.redirect('/projects?firm_id=' + str(firm_id))
class firms(BaseRequestHandler):
def post(self):
if self.request.get('title'):
title = self.request.get('title')
if title:
#google images search
title1 = urllib.quote_plus(title)
url = 'https://www.googleapis.com/customsearch/v1?key=' + custom_search_key + '&cx=' + custom_search_cx + '&searchType=image&fileType=jpg&imgSize=large&imgType=news&num=1&alt=json&q=' + title1
result = urlfetch.fetch(url)
jsonr = json.loads(result.content)
picture = jsonr['items'][0]['link']
link = jsonr['items'][0]['displayLink']
desc1 = jsonr['items'][0]['title']
desc = urllib.unquote_plus(desc1)
#wikipedia description
urlw = "https://en.wikipedia.org//w/api.php?action=query&prop=extracts&format=json&exsentences=1&exlimit=1&exsectionformat=plain&titles=" + title1
resultw = urlfetch.fetch(urlw)
jsonrw = json.loads(resultw.content)
d = jsonrw['query']['pages'].keys()
x = d[0]
try:
desciption = jsonrw['query']['pages'][x]['extract']
except:
desciption = ""
if desciption:
desc = strip_tags(desciption)
else:
picture = self.current_user.avatar_url
link = self.current_user.link
desc = "No data."
else:
self.render('error.html', {
'user': self.current_user,
})
firmss = Firms(parent=firms_key)
firmss.admin = self.current_user.key
firmss.admin_id = self.current_user.key.id()
firmss.members = [self.current_user.email]
firmss.link = link
firmss.picture = picture
firmss.title = title
firmss.desc = desc
firmss.put()
id = firmss.key.id()
search.Index(name=_INDEX_NAME_FIRMS).put(CreateFirmDoc(str(id), self.current_user, title, "null", desc, link))
self.redirect('/firm?firm_id=' + str(firmss.key.id()))
def get(self):
upload_url = blobstore.create_upload_url('/editfirm')
firm_id = 0
try:
firm_id = int(self.request.get('firm_id'))
except:
self.abort(404)
firmed = Firms.get_by_id(firm_id, parent=firms_key)
if firmed.logo:
image_url = images.get_serving_url(firmed.logo, 75)
else:
image_url = "/img/Hinder-red.png"
if (firm_id != 0) and self.logged_in:
self.render('firm.html', {
'title': 'Edit Firm Settings - ' + firmed.title,
'user': self.current_user,
'upload_url': upload_url,
'image_url': image_url,
'firm': firmed,
'firm_id': firm_id
})
else:
self.abort(404)
class EditFirm(BaseRequestHandler, blobstore_handlers.BlobstoreUploadHandler):
def post(self):
desc = self.request.get('desc')
desc = (desc[:75] + '..') if len(desc) > 75 else desc
title = self.request.get('title')
title = (title[:60] + '..') if len(title) > 75 else title
link = self.request.get('link')
parts = urlparse(link)
if not parts.scheme or not parts.netloc:
self.abort(404)
firm_id = int(self.request.get('firm_id'))
firm = Firms.get_by_id(firm_id, parent=firms_key)
logo = self.get_uploads('logo')
if logo:
blob_info = logo[0]
if firm.logo:
blobstore.delete(firm.logo)
firm.logo = blob_info.key()
logo = blob_info.key()
else:
logo = firm.logo
firm.link = link
firm.picture = self.request.get('picture')
firm.title = title
firm.privacy = int(self.request.get('type'))
firm.desc = desc
firm.pos = ndb.GeoPt(self.request.get('lat'), self.request.get('lng'))
firm.put()
notification = Notification(parent=notification_key)
notification.author = self.current_user.key
notification.body_html = "<a href=\"./profile?pro_id=" + str(self.current_user.key.id()) \
+ "\">" + self.current_user.name + "</a> updated " \
+ "<a href=\"./projects?firm_id=" + str(firm.key.id()) \
+ "\">" + firm.title + "</a> Details..."
notification.href = "./projects?firm_id=" + str(firm.key.id())
notification.id = firm.key.id()
for member in firm.members:
notification.users.append(member)
notification.put()
doc_index = search.Index(name=_INDEX_NAME_FIRMS)
if doc_index.get(doc_id=str(firm.key.id())):
doc_index.delete(document_ids=str(firm.key.id()))
search.Index(name=_INDEX_NAME_FIRMS).put(CreateFirmDoc(str(firm_id),
self.current_user, title, str(logo), desc, link))
self.redirect('/')
class add_firm(BaseRequestHandler):
def post(self):
if self.request.get('title'):
title = self.request.get('title')
else:
title = "No Title"
if self.request.get('link'):
link = self.request.get('link')
else:
link = "No link"
if self.request.get('desc'):
desc = self.request.get('desc')
else:
desc = "No Description!"
if self.logged_in:
self.render('add_firm.html', {
'user': self.current_user,
'title': title,
'link': link,
'desc': desc
})
else:
self.render('add_firm.html', {
'title': title,
'link': link,
'desc': desc,
})
class DeleteFirm(BaseRequestHandler):
def get(self):
firm_id = 0
try:
firm_id = int(self.request.get('firm_id'))
except:
self.abort(404)
firmed = Firms.get_by_id(firm_id, parent=firms_key)
if (firm_id != 0) and self.logged_in:
self.render('delete.html', {
'title': 'Delete Firm',
'user': self.current_user,
'firm': firmed,
'firm_id': firm_id
})
else:
self.abort(404)
def post(self):
logging.debug('Deleting the event!: %s' % self.request.get('event_id'))
firm_id = 0
try:
firm_id = int(self.request.get('firm_id'))
except:
self.abort(404)
firmed = Firms.get_by_id(firm_id, parent=firms_key)
projects = Projects.query(Projects.firm_id == firm_id, ancestor=project_key)
comments = Comments.query(ancestor=comments_key)
for project in projects:
for comment in comments:
if comment.project == project.key.id():
comment.key.delete()
project.key.delete()
doc_index = search.Index(name=_INDEX_NAME_FIRMS)
if doc_index.get(doc_id=str(firmed.key.id())):
doc_index.delete(document_ids=str(firmed.key.id()))
firmed.key.delete()
self.redirect('/')
class AddFirmMember(BaseRequestHandler):
def post(self):
firm_id = self.request.get('firm_id')
email = self.request.get('member_email')
firm_id1 = int(self.request.get('firm_id'))
firm = Firms.get_by_id(firm_id1, parent=firms_key)
if email in firm.members:
self.abort(404)
else:
firm.members.append(email)
firm.put()
notification = Notification(parent=notification_key)
notification.author = self.current_user.key
notification.body_html = "<a href=\"./profile?pro_id=" + str(self.current_user.key.id()) \
+ "\">" + self.current_user.name + "</a> added a new member " + \
self.request.get('member_email') + " to " \
+ "<a href=\"./projects?firm_id=" + str(firm.key.id()) \
+ "\">" + firm.title + "</a>."
notification.href = "./projects?firm_id=" + str(firm.key.id())
notification.id = firm.key.id()
for member in firm.members:
notification.users.append(member)
notification.put()
if firm_id:
self.redirect('/projects?firm_id=' + firm_id + '#row')
else:
self.redirect('/')
class RootHandler(BaseRequestHandler):
def get(self):
"""Handles default langing page"""
# Projects
if self.logged_in:
firms = Firms.query(ancestor=firms_key).order(-Firms.date)
email = self.current_user.email
foundFirm = False
firmed = []
for firm in firms:
if email in firm.members:
firmed.append(firm)
foundFirm = True
#projects = Projects.query(ancestor=project_key).order(-Projects.deadline)
found = False
if self.logged_in:
projectsme = Projects.query(Projects.author_id == self.current_user.key.id(),
ancestor=project_key).order(
-Projects.deadline)
for project in projectsme:
found = True
else:
projectsme = None
comments = Comments.query(ancestor=comments_key).order(Comments.date)
notifications = Notification.query(ancestor=notification_key).order(-Notification.time).fetch(5)
notification = []
for notify in notifications:
if email in notify.users and notify.author.get().email != self.current_user.email:
notification.append(notify)
count = len(notification)
self.render('home.html', {
'title': 'Equilibrium',
'notifications': notification,
'count': count,
'firms': firmed,
'user': self.current_user,
'greetings': projectsme,
'comments': comments,
'found': found,
'foundFirm': foundFirm
})
else:
self.render('hot.html')
class About(BaseRequestHandler):
def get(self):
if self.logged_in:
self.render('about.html', {
'title': 'Equilibrium - Work made easy.',
'user': self.current_user
})
else:
self.render('about.html', {
'title': 'Equilibrium - Work made easy.'
})
class DeleteProject(BaseRequestHandler):
def post(self):
project_id = 0
try:
project_id = int(self.request.get('project_id'))
except:
self.abort(404)
project = Projects.get_by_id(project_id, parent=project_key)
comments = Comments.query(Comments.project_id == project_id, ancestor=comments_key)
notifications = Notification.query(Notification.id == project_id, ancestor=notification_key)
for notice in notifications:
notice.key.delete()
for comment in comments:
comment.key.delete()
project.key.delete()
self.redirect('/')
class DeleteComment(BaseRequestHandler):
def post(self):
comment_id = 0
try:
comment_id = int(self.request.get('comment_id'))
except:
self.abort(404)
comment = Comments.get_by_id(comment_id, parent=comments_key)
notifications = Notification.query(Notification.id == comment_id, ancestor=notification_key)
for notice in notifications:
notice.key.delete()
comment.key.delete()
self.redirect('/')
class DeleteMember(BaseRequestHandler):
def post(self):
logging.debug('Deleting the member in !: %s' % self.request.get('firm_id'))
firm_id = 0
member = ''
try:
member = self.request.get('member')
firm_id = int(self.request.get('firm_id'))
except:
self.abort(404)
firmed = Firms.get_by_id(firm_id, parent=firms_key)
if member in firmed.members:
firmed.members.remove(member)
firmed.put()
self.redirect('/')
class Invite(BaseRequestHandler):
def get(self):
self.render('invite.html', {
'title': 'Invite People To Equilibrium - Work made easy.',
'user': self.current_user,
})
def post(self):
email = self.request.get('email')
message = mail.EmailMessage()
message.sender = self.current_user.email
message.to = email
message.subject = "Invitation to Equilibrium!"
message.body = """
You have been invited to Equilibrium!
To accept this invitation, click the following link,
or copy and paste the URL into your browser's address
bar:
http://gcdc2013-equilibrium.appspot.com/
"""
message.send()
self.redirect('/')
class Privacy(BaseRequestHandler):
def get(self):
if self.logged_in:
self.render('privacy.html', {
'title': 'Privacy / TOS For Equilibrium',
'user': self.current_user
})
else:
self.render('privacy.html', {
'title': 'Privacy / TOS For Equilibrium'
})
class Features(BaseRequestHandler):
def get(self):
if self.logged_in:
self.render('features.html', {
'title': 'Features of Equilibrium - Work Made Easy',
'user': self.current_user
})
else:
self.render('features.html', {
'title': 'Features of Equilibrium - Work Made Easy',
})
class Browse(BaseRequestHandler):
def get(self):
firms = Firms.query(ancestor=firms_key).order(-Firms.date)
if self.logged_in:
self.render('browse.html', {
'title': 'Browse - Equilibrium',
'user': self.current_user,
'images': images.get_serving_url,
'firms': firms
})
else:
self.abort(404)
def CreateFirmDoc(id, author, title, logo, desc, link):
if author:
nickname = author.name
else:
nickname = 'anonymous'
return search.Document(doc_id=id,
fields=[search.TextField(name='author', value=nickname),
search.TextField(name='title', value=title),
search.TextField(name='logo', value=logo),
search.TextField(name='desc', value=desc),
search.TextField(name='link', value=link),
search.DateField(name='date', value=datetime.datetime.now().date())])
class Search(BaseRequestHandler):
def get(self):
uri = urlparse(self.request.uri)
query = 'null'
if uri.query:
query = parse_qs(uri.query)
query = query['query'][0]
if query is not 'null':
expr_list = [search.SortExpression(
expression='author', default_value='',
direction=search.SortExpression.DESCENDING)]
sort_opts = search.SortOptions(expressions=expr_list)
query_options = search.QueryOptions(limit=9, sort_options=sort_opts)
query_obj = search.Query(query_string=query, options=query_options)
results_firms = search.Index(name=_INDEX_NAME_FIRMS).search(query=query_obj)
self.render('search.html', {
'user': self.current_user,
'title': "Search - Equilibrium",
'results_firms': results_firms,
'images': images.get_serving_url,
'len': results_firms.number_found
})
else:
self.render('search.html', {
'user': self.current_user,
'title': "Search - Equilibrium"
})
def get(self):
if self.logged_in:
firms = Firms.query(ancestor=firms_key).order(-Firms.date)
email = self.current_user.email
foundFirm = False
firmed = []
for firm in firms:
if email in firm.members:
firmed.append(firm)
foundFirm = True
notifications = Notification.query(ancestor=notification_key).order(-Notification.time).fetch(5)
notification = []
for notify in notifications:
if email in notify.users and notify.author.get().email != self.current_user.email:
notification.append(notify)
count = len(notification)
#projects = Projects.query(ancestor=project_key).order(-Projects.deadline)
found = False
if self.logged_in:
projectsme = Projects.query(Projects.author_id == self.current_user.key.id(),
ancestor=project_key).order(
-Projects.deadline)
for project in projectsme:
found = True
else:
projectsme = None
comments = Comments.query(ancestor=comments_key).order(Comments.date)
self.render('semantic.html', {
'title': 'Semantic - Equilibrium',
'notifications': notification,
'count': count,
'user': self.current_user,
'greetings': projectsme,
'found': found,
'foundFirm': foundFirm,
'comments': comments,
})
else:
self.render('semantic.html', {
'title': 'Semantic - Equilibrium',
})
class ProjectsHandler(BaseRequestHandler):
def get(self):
"""Handles default langing page"""
# Projects
firm_id = 0
try:
firm_id = int(self.request.get('firm_id'))
except:
self.abort(404)
firmed = Firms.get_by_id(firm_id, parent=firms_key)
projects = Projects.query(Projects.firm_id == firm_id, ancestor=project_key).order(-Projects.deadline)
comments = Comments.query(ancestor=comments_key).order(Comments.date)
found = False
for project in projects:
found = True
if self.logged_in:
self.render('projects.html', {
'title': firmed.title + ' on Equilibrium',
'greetings': projects,
'comments': comments,
'user': self.current_user,
'images': images.get_serving_url,
'firm_id': firm_id,
'firm': firmed,
'found': found
})
else:
self.render('projects.html', {
'title': firmed.title + 'on Equilibrium',
'greetings': projects,
'comments': comments,
'logo_url': images.get_serving_url(firmed.logo, 75),
'user': None,
'firm_id': firm_id,
'firm': firmed,
'found': found
})
class ProfileHandler(BaseRequestHandler):
def get(self):
"""Handles GET /profile"""
#projects = Projects.query(Projects.author == self.current_user.key, ancestor=project_key).order(-Projects.deadline)
comments = Comments.query(ancestor=comments_key).order(Comments.date)
found = False
pro_id = 0
try:
pro_id = int(self.request.get('pro_id'))
except:
self.abort(404)
profile = self.auth.store.user_model.get_by_id(pro_id)
projects = Projects.query(Projects.author == profile.key, ancestor=project_key).order(-Projects.deadline)
firms = Firms.query(ancestor=firms_key).order(-Firms.date)
email = profile.email
foundFirm = False
firmed = []
for firm in firms:
if email in firm.members:
firmed.append(firm)
foundFirm = True
for project in projects:
found = True
self.render('profile.html', {
'title': profile.name,
'get_user': profile,
'foundFirm': foundFirm,
'firms': firmed,
'user': self.current_user,
'greetings': projects,
'comments': comments,
'found': found
})
class AuthHandler(BaseRequestHandler, SimpleAuthHandler):
"""Authentication handler for OAuth 2.0, 1.0(a) and OpenID."""
# Enable optional OAuth 2.0 CSRF guard
OAUTH2_CSRF_STATE = True
USER_ATTRS = {
'facebook': {
'id': lambda id: ('avatar_url',
'http://graph.facebook.com/{0}/picture?type=large'.format(id)),
'name': 'name',
'link': 'link'
},
'google': {
'picture': 'avatar_url',
'name': 'name',
'profile': 'link',
'email': 'email'
},
'windows_live': {
'avatar_url': 'avatar_url',
'name': 'name',
'link': 'link'
},
'twitter': {
'profile_image_url': 'avatar_url',
'screen_name': 'name',
'link': 'link'
},
'linkedin': {
'picture-url': 'avatar_url',
'first-name': 'name',
'public-profile-url': 'link'
},
'linkedin2': {
'picture-url': 'avatar_url',
'first-name': 'name',
'public-profile-url': 'link'
},
'foursquare': {
'photo': lambda photo: ('avatar_url', photo.get('prefix') + '100x100' + photo.get('suffix')),
'firstName': 'firstName',
'lastName': 'lastName',
'contact': lambda contact: ('email', contact.get('email')),
'id': lambda id: ('link', 'http://foursquare.com/user/{0}'.format(id))
},
'openid': {
'id': lambda id: ('avatar_url', '/img/missing-avatar.png'),
'nickname': 'name',
'email': 'link'
}
}
def _on_signin(self, data, auth_info, provider):
"""Callback whenever a new or existing user is logging in.
data is a user info dictionary.
auth_info contains access token or oauth token and secret.
"""
auth_id = '%s:%s' % (provider, data['id'])
logging.info('Looking for a user with id %s', auth_id)
user = self.auth.store.user_model.get_by_auth_id(auth_id)
_attrs = self._to_user_model_attrs(data, self.USER_ATTRS[provider])
if user:
logging.info('Found existing user to log in')
# Existing users might've changed their profile data so we update our
# local model anyway. This might result in quite inefficient usage
# of the Datastore, but we do this anyway for demo purposes.
#
# In a real app you could compare _attrs with user's properties fetched
# from the datastore and update local user in case something's changed.
user.populate(**_attrs)
user.put()
self.auth.set_session(
self.auth.store.user_to_dict(user))
else:
# check whether there's a user currently logged in
# then, create a new user if nobody's signed in,
# otherwise add this auth_id to currently logged in user.
if self.logged_in:
logging.info('Updating currently logged in user')
u = self.current_user
u.populate(**_attrs)
# The following will also do u.put(). Though, in a real app
# you might want to check the result, which is
# (boolean, info) tuple where boolean == True indicates success
# See webapp2_extras.appengine.auth.models.User for details.
u.add_auth_id(auth_id)
else:
logging.info('Creating a brand new user')
ok, user = self.auth.store.user_model.create_user(auth_id, **_attrs)
if ok:
self.auth.set_session(self.auth.store.user_to_dict(user))
# Remember auth data during redirect, just for this demo. You wouldn't
# normally do this.
self.session.add_flash(data, 'data - from _on_signin(...)')
self.session.add_flash(auth_info, 'auth_info - from _on_signin(...)')
# Go to the profile page
self.redirect('/')
def logout(self):
self.auth.unset_session()
self.redirect('/')
def handle_exception(self, exception, debug):
logging.error(exception)
self.render('error.html', {'exception': exception})
def _callback_uri_for(self, provider):
return self.uri_for('auth_callback', provider=provider, _full=True)
def _get_consumer_info_for(self, provider):
"""Returns a tuple (key, secret) for auth init requests."""
return secrets.AUTH_CONFIG[provider]
def _to_user_model_attrs(self, data, attrs_map):
"""Get the needed information from the provider dataset."""
user_attrs = {}
for k, v in attrs_map.iteritems():
attr = (v, data.get(k)) if isinstance(v, str) else v(data.get(k))
user_attrs.setdefault(*attr)
return user_attrs