]> git.openstreetmap.org Git - osqa.git/blob - forum/models/question.py
Added the option to limit the tags creation. It will defaults to false, and there...
[osqa.git] / forum / models / question.py
1 from base import *
2 from tag import Tag
3 from django.utils.translation import ugettext as _
4 from forum.modules.decorators import decoratable
5
6 question_view = django.dispatch.Signal(providing_args=['instance', 'user'])
7
8 class QuestionManager(NodeManager):
9     @decoratable.method
10     def search(self, keywords):
11         return self.filter(models.Q(title__icontains=keywords) | models.Q(body__icontains=keywords))
12
13 class Question(Node):
14     class Meta(Node.Meta):
15         proxy = True
16
17     answer_count = DenormalizedField("children", ~models.Q(state_string__contains="(deleted)"), node_type="answer")
18     favorite_count = DenormalizedField("actions", action_type="favorite", canceled=False)
19
20     friendly_name = _("question")
21     objects = QuestionManager()
22
23     @property
24     def closed(self):
25         return self.nis.closed
26
27     @property    
28     def view_count(self):
29         return self.extra_count
30
31     @property
32     def headline(self):
33         if self.nis.deleted:
34             return _('[deleted] ') + self.title
35
36         if self.nis.closed:
37             return _('[closed] ') + self.title
38
39         return self.title
40
41     @property
42     def answer_accepted(self):
43         return self.extra_ref is not None
44
45     @property
46     def accepted_answer(self):
47         return self.extra_ref
48
49     @models.permalink    
50     def get_absolute_url(self):
51         return ('question', (), {'id': self.id, 'slug': django_urlquote(slugify(self.title))})
52
53     def get_revision_url(self):
54         return reverse('question_revisions', args=[self.id])
55
56     def get_related_questions(self, count=10):
57         cache_key = '%s.related_questions:%d:%d' % (settings.APP_URL, count, self.id)
58         related_list = cache.get(cache_key)
59
60         if related_list is None:
61             related_list = Question.objects.filter_state(deleted=False).values('id').filter(tags__id__in=[t.id for t in self.tags.all()]
62             ).exclude(id=self.id).annotate(frequency=models.Count('id')).order_by('-frequency')[:count]
63             cache.set(cache_key, related_list, 60 * 60)
64
65         return [Question.objects.get(id=r['id']) for r in related_list]
66
67
68 def question_viewed(instance, **kwargs):
69     instance.extra_count += 1
70     instance.save()
71
72 question_view.connect(question_viewed)
73
74
75 class QuestionSubscription(models.Model):
76     user = models.ForeignKey(User)
77     question = models.ForeignKey(Node)
78     auto_subscription = models.BooleanField(default=True)
79     last_view = models.DateTimeField(default=datetime.datetime.now())
80
81     class Meta:
82         app_label = 'forum'
83
84
85 class QuestionRevision(NodeRevision):
86     class Meta:
87         proxy = True
88