gremmie@1: """ gremmie@1: Forms for the news application. bgneal@493: gremmie@1: """ gremmie@1: from django import forms bgneal@7: from django.conf import settings bgneal@7: bgneal@997: from core.html import ImageCheckError bgneal@997: from core.html import image_check bgneal@997: from core.markup import site_markup gremmie@1: from news.models import PendingStory gremmie@1: gremmie@1: gremmie@1: class AddNewsForm(forms.ModelForm): bgneal@997: """Form for a user to submit a news story to the admins for review.""" bgneal@997: title = forms.CharField(widget=forms.TextInput(attrs={'size': 52})) bgneal@997: short_markup = forms.CharField( bgneal@997: label="Article text", bgneal@997: widget=forms.Textarea(attrs={ bgneal@1003: 'id': 'id_body', # needed for image related js bgneal@997: 'rows': 60, bgneal@997: 'cols': 80, bgneal@997: 'class': 'markItUp smileyTarget', bgneal@997: 'style': 'height:500px', bgneal@997: })) gremmie@1: bgneal@997: class Meta: bgneal@997: model = PendingStory bgneal@997: fields = ['title', 'category', 'short_markup'] gremmie@1: bgneal@997: class Media: bgneal@997: css = { bgneal@997: 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] + bgneal@997: settings.GPP_THIRD_PARTY_CSS['jquery-ui']) bgneal@997: } bgneal@997: js = ( bgneal@997: settings.GPP_THIRD_PARTY_JS['markitup'] + bgneal@997: settings.GPP_THIRD_PARTY_JS['jquery-ui'] + bgneal@997: ['js/jquery.form.min.js'] bgneal@997: ) bgneal@997: bgneal@997: def clean_short_markup(self): bgneal@997: md = self.cleaned_data['short_markup'] bgneal@997: self.html = None bgneal@997: if not md: bgneal@997: raise forms.ValidationError("Please enter some article text") bgneal@997: bgneal@997: if md: bgneal@997: self.html = site_markup(md) bgneal@997: try: bgneal@997: image_check(self.html) bgneal@997: except ImageCheckError as ex: bgneal@997: raise forms.ValidationError(str(ex)) bgneal@997: bgneal@997: return md bgneal@997: bgneal@997: def save(self, user): bgneal@997: pending_story = super(AddNewsForm, self).save(commit=False) bgneal@997: pending_story.submitter = user bgneal@997: pending_story.save(short_text=self.html) bgneal@997: return pending_story gremmie@1: gremmie@1: gremmie@1: class SendStoryForm(forms.Form): bgneal@997: """Form for sending a news story via email to a friend.""" bgneal@997: friend_name = forms.CharField(label="Friend's Name", max_length=64) bgneal@997: friend_email = forms.EmailField(label="Friend's Email") gremmie@1: bgneal@997: def email(self): bgneal@997: return self.cleaned_data['friend_email'] bgneal@312: bgneal@997: def name(self): bgneal@997: return self.cleaned_data['friend_name'] gremmie@1: