-
Notifications
You must be signed in to change notification settings - Fork 0
/
Psinques.py
525 lines (385 loc) · 17.2 KB
/
Psinques.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
# -*- coding: utf-8 -*-
import os
import logging
import webapp2
from google.appengine.ext.db import KindError
from google.appengine.api import users, datastore_errors
from django.utils import simplejson as json
from MasterHandler import MasterHandler, AjaxError
from DataModels import Persona
import Notifications
from DataModels import Psinque, Group, Contact, UserEmail, UserProfile
from DataManipulation import contactExists
#-----------------------------------------------------------------------------
class PsinquesHandler(MasterHandler):
#****************************
# Private methods
#
def _getContact(self):
# Find contact on this end
contact = Contact.get(self.getRequiredParameter('key'))
if contact is None:
raise AjaxError("Contact not found")
if contact.parent().key() != self.userProfile.key():
raise AjaxError("You cannot modify contacts that do not belong to you")
return contact
def _getContactForOutgoing(self, psinque):
'''
Finds and returns a Contact that has the "psinque" in the
"outgoing" field. This Contact will belong to an unknown
user, so we cannot use ancestor queries.
'''
return psinque.parent().friendsContact
#return Contact.all(). \
#ancestor(psinque.fromUser). \
#filter("outgoing =", psinque). \
#get()
def _getContactForIncoming(self, psinque):
'''
Finds and returns a Contact that has the "psinque" in the
"incoming" field. This Contact is in fact the parent of this
Psinque.
'''
return psinque.parent()
def _clearIncoming(self, contact):
contact.incoming = None
if contact.outgoing is None:
if not contact.friendsContact is None:
contact.friendsContact.friendsContact = None
contact.friendsContact.put()
contact.delete()
else:
contact.put()
def _clearOutgoing(self, contact):
contact.outgoing = None
if contact.incoming is None:
if not contact.friendsContact is None:
contact.friendsContact.friendsContact = None
contact.friendsContact.put()
contact.delete()
else:
contact.put()
def _downgradePsinqueToPublic(self, psinque):
psinque.parent().status = "public"
psinque.parent().put()
psinque.private = False
psinque.persona = psinque.fromUser.publicPersona
psinque.put()
def _removeIncoming(self, contact):
'''
Removes the incoming Psinque from a Contact. If the Contact
is empty, it is removed. This Psinque is also removed from
the friend's Contact. The Psinque is removed as well.
'''
if not contact.friendsContact is None: # in case we use a public psinque, there might be no Contact on the other side
self._clearOutgoing(contact.friendsContact)
self._clearIncoming(contact)
def _removeOutgoing(self, contact):
'''
Removes the outgoing Psinque from a Contact. If the Contact
is empty, it is removed. The Psinque is then downgraded to
a public psinque, so it's not removed from the friend's
Contact.
'''
if not contact.friendsContact is None:
self._clearIncoming(contact.friendsContact)
self._clearOutgoing(contact)
def _getPsinqueByKey(self):
psinqueKey = self.getRequiredParameter('key')
psinque = Psinque.get(psinqueKey)
if psinque is None:
raise AjaxError("Psinque not found.")
return psinque
def _getPsinqueFrom(self, userProfile):
return Psinque.all(keys_only = True). \
ancestor(self.userProfile). \
filter("fromUser =", userProfile). \
get()
def _addRequestToUpgrade(self, contact, friendsProfile):
if contact.incoming.private:
raise AjaxError("You already have access to private data")
# We need to share our own private data first
if contact.persona.public:
contact.persona = self.userProfile.defaultPersona
contact.status = "pending"
contact.put()
newPsinque = Psinque(parent = contact,
fromUser = friendsProfile,
private = True,
persona = friendsProfile.defaultPersona,
status = "pending")
newPsinque.put()
Notifications.notifyPendingPsinque(newPsinque)
def _addPublicPsinque(self, friendsProfile):
contact = contactExists(self.userProfile, friendsProfile)
if contact is None:
contactExisted = False
contact = Contact(parent = self.userProfile,
friend = friendsProfile,
group = self.userProfile.defaultGroup,
persona = self.userProfile.publicPersona)
contact.put()
friendsContact = Contact.all().\
ancestor(friendsProfile).\
filter("friend =", self.userProfile).\
get()
if not friendsContact is None:
persona = friendsContact.persona
if not persona.public:
contact.status = "private"
else:
persona = friendsProfile.publicPersona
newPsinque = Psinque(parent = contact,
fromUser = friendsProfile,
private = False,
persona = persona,
status = "established")
newPsinque.put()
if not friendsContact is None:
if friendsContact.incoming:
contact.outgoing = friendsContact.incoming
friendsContact.outgoing = newPsinque
friendsContact.put()
contact.incoming = newPsinque
contact.put()
return [contactExisted, contact, newPsinque]
def _sendNewContact(self, contact):
personaList = { persona.key(): persona.name for persona in self.userProfile.personas.fetch(100) }
groupList = { group.key(): group.name for group in self.userProfile.groups.fetch(100) }
self.sendContent('templates/Psinques_Contact.html',
templateVariables = {
'contact': contact,
'groups': groupList,
'personas': personaList,
})
#****************************
# Views
#
def view(self):
if self.getUserProfile():
offset = self.request.get('offset')
if not offset:
offset = 0
else:
offset = int(offset)
currentCursor = self.request.get('cursor')
# Pending decisions
pendingPsinques = Psinque.all().filter("fromUser =", self.userProfile).filter("status =", "pending")
pendingList = [{'name': x.parent().persona.displayName,
'key': str(x.key())} for x in pendingPsinques]
# List of contacts
contactQuery = Contact.all(). \
ancestor(self.userProfile). \
order("creationTime")
count = contactQuery.count(1000)
if currentCursor:
contactQuery.with_cursor(currentCursor) # start from the previous position
contacts = contactQuery.fetch(limit = 10)
if len(contacts) == 10:
isThereMore = (offset + 10 < count)
else:
isThereMore = False
personaList = { persona.key(): persona.name for persona in self.userProfile.personas.fetch(100) }
groupList = { group.key(): group.name for group in self.userProfile.groups.fetch(100) }
self.sendContent('templates/Psinques.html',
activeEntry = "Psinques",
templateVariables = {
'offset': offset,
'isThereMore': (offset + len(contacts) < count),
'count': count,
'nextCursor': contactQuery.cursor(),
'contacts': contacts,
'pendings': pendingList,
'groups': groupList,
'personas': personaList,
})
#****************************
# AJAX methods
#
def searchemail(self):
# Search for the owner of the email address
email = self.request.get('email')
email = email.lower()
userEmail = UserEmail.all(keys_only = True). \
filter("itemValue =", email). \
get()
if not userEmail:
self.sendJsonOK({ "found": False })
else:
# Check if it's not my own email address
userProfile = UserProfile.get(userEmail.parent())
if userProfile.key() == self.userProfile.key():
raise AjaxError("It is your own email address.")
# Check if there already is a Psinque from that user
psinque = self._getPsinqueFrom(userProfile)
if not psinque is None:
raise AjaxError("You already have a psinque with this email address.")
# Check if the account is active
if not userProfile.active:
self.sendJsonOK({ "found": False }) # act like this person is not registered
self.sendJsonOK({
"found": True,
"key": str(userProfile.key()),
"displayName": userProfile.displayName,
"publicEnabled": userProfile.publicEnabled,
})
def addpublic(self):
friendsProfile = UserProfile.get(self.getRequiredParameter("key"))
psinque = self._getPsinqueFrom(friendsProfile)
if not psinque is None:
raise AjaxError("You already have this psinque")
contact = self._addPublicPsinque(friendsProfile)
if not contact[0]:
self._sendNewContact(contact[1])
else:
raise AjaxError("Incoming psinque already exists.")
def requestprivate(self):
contact = self._getContact()
existingPsinque = Psinque.all(keys_only = True). \
ancestor(contact). \
filter("private =", True). \
get()
if not existingPsinque is None:
raise AjaxError("Request has already been sent.")
self._addRequestToUpgrade(contact, contact.friend)
self.sendJsonOK()
def addincoming(self):
contact = self._getContact()
if contact.incoming:
raise AjaxError("Incoming psinque already exists.")
friendsProfle = contact.friendsContact.parent()
newPsinque = Psinque(parent = contact,
fromUser = friendsProfle,
private = True,
persona = friendsProfle.defaultPersona,
status = "established")
newPsinque.put()
contact.incoming = newPsinque
contact.put()
contact.friendsContact.outgoing = newPsinque
contact.friendsContact.put()
self._sendNewContact(contact)
def removecontact(self):
contact = self._getContact()
if contact.incoming:
contact.incoming.delete()
if not contact.friendsContact is None:
contact.friendsContact.outgoing = None
contact.friendsContact.friendsContact = None
contact.friendsContact.put()
contact.delete()
self.sendJsonOK()
def changepersona(self):
try:
contact = Contact.get(self.getRequiredParameter('contact'))
except datastore_errors.BadKeyError:
raise AjaxError("Contact does not exist")
try:
persona = Persona.get(self.getRequiredParameter('persona'))
except datastore_errors.BadKeyError:
raise AjaxError("Persona does not exist")
if contact.persona == persona:
self.sendJsonOK()
return
contact.persona = persona
contact.put()
if persona.public:
if contact.outgoing:
contact.outgoing.private = False
contact.outgoing.put()
Notifications.notifyDowngradedPsinque(contact.outgoing)
if contact.friendsContact:
contact.friendsContact.status = "public"
contact.friendsContact.put()
if contact.outgoing:
contact.outgoing.persona = persona
contact.outgoing.put()
self.sendJsonOK()
def changegroup(self):
try:
contact = Contact.get(self.getRequiredParameter('contact'))
except datastore_errors.BadKeyError:
raise AjaxError("Contact does not exist")
#oldGroup = contact.group
try:
persona = Group.get(self.getRequiredParameter('persona'))
except datastore_errors.BadKeyError:
raise AjaxError("Group does not exist")
# First we check if this psinque is not already asigned to that group
contact.group = group
contact.put()
#oldGroupSize = Contact.all(keys_only = True). \
#filter("group =", oldGroup). \
#count(1)
#if oldGroupSize == 0:
#olfGroup.delete()
self.sendJsonOK()
def acceptrequest(self):
try:
psinque = self._getPsinqueByKey()
except AjaxError:
self.displayMessage('templates/Message_PsinqueNotFound.html')
return
contactIn = self._getContactForIncoming(psinque)
contactOut = self._getContactForOutgoing(psinque)
if contactOut is None:
contactOut = Contact(parent = self.userProfile,
friend = contactIn.parent(),
friendsContact = contactIn,
group = self.userProfile.defaultGroup,
persona = self.userProfile.defaultPersona)
contactOut.put()
contactIn.friendsContact = contactOut
if not contactOut.outgoing is None:
raise AjaxError("There already is a psinque from this user")
existingPsinque = contactIn.incoming
if not existingPsinque is None:
if existingPsinque.private:
psinque.delete()
raise AjaxError("There already is a private psinque from this user")
else:
existingPsinque.delete()
contactIn.incoming = psinque
contactIn.status = "private"
contactIn.persona = contactIn.parent().defaultPersona
contactIn.put()
contactOut.outgoing = psinque
contactOut.status = "private"
contactOut.persona = contactOut.parent().defaultPersona
contactOut.put()
psinque.status = "established"
psinque.persona = psinque.fromUser.defaultPersona
psinque.put()
Notifications.notifyAcceptedRequest(psinque)
if self.request.get("email") == "true":
self.displayMessage('templates/Message_Accepted.html',
templateVariables = {
'friendsName': contactOut.displayName,
})
else:
self._sendNewContact(contactOut)
def rejectrequest(self):
psinque = self._getPsinqueByKey()
psinque.delete()
Notifications.notifyRejectedRequest(psinque)
if self.request.get("email") == "true":
self.displayMessage('templates/Message_Rejected.html',
templateVariables = {
'friendsName': psinque.displayName,
})
else:
self.sendJsonOK()
def banrequest(self):
raise AjaxError("Unimplemented")
def addgroup(self):
group = Group(parent = self.userProfile,
name = self.getRequiredParameter("name"))
group.put()
self.sendJsonOK({
"key": str(group.key()),
})
#-----------------------------------------------------------------------------
app = webapp2.WSGIApplication([
(r'/psinques/(\w+)', PsinquesHandler),
#(r'/decisions/(\w+)', OutgoingDecisions),
], debug=True)