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']
30 def __unicode__(self):
31 return self.edited_question or self.question
34 def get_absolute_url(self):
35 return ('questions_question', (self.pk,))
37 def notify_author(self):
40 site = Site.objects.get_current()
45 text_content = loader.get_template('questions/answered_mail.txt'
47 html_content = loader.get_template('questions/answered_mail.html'
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")
55 def save(self, *args, **kwargs):
57 if self.answered and not self.answered_at:
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)