3 from question import Question
5 class AnswerManager(CachedManager):
6 def create_new(self, question=None, author=None, added_at=None, wiki=False, text='', email_notify=False):
15 answer.last_edited_by = answer.author
16 answer.last_edited_at = added_at
17 answer.wikified_at = added_at
22 question.last_activity_at = added_at
23 question.last_activity_by = author
26 AnswerRevision.objects.create(
30 revised_at = added_at,
31 summary = CONST['default_version'],
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)
43 objects = AnswerManager()
45 class Meta(Content.Meta):
48 def mark_accepted(self, user):
49 if not self.accepted and not self.question.answer_accepted:
51 self.accepted_at = datetime.datetime.now()
52 self.accepted_by = user
54 self.question.answer_accepted = True
60 def unmark_accepted(self):
64 self.question.answer_accepted = False
70 def _update_question_answer_count(self, diff):
71 self.question.answer_count = self.question.answer_count + diff
74 def mark_deleted(self, user):
75 if super(Answer, self).mark_deleted(user):
76 self._update_question_answer_count(-1)
78 def unmark_deleted(self):
79 if super(Answer, self).unmark_deleted():
80 self._update_question_answer_count(1)
82 def get_latest_revision(self):
83 return self.revisions.all()[0]
85 def get_question_title(self):
86 return self.question.title
88 def get_absolute_url(self):
89 return '%s#%s' % (self.question.get_absolute_url(), self.id)
91 def save(self, *args, **kwargs):
92 super(Answer, self).save(*args, **kwargs)
95 self._update_question_answer_count(1)
97 def __unicode__(self):
101 class AnswerRevision(ContentRevision):
102 """A revision of an Answer."""
103 answer = models.ForeignKey('Answer', related_name='revisions')
105 def get_absolute_url(self):
106 return reverse('answer_revisions', kwargs={'id':self.answer.id})
108 def get_question_title(self):
109 return self.answer.question.title
111 class Meta(ContentRevision.Meta):
112 db_table = u'answer_revision'
113 ordering = ('-revision',)
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',
121 super(AnswerRevision, self).save(*args, **kwargs)
123 class AnonymousAnswer(AnonymousContent):
124 question = models.ForeignKey('Question', related_name='anonymous_answers')
126 def publish(self,user):
127 added_at = datetime.datetime.now()
129 Answer.objects.create_new(question=self.question,wiki=self.wiki,
130 added_at=added_at,text=self.text,