-
Notifications
You must be signed in to change notification settings - Fork 19
/
handlers.py
151 lines (107 loc) · 4.1 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
import logging
from threading import Timer
from tornado.escape import json_decode
from tornado.httpclient import HTTPClient, HTTPRequest
from newebe.core.handlers import NewebeAuthHandler
from newebe.profile.models import UserManager
from newebe.contacts.models import ContactManager
from newebe.activities.models import Activity
logger = logging.getLogger("newebe.core")
class ProfileUpdater:
'''
Utility class to handle profile forwarding to contacts. Set error
log inside acitivity when error occurs.
'''
sending_data = False
contact_requests = {}
def send_profile_to_contacts(self):
'''
External methods to not send too much times the changed profile.
A timer is set to wait for other modifications before running this
function that sends modification requests to every contacts.
'''
client = HTTPClient()
self.sending_data = False
user = UserManager.getUser()
jsonbody = user.toJson()
activity = Activity(
authorKey = user.key,
author = user.name,
verb = "modifies",
docType = "profile",
method = "PUT",
docId = "none",
isMine = True
)
activity.save()
for contact in ContactManager.getTrustedContacts():
request = HTTPRequest("%scontacts/update-profile/" % contact.url,
method="PUT", body=jsonbody)
response = client.fetch(request)
if response.error:
logger.error("""
Profile sending to a contact failed, error infos are
stored inside activity.
""")
activity.add_error(contact)
activity.save()
logger.info("Profile update successfully sent.")
def forward_profile(self):
'''
Because profile modification occurs a lot in a short time. The
profile is forwarded only every ten minutes. It avoids to create
too much activities for this profile modification.
'''
t = Timer(1.0 * 60 * 10, self.send_profile_to_contacts)
if not self.sending_data:
self.sending_data = True
t.start()
profile_updater = ProfileUpdater()
class UserHandler(NewebeAuthHandler):
'''
This resource allows :
* GET : retrieve current user (newebe owner) data.
* POST : create a new user (if user exists, error response is returned).
* PUT : modify current user data. Send profile to every contacts
after a pre-defined time.
'''
def get(self):
'''
Retrieves current user (newebe owner) data at JSON format.
'''
user = UserManager.getUser()
self.return_document(user)
def put(self):
'''
Modifies current user data with sent data (user object at JSON format).
Then forwards it to contacts after a pre-defined time.
'''
user = UserManager.getUser()
data = self.get_body_as_dict(
expectedFields=["name", "url", "description"])
if data:
user.name = data["name"]
user.url = data["url"]
user.description = data["description"]
user.save()
profile_updater.forward_profile()
self.return_success("User successfully Modified.")
else:
self.return_failure("Wrong data were sent.")
# Template handlers.
class ProfileContentTHandler(NewebeAuthHandler):
def get(self):
self.render("templates/profile_content.html")
class ProfileMenuContentTHandler(NewebeAuthHandler):
def get(self):
self.render("templates/profile_menu_content.html")
class ProfileTHandler(NewebeAuthHandler):
def get(self):
self.render("templates/profile.html",
isTheme=self.is_file_theme_exists())
class ProfileTutorial1THandler(NewebeAuthHandler):
def get(self):
self.render("templates/tutorial_1.html")
class ProfileTutorial2THandler(NewebeAuthHandler):
def get(self):
self.render("templates/tutorial_2.html")