]> git.openstreetmap.org Git - osqa.git/blob - forum/models/answer.py
Initial commit
[osqa.git] / forum / models / answer.py
1 from base import *\r
2 \r
3 from question import Question\r
4 \r
5 class AnswerManager(models.Manager):\r
6     @staticmethod\r
7     def create_new(cls, question=None, author=None, added_at=None, wiki=False, text='', email_notify=False):\r
8         answer = Answer(\r
9             question = question,\r
10             author = author,\r
11             added_at = added_at,\r
12             wiki = wiki,\r
13             html = text\r
14         )\r
15         if answer.wiki:\r
16             answer.last_edited_by = answer.author\r
17             answer.last_edited_at = added_at\r
18             answer.wikified_at = added_at\r
19 \r
20         answer.save()\r
21 \r
22         #update question data\r
23         question.last_activity_at = added_at\r
24         question.last_activity_by = author\r
25         question.save()\r
26         Question.objects.update_answer_count(question)\r
27 \r
28         AnswerRevision.objects.create(\r
29             answer     = answer,\r
30             revision   = 1,\r
31             author     = author,\r
32             revised_at = added_at,\r
33             summary    = CONST['default_version'],\r
34             text       = text\r
35         )\r
36 \r
37         #set notification/delete\r
38         if email_notify:\r
39             if author not in question.followed_by.all():\r
40                 question.followed_by.add(author)\r
41         else:\r
42             #not sure if this is necessary. ajax should take care of this...\r
43             try:\r
44                 question.followed_by.remove(author)\r
45             except:\r
46                 pass\r
47 \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
50         """\r
51         Retrieves visibile answers for the given question. Delete answers\r
52         are only visibile to the person who deleted them.\r
53         """\r
54 \r
55         if user is None or not user.is_authenticated():\r
56             return self.filter(question=question, deleted=False)\r
57         else:\r
58             return self.filter(models.Q(question=question),\r
59                                models.Q(deleted=False) | models.Q(deleted_by=user))\r
60 \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
63     #    """\r
64     #    Retrieves visibile answers for the given question. Which are not included own answers\r
65     #    """\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
69 \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
74 \r
75     objects = AnswerManager()\r
76 \r
77     class Meta(Content.Meta):\r
78         db_table = u'answer'\r
79 \r
80     def get_user_vote(self, user):\r
81         if user.__class__.__name__ == "AnonymousUser":\r
82             return None\r
83 \r
84         votes = self.votes.filter(user=user)\r
85         if votes and votes.count() > 0:\r
86             return votes[0]\r
87         else:\r
88             return None\r
89 \r
90     def get_latest_revision(self):\r
91         return self.revisions.all()[0]\r
92 \r
93     def get_question_title(self):\r
94         return self.question.title\r
95 \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
98 \r
99     def __unicode__(self):\r
100         return self.html\r
101         \r
102 \r
103 class AnswerRevision(ContentRevision):\r
104     """A revision of an Answer."""\r
105     answer     = models.ForeignKey('Answer', related_name='revisions')\r
106 \r
107     def get_absolute_url(self):\r
108         return reverse('answer_revisions', kwargs={'id':self.answer.id})\r
109 \r
110     def get_question_title(self):\r
111         return self.answer.question.title\r
112 \r
113     class Meta(ContentRevision.Meta):\r
114         db_table = u'answer_revision'\r
115         ordering = ('-revision',)\r
116 \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
122                                                 flat=True)[0] + 1\r
123         super(AnswerRevision, self).save(**kwargs)\r
124 \r
125 class AnonymousAnswer(AnonymousContent):\r
126     question = models.ForeignKey('Question', related_name='anonymous_answers')\r
127 \r
128     def publish(self,user):\r
129         added_at = datetime.datetime.now()\r
130         #print user.id\r
131         AnswerManager.create_new(question=self.question,wiki=self.wiki,\r
132                             added_at=added_at,text=self.text,\r
133                             author=user)\r
134         self.delete()\r