annotate news/forms.py @ 1163:44e55e4317f8

Merge with mainline.
author Brian Neal <bgneal@gmail.com>
date Tue, 07 Mar 2017 19:47:18 -0600
parents 21c592cac71c
children
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
bgneal@997 7 from core.html import ImageCheckError
bgneal@997 8 from core.html import image_check
bgneal@997 9 from core.markup import site_markup
gremmie@1 10 from news.models import PendingStory
gremmie@1 11
gremmie@1 12
gremmie@1 13 class AddNewsForm(forms.ModelForm):
bgneal@997 14 """Form for a user to submit a news story to the admins for review."""
bgneal@997 15 title = forms.CharField(widget=forms.TextInput(attrs={'size': 52}))
bgneal@997 16 short_markup = forms.CharField(
bgneal@997 17 label="Article text",
bgneal@997 18 widget=forms.Textarea(attrs={
bgneal@1003 19 'id': 'id_body', # needed for image related js
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 def clean_short_markup(self):
bgneal@997 31 md = self.cleaned_data['short_markup']
bgneal@997 32 self.html = None
bgneal@997 33 if not md:
bgneal@997 34 raise forms.ValidationError("Please enter some article text")
bgneal@997 35
bgneal@997 36 if md:
bgneal@997 37 self.html = site_markup(md)
bgneal@997 38 try:
bgneal@997 39 image_check(self.html)
bgneal@997 40 except ImageCheckError as ex:
bgneal@997 41 raise forms.ValidationError(str(ex))
bgneal@997 42
bgneal@997 43 return md
bgneal@997 44
bgneal@997 45 def save(self, user):
bgneal@997 46 pending_story = super(AddNewsForm, self).save(commit=False)
bgneal@997 47 pending_story.submitter = user
bgneal@997 48 pending_story.save(short_text=self.html)
bgneal@997 49 return pending_story
gremmie@1 50
gremmie@1 51
gremmie@1 52 class SendStoryForm(forms.Form):
bgneal@997 53 """Form for sending a news story via email to a friend."""
bgneal@997 54 friend_name = forms.CharField(label="Friend's Name", max_length=64)
bgneal@997 55 friend_email = forms.EmailField(label="Friend's Email")
gremmie@1 56
bgneal@997 57 def email(self):
bgneal@997 58 return self.cleaned_data['friend_email']
bgneal@312 59
bgneal@997 60 def name(self):
bgneal@997 61 return self.cleaned_data['friend_name']
gremmie@1 62