b441a00c31364260af8f29566740dfd54492e2aa
[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 ack_author(self):
59         if not self.email:
60             return
61         site = Site.objects.get_current()
62         context = Context({
63                 'question': self,
64                 'site': site,
65             })
66         text_content = loader.get_template('questions/ack_mail.txt'
67             ).render(context)
68         html_content = loader.get_template('questions/ack_mail.html'
69             ).render(context)
70         msg = EmailMultiAlternatives(
71             u'Twoje pytanie zostało zarejestrowane w serwisie %s.' % site.domain,
72             text_content, settings.SERVER_EMAIL, [self.email])
73         msg.attach_alternative(html_content, "text/html")
74         msg.send()
75
76     def save(self, *args, **kwargs):
77         now = datetime.now()
78         notify = False
79         if self.answered and not self.answered_at:
80             notify = True
81             self.answered_at = now
82         if self.published and not self.published_at:
83             self.published_at = now
84         ret = super(Question, self).save(*args, **kwargs)
85         if notify:
86             self.notify_author()
87         return ret