X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/a9eef437702d5df7a2f97010e6798c689371808c..cd19cb00154021685ea43d164a4dc8599b2e0a0e:/forum/models/answer.py diff --git a/forum/models/answer.py b/forum/models/answer.py old mode 100755 new mode 100644 index 14199de..01e0aae --- a/forum/models/answer.py +++ b/forum/models/answer.py @@ -1,134 +1,80 @@ -from base import * - -from question import Question - -class AnswerManager(models.Manager): - @staticmethod - def create_new(cls, question=None, author=None, added_at=None, wiki=False, text='', email_notify=False): - answer = Answer( - question = question, - author = author, - added_at = added_at, - wiki = wiki, - html = text - ) - if answer.wiki: - answer.last_edited_by = answer.author - answer.last_edited_at = added_at - answer.wikified_at = added_at - - answer.save() - - #update question data - question.last_activity_at = added_at - question.last_activity_by = author - question.save() - Question.objects.update_answer_count(question) - - AnswerRevision.objects.create( - answer = answer, - revision = 1, - author = author, - revised_at = added_at, - summary = CONST['default_version'], - text = text - ) - - #set notification/delete - if email_notify: - if author not in question.followed_by.all(): - question.followed_by.add(author) - else: - #not sure if this is necessary. ajax should take care of this... - try: - question.followed_by.remove(author) - except: - pass - - #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' - def get_answers_from_question(self, question, user=None): - """ - Retrieves visibile answers for the given question. Delete answers - are only visibile to the person who deleted them. - """ - - if user is None or not user.is_authenticated(): - return self.filter(question=question, deleted=False) - else: - return self.filter(models.Q(question=question), - models.Q(deleted=False) | models.Q(deleted_by=user)) - - #todo: I think this method is not being used anymore, I'll just comment it for now - #def get_answers_from_questions(self, user_id): - # """ - # Retrieves visibile answers for the given question. Which are not included own answers - # """ - # cursor = connection.cursor() - # cursor.execute(self.GET_ANSWERS_FROM_USER_QUESTIONS, [user_id, user_id]) - # return cursor.fetchall() - -class Answer(Content, DeletableContent): - question = models.ForeignKey('Question', related_name='answers') - accepted = models.BooleanField(default=False) - accepted_at = models.DateTimeField(null=True, blank=True) - - objects = AnswerManager() - - class Meta(Content.Meta): - db_table = u'answer' - - def get_user_vote(self, user): - if user.__class__.__name__ == "AnonymousUser": - return None - - votes = self.votes.filter(user=user) - if votes and votes.count() > 0: - return votes[0] - else: - return None - - def get_latest_revision(self): - return self.revisions.all()[0] - - def get_question_title(self): - return self.question.title - - def get_absolute_url(self): - return '%s%s#%s' % (reverse('question', args=[self.question.id]), django_urlquote(slugify(self.question.title)), self.id) - - def __unicode__(self): - return self.html - - -class AnswerRevision(ContentRevision): - """A revision of an Answer.""" - answer = models.ForeignKey('Answer', related_name='revisions') - - def get_absolute_url(self): - return reverse('answer_revisions', kwargs={'id':self.answer.id}) - - def get_question_title(self): - return self.answer.question.title - - class Meta(ContentRevision.Meta): - db_table = u'answer_revision' - ordering = ('-revision',) - - def save(self, **kwargs): - """Looks up the next available revision number if not set.""" - if not self.revision: - self.revision = AnswerRevision.objects.filter( - answer=self.answer).values_list('revision', - flat=True)[0] + 1 - super(AnswerRevision, self).save(**kwargs) - -class AnonymousAnswer(AnonymousContent): - question = models.ForeignKey('Question', related_name='anonymous_answers') - - def publish(self,user): - added_at = datetime.datetime.now() - #print user.id - AnswerManager.create_new(question=self.question,wiki=self.wiki, - added_at=added_at,text=self.text, - author=user) - self.delete() +from base import * + +from question import Question + +class Answer(QandA): + accepted = models.BooleanField(default=False) + accepted_at = models.DateTimeField(null=True, blank=True) + accepted_by = models.ForeignKey(User, null=True) + + class Meta(QandA.Meta): + db_table = u'answer' + + @property + def headline(self): + return self.question.headline + + def mark_accepted(self, user): + if not self.accepted and not self.question.answer_accepted: + self.accepted = True + self.accepted_at = datetime.datetime.now() + self.accepted_by = user + self.save() + self.question.accepted_answer = self + self.question.save() + answer_accepted.send(sender=Answer, answer=self, user=user) + return True + + return False + + def unmark_accepted(self, user): + if self.accepted: + self.accepted = False + self.save() + self.question.accepted_answer = None + self.question.save() + answer_accepted_canceled.send(sender=Answer, answer=self, user=user) + return True + + return False + + def _update_question_answer_count(self, diff): + self.question.answer_count = self.question.answer_count + diff + self.question.save() + + def activate_revision(self, user, revision): + super(Answer, self).activate_revision(user, revision) + self.question.update_last_activity(user) + + def mark_deleted(self, user): + if super(Answer, self).mark_deleted(user): + self._update_question_answer_count(-1) + + def unmark_deleted(self): + if super(Answer, self).unmark_deleted(): + self._update_question_answer_count(1) + + def get_latest_revision(self): + return self.revisions.all()[0] + + def get_question_title(self): + return self.question.title + + def get_absolute_url(self): + return '%s#%s' % (self.question.get_absolute_url(), self.id) + + def save(self, *args, **kwargs): + super(Answer, self).save(*args, **kwargs) + + if self._is_new: + self._update_question_answer_count(1) + + def __unicode__(self): + return self.html + +answer_accepted = django.dispatch.Signal(providing_args=['answer', 'user']) +answer_accepted_canceled = django.dispatch.Signal(providing_args=['answer', 'user']) + +class AnswerRevision(NodeRevision): + class Meta: + proxy = True \ No newline at end of file