fix email to teachers (don't send to None)
[edumed.git] / contact / models.py
1 # -*- coding: utf-8 -*-
2 from django.db import models
3 from django.db.models import permalink
4 from django.utils.translation import ugettext_lazy as _
5 from jsonfield import JSONField
6 from . import app_settings
7
8
9 class Contact(models.Model):
10     created_at = models.DateTimeField(_('submission date'), auto_now_add=True)
11     ip = models.IPAddressField(_('IP address'))
12     contact = models.CharField(_('contact'), max_length=128)
13     form_tag = models.CharField(_('form'), max_length=32, db_index=True)
14     body = JSONField(_('body'))
15
16     @staticmethod
17     def pretty_print(value, for_html=False):
18         if type(value) in (tuple, list, dict):
19             import yaml
20             value = yaml.safe_dump(value, allow_unicode=True, default_flow_style=False)
21             if for_html:
22                 from django.utils.encoding import smart_unicode
23                 value = smart_unicode(value).replace(u" ", unichr(160))
24         return value
25
26     class Meta:
27         ordering = ('-created_at',)
28         verbose_name = _('submitted form')
29         verbose_name_plural = _('submitted forms')
30
31     def __unicode__(self):
32         return unicode(self.created_at)
33
34     def get_form_class(self):
35         from contact.forms import contact_forms
36         return contact_forms.get(self.form_tag)
37
38     def get_update_form_class(self):
39         from contact.forms import update_forms
40         return update_forms.get(self.form_tag, self.get_form_class())
41
42     @permalink
43     def update_url(self):
44         form_class = self.get_update_form_class()
45         confirmation = form_class.confirmation_class.objects.get(contact=self)
46         return 'edit_form', [], {'form_tag': self.form_tag, 'contact_id': self.id, 'key': confirmation.key}
47
48     def send_confirmation(self, context=None, form=None):
49         from django.template.loader import render_to_string
50         from django.core.mail import send_mail
51         from django.contrib.sites.models import Site
52         if not form:
53             form_class = self.get_form_class()
54             form = form_class()
55         dictionary = form.get_dictionary(self)
56         mail_subject = render_to_string([
57             'contact/%s/mail_subject.txt' % self.form_tag,
58             'contact/mail_subject.txt',
59         ], dictionary, context).strip()
60         if form.result_page:
61             mail_body = render_to_string(
62                 'contact/%s/results_email.txt' % self.form_tag,
63                 {
64                     'contact': self,
65                     'results': form.results(self),
66                 }, context)
67         else:
68             mail_body = render_to_string([
69                 'contact/%s/mail_body.txt' % self.form_tag,
70                 'contact/mail_body.txt',
71             ], dictionary, context)
72         site = Site.objects.get_current()
73         send_mail(mail_subject, mail_body, 'no-reply@%s' % site.domain, [self.contact], fail_silently=True)
74
75
76 class Attachment(models.Model):
77     contact = models.ForeignKey(Contact)
78     tag = models.CharField(max_length=64)
79     file = models.FileField(upload_to='contact/attachment')
80
81     @models.permalink
82     def get_absolute_url(self):
83         return 'contact_attachment', [self.contact_id, self.tag]
84
85
86 __import__(app_settings.FORMS_MODULE)