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
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)
29 ordering = ['-created_at']
30 verbose_name = _('question')
31 verbose_name_plural = _('questions')
33 def __unicode__(self):
34 return self.edited_question or self.question
37 def get_absolute_url(self):
38 return ('questions_question', (self.pk,))
40 def notify_author(self):
43 site = Site.objects.get_current()
48 text_content = loader.get_template('questions/answered_mail.txt'
50 html_content = loader.get_template('questions/answered_mail.html'
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")
61 site = Site.objects.get_current()
66 text_content = loader.get_template('questions/ack_mail.txt'
68 html_content = loader.get_template('questions/ack_mail.html'
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")
76 def save(self, *args, **kwargs):
79 if self.answered and not self.answered_at:
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)