ask-a-lawyer stuff
[prawokultury.git] / questions / models.py
1 # -*- coding: utf-8
2 from datetime import datetime
3 from django.contrib.sites.models import Site
4 from django.core.mail import EmailMultiAlternatives
5 from django.db import models
6 from django.template import loader, Context
7 from django.utils.translation import ugettext_lazy as _
8 from markupfield.fields import MarkupField
9
10 class Question(models.Model):
11     email = models.EmailField(_('contact e-mail'), null=True, blank=True)
12     question = models.TextField(_('question'), db_index=True)
13     created_at = models.DateTimeField(_('created at'), auto_now_add=True)
14     changed_at = models.DateTimeField(_('changed at'), auto_now=True)
15     approved = models.BooleanField(_('approved'), default=False)
16     edited_question = models.TextField(_('edited question'), db_index=True, null=True, blank=True,
17             help_text=_("Leave empty if question doesn't need editing."))
18     answer = MarkupField(_('answer'), markup_type='textile_pl', blank=True,
19             help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.'))
20     answered = models.BooleanField(_('answered'), db_index=True, default=False,
21             help_text=_('Check to send the answer to user.'))
22     answered_at = models.DateTimeField(_('answered at'), null=True, blank=True, db_index=True)
23     published = models.BooleanField(_('published'), db_index=True, default=False,
24         help_text=_('Check to display answered question on site.'))
25     published_at = models.DateTimeField(_('published at'), null=True, blank=True, db_index=True)
26
27     class Meta:
28         ordering = ['-created_at']
29         verbose_name = _('question')
30         verbose_name_plural = _('questions')
31
32     def __unicode__(self):
33         return self.edited_question or self.question
34
35     @models.permalink
36     def get_absolute_url(self):
37         return ('questions_question', (self.pk,))
38
39     def notify_author(self):
40         if not self.email:
41             return
42         site = Site.objects.get_current()
43         context = Context({
44                 'question': self,
45                 'site': site,
46             })
47         text_content = loader.get_template('questions/answered_mail.txt'
48             ).render(context)
49         html_content = loader.get_template('questions/answered_mail.html'
50             ).render(context)
51         msg = EmailMultiAlternatives(
52             u'Odpowiedź na Twoje pytanie w serwisie %s.' % site.domain,
53             text_content, settings.SERVER_EMAIL, [self.email])
54         msg.attach_alternative(html_content, "text/html")
55         msg.send()
56
57     def save(self, *args, **kwargs):
58         now = datetime.now()
59         if self.answered and not self.answered_at:
60             self.notify_author()
61             self.answered_at = now
62         if self.published and not self.published_at:
63             self.published_at = now
64         super(Question, self).save(*args, **kwargs)