]> git.openstreetmap.org Git - osqa.git/blob - forum/models/answer.py
85d671a8cadc1afd7b422daf71462b62aa95fab5
[osqa.git] / forum / models / answer.py
1 from base import *
2
3 from question import Question
4
5 class AnswerManager(CachedManager):
6     def create_new(self, question=None, author=None, added_at=None, wiki=False, text='', email_notify=False):
7         answer = Answer(
8             question = question,
9             author = author,
10             added_at = added_at,
11             wiki = wiki,
12             html = text
13         )
14         if answer.wiki:
15             answer.last_edited_by = answer.author
16             answer.last_edited_at = added_at
17             answer.wikified_at = added_at
18
19         answer.save()
20
21         #update question data
22         question.last_activity_at = added_at
23         question.last_activity_by = author
24         question.save()
25
26         AnswerRevision.objects.create(
27             answer     = answer,
28             revision   = 1,
29             author     = author,
30             revised_at = added_at,
31             summary    = CONST['default_version'],
32             text       = text
33         )
34
35
36 class Answer(Content):
37     question = models.ForeignKey('Question', related_name='answers')
38     accepted    = models.BooleanField(default=False)
39     accepted_at = models.DateTimeField(null=True, blank=True)
40     accepted_by = models.ForeignKey(User, null=True)
41
42
43     objects = AnswerManager()
44
45     class Meta(Content.Meta):
46         db_table = u'answer'
47
48     def mark_accepted(self, user):
49         if not self.accepted and not self.question.answer_accepted:
50             self.accepted = True
51             self.accepted_at = datetime.datetime.now()
52             self.accepted_by = user
53             self.save()
54             self.question.answer_accepted = True
55             self.question.save()
56             return True
57
58         return False
59
60     def unmark_accepted(self):
61         if self.accepted:
62             self.accepted = False
63             self.save()
64             self.question.answer_accepted = False
65             self.question.save()
66             return True
67
68         return False
69
70     def _update_question_answer_count(self, diff):
71         self.question.answer_count = self.question.answer_count + diff
72         self.question.save()
73
74     def mark_deleted(self, user):
75         if super(Answer, self).mark_deleted(user):
76             self._update_question_answer_count(-1)
77
78     def unmark_deleted(self):
79         if super(Answer, self).unmark_deleted():
80             self._update_question_answer_count(1)
81
82     def get_latest_revision(self):
83         return self.revisions.all()[0]
84
85     def get_question_title(self):
86         return self.question.title
87
88     def get_absolute_url(self):
89         return '%s#%s' % (self.question.get_absolute_url(), self.id)
90
91     def save(self, *args, **kwargs):
92         super(Answer, self).save(*args, **kwargs)
93
94         if self._is_new:
95             self._update_question_answer_count(1)
96
97     def __unicode__(self):
98         return self.html
99         
100
101 class AnswerRevision(ContentRevision):
102     """A revision of an Answer."""
103     answer     = models.ForeignKey('Answer', related_name='revisions')
104
105     def get_absolute_url(self):
106         return reverse('answer_revisions', kwargs={'id':self.answer.id})
107
108     def get_question_title(self):
109         return self.answer.question.title
110
111     class Meta(ContentRevision.Meta):
112         db_table = u'answer_revision'
113         ordering = ('-revision',)
114
115     def save(self, *args, **kwargs):
116         """Looks up the next available revision number if not set."""
117         if not self.revision:
118             self.revision = AnswerRevision.objects.filter(
119                 answer=self.answer).values_list('revision',
120                                                 flat=True)[0] + 1
121         super(AnswerRevision, self).save(*args, **kwargs)
122
123 class AnonymousAnswer(AnonymousContent):
124     question = models.ForeignKey('Question', related_name='anonymous_answers')
125
126     def publish(self,user):
127         added_at = datetime.datetime.now()
128         #print user.id
129         Answer.objects.create_new(question=self.question,wiki=self.wiki,
130                             added_at=added_at,text=self.text,
131                             author=user)
132         self.delete()