2 from django.contrib.contenttypes.models import ContentType
\r
3 from django.contrib.auth.models import User as DjangoUser, AnonymousUser as DjangoAnonymousUser
\r
4 from django.db.models import Q
\r
6 from hashlib import md5
\r
8 from md5 import new as md5
\r
11 from random import Random
\r
13 from django.utils.translation import ugettext as _
\r
14 import django.dispatch
\r
17 QUESTIONS_PER_PAGE_CHOICES = (
\r
23 class UserManager(CachedManager):
\r
24 def get_site_owner(self):
\r
25 return self.all().order_by('date_joined')[0]
\r
27 class AnonymousUser(DjangoAnonymousUser):
\r
28 def get_visible_answers(self, question):
\r
29 return question.answers.filter_state(deleted=False)
\r
31 def can_view_deleted_post(self, post):
\r
34 def can_vote_up(self):
\r
37 def can_vote_down(self):
\r
40 def can_flag_offensive(self, post=None):
\r
43 def can_view_offensive_flags(self, post=None):
\r
46 def can_comment(self, post):
\r
49 def can_like_comment(self, comment):
\r
52 def can_edit_comment(self, comment):
\r
55 def can_delete_comment(self, comment):
\r
58 def can_convert_to_comment(self, answer):
\r
61 def can_accept_answer(self, answer):
\r
64 def can_create_tags(self):
\r
67 def can_edit_post(self, post):
\r
70 def can_wikify(self, post):
\r
73 def can_cancel_wiki(self, post):
\r
76 def can_retag_questions(self):
\r
79 def can_close_question(self, question):
\r
82 def can_reopen_question(self, question):
\r
85 def can_delete_post(self, post):
\r
88 def can_upload_files(self):
\r
91 def true_if_is_super_or_staff(fn):
\r
92 def decorated(self, *args, **kwargs):
\r
93 return self.is_superuser or self.is_staff or fn(self, *args, **kwargs)
\r
96 class User(BaseModel, DjangoUser):
\r
97 is_approved = models.BooleanField(default=False)
\r
98 email_isvalid = models.BooleanField(default=False)
\r
100 reputation = models.PositiveIntegerField(default=0)
\r
101 gold = models.PositiveIntegerField(default=0)
\r
102 silver = models.PositiveIntegerField(default=0)
\r
103 bronze = models.PositiveIntegerField(default=0)
\r
105 last_seen = models.DateTimeField(default=datetime.datetime.now)
\r
106 real_name = models.CharField(max_length=100, blank=True)
\r
107 website = models.URLField(max_length=200, blank=True)
\r
108 location = models.CharField(max_length=100, blank=True)
\r
109 date_of_birth = models.DateField(null=True, blank=True)
\r
110 about = models.TextField(blank=True)
\r
112 subscriptions = models.ManyToManyField('Node', related_name='subscribers', through='QuestionSubscription')
\r
114 vote_up_count = DenormalizedField("actions", canceled=False, action_type="voteup")
\r
115 vote_down_count = DenormalizedField("actions", canceled=False, action_type="votedown")
\r
117 objects = UserManager()
\r
120 def gravatar(self):
\r
121 return md5(self.email).hexdigest()
\r
123 def save(self, *args, **kwargs):
\r
124 if self.reputation < 0:
\r
125 self.reputation = 0
\r
127 new = not bool(self.id)
\r
129 super(User, self).save(*args, **kwargs)
\r
132 sub_settings = SubscriptionSettings(user=self)
\r
133 sub_settings.save()
\r
135 def get_absolute_url(self):
\r
136 return self.get_profile_url()
\r
138 def get_messages(self):
\r
140 for m in self.message_set.all():
\r
141 messages.append(m.message)
\r
144 def delete_messages(self):
\r
145 self.message_set.all().delete()
\r
148 def get_profile_url(self):
\r
149 return ('user_profile', (), {'id': self.id, 'slug': slugify(self.username)})
\r
151 def get_profile_link(self):
\r
152 profile_link = u'<a href="%s">%s</a>' % (self.get_profile_url(),self.username)
\r
153 return mark_safe(profile_link)
\r
155 def get_visible_answers(self, question):
\r
156 return question.answers.filter_state(deleted=False)
\r
158 def get_vote_count_today(self):
\r
159 today = datetime.date.today()
\r
160 return self.actions.filter(canceled=False, action_type__in=("voteup", "votedown"),
\r
161 action_date__range=(today - datetime.timedelta(days=1), today)).count()
\r
163 def get_reputation_by_upvoted_today(self):
\r
164 today = datetime.datetime.now()
\r
165 sum = self.reputes.filter(reputed_at__range=(today - datetime.timedelta(days=1), today)).aggregate(models.Sum('value'))
\r
166 #todo: redo this, maybe transform in the daily cap
\r
167 #if sum.get('value__sum', None) is not None: return sum['value__sum']
\r
170 def get_flagged_items_count_today(self):
\r
171 today = datetime.date.today()
\r
172 return self.actions.filter(canceled=False, action_type="flag",
\r
173 action_date__range=(today - datetime.timedelta(days=1), today)).count()
\r
175 @true_if_is_super_or_staff
\r
176 def can_view_deleted_post(self, post):
\r
177 return post.author == self
\r
179 @true_if_is_super_or_staff
\r
180 def can_vote_up(self):
\r
181 return self.reputation >= int(settings.REP_TO_VOTE_UP)
\r
183 @true_if_is_super_or_staff
\r
184 def can_vote_down(self):
\r
185 return self.reputation >= int(settings.REP_TO_VOTE_DOWN)
\r
187 def can_flag_offensive(self, post=None):
\r
188 if post is not None and post.author == self:
\r
190 return self.is_superuser or self.is_staff or self.reputation >= int(settings.REP_TO_FLAG)
\r
192 @true_if_is_super_or_staff
\r
193 def can_view_offensive_flags(self, post=None):
\r
194 if post is not None and post.author == self:
\r
196 return self.reputation >= int(settings.REP_TO_VIEW_FLAGS)
\r
198 @true_if_is_super_or_staff
\r
199 def can_comment(self, post):
\r
200 return self == post.author or self.reputation >= int(settings.REP_TO_COMMENT
\r
201 ) or (post.__class__.__name__ == "Answer" and self == post.question.author)
\r
203 @true_if_is_super_or_staff
\r
204 def can_like_comment(self, comment):
\r
205 return self != comment.author and (self.reputation >= int(settings.REP_TO_LIKE_COMMENT))
\r
207 @true_if_is_super_or_staff
\r
208 def can_edit_comment(self, comment):
\r
209 return (comment.author == self and comment.added_at >= datetime.datetime.now() - datetime.timedelta(minutes=60)
\r
210 ) or self.is_superuser
\r
212 @true_if_is_super_or_staff
\r
213 def can_delete_comment(self, comment):
\r
214 return self == comment.author or self.reputation >= int(settings.REP_TO_DELETE_COMMENTS)
\r
216 def can_convert_to_comment(self, answer):
\r
217 return (not answer.marked) and (self.is_superuser or self.is_staff or answer.author == self or self.reputation >= int(settings.REP_TO_CONVERT_TO_COMMENT))
\r
219 @true_if_is_super_or_staff
\r
220 def can_accept_answer(self, answer):
\r
221 return self == answer.question.author
\r
223 @true_if_is_super_or_staff
\r
224 def can_create_tags(self):
\r
225 return self.reputation >= int(settings.REP_TO_CREATE_TAGS)
\r
227 @true_if_is_super_or_staff
\r
228 def can_edit_post(self, post):
\r
229 return self == post.author or self.reputation >= int(settings.REP_TO_EDIT_OTHERS
\r
230 ) or (post.nis.wiki and self.reputation >= int(settings.REP_TO_EDIT_WIKI))
\r
232 @true_if_is_super_or_staff
\r
233 def can_wikify(self, post):
\r
234 return self == post.author or self.reputation >= int(settings.REP_TO_WIKIFY)
\r
236 @true_if_is_super_or_staff
\r
237 def can_cancel_wiki(self, post):
\r
238 return self == post.author
\r
240 @true_if_is_super_or_staff
\r
241 def can_retag_questions(self):
\r
242 return self.reputation >= int(settings.REP_TO_RETAG)
\r
244 @true_if_is_super_or_staff
\r
245 def can_close_question(self, question):
\r
246 return (self == question.author and self.reputation >= int(settings.REP_TO_CLOSE_OWN)
\r
247 ) or self.reputation >= int(settings.REP_TO_CLOSE_OTHERS)
\r
249 @true_if_is_super_or_staff
\r
250 def can_reopen_question(self, question):
\r
251 return self == question.author and self.reputation >= settings.REP_TO_REOPEN_OWN
\r
253 @true_if_is_super_or_staff
\r
254 def can_delete_post(self, post):
\r
255 if post.node_type == "comment":
\r
256 return self.can_delete_comment(post)
\r
258 return (self == post.author and (post.__class__.__name__ == "Answer" or
\r
259 not post.answers.exclude(author=self).count()))
\r
261 @true_if_is_super_or_staff
\r
262 def can_upload_files(self):
\r
263 return self.reputation >= int(settings.REP_TO_UPLOAD)
\r
265 def check_password(self, old_passwd):
\r
266 self.__dict__.update(self.__class__.objects.filter(id=self.id).values('password')[0])
\r
267 return DjangoUser.check_password(self, old_passwd)
\r
271 app_label = 'forum'
\r
273 class SubscriptionSettings(models.Model):
\r
274 user = models.OneToOneField(User, related_name='subscription_settings')
\r
276 enable_notifications = models.BooleanField(default=True)
\r
279 member_joins = models.CharField(max_length=1, default='n')
\r
280 new_question = models.CharField(max_length=1, default='d')
\r
281 new_question_watched_tags = models.CharField(max_length=1, default='i')
\r
282 subscribed_questions = models.CharField(max_length=1, default='i')
\r
285 all_questions = models.BooleanField(default=False)
\r
286 all_questions_watched_tags = models.BooleanField(default=False)
\r
287 questions_asked = models.BooleanField(default=True)
\r
288 questions_answered = models.BooleanField(default=True)
\r
289 questions_commented = models.BooleanField(default=False)
\r
290 questions_viewed = models.BooleanField(default=False)
\r
292 #notify activity on subscribed
\r
293 notify_answers = models.BooleanField(default=True)
\r
294 notify_reply_to_comments = models.BooleanField(default=True)
\r
295 notify_comments_own_post = models.BooleanField(default=True)
\r
296 notify_comments = models.BooleanField(default=False)
\r
297 notify_accepted = models.BooleanField(default=False)
\r
300 app_label = 'forum'
\r
302 from forum.utils.time import one_day_from_now
\r
304 class ValidationHashManager(models.Manager):
\r
305 def _generate_md5_hash(self, user, type, hash_data, seed):
\r
306 return md5("%s%s%s%s" % (seed, "".join(map(str, hash_data)), user.id, type)).hexdigest()
\r
308 def create_new(self, user, type, hash_data=[], expiration=None):
\r
309 seed = ''.join(Random().sample(string.letters+string.digits, 12))
\r
310 hash = self._generate_md5_hash(user, type, hash_data, seed)
\r
312 obj = ValidationHash(hash_code=hash, seed=seed, user=user, type=type)
\r
314 if expiration is not None:
\r
315 obj.expiration = expiration
\r
324 def validate(self, hash, user, type, hash_data=[]):
\r
326 obj = self.get(hash_code=hash)
\r
330 if obj.type != type:
\r
333 if obj.user != user:
\r
336 valid = (obj.hash_code == self._generate_md5_hash(obj.user, type, hash_data, obj.seed))
\r
339 if obj.expiration < datetime.datetime.now():
\r
348 class ValidationHash(models.Model):
\r
349 hash_code = models.CharField(max_length=255,unique=True)
\r
350 seed = models.CharField(max_length=12)
\r
351 expiration = models.DateTimeField(default=one_day_from_now)
\r
352 type = models.CharField(max_length=12)
\r
353 user = models.ForeignKey(User)
\r
355 objects = ValidationHashManager()
\r
358 unique_together = ('user', 'type')
\r
359 app_label = 'forum'
\r
362 return self.hash_code
\r
364 class AuthKeyUserAssociation(models.Model):
\r
365 key = models.CharField(max_length=255,null=False,unique=True)
\r
366 provider = models.CharField(max_length=64)
\r
367 user = models.ForeignKey(User, related_name="auth_keys")
\r
368 added_at = models.DateTimeField(default=datetime.datetime.now)
\r
371 app_label = 'forum'
\r