/
profile.py
201 lines (154 loc) · 5.49 KB
/
profile.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
# This file is part of Booktype.
# Copyright (c) 2012 Aleksandar Erkalovic <aleksandar.erkalovic@sourcefabric.org>
#
# Booktype is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Booktype is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Booktype. If not, see <http://www.gnu.org/licenses/>.
from django.db import transaction
from django.conf import settings
try:
STATUS_URL = settings.STATUS_URL
except AttributeError:
STATUS_URL = 'http://status.flossmanuals.net/'
def remote_get_status_messages(request, message, profileid):
"""
Fetches RSS feed from status.net and returns its content.
Output:
- list
@todo: Not used anymore. Probably should remove it.
@type request: C{django.http.HttpRequest}
@param request: Client Request object
@type message: C{dict}
@param message: Message object
@type profileid: C{string}
@param profile: Unique Profile id
@rtype: C{dict}
@return: Returns feed content
"""
return {}
# import feedparser
#
# d = feedparser.parse('%s%s/rss' % (STATUS_URL, profileid))
#
# messages = [(x['content'][0]['value'], ) for x in d['entries']]
#
# return {"list": messages}
def remote_group_create(request, message, profileid):
"""
Creates new Booki Group.
Input:
- groupName
- groupDescription
Output:
- list
@type request: C{django.http.HttpRequest}
@param request: Client Request object
@type message: C{dict}
@param message: Message object
@type profileid: C{string}
@param profile: Unique Profile id
@rtype: C{dict}
@return: Returns success of the command
"""
from booki.utils.book import createBookiGroup, BookiGroupExist
groupName = message.get("groupName", "")
groupDescription = message.get("groupDescription", "")
try:
group = createBookiGroup(groupName, groupDescription, request.user)
group.members.add(request.user)
except BookiGroupExist:
transaction.rollback()
return {"created": False}
else:
transaction.commit()
return {"created": True}
def remote_init_profile(request, message, profileid):
"""
Initializes data.
@type request: C{django.http.HttpRequest}
@param request: Client Request object
@type message: C{dict}
@param message: Message object
@type profileid: C{string}
@param profile: Unique Profile id
"""
import sputnik
## get online users
try:
_onlineUsers = sputnik.smembers("sputnik:channel:%s:users" % message["channel"])
except:
_onlineUsers = []
if request.user.username not in _onlineUsers:
try:
sputnik.sadd("sputnik:channel:%s:users" % message["channel"], request.user.username)
except:
pass
return {}
def remote_mood_set(request, message, profileid):
"""
Sets new mood for this profile.
Input:
- value
@type request: C{django.http.HttpRequest}
@param request: Client Request object
@type message: C{dict}
@param message: Message object
@type profileid: C{string}
@param profile: Unique Profile id
"""
# should check permissions
from django.utils.html import strip_tags
## maximum size is 30 characters only
## html tags are removed
moodMessage = strip_tags(message.get("value",""))[:30]
import booki.account.signals
booki.account.signals.account_status_changed.send(sender = request.user, message = message.get('value', ''))
# save new permissions
profile = request.user.get_profile()
profile.mood = moodMessage
try:
profile.save()
except:
transaction.rollback()
else:
transaction.commit()
## propagate to other users
## probably should only send it to /booki/ channel
import sputnik
for chnl in sputnik.smembers("sputnik:channels"):
if sputnik.sismember("sputnik:channel:%s:users" % message['channel'], request.user.username):
sputnik.addMessageToChannel(request, chnl, {"command": "user_status_changed",
"from": request.user.username,
"message": moodMessage},
myself=True)
return {}
def remote_hide_book(request, message, profileid):
from booki.editor import models
book = models.Book.objects.get(url_title = message["bookID"])
if message["action"] == "hide":
book.hidden = True
else:
book.hidden = False
book.save()
transaction.commit()
return {"result": True}
def remote_load_info(request, message, profileid):
from django.utils.html import escape
user = request.user
profile = request.user.get_profile()
description = escape(profile.description).replace('\r', '')
lines = description.split('\n')
info = {'username': user.username,
'fullname': user.first_name,
'description': '<br/>'.join(lines)
}
return {"result": True, "info": info}