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