comparison news/forms.py @ 997:19b86e684cc2

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