Test coverage report: frequently.forms

# frequently.forms: 44 total statements, 100.0% covered

Generated: Fri 2015-09-11 18:53 CEST

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

Stats: 37 executed, 0 missed, 7 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 .models import Entry`
13. ``
14. ``
15. `class EntryForm(forms.ModelForm):`
16. `"""`
17. `Form to submit a new entry.`
18. ``
19. `"""`
20. `class Meta:`
21. `model = Entry`
22. `fields = ('question', 'submitted_by', )`
23. ``
24. `def __init__(self, owner=None, *args, **kwargs):`
25. `self.owner = owner`
26. `super(EntryForm, self).__init__(*args, **kwargs)`
27. `self.fields['submitted_by'].label = _('Email')`
28. `self.fields['submitted_by'].required = True`
29. `if not getattr(settings, 'FREQUENTLY_REQUIRE_EMAIL', True):`
30. `self.fields.pop('submitted_by')`
31. ``
32. `def save(self, *args, **kwargs):`
33. `# Create unique slug`
34. `self.instance.slug = slugify(self.cleaned_data['question'])[:100]`
35. `while True:`
36. `try:`
37. `Entry.objects.get(slug=self.instance.slug)`
38. `except Entry.DoesNotExist:`
39. `break`
40. `else:`
41. `self.instance.slug = '{0}-1'.format(self.instance.slug)`
42. ``
43. `submitted_by = self.cleaned_data.get('submitted_by')`
44. `if self.owner:`
45. `self.instance.owner = self.owner`
46. `if not getattr(settings, 'FREQUENTLY_REQUIRE_EMAIL', True):`
47. `self.instance.submitted_by = self.owner.email`
48. `submitted_by = self.instance.submitted_by`
49. ``
50. `obj = super(EntryForm, self).save(*args, **kwargs)`
51. `try:`
52. `settings.FREQUENTLY_RECIPIENTS`
53. `ctx_dict = {`
54. `'submitted_by': submitted_by,`
55. `'question': self.cleaned_data['question'],`
56. `'instance': obj,`
57. `}`
58. `subject = render_to_string(`
59. `'frequently/email/new_question_subject.txt', ctx_dict)`
60. `subject = ''.join(subject.splitlines())`
61. `message = render_to_string(`
62. `'frequently/email/new_question_body.txt', ctx_dict)`
63. `to = [item[1] for item in settings.FREQUENTLY_RECIPIENTS]`
64. `send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, to)`
65. `except AttributeError:`
66. `pass`
67. `return obj`