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
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)
28 ordering = ['-created_at']
29 verbose_name = _('question')
30 verbose_name_plural = _('questions')
32 def __unicode__(self):
33 return self.edited_question or self.question
36 def get_absolute_url(self):
37 return ('questions_question', (self.pk,))
39 def notify_author(self):
42 site = Site.objects.get_current()
47 text_content = loader.get_template('questions/answered_mail.txt'
49 html_content = loader.get_template('questions/answered_mail.html'
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")
57 def save(self, *args, **kwargs):
59 if self.answered and not self.answered_at:
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)