X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/d12ee4e0ec425e7f508c93ad1295a21a67ae1a30..cd7d2e5171bebc3b447f24878cb8f56caefe1e58:/contact/models.py?ds=inline diff --git a/contact/models.py b/contact/models.py index 21d8405..7fe8f9c 100644 --- a/contact/models.py +++ b/contact/models.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from django.core.files.storage import FileSystemStorage from django.db import models +from django.db.models import permalink from django.utils.translation import ugettext_lazy as _ from jsonfield import JSONField from . import app_settings @@ -13,6 +13,16 @@ class Contact(models.Model): form_tag = models.CharField(_('form'), max_length=32, db_index=True) body = JSONField(_('body')) + @staticmethod + def pretty_print(value, for_html=False): + if type(value) in (tuple, list, dict): + import yaml + value = yaml.safe_dump(value, allow_unicode=True, default_flow_style=False) + if for_html: + from django.utils.encoding import smart_unicode + value = smart_unicode(value).replace(u" ", unichr(160)) + return value + class Meta: ordering = ('-created_at',) verbose_name = _('submitted form') @@ -21,6 +31,47 @@ class Contact(models.Model): def __unicode__(self): return unicode(self.created_at) + def get_form_class(self): + from contact.forms import contact_forms + return contact_forms.get(self.form_tag) + + def get_update_form_class(self): + from contact.forms import update_forms + return update_forms.get(self.form_tag, self.get_form_class()) + + @permalink + def update_url(self): + form_class = self.get_update_form_class() + confirmation = form_class.confirmation_class.objects.get(contact=self) + return 'edit_form', [], {'form_tag': self.form_tag, 'contact_id': self.id, 'key': confirmation.key} + + def send_confirmation(self, context=None, form=None): + from django.template.loader import render_to_string + from django.core.mail import send_mail + from django.contrib.sites.models import Site + if not form: + form_class = self.get_form_class() + form = form_class() + dictionary = form.get_dictionary(self) + mail_subject = render_to_string([ + 'contact/%s/mail_subject.txt' % self.form_tag, + 'contact/mail_subject.txt', + ], dictionary, context).strip() + if form.result_page: + mail_body = render_to_string( + 'contact/%s/results_email.txt' % self.form_tag, + { + 'contact': self, + 'results': form.results(self), + }, context) + else: + mail_body = render_to_string([ + 'contact/%s/mail_body.txt' % self.form_tag, + 'contact/mail_body.txt', + ], dictionary, context) + site = Site.objects.get_current() + send_mail(mail_subject, mail_body, 'no-reply@%s' % site.domain, [self.contact], fail_silently=True) + class Attachment(models.Model): contact = models.ForeignKey(Contact) @@ -29,7 +80,7 @@ class Attachment(models.Model): @models.permalink def get_absolute_url(self): - return ('contact_attachment', [self.contact_id, self.tag]) + return 'contact_attachment', [self.contact_id, self.tag] __import__(app_settings.FORMS_MODULE)