]> git.openstreetmap.org Git - osqa.git/blob - forum/models/question.py
010647cd3e9ccc736e2e14b77af6f6c2389fb66d
[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 QuestionManager(NodeManager):
8     def search(self, keywords):
9         return False, self.filter(models.Q(title__icontains=keywords) | models.Q(body__icontains=keywords))
10
11 class Question(Node):
12     class Meta(Node.Meta):
13         proxy = True
14
15     answer_count = DenormalizedField("children", ~models.Q(state_string__contains="(deleted)"), node_type="answer")
16     accepted_count = DenormalizedField("children", ~models.Q(state_string__contains="(deleted)"), node_type="answer", marked=True)
17     favorite_count = DenormalizedField("actions", action_type="favorite", canceled=False)
18
19     friendly_name = _("question")
20     objects = QuestionManager()
21
22     @property
23     def closed(self):
24         return self.nis.closed
25
26     @property    
27     def view_count(self):
28         return self.extra_count
29
30     @property
31     def headline(self):
32         if self.nis.deleted:
33             return _('[deleted] ') + self.title
34
35         if self.nis.closed:
36             return _('[closed] ') + self.title
37
38         return self.title
39
40     @property
41     def accepted_answers(self):
42         return self.answers.filter(~models.Q(state_string__contains="(deleted)"), marked=True)
43
44     @models.permalink    
45     def get_absolute_url(self):
46         return ('question', (), {'id': self.id, 'slug': django_urlquote(slugify(self.title))})
47         
48     def meta_description(self):
49         return self.summary
50
51     def get_revision_url(self):
52         return reverse('question_revisions', args=[self.id])
53
54     def get_related_questions(self, count=10):
55         cache_key = '%s.related_questions:%d:%d' % (settings.APP_URL, count, self.id)
56         related_list = cache.get(cache_key)
57
58         if related_list is None:
59             related_list = Question.objects.filter_state(deleted=False).values('id').filter(tags__id__in=[t.id for t in self.tags.all()]
60             ).exclude(id=self.id).annotate(frequency=models.Count('id')).order_by('-frequency')[:count]
61             cache.set(cache_key, related_list, 60 * 60)
62
63         return [Question.objects.get(id=r['id']) for r in related_list]
64     
65     def get_active_users(self):
66         active_users = []
67         
68         active_users.append(self.author)
69         
70         for answer in self.answers:
71             active_users.append(answer.author)
72         
73         for child in self.children.all():
74             active_users.append(child.author)
75             for grandchild in child.children.all():
76                 active_users.append(grandchild.author)
77         
78         # Remove duplicates
79         unique_active_users = []
80         for user in active_users:
81             if user not in unique_active_users:
82                 unique_active_users.append(user)
83         active_users = unique_active_users
84         del unique_active_users
85         
86         return active_users
87
88
89 def question_viewed(instance, **kwargs):
90     instance.extra_count += 1
91     instance.save()
92
93 question_view.connect(question_viewed)
94
95
96 class QuestionSubscription(models.Model):
97     user = models.ForeignKey(User)
98     question = models.ForeignKey(Node)
99     auto_subscription = models.BooleanField(default=True)
100     last_view = models.DateTimeField(default=datetime.datetime.now())
101
102     class Meta:
103         app_label = 'forum'
104
105
106 class QuestionRevision(NodeRevision):
107     class Meta:
108         proxy = True
109