2955ddb29b22d538cbddaf96848f937209931c65
[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 from taggit_autosuggest.managers import TaggableManager
11
12
13 class Question(models.Model):
14     email = models.EmailField(_('contact e-mail'), null=True, blank=True)
15     question = models.TextField(_('question'), db_index=True)
16     created_at = models.DateTimeField(_('created at'), auto_now_add=True)
17     changed_at = models.DateTimeField(_('changed at'), auto_now=True)
18     approved = models.BooleanField(_('approved'), default=False)
19     edited_question = models.TextField(_('edited question'), db_index=True, null=True, blank=True,
20             help_text=_("Leave empty if question doesn't need editing."))
21     answer = MarkupField(_('answer'), markup_type='textile_pl', blank=True,
22             help_text=_('Use <a href="http://textile.thresholdstate.com/">Textile</a> syntax.'))
23     answered = models.BooleanField(_('answered'), db_index=True, default=False,
24             help_text=_('Check to send the answer to user.'))
25     answered_at = models.DateTimeField(_('answered at'), null=True, blank=True, db_index=True)
26     published = models.BooleanField(_('published'), db_index=True, default=False,
27         help_text=_('Check to display answered question on site.'))
28     published_at = models.DateTimeField(_('published at'), null=True, blank=True, db_index=True)
29
30     tags = TaggableManager()
31
32     class Meta:
33         ordering = ['-created_at']
34         verbose_name = _('question')
35         verbose_name_plural = _('questions')
36
37     def __unicode__(self):
38         return self.edited_question or self.question
39
40     @models.permalink
41     def get_absolute_url(self):
42         return ('questions_question', (self.pk,))
43
44     def notify_author(self):
45         if not self.email:
46             return
47         site = Site.objects.get_current()
48         context = Context({
49                 'question': self,
50                 'site': site,
51             })
52         text_content = loader.get_template('questions/answered_mail.txt'
53             ).render(context)
54         html_content = loader.get_template('questions/answered_mail.html'
55             ).render(context)
56         msg = EmailMultiAlternatives(
57             u'Odpowiedź na Twoje pytanie w serwisie %s.' % site.domain,
58             text_content, settings.SERVER_EMAIL, [self.email])
59         msg.attach_alternative(html_content, "text/html")
60         msg.send()
61
62     def ack_author(self):
63         if not self.email:
64             return
65         site = Site.objects.get_current()
66         context = Context({
67                 'question': self,
68                 'site': site,
69             })
70         text_content = loader.get_template('questions/ack_mail.txt'
71             ).render(context)
72         html_content = loader.get_template('questions/ack_mail.html'
73             ).render(context)
74         msg = EmailMultiAlternatives(
75             u'Twoje pytanie zostało zarejestrowane w serwisie %s.' % site.domain,
76             text_content, settings.SERVER_EMAIL, [self.email])
77         msg.attach_alternative(html_content, "text/html")
78         msg.send()
79
80     def save(self, *args, **kwargs):
81         now = datetime.now()
82         notify = False
83         if self.answered and not self.answered_at:
84             notify = True
85             self.answered_at = now
86         if self.published and not self.published_at:
87             self.published_at = now
88         ret = super(Question, self).save(*args, **kwargs)
89         if notify:
90             self.notify_author()
91         return ret