forked from pinax/pinax-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
98 lines (78 loc) · 2.95 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from datetime import datetime
from django import forms
from django.core.exceptions import ImproperlyConfigured
from django.utils.functional import curry
from biblion.models import Post, Revision
from biblion.settings import PARSER
from biblion.utils import can_tweet, load_path_attr
class AdminPostForm(forms.ModelForm):
title = forms.CharField(
max_length = 90,
widget = forms.TextInput(
attrs = {"style": "width: 50%;"},
),
)
slug = forms.CharField(
widget = forms.TextInput(
attrs = {"style": "width: 50%;"},
)
)
teaser = forms.CharField(
widget = forms.Textarea(
attrs = {"style": "width: 80%;"},
),
)
content = forms.CharField(
widget = forms.Textarea(
attrs = {"style": "width: 80%; height: 300px;"},
)
)
publish = forms.BooleanField(
required = False,
help_text = u"Checking this will publish this articles on the site",
)
if can_tweet():
tweet = forms.BooleanField(
required = False,
help_text = u"Checking this will send out a tweet for this post",
)
class Meta:
model = Post
def __init__(self, *args, **kwargs):
super(AdminPostForm, self).__init__(*args, **kwargs)
post = self.instance
# grab the latest revision of the Post instance
latest_revision = post.latest()
if latest_revision:
# set initial data from the latest revision
self.fields["teaser"].initial = latest_revision.teaser
self.fields["content"].initial = latest_revision.content
# @@@ can a post be unpublished then re-published? should be pulled
# from latest revision maybe?
self.fields["publish"].initial = bool(post.published)
def save(self):
post = super(AdminPostForm, self).save(commit=False)
if post.pk is None:
if self.cleaned_data["publish"]:
post.published = datetime.now()
else:
if Post.objects.filter(pk=post.pk, published=None).count():
if self.cleaned_data["publish"]:
post.published = datetime.now()
render_func = curry(load_path_attr(PARSER[0], **PARSER[1]))
post.teaser_html = render_func(self.cleaned_data["teaser"])
post.content_html = render_func(self.cleaned_data["content"])
post.updated = datetime.now()
post.save()
r = Revision()
r.post = post
r.title = post.title
r.teaser = self.cleaned_data["teaser"]
r.content = self.cleaned_data["content"]
r.author = post.author
r.updated = post.updated
r.published = post.published
r.save()
if can_tweet() and self.cleaned_data["tweet"]:
post.tweet()
return post