Test coverage report: frequently.forms

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

Generated: Mon 2013-04-29 09:50 CEST

Source file: /home/tobi/Projects/django-frequently/src/frequently/forms.py

Stats: 30 executed, 0 missed, 7 excluded, 20 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 frequently.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. ``
30. `def save(self, *args, **kwargs):`
31. `# Create unique slug`
32. `self.instance.slug = slugify(self.cleaned_data['question'])`
33. `while True:`
34. `try:`
35. `Entry.objects.get(slug=self.instance.slug)`
36. `except Entry.DoesNotExist:`
37. `break`
38. `else:`
39. `self.instance.slug = '{0}-1'.format(self.instance.slug)`
40. `if self.owner:`
41. `self.instance.owner = self.owner`
42. `try:`
43. `settings.FREQUENTLY_RECIPIENTS`
44. `ctx_dict = {`
45. `'submitted_by': self.cleaned_data['submitted_by'],`
46. `'question': self.cleaned_data['question'],`
47. `}`
48. `subject = render_to_string(`
49. `'frequently/email/new_question_subject.txt', ctx_dict)`
50. `subject = ''.join(subject.splitlines())`
51. `message = render_to_string(`
52. `'frequently/email/new_question_body.txt', ctx_dict)`
53. `to = [item[1] for item in settings.FREQUENTLY_RECIPIENTS]`
54. `send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, to)`
55. `except AttributeError:`
56. `pass`
57. `return super(EntryForm, self).save(*args, **kwargs)`