-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.py
193 lines (163 loc) · 6.85 KB
/
queue.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
192
193
## ----------------------------------------------------------------------------
# Lollysite is a website builder and blogging platform for Google App Engine.
#
# Copyright (c) 2009, 2010 Andrew Chilton <andy@chilts.org>.
#
# Homepage : http://www.chilts.org/project/lollysite/
# Ohloh : https://www.ohloh.net/p/lollysite/
# FreshMeat : http://freshmeat.net/projects/lollysite
# Source : http://gitorious.org/lollysite/
#
# This file is part of Lollysite.
#
# Lollysite is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Lollysite is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Lollysite. If not, see <http://www.gnu.org/licenses/>.
#
## ----------------------------------------------------------------------------
# import standard modules
import cgi
import os
import logging
import re
# Google specific modules
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.db import GqlQuery
from google.appengine.api import mail
# local modules
import webbase
from models import Section, Node, Comment
import config
## ----------------------------------------------------------------------------
# try http://localhost:8080/_ah/queue/section-regenerate?key=agpjaGlsdHMtb3Jncg0LEgdTZWN0aW9uGGQM
class SectionRegenerate(webbase.WebBase):
def post(self):
self.response.headers['Content-Type'] = 'text/plain'
self.write('Started section regeneration task:')
key = self.request.get('key')
self.write('- key = ' + key)
# ok, so get the section first
section = Section.get( self.request.get('key') )
if section is None:
self.write('No section found')
logging.warn( 'No section found for key: ' + key )
return
# keep a count of all these things
labels = {}
archives = {}
# get all the nodes
# FixMe: why doesn't "nodes = section.nodes()" work ???
nodes = Node.all().filter('section =', section.key())
self.write('Nodes:')
for node in nodes:
self.write('- node=' + node.kind()) # FixMe: why doesn't "node.name()" work :(
# count all of the labels
for label in node.label:
self.write(' - ' + label)
if label in labels:
labels[label] += 1
else:
labels[label] = 1
# the archive counts
for archive in node.archive:
self.write(' - ' + archive)
if archive in archives:
archives[archive] += 1
else:
archives[archive] = 1
# after all that, make them into a list (for ease of use in Django Templates)
archives = [
{ 'archive' : x, 'count' : archives[x] }
for x in sorted(archives)
if re.search(r'^\d\d\d\d(-\d\d)?$', x, re.DOTALL | re.VERBOSE) # just get years and months
]
labels = [
{ 'label' : x, 'count' : labels[x] }
for x in sorted(labels)
]
# now that we have our counts, save it as JSON
section.archive_json = archives
section.label_json = labels
section.put()
self.write('Finished')
# try http://localhost:8080/_ah/queue/section-check-duplicate-nodes?key=?&name=?
class SectionCheckDuplicateNodes(webbase.WebBase):
def post(self):
self.response.headers['Content-Type'] = 'text/plain'
self.write('Started section duplicate node check:')
section_key = self.request.get('section_key')
name = self.request.get('name')
self.write('- key = ' + section_key)
self.write('- name = ' + name)
# get the section first (and if the section_key is crap just finish)
section = None
try:
section = Section.get( self.request.get('section_key') )
except:
# by just returning here, it ends up a 200, so all is good
return
if section is None:
self.write('No section found')
logging.warn( 'No section found for key: ' + section_key )
return
nodes = Node.all().filter('section =', section).filter('name =', name)
if nodes.count() <= 1:
msg = 'Only [%d] nodes of this name in this section' % nodes.count()
self.write(msg)
return
msg = 'More than one node named [%s] in section [%s]' % (name, section.path)
self.write(msg)
logging.warn(msg)
admin_email = util.config_value('Admin Email')
if not mail.is_email_valid(admin_email):
return
url_edit = util.construct_url() + '/admin/node/'
body = 'Section %s has two nodes named %s ' % (section.path, name)
mail.send_mail(admin_email, admin_email, 'Duplicate node name in section ' + section.path, body)
# try http://localhost:8080/_ah/queue/node-regenerate?key=?
class NodeRegenerate(webbase.WebBase):
def post(self):
self.response.headers['Content-Type'] = 'text/plain'
self.write('Started node regeneration task:')
key = self.request.get('key')
self.write('- key = ' + key)
# ok, so get the section first
node = db.Model.get( self.request.get('key') )
if node is None:
self.write('No node found')
return
self.write('Section = ' + node.section.path)
self.write('Node = ' + node.name)
# get the approved comments and update the node itself
comments = Comment.all().filter('node =', node.key()).filter('status =', 'approved')
node.comment_count = comments.count()
self.write('- count=' + str(node.comment_count))
node.put()
self.write('Finished')
application = webapp.WSGIApplication(
[
# these locations are the default
# See: http://code.google.com/appengine/docs/python/taskqueue/overview.html#Queue_Default_URLs
('/_ah/queue/section-regenerate', SectionRegenerate),
('/_ah/queue/node-regenerate', NodeRegenerate),
('/_ah/queue/section-check-duplicate-nodes', SectionCheckDuplicateNodes),
],
debug = True
)
## ----------------------------------------------------------------------------
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
## ----------------------------------------------------------------------------