3 from question import Question
\r
5 class AnswerManager(models.Manager):
\r
7 def create_new(cls, question=None, author=None, added_at=None, wiki=False, text='', email_notify=False):
\r
11 added_at = added_at,
\r
16 answer.last_edited_by = answer.author
\r
17 answer.last_edited_at = added_at
\r
18 answer.wikified_at = added_at
\r
22 #update question data
\r
23 question.last_activity_at = added_at
\r
24 question.last_activity_by = author
\r
26 Question.objects.update_answer_count(question)
\r
28 AnswerRevision.objects.create(
\r
32 revised_at = added_at,
\r
33 summary = CONST['default_version'],
\r
37 #set notification/delete
\r
39 if author not in question.followed_by.all():
\r
40 question.followed_by.add(author)
\r
42 #not sure if this is necessary. ajax should take care of this...
\r
44 question.followed_by.remove(author)
\r
48 #GET_ANSWERS_FROM_USER_QUESTIONS = u'SELECT answer.* FROM answer INNER JOIN question ON answer.question_id = question.id WHERE question.author_id =%s AND answer.author_id <> %s'
\r
49 def get_answers_from_question(self, question, user=None):
\r
51 Retrieves visibile answers for the given question. Delete answers
\r
52 are only visibile to the person who deleted them.
\r
55 if user is None or not user.is_authenticated():
\r
56 return self.filter(question=question, deleted=False)
\r
58 return self.filter(models.Q(question=question),
\r
59 models.Q(deleted=False) | models.Q(deleted_by=user))
\r
61 #todo: I think this method is not being used anymore, I'll just comment it for now
\r
62 #def get_answers_from_questions(self, user_id):
\r
64 # Retrieves visibile answers for the given question. Which are not included own answers
\r
66 # cursor = connection.cursor()
\r
67 # cursor.execute(self.GET_ANSWERS_FROM_USER_QUESTIONS, [user_id, user_id])
\r
68 # return cursor.fetchall()
\r
70 class Answer(Content, DeletableContent):
\r
71 question = models.ForeignKey('Question', related_name='answers')
\r
72 accepted = models.BooleanField(default=False)
\r
73 accepted_at = models.DateTimeField(null=True, blank=True)
\r
75 objects = AnswerManager()
\r
77 class Meta(Content.Meta):
\r
78 db_table = u'answer'
\r
80 def get_user_vote(self, user):
\r
81 if user.__class__.__name__ == "AnonymousUser":
\r
84 votes = self.votes.filter(user=user)
\r
85 if votes and votes.count() > 0:
\r
90 def get_latest_revision(self):
\r
91 return self.revisions.all()[0]
\r
93 def get_question_title(self):
\r
94 return self.question.title
\r
96 def get_absolute_url(self):
\r
97 return '%s%s#%s' % (reverse('question', args=[self.question.id]), django_urlquote(slugify(self.question.title)), self.id)
\r
99 def __unicode__(self):
\r
103 class AnswerRevision(ContentRevision):
\r
104 """A revision of an Answer."""
\r
105 answer = models.ForeignKey('Answer', related_name='revisions')
\r
107 def get_absolute_url(self):
\r
108 return reverse('answer_revisions', kwargs={'id':self.answer.id})
\r
110 def get_question_title(self):
\r
111 return self.answer.question.title
\r
113 class Meta(ContentRevision.Meta):
\r
114 db_table = u'answer_revision'
\r
115 ordering = ('-revision',)
\r
117 def save(self, **kwargs):
\r
118 """Looks up the next available revision number if not set."""
\r
119 if not self.revision:
\r
120 self.revision = AnswerRevision.objects.filter(
\r
121 answer=self.answer).values_list('revision',
\r
123 super(AnswerRevision, self).save(**kwargs)
\r
125 class AnonymousAnswer(AnonymousContent):
\r
126 question = models.ForeignKey('Question', related_name='anonymous_answers')
\r
128 def publish(self,user):
\r
129 added_at = datetime.datetime.now()
\r
131 AnswerManager.create_new(question=self.question,wiki=self.wiki,
\r
132 added_at=added_at,text=self.text,
\r