]> git.openstreetmap.org Git - osqa.git/blob - forum/models/question.py
ALteration of the schema to a single content model. As a bonus there is a complete...
[osqa.git] / forum / models / question.py
1 from base import *
2 from tag import Tag
3 from django.utils.translation import ugettext as _
4
5 question_view = django.dispatch.Signal(providing_args=['instance', 'user'])
6
7 class Question(QandA):
8     answer_accepted = models.BooleanField(default=False)
9     closed          = models.BooleanField(default=False)
10     closed_by       = models.ForeignKey(User, null=True, blank=True, related_name='closed_questions')
11     closed_at       = models.DateTimeField(null=True, blank=True)
12     close_reason    = models.SmallIntegerField(choices=CLOSE_REASONS, null=True, blank=True)
13     subscribers     = models.ManyToManyField(User, related_name='subscriptions', through='QuestionSubscription')
14
15     # Denormalised data
16     answer_count         = models.PositiveIntegerField(default=0)
17     view_count           = models.IntegerField(default=0)
18     favourite_count      = models.IntegerField(default=0)
19     last_activity_at     = models.DateTimeField(default=datetime.datetime.now)
20     last_activity_by     = models.ForeignKey(User, related_name='last_active_in_questions')
21
22     favorited_by         = models.ManyToManyField(User, through='FavoriteQuestion', related_name='favorite_questions')
23
24     class Meta(QandA.Meta):
25         db_table = u'question'
26
27     @property
28     def headline(self):
29         if self.closed:
30             return _('[closed] ') + self.title
31
32         if self.deleted:
33             return _('[deleted] ') + self.title
34
35         return self.title
36
37     def delete(self):
38         super(Question, self).delete()
39         try:
40             ping_google()
41         except Exception:
42             logging.debug('problem pinging google did you register you sitemap with google?')
43
44     def update_last_activity(self, user):
45         self.last_activity_by = user
46         self.last_activity_at = datetime.datetime.now()
47         self.save()
48
49     def activate_revision(self, user, revision):
50         super(Question, self).activate_revision(user, revision)
51         self.update_last_activity(user)
52
53     @models.permalink    
54     def get_absolute_url(self):
55         return ('question', (), {'id': self.id, 'slug': django_urlquote(slugify(self.title))})
56
57     def get_answer_count_by_user(self, user_id):
58         from answer import Answer
59         query_set = Answer.objects.filter(author__id=user_id)
60         return query_set.filter(question=self).count()
61
62     def get_question_title(self):
63         if self.closed:
64             attr = CONST['closed']
65         elif self.deleted:
66             attr = CONST['deleted']
67         else:
68             attr = None
69         if attr is not None:
70             return u'%s %s' % (self.title, attr)
71         else:
72             return self.title
73
74     def get_revision_url(self):
75         return reverse('question_revisions', args=[self.id])
76
77     def get_latest_revision(self):
78         return self.revisions.all()[0]
79
80     def get_related_questions(self, count=10):
81         cache_key = '%s.related_questions:%d:%d' % (settings.APP_URL, count, self.id)
82         related_list = cache.get(cache_key)
83
84         if related_list is None:
85             related_list = Question.objects.values('id').filter(tags__id__in=[t.id for t in self.tags.all()]
86             ).exclude(id=self.id).exclude(deleted=True).annotate(frequency=models.Count('id')).order_by('-frequency')[:count]
87             cache.set(cache_key, related_list, 60 * 60)
88
89         return [Question.objects.get(id=r['id']) for r in related_list]
90
91     def __unicode__(self):
92         return self.title
93
94 def question_viewed(instance, **kwargs):
95     instance.view_count += 1
96     instance.save()
97
98 question_view.connect(question_viewed)
99
100 class FavoriteQuestion(models.Model):
101     question      = models.ForeignKey('Question')
102     user          = models.ForeignKey(User, related_name='user_favorite_questions')
103     added_at      = models.DateTimeField(default=datetime.datetime.now)
104
105     class Meta:
106         unique_together = ('question', 'user')
107         app_label = 'forum'
108         db_table = u'favorite_question'
109
110     def __unicode__(self):
111         return '[%s] favorited at %s' %(self.user, self.added_at)
112
113     def _update_question_fav_count(self, diff):
114         self.question.favourite_count = self.question.favourite_count + diff
115         self.question.save()
116
117     def save(self, *args, **kwargs):
118         super(FavoriteQuestion, self).save(*args, **kwargs)
119         if self._is_new:
120             self._update_question_fav_count(1)
121
122     def delete(self):
123         self._update_question_fav_count(-1)
124         super(FavoriteQuestion, self).delete()
125
126 class QuestionSubscription(models.Model):
127     user = models.ForeignKey(User)
128     question = models.ForeignKey(Question)
129     auto_subscription = models.BooleanField(default=True)
130     last_view = models.DateTimeField(default=datetime.datetime.now())
131
132     class Meta:
133         app_label = 'forum'
134
135
136 class QuestionRevision(NodeRevision):
137     class Meta:
138         proxy = True
139