| author | raph |
| Fri, 04 Dec 2009 14:03:03 +0100 | |
| changeset 22 | 0b8b521da2ce |
| parent 17 | a4be0b8a905d |
| child 24 | c8a95e540b79 |
| permissions | -rw-r--r-- |
| 0 | 1 |
from cm.converters.pandoc_converters import \ |
2 |
CHOICES_INPUT_FORMATS as CHOICES_INPUT_FORMATS_PANDOC, \ |
|
3 |
DEFAULT_INPUT_FORMAT as DEFAULT_INPUT_FORMAT_PANDOC, pandoc_convert |
|
4 |
from cm.models_base import PermanentModel, KeyManager, Manager, KeyModel, AuthorModel |
|
5 |
from cm.models_utils import * |
|
6 |
from cm.utils.dj import absolute_reverse |
|
7 |
from cm.utils.date import datetime_to_user_str |
|
8 |
from cm.utils.comment_positioning import compute_new_comment_positions |
|
9 |
from django import forms |
|
10 |
from django.db.models import Q |
|
11 |
from django.template.loader import render_to_string |
|
12 |
from django.conf import settings |
|
13 |
from django.template import RequestContext |
|
14 |
from django.contrib.auth.models import Permission |
|
15 |
from django.contrib.contenttypes import generic |
|
16 |
from django.contrib.contenttypes.models import ContentType |
|
17 |
from django.core.files.base import ContentFile |
|
18 |
from django.core.urlresolvers import reverse |
|
19 |
from django.template.defaultfilters import timesince |
|
20 |
from django.db import models |
|
21 |
from django.utils.translation import ugettext as _, ugettext_lazy, ugettext_noop |
|
22 |
from tagging.fields import TagField |
|
23 |
import pickle |
|
24 |
from django.db import connection |
|
25 |
||
26 |
||
27 |
||
28 |
class TextManager(Manager): |
|
29 |
def create_text(self, title, format, content, note, name, email, tags, user=None, state='approved', **kwargs): |
|
30 |
text = self.create(name=name, email=email, user=user, state=state) |
|
31 |
text_version = TextVersion.objects.create(title=title, format=format, content=content, text=text, note=note, name=name, email=email, tags=tags, user=user) |
|
32 |
return text |
|
33 |
||
34 |
def create_new_version(self, text, title, format, content, note, name, email, tags, user=None, **kwargs): |
|
35 |
text_version = TextVersion.objects.create(title=title, format=format, content=content, text=text, note=note, name=name, email=email, tags=tags, user=user) |
|
36 |
return text_version |
|
37 |
||
38 |
class Text(PermanentModel, AuthorModel): |
|
39 |
modified = models.DateTimeField(auto_now=True) |
|
40 |
created = models.DateTimeField(auto_now_add=True) |
|
41 |
||
42 |
private_feed_key = models.CharField(max_length=20, db_index=True, unique=True, blank=True, null=True, default=None) |
|
43 |
||
44 |
# denormalized fields |
|
45 |
last_text_version = models.ForeignKey("TextVersion", related_name='related_text', null=True, blank=True) |
|
46 |
title = models.TextField() |
|
47 |
||
48 |
objects = TextManager() |
|
49 |
||
50 |
def get_latest_version(self): |
|
51 |
return self.last_text_version |
|
52 |
||
53 |
def fetch_latest_version(self): |
|
54 |
versions = self.get_versions() |
|
55 |
if versions: |
|
56 |
return versions[0] |
|
57 |
else: |
|
58 |
return None |
|
59 |
||
60 |
def update_denorm_fields(self): |
|
61 |
real_last_text_version = self.fetch_latest_version() |
|
62 |
||
63 |
modif = False |
|
64 |
if real_last_text_version and real_last_text_version != self.last_text_version: |
|
65 |
self.last_text_version = real_last_text_version |
|
66 |
modif = True |
|
67 |
||
68 |
if real_last_text_version and real_last_text_version.title and real_last_text_version.title != self.title: |
|
69 |
self.title = real_last_text_version.title |
|
70 |
modif = True |
|
71 |
||
72 |
if real_last_text_version and real_last_text_version.modified != self.modified: |
|
73 |
self.modified = real_last_text_version.modified |
|
74 |
modif = True |
|
75 |
||
76 |
if modif: |
|
77 |
self.save() |
|
78 |
||
79 |
||
80 |
def get_title(self): |
|
81 |
return self.get_latest_version().title |
|
82 |
||
83 |
def get_versions(self): |
|
84 |
""" |
|
85 |
Versions with most recent first |
|
86 |
""" |
|
87 |
versions = TextVersion.objects.filter(text__exact=self).order_by('-created') |
|
88 |
# TODO: use new postgresql 8.4 row_number as extra select to do that |
|
89 |
for index in xrange(len(versions)): |
|
90 |
v = versions[index] |
|
91 |
# version_number is 1-based |
|
92 |
setattr(v, 'version_number', len(versions) - index) |
|
93 |
#for v in versions: |
|
94 |
# print v.created,v.id,v.version_number |
|
95 |
return versions |
|
96 |
||
97 |
def get_version(self, version_number): |
|
98 |
""" |
|
99 |
Get version number 'version_number' (1-based) |
|
100 |
""" |
|
101 |
version = TextVersion.objects.filter(text__exact=self).order_by('created')[version_number - 1:version_number][0] |
|
102 |
return version |
|
103 |
||
104 |
def get_inversed_versions(self): |
|
105 |
versions = TextVersion.objects.filter(text__exact=self).order_by('created') |
|
106 |
# TODO: use new postgresql 8.4 row_number as extra select to do that |
|
107 |
for index in xrange(len(versions)): |
|
108 |
v = versions[index] |
|
109 |
# version_number is 1-based |
|
110 |
setattr(v, 'version_number', index + 1) |
|
111 |
return versions |
|
112 |
||
113 |
def get_versions_number(self): |
|
114 |
return self.get_versions().count() |
|
115 |
||
116 |
def is_admin(self, adminkey=None): |
|
117 |
if adminkey and self.adminkey == adminkey: |
|
118 |
return True |
|
119 |
else: |
|
120 |
return False |
|
121 |
||
122 |
def revert_to_version(self, v_id): |
|
123 |
text_version = self.get_version(int(v_id)) |
|
124 |
new_text_version = TextVersion.objects.duplicate(text_version, True) |
|
125 |
return new_text_version |
|
126 |
||
127 |
def edit(self, new_title, new_format, new_content, new_tags=None, new_note=None, keep_comments=True, new_version=True): |
|
128 |
text_version = self.get_latest_version() |
|
129 |
||
130 |
if new_version: |
|
131 |
text_version = TextVersion.objects.duplicate(text_version, keep_comments) |
|
132 |
text_version.edit(new_title, new_format, new_content, new_tags, new_note, keep_comments) |
|
133 |
return text_version |
|
134 |
||
135 |
def __unicode__(self): |
|
136 |
return self.title |
|
137 |
||
138 |
DEFAULT_INPUT_FORMAT = getattr(settings, 'DEFAULT_INPUT_FORMAT', DEFAULT_INPUT_FORMAT_PANDOC) |
|
139 |
CHOICES_INPUT_FORMATS = getattr(settings, 'CHOICES_INPUT_FORMATS', CHOICES_INPUT_FORMATS_PANDOC) |
|
140 |
||
141 |
class TextVersionManager(models.Manager): |
|
142 |
||
143 |
def duplicate(self, text_version, duplicate_comments=True): |
|
144 |
#import pdb;pdb.set_trace() |
|
145 |
old_comment_set = set(text_version.comment_set.all()) |
|
146 |
text_version.id = None |
|
147 |
#import pdb;pdb.set_trace() |
|
148 |
text_version.save() |
|
149 |
||
150 |
duplicate_text_version = text_version |
|
151 |
||
152 |
if duplicate_comments: |
|
153 |
old_comment_map = {} |
|
154 |
while len(old_comment_set): |
|
155 |
for c in old_comment_set: |
|
156 |
if not c.reply_to or c.reply_to.id in old_comment_map: |
|
157 |
old_id = c.id |
|
158 |
old_comment_set.remove(c) |
|
159 |
reply_to = None |
|
160 |
if c.reply_to: |
|
161 |
reply_to = old_comment_map[c.reply_to.id] |
|
162 |
c2 = Comment.objects.duplicate(c, duplicate_text_version, reply_to) |
|
163 |
old_comment_map[old_id] = c2 |
|
164 |
break |
|
165 |
||
166 |
return duplicate_text_version |
|
167 |
||
168 |
class TextVersion(AuthorModel): |
|
169 |
modified = models.DateTimeField(auto_now=True) |
|
170 |
created = models.DateTimeField(auto_now_add=True) |
|
171 |
||
172 |
title = models.TextField(ugettext_lazy("Title")) |
|
173 |
format = models.CharField(ugettext_lazy("Format"), max_length=20, blank=False, default=DEFAULT_INPUT_FORMAT, choices=CHOICES_INPUT_FORMATS) |
|
174 |
content = models.TextField(ugettext_lazy("Content")) |
|
175 |
tags = TagField(ugettext_lazy("Tags"), max_length=1000) |
|
176 |
||
177 |
note = models.CharField(ugettext_lazy("Note"), max_length=100, null=True, blank=True) |
|
178 |
||
179 |
mod_posteriori = models.BooleanField(ugettext_lazy('Moderation a posteriori?'), default=True) |
|
180 |
||
181 |
text = models.ForeignKey("Text") |
|
182 |
||
183 |
objects = TextVersionManager() |
|
184 |
||
185 |
def get_content(self, format='html'): |
|
186 |
converted_content = pandoc_convert(self.content, self.format, format) |
|
187 |
return converted_content |
|
188 |
||
189 |
# def _get_comments(self, user = None, filter_reply = 0): |
|
190 |
# """ |
|
191 |
# get comments viewable by this user (user = None or user = AnonymousUser => everyone) |
|
192 |
# filter_reply = 0: comments and replies |
|
193 |
# 1: comments |
|
194 |
# 2: replies |
|
195 |
# """ |
|
196 |
# from cm.security import has_perm_on_text # should stay here to avoid circular dependencies |
|
197 |
# |
|
198 |
# if has_perm(user, 'can_view_unapproved_comment', self.text): |
|
199 |
# comments = self.comment_set.all() |
|
200 |
# elif has_perm(user, 'can_view_approved_comment', self.text): |
|
201 |
# comments = self.comment_set.filter(visible=True) |
|
|
17
a4be0b8a905d
bug fix : can view own comment on comment add on mod priori
reno
parents:
16
diff
changeset
|
202 |
# elif has_perm(user, 'can_view_comment_own', self.text): |
| 0 | 203 |
# comments = self.comment_set.filter(user=user) |
204 |
# else: |
|
205 |
# return Comment.objects.none() # empty queryset |
|
206 |
# if filter_reply: |
|
207 |
# comments = comments.filter) |
|
208 |
# return comments |
|
209 |
# |
|
210 |
# def get_comments_as_json(self, user = None): |
|
211 |
# return simplejson.dumps(self._get_comments(user, filter_reply=0)) |
|
212 |
# |
|
213 |
# def get_comments_and_replies(self, user = None): |
|
214 |
# return (self.get_comments(user), |
|
215 |
# self.get_replies(user)) |
|
216 |
# |
|
217 |
def get_comments(self): |
|
218 |
"Warning: data access without security" |
|
219 |
return self.comment_set.filter(reply_to=None, deleted=False) |
|
220 |
||
221 |
def get_replies(self): |
|
222 |
"Warning: data access without security" |
|
223 |
return self.comment_set.filter(~Q(reply_to == None), Q(deleted=False)) |
|
224 |
||
225 |
def __unicode__(self): |
|
226 |
return '<%d> %s' % (self.id, self.title) |
|
227 |
||
228 |
def edit(self, new_title, new_format, new_content, new_tags=None, new_note=None, keep_comments=True): # TODO : tags |
|
229 |
if not keep_comments : |
|
230 |
self.comment_set.all().delete() |
|
231 |
elif self.content != new_content or new_format != self.format: |
|
232 |
comments = self.get_comments() ; |
|
233 |
tomodify_comments, toremove_comments = compute_new_comment_positions(self.content, self.format, new_content, new_format, comments) |
|
234 |
#print "tomodify_comments",len(tomodify_comments) |
|
235 |
#print "toremove_comments",len(toremove_comments) |
|
236 |
[comment.save() for comment in tomodify_comments] |
|
237 |
[comment.delete() for comment in toremove_comments] |
|
238 |
self.title = new_title |
|
239 |
if new_tags: |
|
240 |
self.tags = new_tags |
|
241 |
if new_note: |
|
242 |
self.note = new_note |
|
243 |
self.content = new_content |
|
244 |
self.format = new_format |
|
245 |
self.save() |
|
246 |
||
247 |
class CommentManager(Manager): |
|
248 |
||
249 |
def duplicate(self, comment, text_version, reply_to=None): |
|
250 |
comment.id = None |
|
251 |
comment.text_version = text_version |
|
252 |
if reply_to: |
|
253 |
comment.reply_to = reply_to |
|
254 |
self.update_keys(comment) |
|
255 |
comment.save() |
|
256 |
return comment |
|
257 |
||
258 |
class Comment(PermanentModel, AuthorModel): |
|
259 |
modified = models.DateTimeField(auto_now=True) |
|
260 |
created = models.DateTimeField(auto_now_add=True) |
|
261 |
||
262 |
text_version = models.ForeignKey("TextVersion") |
|
263 |
||
264 |
# comment_set will be replies |
|
265 |
reply_to = models.ForeignKey("Comment", null=True, blank=True) |
|
266 |
||
267 |
title = models.TextField() |
|
268 |
content = models.TextField() |
|
269 |
content_html = models.TextField() |
|
270 |
||
271 |
format = models.CharField(_("Format:"), max_length=20, blank=False, default=DEFAULT_INPUT_FORMAT, choices=CHOICES_INPUT_FORMATS) |
|
272 |
||
273 |
tags = TagField() |
|
274 |
||
275 |
start_wrapper = models.IntegerField(null=True, blank=True) |
|
276 |
end_wrapper = models.IntegerField(null=True, blank=True) |
|
277 |
start_offset = models.IntegerField(null=True, blank=True) |
|
278 |
end_offset = models.IntegerField(null=True, blank=True) |
|
279 |
||
280 |
objects = CommentManager() |
|
281 |
||
282 |
def __unicode__(self): |
|
283 |
return '<%d> %s' % (self.id, self.title) |
|
284 |
||
285 |
def is_reply(self): |
|
286 |
return self.reply_to != None |
|
287 |
||
| 16 | 288 |
def is_thread_full_visible(self, own_user=None): |
289 |
""" |
|
290 |
own_user: comment belonging to this user are also visible |
|
291 |
""" |
|
292 |
if self.state == 'approved' or (own_user and self.user == own_user): |
|
293 |
if self.reply_to==None: |
|
294 |
return True |
|
295 |
else: |
|
296 |
return self.reply_to.is_thread_full_visible(own_user) |
|
297 |
return False |
|
298 |
||
| 0 | 299 |
def top_comment(self): |
300 |
if self.reply_to == None : |
|
301 |
return self |
|
302 |
else : |
|
303 |
return self.reply_to.top_comment() |
|
304 |
||
305 |
def depth(self): |
|
306 |
if self.reply_to == None : |
|
307 |
return 0 |
|
308 |
else : |
|
309 |
return 1 + self.reply_to.depth() |
|
310 |
||
311 |
def delete(self): |
|
312 |
PermanentModel.delete(self) |
|
313 |
# delete replies |
|
314 |
[c.delete() for c in self.comment_set.all()] |
|
315 |
||
316 |
# http://docs.djangoproject.com/en/dev/topics/files/#topics-files |
|
317 |
||
318 |
# default conf values |
|
319 |
DEFAULT_CONF = { |
|
320 |
'workspace_name' : 'Workspace', |
|
321 |
'site_url' : settings.SITE_URL, |
|
322 |
'email_from' : settings.DEFAULT_FROM_EMAIL, |
|
323 |
} |
|
324 |
||
325 |
from cm.role_models import change_role_model |
|
326 |
||
327 |
class ConfigurationManager(models.Manager): |
|
328 |
def set_workspace_name(self, workspace_name): |
|
329 |
if workspace_name and not self.get_key('workspace_name')!=u'Workspace': |
|
330 |
self.set_key('workspace_name', _(u"%(workspace_name)s's workspace") %{'workspace_name':workspace_name}) |
|
331 |
||
332 |
def get_key(self, key, default_value=None): |
|
333 |
try: |
|
334 |
return self.get(key=key).value |
|
335 |
except Configuration.DoesNotExist: |
|
336 |
return DEFAULT_CONF.get(key, default_value) |
|
337 |
||
338 |
def set_key(self, key, value): |
|
339 |
conf, created = self.get_or_create(key=key) |
|
340 |
if created or conf.value != value: |
|
341 |
conf.value = value |
|
342 |
conf.save() |
|
343 |
if key == 'workspace_role_model': |
|
344 |
change_role_model(value) |
|
345 |
||
346 |
def __getitem__(self, key): |
|
347 |
return self.get_key(key, None) |
|
348 |
||
349 |
import base64 |
|
350 |
||
351 |
class Configuration(models.Model): |
|
352 |
key = models.TextField(blank=False) # , unique=True cannot be added: creates error on mysql (?) |
|
353 |
raw_value = models.TextField(blank=False) |
|
354 |
||
355 |
def get_value(self): |
|
356 |
return pickle.loads(base64.b64decode(self.raw_value.encode('utf8'))) |
|
357 |
||
358 |
def set_value(self, value): |
|
359 |
self.raw_value = base64.b64encode(pickle.dumps(value, 0)).encode('utf8') |
|
360 |
||
361 |
value = property(get_value, set_value) |
|
362 |
||
363 |
objects = ConfigurationManager() |
|
364 |
||
365 |
def __unicode__(self): |
|
366 |
return '%s: %s' % (self.key, self.value) |
|
367 |
||
368 |
ApplicationConfiguration = Configuration.objects |
|
369 |
||
370 |
class AttachmentManager(KeyManager): |
|
371 |
def create_attachment(self, text_version, filename, data): |
|
372 |
attach = self.create(text_version=text_version) |
|
373 |
ff = ContentFile(data) |
|
374 |
attach.data.save(filename, ff) |
|
375 |
return attach |
|
376 |
||
377 |
class Attachment(KeyModel): |
|
378 |
data = models.FileField(upload_to="attachments/%Y/%m/%d/", max_length=1000) |
|
379 |
text_version = models.ForeignKey(TextVersion) |
|
380 |
||
381 |
objects = AttachmentManager() |
|
382 |
||
383 |
class NotificationManager(KeyManager): |
|
| 12 | 384 |
def create_notification(self, text, type, active, email_or_user): |
385 |
notification = self.create(text=text, type=type, active=active) |
|
386 |
notification.set_email_or_user(email_or_user) |
|
387 |
return notification |
|
| 0 | 388 |
|
| 12 | 389 |
def get_notifications(self, text, type, email_or_user): |
| 0 | 390 |
if isinstance(email_or_user,unicode): |
391 |
prev_notifications = Notification.objects.filter(text=text, type=type, email=email_or_user) |
|
392 |
else: |
|
393 |
prev_notifications = Notification.objects.filter(text=text, type=type, user=email_or_user) |
|
| 12 | 394 |
|
| 0 | 395 |
if prev_notifications: |
396 |
return prev_notifications[0] |
|
397 |
else: |
|
398 |
return None |
|
399 |
||
| 12 | 400 |
def set_notification(self, text, type, active, email_or_user): |
401 |
notification = self.get_notifications(text, type, email_or_user) |
|
402 |
if notification == None : |
|
403 |
self.create_notification(text, type, active, email_or_user) |
|
404 |
else : |
|
405 |
notification.active = active |
|
| 0 | 406 |
notification.save() |
407 |
||
408 |
class Notification(KeyModel, AuthorModel): |
|
409 |
text = models.ForeignKey(Text, null=True, blank=True) |
|
410 |
type = models.CharField(max_length=30, null=True, blank=True) |
|
411 |
active = models.BooleanField(default=True) # active = False means user desactivation |
|
412 |
||
413 |
objects = NotificationManager() |
|
414 |
||
415 |
def desactivate_notification_url(self): |
|
416 |
return reverse('desactivate-notification', args=[self.adminkey]) |
|
417 |
||
418 |
def desactivate(self): |
|
419 |
if self.type=='own': |
|
420 |
self.active = False |
|
421 |
self.save() |
|
422 |
else: |
|
423 |
self.delete() |
|
424 |
||
425 |
# right management |
|
426 |
class UserRoleManager(models.Manager): |
|
427 |
def create_userroles_text(self, text): |
|
428 |
# make sure every user has a userrole on this text |
|
429 |
for user in User.objects.all(): |
|
430 |
userrole, _ = self.get_or_create(user=user, text=text) |
|
431 |
# anon user |
|
432 |
userrole, _ = self.get_or_create(user=None, text=text) |
|
433 |
# anon global user |
|
434 |
global_userrole, _ = self.get_or_create(user=None, text=None) |
|
435 |
||
436 |
class UserRole(models.Model): |
|
437 |
role = models.ForeignKey("Role", null=True, blank=True) |
|
438 |
||
439 |
# user == null => anyone |
|
440 |
user = models.ForeignKey(User, null=True, blank=True) |
|
441 |
||
442 |
# text == null => any text (workspace role) |
|
443 |
text = models.ForeignKey(Text, null=True, blank=True) |
|
444 |
||
445 |
objects = UserRoleManager() |
|
446 |
||
447 |
class Meta: |
|
448 |
unique_together = (('role', 'user', 'text',)) |
|
449 |
||
450 |
def __unicode__(self): |
|
451 |
if self.role: |
|
452 |
rolename = _(self.role.name) |
|
453 |
else: |
|
454 |
rolename = '' |
|
455 |
||
456 |
if self.user: |
|
457 |
return u"%s: %s %s %s" % (self.__class__.__name__, self.user.username, self.text, rolename) |
|
458 |
else: |
|
459 |
return u"%s: *ALL* %s %s" % (self.__class__.__name__, self.text, rolename) |
|
460 |
||
461 |
def __repr__(self): |
|
462 |
return self.__unicode__() |
|
463 |
||
464 |
from cm.models_base import generate_key |
|
465 |
from cm.utils.misc import update |
|
466 |
||
467 |
class Role(models.Model): |
|
468 |
""" |
|
469 |
'Static' application roles |
|
470 |
""" |
|
471 |
name = models.CharField(ugettext_lazy('name'), max_length=50, unique=True) |
|
472 |
description = models.TextField(ugettext_lazy('description')) |
|
473 |
#order = models.IntegerField(unique=True) |
|
474 |
permissions = models.ManyToManyField(Permission, related_name="roles") |
|
475 |
||
476 |
global_scope = models.BooleanField('global scope', default=False) # applies to global scope only |
|
477 |
anon = models.BooleanField('anonymous', default=False) # role possible for anonymous users? |
|
478 |
||
479 |
def __unicode__(self): |
|
480 |
return _(self.name) |
|
481 |
||
482 |
def __hash__(self): |
|
483 |
return self.id |
|
484 |
||
485 |
def name_i18n(self): |
|
486 |
return _(self.name) |
|
487 |
||
488 |
from django.utils.safestring import mark_safe |
|
489 |
||
490 |
class RegistrationManager(KeyManager): |
|
491 |
def activate_user(self, activation_key): |
|
492 |
""" |
|
493 |
Validates an activation key and activates the corresponding |
|
494 |
``User`` if valid. |
|
495 |
If the key is valid , returns the ``User`` as second arg |
|
496 |
First is boolean indicating if user has just been activated |
|
497 |
""" |
|
498 |
# Make sure the key we're trying conforms to the pattern of a |
|
499 |
# SHA1 hash; if it doesn't, no point trying to look it up in |
|
500 |
# the database. |
|
501 |
try: |
|
502 |
profile = self.get(admin_key=activation_key) |
|
503 |
except self.model.DoesNotExist: |
|
504 |
return False, False |
|
505 |
user = profile.user |
|
506 |
activated = False |
|
507 |
if not user.is_active: |
|
508 |
user.is_active = True |
|
509 |
user.save() |
|
510 |
activated = True |
|
511 |
return (activated, user) |
|
512 |
||
513 |
def _create_manager(self, email, username, password, first_name, last_name): |
|
514 |
if username and email and password and len(User.objects.filter(username=username)) == 0: |
|
515 |
user = User.objects.create(username=username, email=email, first_name=first_name, last_name=last_name, is_active=True) |
|
516 |
user.set_password(password) |
|
517 |
user.save() |
|
518 |
||
519 |
profile = UserProfile.objects.create(user=user) |
|
520 |
||
521 |
manager = Role.objects.get(name='Manager') |
|
522 |
UserRole.objects.create(text=None, user=user, role=manager) |
|
523 |
return user |
|
524 |
else: |
|
525 |
return None |
|
526 |
||
527 |
||
528 |
def create_inactive_user(self, email, send_invitation, **kwargs): |
|
529 |
#prevent concurrent access |
|
530 |
cursor = connection.cursor() |
|
531 |
sql = "LOCK TABLE auth_user IN EXCLUSIVE MODE" |
|
532 |
cursor.execute(sql) |
|
533 |
||
534 |
try: |
|
535 |
user_with_email = User.objects.get(email__iexact=email) |
|
536 |
except User.DoesNotExist: |
|
537 |
user = User.objects.create(username=email, email=email) |
|
538 |
profile = UserProfile.objects.create(user=user) |
|
539 |
update(user, kwargs) |
|
540 |
update(profile, kwargs) |
|
541 |
||
542 |
user.is_active = False |
|
543 |
user.save() |
|
544 |
profile.save() |
|
545 |
||
546 |
note = kwargs.get('note', None) |
|
547 |
if send_invitation: |
|
548 |
profile.send_activation_email(note) |
|
549 |
return user |
|
550 |
else: |
|
551 |
return user_with_email |
|
552 |
||
553 |
||
554 |
from cm.utils.mail import send_mail |
|
555 |
||
556 |
class UserProfile(KeyModel): |
|
557 |
modified = models.DateTimeField(auto_now=True) |
|
558 |
created = models.DateTimeField(auto_now_add=True) |
|
559 |
||
560 |
user = models.ForeignKey(User, unique=True) |
|
561 |
||
562 |
allow_contact = models.BooleanField(ugettext_lazy(u'Allow contact'), default=True, help_text=ugettext_lazy(u"Allow email messages from other users")) |
|
563 |
preferred_language = models.CharField(ugettext_lazy(u'Preferred language'), max_length=2, default="en") |
|
564 |
is_temp = models.BooleanField(default=False) |
|
565 |
is_email_error = models.BooleanField(default=False) |
|
566 |
is_suspended = models.BooleanField(ugettext_lazy(u'Suspended access'), default=False) # used to disable access or to wait for approval when registering |
|
567 |
||
568 |
objects = RegistrationManager() |
|
569 |
||
570 |
class Meta: |
|
571 |
permissions = ( |
|
572 |
("can_create_user", "Can create user"), |
|
573 |
("can_delete_user", "Can delete user"), |
|
574 |
) |
|
575 |
||
576 |
def __unicode__(self): |
|
577 |
return unicode(self.user) |
|
578 |
||
579 |
def global_userrole(self): |
|
580 |
try: |
|
581 |
return UserRole.objects.get(user=self.user, text=None) |
|
582 |
except UserRole.DoesNotExist: |
|
583 |
return None |
|
584 |
||
585 |
def admin_print(self): |
|
586 |
if self.is_suspended: |
|
587 |
if self.user.is_active: |
|
588 |
return mark_safe('%s (%s)' % (self.user.username, _(u'suspended'),)) |
|
589 |
else: |
|
590 |
return mark_safe('%s (%s)' % (self.user.username, _(u'waiting approval'),)) |
|
591 |
else: |
|
592 |
if self.user.is_active: |
|
593 |
return mark_safe('%s' % self.user.username) |
|
594 |
else: |
|
595 |
email_username = self.user.email.split('@')[0] |
|
596 |
return mark_safe('%s (%s)' % (self.user.username, _(u'pending'),)) |
|
597 |
||
598 |
def simple_print(self): |
|
599 |
if self.user.is_active: |
|
600 |
return self.user.username |
|
601 |
else: |
|
602 |
return self.user.email |
|
603 |
||
604 |
def send_activation_email(self, note=None): |
|
605 |
self._send_act_invit_email(note=note, template='email/activation_email.txt') |
|
606 |
||
607 |
def send_invitation_email(self, note=None): |
|
608 |
self._send_act_invit_email(note=note, template='email/invitation_email.txt') |
|
609 |
||
610 |
def _send_act_invit_email(self, template, note=None): |
|
611 |
subject = _(u'Invitation') |
|
612 |
||
613 |
activate_url = reverse('user-activate', args=[self.adminkey]) |
|
614 |
message = render_to_string(template, |
|
615 |
{ |
|
616 |
'activate_url' : activate_url, |
|
617 |
'note' : note, |
|
618 |
'CONF': ApplicationConfiguration |
|
619 |
}) |
|
620 |
||
621 |
send_mail(subject, message, ApplicationConfiguration['email_from'], [self.user.email]) |
|
622 |
||
623 |
from django.db.models import signals |
|
624 |
||
625 |
#def create_profile(sender, **kwargs): |
|
626 |
# created = kwargs['created'] |
|
627 |
# if created: |
|
628 |
# user = kwargs['instance'] |
|
629 |
# UserProfile.objects.create(user = user) |
|
630 |
||
631 |
def delete_profile(sender, **kwargs): |
|
632 |
user_profile = kwargs['instance'] |
|
633 |
user = user_profile.user |
|
634 |
user.delete() |
|
635 |
||
636 |
#signals.post_save.connect(create_profile, sender=User) |
|
637 |
signals.post_delete.connect(delete_profile, sender=UserProfile) |
|
638 |
||
639 |
class ActivityManager(models.Manager): |
|
640 |
pass |
|
641 |
||
642 |
class Activity(models.Model): |
|
643 |
created = models.DateTimeField(auto_now_add=True) |
|
644 |
originator_user = models.ForeignKey(User, related_name='originator_activity', null=True, blank=True, default=None) |
|
645 |
text = models.ForeignKey(Text, null=True, blank=True, default=None) |
|
646 |
text_version = models.ForeignKey(TextVersion, null=True, blank=True, default=None) |
|
647 |
comment = models.ForeignKey(Comment, null=True, blank=True, default=None) |
|
648 |
user = models.ForeignKey(User, null=True, blank=True, default=None) |
|
649 |
type = models.CharField(max_length=30) |
|
650 |
ip = models.IPAddressField(null=True, blank=True, default=None) |
|
651 |
||
652 |
objects = ActivityManager() |
|
653 |
||
654 |
# viewable activities (i.e. now 'text-view') |
|
655 |
VIEWABLE_ACTIVITIES = { |
|
656 |
'view_comments' : ['comment_created', 'comment_removed'], |
|
657 |
'view_users' : ['user_created', 'user_activated', 'user_refused', 'user_enabled', 'user_approved', 'user_suspended'], |
|
658 |
'view_texts' : ['text_created', 'text_removed', 'text_edited', 'text_edited_new_version'], |
|
659 |
} |
|
660 |
ACTIVITIES_TYPES = reduce(list.__add__, VIEWABLE_ACTIVITIES.values()) |
|
661 |
||
662 |
IMGS = { |
|
663 |
'text_created' : u'page_add_small.png', |
|
664 |
'text_removed' : u'page_delete_small.png', |
|
665 |
'text_edited' : u'page_save_small.png', |
|
666 |
'text_edited_new_version' : u'page_save_small.png', |
|
667 |
'user_created' : u'user_add_small.png', |
|
668 |
'user_enabled' : u'user_add_small.png', |
|
669 |
'user_refused': u'user_delete_small.png', |
|
670 |
'user_suspended': u'user_delete_small.png', |
|
671 |
'user_approved': u'user_add_small.png', |
|
672 |
'user_activated' : u'user_go.png', |
|
673 |
'comment_created' : u'note_add_small.png', |
|
674 |
'comment_removed' : u'note_delete_small.png', |
|
675 |
} |
|
676 |
||
677 |
#type/msg |
|
678 |
MSGS = { |
|
| 5 | 679 |
'text_edited' : ugettext_lazy(u'Text %(link_to_text)s edited'), |
680 |
'text_edited_new_version' : ugettext_lazy(u'Text %(link_to_text)s edited (new version created)'), |
|
681 |
'text_created' : ugettext_lazy(u'Text %(link_to_text)s added'), |
|
682 |
'text_removed' : ugettext_lazy(u'Text %(link_to_text)s removed'), |
|
683 |
'comment_created' : ugettext_lazy(u'Comment %(link_to_comment)s added on text %(link_to_text)s'), |
|
684 |
'comment_removed' : ugettext_lazy(u'Comment %(link_to_comment)s removed from text %(link_to_text)s'), |
|
685 |
'user_created' : ugettext_lazy(u'User %(username)s added'), |
|
686 |
'user_enabled' : ugettext_lazy(u'User %(username)s access to workspace enabled'), |
|
687 |
'user_refused' : ugettext_lazy(u'User %(username)s access to workspace refused'), |
|
688 |
'user_suspended' : ugettext_lazy(u'User %(username)s access to workspace suspended'), |
|
689 |
'user_activated' : ugettext_lazy(u'User %(username)s access to workspace activated'), |
|
690 |
'user_approved' : ugettext_lazy(u'User %(username)s has activated his account'), |
|
| 0 | 691 |
} |
692 |
||
693 |
def is_same_user(self, other_activity): |
|
694 |
if (self.originator_user != None or other_activity.originator_user != None) and self.originator_user != other_activity.originator_user: |
|
695 |
return False |
|
696 |
else: |
|
697 |
return self.ip != None and self.ip == other_activity.ip |
|
698 |
||
699 |
def linkable_text_title(self, html=True, link=True): |
|
700 |
# html: whether or not output sould be html |
|
701 |
format_args = {'link':absolute_reverse('text-view', args=[self.text.key]), 'title':self.text.title} |
|
702 |
if html and not self.text.deleted : |
|
703 |
return mark_safe(u'<a href="%(link)s">%(title)s</a>' % format_args) |
|
704 |
else: |
|
705 |
if link and not self.text.deleted: |
|
706 |
return u'%(title)s (%(link)s)' % format_args |
|
707 |
else: |
|
708 |
return self.text.title ; |
|
709 |
||
710 |
def linkable_comment_title(self, html=True, link=True): |
|
711 |
if self.comment: |
|
712 |
format_args = {'link':absolute_reverse('text-view-show-comment', args=[self.text.key, self.comment.key]), 'title':self.comment.title} |
|
713 |
if html and not self.comment.deleted and not self.text.deleted: |
|
714 |
return mark_safe(u'<a href="%(link)s">%(title)s</a>' % format_args) |
|
715 |
else : |
|
716 |
if link and not self.comment.deleted and not self.text.deleted: |
|
717 |
return u'%(title)s (%(link)s)' % format_args |
|
718 |
else: |
|
719 |
return self.comment.title ; |
|
720 |
else: |
|
721 |
return u'' |
|
722 |
||
723 |
def __unicode__(self): |
|
724 |
return u"%s %s %s %s %s" % (self.type, self.originator_user, self.text, self.comment, self.user) |
|
725 |
||
726 |
def img_name(self): |
|
727 |
return self.IMGS.get(self.type) |
|
728 |
||
729 |
def printable_data_nohtml_link(self): |
|
730 |
return self.printable_data(html=False, link=True) |
|
731 |
||
732 |
def printable_data(self, html=True, link=True): |
|
733 |
msg = self.MSGS.get(self.type, None) |
|
734 |
if msg: |
|
735 |
return mark_safe(msg % { |
|
736 |
'link_to_text' : self.linkable_text_title(html=html, link=link) if self.text else None, |
|
737 |
'link_to_comment' : self.linkable_comment_title(html=html, link=link) if self.comment else None, |
|
738 |
'username' : self.user.username if self.user else None, |
|
739 |
}) |
|
740 |
return '' |
|
741 |
||
742 |
def printable_metadata(self, html=True): |
|
743 |
ret = [] |
|
744 |
if self.type == 'user_activated': |
|
745 |
ret.append(_(u'by "%(username)s"') % {'username' : self.originator_user.username}) |
|
746 |
ret.append(' ') |
|
747 |
ret.append(_(u"%(time_since)s ago") % {'time_since':timesince(self.created)}) |
|
748 |
return ''.join(ret) |
|
749 |
||
750 |
def printable_metadata_absolute(self, html=True): |
|
751 |
ret = [] |
|
752 |
if self.type == 'user_activated': |
|
753 |
ret.append(_(u'by "%(username)s"') % {'username' : self.originator_user.username}) |
|
754 |
ret.append(u' ') |
|
755 |
ret.append(datetime_to_user_str(self.created)) |
|
756 |
return u''.join(ret) |
|
757 |
||
758 |
import cm.denorm_engine |
|
759 |
import cm.admin |
|
760 |
import cm.main |
|
761 |
import cm.activity |
|
762 |
import cm.notifications |
|
763 |
||
764 |
# we fill username with email so we need a bigger value |
|
765 |
User._meta.get_field('username').max_length = 75 |