-from base import *\r
-\r
-from question import Question\r
-\r
-class AnswerManager(models.Manager):\r
- @staticmethod\r
- def create_new(cls, question=None, author=None, added_at=None, wiki=False, text='', email_notify=False):\r
- answer = Answer(\r
- question = question,\r
- author = author,\r
- added_at = added_at,\r
- wiki = wiki,\r
- html = text\r
- )\r
- if answer.wiki:\r
- answer.last_edited_by = answer.author\r
- answer.last_edited_at = added_at\r
- answer.wikified_at = added_at\r
-\r
- answer.save()\r
-\r
- #update question data\r
- question.last_activity_at = added_at\r
- question.last_activity_by = author\r
- question.save()\r
- Question.objects.update_answer_count(question)\r
-\r
- AnswerRevision.objects.create(\r
- answer = answer,\r
- revision = 1,\r
- author = author,\r
- revised_at = added_at,\r
- summary = CONST['default_version'],\r
- text = text\r
- )\r
-\r
- #set notification/delete\r
- if email_notify:\r
- if author not in question.followed_by.all():\r
- question.followed_by.add(author)\r
- else:\r
- #not sure if this is necessary. ajax should take care of this...\r
- try:\r
- question.followed_by.remove(author)\r
- except:\r
- pass\r
-\r
- #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
- def get_answers_from_question(self, question, user=None):\r
- """\r
- Retrieves visibile answers for the given question. Delete answers\r
- are only visibile to the person who deleted them.\r
- """\r
-\r
- if user is None or not user.is_authenticated():\r
- return self.filter(question=question, deleted=False)\r
- else:\r
- return self.filter(models.Q(question=question),\r
- models.Q(deleted=False) | models.Q(deleted_by=user))\r
-\r
- #todo: I think this method is not being used anymore, I'll just comment it for now\r
- #def get_answers_from_questions(self, user_id):\r
- # """\r
- # Retrieves visibile answers for the given question. Which are not included own answers\r
- # """\r
- # cursor = connection.cursor()\r
- # cursor.execute(self.GET_ANSWERS_FROM_USER_QUESTIONS, [user_id, user_id])\r
- # return cursor.fetchall()\r
-\r
-class Answer(Content, DeletableContent):\r
- question = models.ForeignKey('Question', related_name='answers')\r
- accepted = models.BooleanField(default=False)\r
- accepted_at = models.DateTimeField(null=True, blank=True)\r
-\r
- objects = AnswerManager()\r
-\r
- class Meta(Content.Meta):\r
- db_table = u'answer'\r
-\r
- def get_user_vote(self, user):\r
- if user.__class__.__name__ == "AnonymousUser":\r
- return None\r
-\r
- votes = self.votes.filter(user=user)\r
- if votes and votes.count() > 0:\r
- return votes[0]\r
- else:\r
- return None\r
-\r
- def get_latest_revision(self):\r
- return self.revisions.all()[0]\r
-\r
- def get_question_title(self):\r
- return self.question.title\r
-\r
- def get_absolute_url(self):\r
- return '%s%s#%s' % (reverse('question', args=[self.question.id]), django_urlquote(slugify(self.question.title)), self.id)\r
-\r
- def __unicode__(self):\r
- return self.html\r
- \r
-\r
-class AnswerRevision(ContentRevision):\r
- """A revision of an Answer."""\r
- answer = models.ForeignKey('Answer', related_name='revisions')\r
-\r
- def get_absolute_url(self):\r
- return reverse('answer_revisions', kwargs={'id':self.answer.id})\r
-\r
- def get_question_title(self):\r
- return self.answer.question.title\r
-\r
- class Meta(ContentRevision.Meta):\r
- db_table = u'answer_revision'\r
- ordering = ('-revision',)\r
-\r
- def save(self, **kwargs):\r
- """Looks up the next available revision number if not set."""\r
- if not self.revision:\r
- self.revision = AnswerRevision.objects.filter(\r
- answer=self.answer).values_list('revision',\r
- flat=True)[0] + 1\r
- super(AnswerRevision, self).save(**kwargs)\r
-\r
-class AnonymousAnswer(AnonymousContent):\r
- question = models.ForeignKey('Question', related_name='anonymous_answers')\r
-\r
- def publish(self,user):\r
- added_at = datetime.datetime.now()\r
- #print user.id\r
- AnswerManager.create_new(question=self.question,wiki=self.wiki,\r
- added_at=added_at,text=self.text,\r
- author=user)\r
- self.delete()\r