forked from Arachnid/bloggart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.py
191 lines (161 loc) · 5.78 KB
/
handlers.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import datetime
import logging
import os
from google.appengine.ext import deferred
from google.appengine.ext import webapp
import config
import markup
import models
import post_deploy
import utils
from django import forms
class ModelFormOptions(object):
"""A simple class to hold internal options for a ModelForm class.
Instance attributes:
model: a db.Model class, or None
fields: list of field names to be defined, or None
exclude: list of field names to be skipped, or None
These instance attributes are copied from the 'Meta' class that is
usually present in a ModelForm class, and all default to None.
"""
def __init__(self, options=None):
self.model = getattr(options, 'model', None)
self.fields = getattr(options, 'fields', None)
self.exclude = getattr(options, 'exclude', None)
class InitialDataForm(object):
def __init__(self, instance=None, initial=None, *args, **kwargs):
opts = ModelFormOptions(getattr(self, 'meta', None))
object_data = {}
if instance is not None:
for name, prop in instance.properties().iteritems():
if opts.fields and name not in opts.fields:
continue
if opts.exclude and name in opts.exclude:
continue
if hasattr(prop, 'get_value_for_form'):
object_data[name] = prop.get_value_for_form(instance)
else:
object_data[name] = getattr(instance, name)
if initial is not None:
object_data.update(initial)
kwargs['initial'] = object_data
super(InitialDataForm, self).__init__(*args, **kwargs)
class PostForm(InitialDataForm, forms.Form):
title = forms.CharField(widget=forms.TextInput(attrs={'id':'name'}))
body = forms.CharField(widget=forms.Textarea(attrs={
'id':'message',
'rows': 10,
'cols': 20}))
body_markup = forms.ChoiceField(
choices=[(k, v[0]) for k, v in markup.MARKUP_MAP.iteritems()])
tags = forms.CharField(widget=forms.Textarea(attrs={'rows': 5, 'cols': 20}))
draft = forms.BooleanField(required=False)
class Meta:
model = models.BlogPost
fields = [ 'title', 'body', 'tags' ]
def with_post(fun):
def decorate(self, post_id=None):
post = None
if post_id:
post = models.BlogPost.get_by_id(int(post_id))
if not post:
self.error(404)
return
fun(self, post)
return decorate
class BaseHandler(webapp.RequestHandler):
def render_to_response(self, template_name, template_vals=None, theme=None):
if not template_vals:
template_vals = {}
template_vals.update({
'path': self.request.path,
'handler_class': self.__class__.__name__,
'is_admin': True,
})
template_name = os.path.join("admin", template_name)
self.response.out.write(utils.render_template(template_name, template_vals,
theme))
class AdminHandler(BaseHandler):
def get(self):
from generators import generator_list
offset = int(self.request.get('start', 0))
count = int(self.request.get('count', 20))
posts = models.BlogPost.all().order('-published').fetch(count, offset)
template_vals = {
'offset': offset,
'count': count,
'last_post': offset + len(posts) - 1,
'prev_offset': max(0, offset - count),
'next_offset': offset + count,
'posts': posts,
'generators': [cls.__name__ for cls in generator_list],
}
self.render_to_response("index.html", template_vals)
class PostHandler(BaseHandler):
def render_form(self, form):
self.render_to_response("edit.html", {'form': form})
@with_post
def get(self, post):
self.render_form(PostForm(
instance=post,
initial={
'draft': post and not post.path,
'body_markup': post and post.body_markup or config.default_markup,
}))
@with_post
def post(self, post):
form = PostForm(data=self.request.POST, instance=post,
initial={'draft': post and post.published is None})
if form.is_valid():
data = {
'title': form.cleaned_data['title'],
'body': form.cleaned_data['body'],
'body_markup': form.cleaned_data['body_markup'],
}
if post is None:
post = models.BlogPost(**data)
else:
for name, value in data.iteritems():
setattr(post, name, value)
post.tags = post.properties()['tags'] \
.make_value_from_form(form.cleaned_data['tags'])
if form.cleaned_data['draft']:# Draft post
post.published = datetime.datetime.max
post.put()
else:
if not post.path: # Publish post
post.updated = post.published = datetime.datetime.now()
else:# Edit post
post.updated = datetime.datetime.now()
post.publish(regenerate=True)
self.render_to_response("published.html", {
'post': post,
'draft': form.cleaned_data['draft']})
else:
self.render_form(form)
class DeleteHandler(BaseHandler):
@with_post
def post(self, post):
if post.path:# Published post
post.remove()
else:# Draft
post.delete()
self.render_to_response("deleted.html", None)
class PreviewHandler(BaseHandler):
@with_post
def get(self, post):
# Temporary set a published date iff it's still
# datetime.max. Django's date filter has a problem with
# datetime.max and a "real" date looks better.
if post.published == datetime.datetime.max:
post.published = datetime.datetime.now()
self.response.out.write(utils.render_template('post.html', {
'post': post,
'is_admin': True}))
class RegenerateHandler(BaseHandler):
def post(self):
generators = self.request.get_all("generators")
deferred.defer(post_deploy.try_post_deploy, force=True, regenerate_kwargs={
'classes': generators
})
self.render_to_response("regenerating.html")