]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/models/answer.py
default session engine was cache, but default session engine was dummy, so sessions...
[osqa.git] / forum / models / answer.py
old mode 100755 (executable)
new mode 100644 (file)
index 14199de..85d671a
-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
+from base import *
+
+from question import Question
+
+class AnswerManager(CachedManager):
+    def create_new(self, 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()
+
+        AnswerRevision.objects.create(
+            answer     = answer,
+            revision   = 1,
+            author     = author,
+            revised_at = added_at,
+            summary    = CONST['default_version'],
+            text       = text
+        )
+
+
+class Answer(Content):
+    question = models.ForeignKey('Question', related_name='answers')
+    accepted    = models.BooleanField(default=False)
+    accepted_at = models.DateTimeField(null=True, blank=True)
+    accepted_by = models.ForeignKey(User, null=True)
+
+
+    objects = AnswerManager()
+
+    class Meta(Content.Meta):
+        db_table = u'answer'
+
+    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.answer_accepted = True
+            self.question.save()
+            return True
+
+        return False
+
+    def unmark_accepted(self):
+        if self.accepted:
+            self.accepted = False
+            self.save()
+            self.question.answer_accepted = False
+            self.question.save()
+            return True
+
+        return False
+
+    def _update_question_answer_count(self, diff):
+        self.question.answer_count = self.question.answer_count + diff
+        self.question.save()
+
+    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
+        
+
+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, *args, **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(*args, **kwargs)
+
+class AnonymousAnswer(AnonymousContent):
+    question = models.ForeignKey('Question', related_name='anonymous_answers')
+
+    def publish(self,user):
+        added_at = datetime.datetime.now()
+        #print user.id
+        Answer.objects.create_new(question=self.question,wiki=self.wiki,
+                            added_at=added_at,text=self.text,
+                            author=user)
+        self.delete()