94c6e1d9440cec9a3a2589323bc4da27509654df
[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
30     def __unicode__(self):
31         return self.edited_question or self.question
32
33     @models.permalink
34     def get_absolute_url(self):
35         return ('questions_question', (self.pk,))
36
37     def notify_author(self):
38         if not self.email:
39             return
40         site = Site.objects.get_current()
41         context = Context({
42                 'question': self,
43                 'site': site,
44             })
45         text_content = loader.get_template('questions/answered_mail.txt'
46             ).render(context)
47         html_content = loader.get_template('questions/answered_mail.html'
48             ).render(context)
49         msg = EmailMultiAlternatives(
50             u'Odpowiedź na Twoje pytanie w serwisie %s.' % site.domain,
51             text_content, settings.SERVER_EMAIL, [self.email])
52         msg.attach_alternative(html_content, "text/html")
53         msg.send()
54
55     def save(self, *args, **kwargs):
56         now = datetime.now()
57         if self.answered and not self.answered_at:
58             self.notify_author()
59             self.answered_at = now
60         if self.published and not self.published_at:
61             self.published_at = now
62         super(Question, self).save(*args, **kwargs)