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_edit_post(self, post):
\r
67 def can_wikify(self, post):
\r
70 def can_retag_questions(self):
\r
73 def can_close_question(self, question):
\r
76 def can_reopen_question(self, question):
\r
79 def can_delete_post(self, post):
\r
82 def can_upload_files(self):
\r
85 def true_if_is_super_or_staff(fn):
\r
86 def decorated(self, *args, **kwargs):
\r
87 return self.is_superuser or self.is_staff or fn(self, *args, **kwargs)
\r
90 class User(BaseModel, DjangoUser):
\r
91 is_approved = models.BooleanField(default=False)
\r
92 email_isvalid = models.BooleanField(default=False)
\r
94 reputation = models.PositiveIntegerField(default=0)
\r
95 gold = models.PositiveIntegerField(default=0)
\r
96 silver = models.PositiveIntegerField(default=0)
\r
97 bronze = models.PositiveIntegerField(default=0)
\r
99 last_seen = models.DateTimeField(default=datetime.datetime.now)
\r
100 real_name = models.CharField(max_length=100, blank=True)
\r
101 website = models.URLField(max_length=200, blank=True)
\r
102 location = models.CharField(max_length=100, blank=True)
\r
103 date_of_birth = models.DateField(null=True, blank=True)
\r
104 about = models.TextField(blank=True)
\r
106 subscriptions = models.ManyToManyField('Node', related_name='subscribers', through='QuestionSubscription')
\r
108 vote_up_count = DenormalizedField("actions", canceled=False, action_type="voteup")
\r
109 vote_down_count = DenormalizedField("actions", canceled=False, action_type="votedown")
\r
111 objects = UserManager()
\r
114 def gravatar(self):
\r
115 return md5(self.email).hexdigest()
\r
117 def save(self, *args, **kwargs):
\r
118 if self.reputation < 0:
\r
119 self.reputation = 0
\r
121 new = not bool(self.id)
\r
123 super(User, self).save(*args, **kwargs)
\r
126 sub_settings = SubscriptionSettings(user=self)
\r
127 sub_settings.save()
\r
129 def get_absolute_url(self):
\r
130 return self.get_profile_url()
\r
132 def get_messages(self):
\r
134 for m in self.message_set.all():
\r
135 messages.append(m.message)
\r
138 def delete_messages(self):
\r
139 self.message_set.all().delete()
\r
142 def get_profile_url(self):
\r
143 return ('user_profile', (), {'id': self.id, 'slug': slugify(self.username)})
\r
145 def get_profile_link(self):
\r
146 profile_link = u'<a href="%s">%s</a>' % (self.get_profile_url(),self.username)
\r
147 return mark_safe(profile_link)
\r
149 def get_visible_answers(self, question):
\r
150 return question.answers.filter_state(deleted=False)
\r
152 def get_vote_count_today(self):
\r
153 today = datetime.date.today()
\r
154 return self.actions.filter(canceled=False, action_type__in=("voteup", "votedown"),
\r
155 action_date__range=(today - datetime.timedelta(days=1), today)).count()
\r
157 def get_reputation_by_upvoted_today(self):
\r
158 today = datetime.datetime.now()
\r
159 sum = self.reputes.filter(reputed_at__range=(today - datetime.timedelta(days=1), today)).aggregate(models.Sum('value'))
\r
160 #todo: redo this, maybe transform in the daily cap
\r
161 #if sum.get('value__sum', None) is not None: return sum['value__sum']
\r
164 def get_flagged_items_count_today(self):
\r
165 today = datetime.date.today()
\r
166 return self.actions.filter(canceled=False, action_type="flag",
\r
167 action_date__range=(today - datetime.timedelta(days=1), today)).count()
\r
169 @true_if_is_super_or_staff
\r
170 def can_view_deleted_post(self, post):
\r
171 return post.author == self
\r
173 @true_if_is_super_or_staff
\r
174 def can_vote_up(self):
\r
175 return self.reputation >= int(settings.REP_TO_VOTE_UP)
\r
177 @true_if_is_super_or_staff
\r
178 def can_vote_down(self):
\r
179 return self.reputation >= int(settings.REP_TO_VOTE_DOWN)
\r
181 def can_flag_offensive(self, post=None):
\r
182 if post is not None and post.author == self:
\r
184 return self.is_superuser or self.is_staff or self.reputation >= int(settings.REP_TO_FLAG)
\r
186 @true_if_is_super_or_staff
\r
187 def can_view_offensive_flags(self, post=None):
\r
188 if post is not None and post.author == self:
\r
190 return self.reputation >= int(settings.REP_TO_VIEW_FLAGS)
\r
192 @true_if_is_super_or_staff
\r
193 def can_comment(self, post):
\r
194 return self == post.author or self.reputation >= int(settings.REP_TO_COMMENT
\r
195 ) or (post.__class__.__name__ == "Answer" and self == post.question.author)
\r
197 @true_if_is_super_or_staff
\r
198 def can_like_comment(self, comment):
\r
199 return self != comment.author and (self.reputation >= int(settings.REP_TO_LIKE_COMMENT))
\r
201 @true_if_is_super_or_staff
\r
202 def can_edit_comment(self, comment):
\r
203 return (comment.author == self and comment.added_at >= datetime.datetime.now() - datetime.timedelta(minutes=60)
\r
204 ) or self.is_superuser
\r
206 @true_if_is_super_or_staff
\r
207 def can_delete_comment(self, comment):
\r
208 return self == comment.author or self.reputation >= int(settings.REP_TO_DELETE_COMMENTS)
\r
210 def can_convert_to_comment(self, answer):
\r
211 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
213 @true_if_is_super_or_staff
\r
214 def can_accept_answer(self, answer):
\r
215 return self == answer.question.author
\r
217 @true_if_is_super_or_staff
\r
218 def can_edit_post(self, post):
\r
219 return self == post.author or self.reputation >= int(settings.REP_TO_EDIT_OTHERS
\r
220 ) or (post.nis.wiki and self.reputation >= int(settings.REP_TO_EDIT_WIKI))
\r
222 @true_if_is_super_or_staff
\r
223 def can_wikify(self, post):
\r
224 return self == post.author or self.reputation >= int(settings.REP_TO_WIKIFY)
\r
226 @true_if_is_super_or_staff
\r
227 def can_retag_questions(self):
\r
228 return self.reputation >= int(settings.REP_TO_RETAG)
\r
230 @true_if_is_super_or_staff
\r
231 def can_close_question(self, question):
\r
232 return (self == question.author and self.reputation >= int(settings.REP_TO_CLOSE_OWN)
\r
233 ) or self.reputation >= int(settings.REP_TO_CLOSE_OTHERS)
\r
235 @true_if_is_super_or_staff
\r
236 def can_reopen_question(self, question):
\r
237 return self == question.author and self.reputation >= settings.REP_TO_REOPEN_OWN
\r
239 @true_if_is_super_or_staff
\r
240 def can_delete_post(self, post):
\r
241 if post.node_type == "comment":
\r
242 return self.can_delete_comment(post)
\r
244 return (self == post.author and (post.__class__.__name__ == "Answer" or
\r
245 not post.answers.exclude(author=self).count()))
\r
247 @true_if_is_super_or_staff
\r
248 def can_upload_files(self):
\r
249 return self.reputation >= int(settings.REP_TO_UPLOAD)
\r
252 app_label = 'forum'
\r
254 class SubscriptionSettings(models.Model):
\r
255 user = models.OneToOneField(User, related_name='subscription_settings')
\r
257 enable_notifications = models.BooleanField(default=True)
\r
260 member_joins = models.CharField(max_length=1, default='n')
\r
261 new_question = models.CharField(max_length=1, default='d')
\r
262 new_question_watched_tags = models.CharField(max_length=1, default='i')
\r
263 subscribed_questions = models.CharField(max_length=1, default='i')
\r
266 all_questions = models.BooleanField(default=False)
\r
267 all_questions_watched_tags = models.BooleanField(default=False)
\r
268 questions_asked = models.BooleanField(default=True)
\r
269 questions_answered = models.BooleanField(default=True)
\r
270 questions_commented = models.BooleanField(default=False)
\r
271 questions_viewed = models.BooleanField(default=False)
\r
273 #notify activity on subscribed
\r
274 notify_answers = models.BooleanField(default=True)
\r
275 notify_reply_to_comments = models.BooleanField(default=True)
\r
276 notify_comments_own_post = models.BooleanField(default=True)
\r
277 notify_comments = models.BooleanField(default=False)
\r
278 notify_accepted = models.BooleanField(default=False)
\r
281 app_label = 'forum'
\r
283 from forum.utils.time import one_day_from_now
\r
285 class ValidationHashManager(models.Manager):
\r
286 def _generate_md5_hash(self, user, type, hash_data, seed):
\r
287 return md5("%s%s%s%s" % (seed, "".join(map(str, hash_data)), user.id, type)).hexdigest()
\r
289 def create_new(self, user, type, hash_data=[], expiration=None):
\r
290 seed = ''.join(Random().sample(string.letters+string.digits, 12))
\r
291 hash = self._generate_md5_hash(user, type, hash_data, seed)
\r
293 obj = ValidationHash(hash_code=hash, seed=seed, user=user, type=type)
\r
295 if expiration is not None:
\r
296 obj.expiration = expiration
\r
305 def validate(self, hash, user, type, hash_data=[]):
\r
307 obj = self.get(hash_code=hash)
\r
311 if obj.type != type:
\r
314 if obj.user != user:
\r
317 valid = (obj.hash_code == self._generate_md5_hash(obj.user, type, hash_data, obj.seed))
\r
320 if obj.expiration < datetime.datetime.now():
\r
329 class ValidationHash(models.Model):
\r
330 hash_code = models.CharField(max_length=255,unique=True)
\r
331 seed = models.CharField(max_length=12)
\r
332 expiration = models.DateTimeField(default=one_day_from_now)
\r
333 type = models.CharField(max_length=12)
\r
334 user = models.ForeignKey(User)
\r
336 objects = ValidationHashManager()
\r
339 unique_together = ('user', 'type')
\r
340 app_label = 'forum'
\r
343 return self.hash_code
\r
345 class AuthKeyUserAssociation(models.Model):
\r
346 key = models.CharField(max_length=255,null=False,unique=True)
\r
347 provider = models.CharField(max_length=64)
\r
348 user = models.ForeignKey(User, related_name="auth_keys")
\r
349 added_at = models.DateTimeField(default=datetime.datetime.now)
\r
352 app_label = 'forum'
\r