annotate news/forms.py @ 998:e2c3d7ecfa30

Added tests for submit news.
author Brian Neal <bgneal@gmail.com>
date Fri, 20 Nov 2015 23:07:37 -0600
parents 19b86e684cc2
children fa52bfb28217
rev   line source
gremmie@1 1 """
gremmie@1 2 Forms for the news application.
bgneal@493 3
gremmie@1 4 """
gremmie@1 5 from django import forms
bgneal@7 6 from django.conf import settings
bgneal@7 7
bgneal@997 8 from core.html import ImageCheckError
bgneal@997 9 from core.html import image_check
bgneal@997 10 from core.markup import site_markup
gremmie@1 11 from news.models import PendingStory
gremmie@1 12
gremmie@1 13
gremmie@1 14 class AddNewsForm(forms.ModelForm):
bgneal@997 15 """Form for a user to submit a news story to the admins for review."""
bgneal@997 16 title = forms.CharField(widget=forms.TextInput(attrs={'size': 52}))
bgneal@997 17 short_markup = forms.CharField(
bgneal@997 18 label="Article text",
bgneal@997 19 widget=forms.Textarea(attrs={
bgneal@997 20 'rows': 60,
bgneal@997 21 'cols': 80,
bgneal@997 22 'class': 'markItUp smileyTarget',
bgneal@997 23 'style': 'height:500px',
bgneal@997 24 }))
gremmie@1 25
bgneal@997 26 class Meta:
bgneal@997 27 model = PendingStory
bgneal@997 28 fields = ['title', 'category', 'short_markup']
gremmie@1 29
bgneal@997 30 class Media:
bgneal@997 31 css = {
bgneal@997 32 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
bgneal@997 33 settings.GPP_THIRD_PARTY_CSS['jquery-ui'])
bgneal@997 34 }
bgneal@997 35 js = (
bgneal@997 36 settings.GPP_THIRD_PARTY_JS['markitup'] +
bgneal@997 37 settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
bgneal@997 38 ['js/jquery.form.min.js']
bgneal@997 39 )
bgneal@997 40
bgneal@997 41 def clean_short_markup(self):
bgneal@997 42 md = self.cleaned_data['short_markup']
bgneal@997 43 self.html = None
bgneal@997 44 if not md:
bgneal@997 45 raise forms.ValidationError("Please enter some article text")
bgneal@997 46
bgneal@997 47 if md:
bgneal@997 48 self.html = site_markup(md)
bgneal@997 49 try:
bgneal@997 50 image_check(self.html)
bgneal@997 51 except ImageCheckError as ex:
bgneal@997 52 raise forms.ValidationError(str(ex))
bgneal@997 53
bgneal@997 54 return md
bgneal@997 55
bgneal@997 56 def save(self, user):
bgneal@997 57 pending_story = super(AddNewsForm, self).save(commit=False)
bgneal@997 58 pending_story.submitter = user
bgneal@997 59 pending_story.save(short_text=self.html)
bgneal@997 60 return pending_story
gremmie@1 61
gremmie@1 62
gremmie@1 63 class SendStoryForm(forms.Form):
bgneal@997 64 """Form for sending a news story via email to a friend."""
bgneal@997 65 friend_name = forms.CharField(label="Friend's Name", max_length=64)
bgneal@997 66 friend_email = forms.EmailField(label="Friend's Email")
gremmie@1 67
bgneal@997 68 def email(self):
bgneal@997 69 return self.cleaned_data['friend_email']
bgneal@312 70
bgneal@997 71 def name(self):
bgneal@997 72 return self.cleaned_data['friend_name']
gremmie@1 73