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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
from question import Question
from answer import Answer
from q_and_a import QuestionRevision, QuestionView, AnonymousAnswer, AnonymousQuestion, AnswerRevision, FavoriteQuestion
from tag import Tag, MarkedTag
from meta import Vote, Comment, FlaggedItem
from user import Activity, AnonymousEmail, EmailFeedSetting
from repute import Badge, Award, Repute
from base import *
# User extend properties
QUESTIONS_PER_PAGE_CHOICES = (
(10, u'10'),
(30, u'30'),
(50, u'50'),
)
def user_is_username_taken(cls,username):
try:
cls.objects.get(username=username)
return True
except cls.MultipleObjectsReturned:
return True
except cls.DoesNotExist:
return False
def user_get_q_sel_email_feed_frequency(self):
#print 'looking for frequency for user %s' % self
try:
feed_setting = EmailFeedSetting.objects.get(subscriber=self,feed_type='q_sel')
except Exception, e:
#print 'have error %s' % e.message
raise e
#print 'have freq=%s' % feed_setting.frequency
return feed_setting.frequency
User.add_to_class('is_approved', models.BooleanField(default=False))
User.add_to_class('email_isvalid', models.BooleanField(default=False))
User.add_to_class('email_key', models.CharField(max_length=32, null=True))
User.add_to_class('reputation', models.PositiveIntegerField(default=1))
User.add_to_class('gravatar', models.CharField(max_length=32))
User.add_to_class('favorite_questions',
models.ManyToManyField(Question, through=FavoriteQuestion,
related_name='favorited_by'))
User.add_to_class('badges', models.ManyToManyField(Badge, through=Award,
related_name='awarded_to'))
User.add_to_class('gold', models.SmallIntegerField(default=0))
User.add_to_class('silver', models.SmallIntegerField(default=0))
User.add_to_class('bronze', models.SmallIntegerField(default=0))
User.add_to_class('questions_per_page',
models.SmallIntegerField(choices=QUESTIONS_PER_PAGE_CHOICES, default=10))
User.add_to_class('last_seen',
models.DateTimeField(default=datetime.datetime.now))
User.add_to_class('real_name', models.CharField(max_length=100, blank=True))
User.add_to_class('website', models.URLField(max_length=200, blank=True))
User.add_to_class('location', models.CharField(max_length=100, blank=True))
User.add_to_class('date_of_birth', models.DateField(null=True, blank=True))
User.add_to_class('about', models.TextField(blank=True))
User.add_to_class('is_username_taken',classmethod(user_is_username_taken))
User.add_to_class('get_q_sel_email_feed_frequency',user_get_q_sel_email_feed_frequency)
User.add_to_class('hide_ignored_questions', models.BooleanField(default=False))
User.add_to_class('tag_filter_setting',
models.CharField(
max_length=16,
choices=TAG_EMAIL_FILTER_CHOICES,
default='ignored'
)
)
# custom signal
tags_updated = django.dispatch.Signal(providing_args=["question"])
edit_question_or_answer = django.dispatch.Signal(providing_args=["instance", "modified_by"])
delete_post_or_answer = django.dispatch.Signal(providing_args=["instance", "deleted_by"])
mark_offensive = django.dispatch.Signal(providing_args=["instance", "mark_by"])
user_updated = django.dispatch.Signal(providing_args=["instance", "updated_by"])
user_logged_in = django.dispatch.Signal(providing_args=["session"])
def get_messages(self):
messages = []
for m in self.message_set.all():
messages.append(m.message)
return messages
def delete_messages(self):
self.message_set.all().delete()
def get_profile_url(self):
"""Returns the URL for this User's profile."""
return '%s%s/' % (reverse('user', args=[self.id]), slugify(self.username))
def get_profile_link(self):
profile_link = u'<a href="%s">%s</a>' % (self.get_profile_url(),self.username)
logging.debug('in get profile link %s' % profile_link)
return mark_safe(profile_link)
User.add_to_class('get_profile_url', get_profile_url)
User.add_to_class('get_profile_link', get_profile_link)
User.add_to_class('get_messages', get_messages)
User.add_to_class('delete_messages', delete_messages)
def calculate_gravatar_hash(instance, **kwargs):
"""Calculates a User's gravatar hash from their email address."""
if kwargs.get('raw', False):
return
instance.gravatar = hashlib.md5(instance.email).hexdigest()
def record_ask_event(instance, created, **kwargs):
if created:
activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ASK_QUESTION)
activity.save()
def record_answer_event(instance, created, **kwargs):
if created:
activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ANSWER)
activity.save()
def record_comment_event(instance, created, **kwargs):
if created:
from django.contrib.contenttypes.models import ContentType
question_type = ContentType.objects.get_for_model(Question)
question_type_id = question_type.id
if (instance.content_type_id == question_type_id):
type = TYPE_ACTIVITY_COMMENT_QUESTION
else:
type = TYPE_ACTIVITY_COMMENT_ANSWER
activity = Activity(user=instance.user, active_at=instance.added_at, content_object=instance, activity_type=type)
activity.save()
def record_revision_question_event(instance, created, **kwargs):
if created and instance.revision <> 1:
activity = Activity(user=instance.author, active_at=instance.revised_at, content_object=instance, activity_type=TYPE_ACTIVITY_UPDATE_QUESTION)
activity.save()
def record_revision_answer_event(instance, created, **kwargs):
if created and instance.revision <> 1:
activity = Activity(user=instance.author, active_at=instance.revised_at, content_object=instance, activity_type=TYPE_ACTIVITY_UPDATE_ANSWER)
activity.save()
def record_award_event(instance, created, **kwargs):
"""
After we awarded a badge to user, we need to record this activity and notify user.
We also recaculate awarded_count of this badge and user information.
"""
if created:
activity = Activity(user=instance.user, active_at=instance.awarded_at, content_object=instance,
activity_type=TYPE_ACTIVITY_PRIZE)
activity.save()
instance.badge.awarded_count += 1
instance.badge.save()
if instance.badge.type == Badge.GOLD:
instance.user.gold += 1
if instance.badge.type == Badge.SILVER:
instance.user.silver += 1
if instance.badge.type == Badge.BRONZE:
instance.user.bronze += 1
instance.user.save()
def notify_award_message(instance, created, **kwargs):
"""
Notify users when they have been awarded badges by using Django message.
"""
if created:
user = instance.user
user.message_set.create(message=u"Congratulations, you have received a badge '%s'" % instance.badge.name)
def record_answer_accepted(instance, created, **kwargs):
"""
when answer is accepted, we record this for question author - who accepted it.
"""
if not created and instance.accepted:
activity = Activity(user=instance.question.author, active_at=datetime.datetime.now(), \
content_object=instance, activity_type=TYPE_ACTIVITY_MARK_ANSWER)
activity.save()
def update_last_seen(instance, created, **kwargs):
"""
when user has activities, we update 'last_seen' time stamp for him
"""
user = instance.user
user.last_seen = datetime.datetime.now()
user.save()
def record_vote(instance, created, **kwargs):
"""
when user have voted
"""
if created:
if instance.vote == 1:
vote_type = TYPE_ACTIVITY_VOTE_UP
else:
vote_type = TYPE_ACTIVITY_VOTE_DOWN
activity = Activity(user=instance.user, active_at=instance.voted_at, content_object=instance, activity_type=vote_type)
activity.save()
def record_cancel_vote(instance, **kwargs):
"""
when user canceled vote, the vote will be deleted.
"""
activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_CANCEL_VOTE)
activity.save()
def record_delete_question(instance, delete_by, **kwargs):
"""
when user deleted the question
"""
if instance.__class__ == "Question":
activity_type = TYPE_ACTIVITY_DELETE_QUESTION
else:
activity_type = TYPE_ACTIVITY_DELETE_ANSWER
activity = Activity(user=delete_by, active_at=datetime.datetime.now(), content_object=instance, activity_type=activity_type)
activity.save()
def record_mark_offensive(instance, mark_by, **kwargs):
activity = Activity(user=mark_by, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_MARK_OFFENSIVE)
activity.save()
def record_update_tags(question, **kwargs):
"""
when user updated tags of the question
"""
activity = Activity(user=question.author, active_at=datetime.datetime.now(), content_object=question, activity_type=TYPE_ACTIVITY_UPDATE_TAGS)
activity.save()
def record_favorite_question(instance, created, **kwargs):
"""
when user add the question in him favorite questions list.
"""
if created:
activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_FAVORITE)
activity.save()
def record_user_full_updated(instance, **kwargs):
activity = Activity(user=instance, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_USER_FULL_UPDATED)
activity.save()
def post_stored_anonymous_content(sender,user,session_key,signal,*args,**kwargs):
aq_list = AnonymousQuestion.objects.filter(session_key = session_key)
aa_list = AnonymousAnswer.objects.filter(session_key = session_key)
import settings
if settings.EMAIL_VALIDATION == 'on':#add user to the record
for aq in aq_list:
aq.author = user
aq.save()
for aa in aa_list:
aa.author = user
aa.save()
#maybe add pending posts message?
else: #just publish the questions
for aq in aq_list:
aq.publish(user)
for aa in aa_list:
aa.publish(user)
#signal for User modle save changes
pre_save.connect(calculate_gravatar_hash, sender=User)
post_save.connect(record_ask_event, sender=Question)
post_save.connect(record_answer_event, sender=Answer)
post_save.connect(record_comment_event, sender=Comment)
post_save.connect(record_revision_question_event, sender=QuestionRevision)
post_save.connect(record_revision_answer_event, sender=AnswerRevision)
post_save.connect(record_award_event, sender=Award)
post_save.connect(notify_award_message, sender=Award)
post_save.connect(record_answer_accepted, sender=Answer)
post_save.connect(update_last_seen, sender=Activity)
post_save.connect(record_vote, sender=Vote)
post_delete.connect(record_cancel_vote, sender=Vote)
delete_post_or_answer.connect(record_delete_question, sender=Question)
delete_post_or_answer.connect(record_delete_question, sender=Answer)
mark_offensive.connect(record_mark_offensive, sender=Question)
mark_offensive.connect(record_mark_offensive, sender=Answer)
tags_updated.connect(record_update_tags, sender=Question)
post_save.connect(record_favorite_question, sender=FavoriteQuestion)
user_updated.connect(record_user_full_updated, sender=User)
user_logged_in.connect(post_stored_anonymous_content)
Question = Question
QuestionRevision = QuestionRevision
QuestionView = QuestionView
FavoriteQuestion = FavoriteQuestion
AnonymousQuestion = AnonymousQuestion
Answer = Answer
AnswerRevision = AnswerRevision
AnonymousAnswer = AnonymousAnswer
Tag = Tag
Comment = Comment
Vote = Vote
FlaggedItem = FlaggedItem
MarkedTag = MarkedTag
Badge = Badge
Award = Award
Repute = Repute
Activity = Activity
EmailFeedSetting = EmailFeedSetting
AnonymousEmail = AnonymousEmail
__all__ = [
'Question',
'QuestionRevision',
'QuestionView',
'FavoriteQuestion',
'AnonymousQuestion',
'Answer',
'AnswerRevision',
'AnonymousAnswer',
'Tag',
'Comment',
'Vote',
'FlaggedItem',
'MarkedTag',
'Badge',
'Award',
'Repute',
'Activity',
'EmailFeedSetting',
'AnonymousEmail',
]
|