Test coverage report: frequently.forms

# frequently.forms: 45 total statements, 91.9% covered

Generated: Tue 2013-12-10 08:42 CET

Source file: /home/dkaufhold/projects/django-frequently/src/frequently/forms.py

Stats: 34 executed, 3 missed, 8 excluded, 23 ignored

1. `"""`
2. `Forms for the ``django-frequently`` application.`
3. ``
4. `"""`
5. `from django import forms`
6. `from django.conf import settings`
7. `from django.core.mail import send_mail`
8. `from django.template.defaultfilters import slugify`
9. `from django.template.loader import render_to_string`
10. `from django.utils.translation import ugettext_lazy as _`
11. ``
12. `from . import app_settings`
13. `from .models import Entry`
14. ``
15. ``
16. `class EntryForm(forms.ModelForm):`
17. `"""`
18. `Form to submit a new entry.`
19. ``
20. `"""`
21. `class Meta:`
22. `model = Entry`
23. `fields = ('question', 'submitted_by', )`
24. ``
25. `def __init__(self, owner=None, *args, **kwargs):`
26. `self.owner = owner`
27. `super(EntryForm, self).__init__(*args, **kwargs)`
28. `self.fields['submitted_by'].label = _('Email')`
29. `self.fields['submitted_by'].required = True`
30. `if not app_settings.REQUIRE_EMAIL:`
31. `self.fields.pop('submitted_by')`
32. ``
33. `def save(self, *args, **kwargs):`
34. `# Create unique slug`
35. `self.instance.slug = slugify(self.cleaned_data['question'])`
36. `while True:`
37. `try:`
38. `Entry.objects.get(slug=self.instance.slug)`
39. `except Entry.DoesNotExist:`
40. `break`
41. `else:`
42. `self.instance.slug = '{0}-1'.format(self.instance.slug)`
43. ``
44. `submitted_by = self.cleaned_data.get('submitted_by')`
45. `if self.owner:`
46. `self.instance.owner = self.owner`
47. `if not app_settings.REQUIRE_EMAIL:`
48. `self.instance.submitted_by = self.owner.email`
49. `submitted_by = self.instance.submitted_by`
50. ``
51. `obj = super(EntryForm, self).save(*args, **kwargs)`
52. `try:`
53. `settings.FREQUENTLY_RECIPIENTS`
54. `ctx_dict = {`
55. `'submitted_by': submitted_by,`
56. `'question': self.cleaned_data['question'],`
57. `'instance': obj,`
58. `}`
59. `subject = render_to_string(`
60. `'frequently/email/new_question_subject.txt', ctx_dict)`
61. `subject = ''.join(subject.splitlines())`
62. `message = render_to_string(`
63. `'frequently/email/new_question_body.txt', ctx_dict)`
64. `to = [item[1] for item in settings.FREQUENTLY_RECIPIENTS]`
65. `send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, to)`
66. `except AttributeError:`
67. `pass`
68. `return obj`