Review generators : factorize a lot of code
Add delete hooks
This commit is contained in:
parent
b7c4cf4e2f
commit
2c7f4be96e
|
@ -21,39 +21,6 @@ class Archive(Index):
|
|||
|
||||
return None
|
||||
|
||||
def createArchives(self, src, output, dom, hooks, posts):
|
||||
filename = self.filename + '.html'
|
||||
self.nb_pages = 0
|
||||
self.cur_page = 0
|
||||
self.cur_post = 0
|
||||
|
||||
if len(posts) > self.posts_per_page:
|
||||
self.nb_pages = self.computeNbPages(len(posts), self.posts_per_page)
|
||||
|
||||
self.dirname = '/archive/' + str(self.cur_year)
|
||||
|
||||
if not os.path.exists(output + self.dirname):
|
||||
os.mkdir(output + self.dirname)
|
||||
|
||||
while self.cur_page <= self.nb_pages:
|
||||
#print 'Generate ' + filename
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
nodes[0] = self.parse(src, hooks, posts, dom, nodes[0])
|
||||
self.writeIfNotTheSame(output + self.dirname + '/' + filename, nodes[0])
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = self.filename + str(self.cur_page) + '.html'
|
||||
dom = parse(src + '/_archive.html')
|
||||
|
||||
while os.path.exists(filename):
|
||||
self.addReport('Removing unused ' + filename)
|
||||
os.unlink(filename)
|
||||
filename = filename + '.gz'
|
||||
if os.path.exists(filename):
|
||||
self.addReport('Removing unused ' + filename)
|
||||
os.unlink(filename)
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = output + self.dirname + '/' + self.filename + str(self.cur_page) + '.html'
|
||||
|
||||
def generate(self, blog, src, output):
|
||||
from dynastie.models import Post, Blog
|
||||
|
||||
|
@ -63,29 +30,8 @@ class Archive(Index):
|
|||
'tags' : self.createTags,
|
||||
'replace' : self.createReplace}
|
||||
|
||||
self.blog = blog
|
||||
|
||||
if not os.path.exists(src + '/_archive.html'):
|
||||
self.addError('No _archive.html found, exiting')
|
||||
return self.report
|
||||
|
||||
try:
|
||||
dom = parse(src + '/_archive.html')
|
||||
except xml.dom.DOMException as e:
|
||||
self.addError('Error parsing _archive.html : ' + e)
|
||||
return self.report
|
||||
|
||||
if not os.path.exists(output + '/archive'):
|
||||
os.mkdir(output + '/archive')
|
||||
|
||||
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
||||
if not post_nodes is None:
|
||||
if post_nodes[0].hasAttribute("limit"):
|
||||
self.posts_per_page = int(post_nodes[0].getAttribute("limit"))
|
||||
else:
|
||||
self.posts_per_page = 5
|
||||
else:
|
||||
self.addError('No tag dyn:posts found')
|
||||
dom = self.parseTemplate(blog, src, output, 'archive', 'archive')
|
||||
if dom is None: return self.report
|
||||
|
||||
posts = Post.objects.filter(published=True, front_page=True).order_by('creation_date')
|
||||
|
||||
|
@ -103,7 +49,13 @@ class Archive(Index):
|
|||
if posts[i].creation_date.year != posts[i+1].creation_date.year:
|
||||
dom = parse(src + '/_archive.html')
|
||||
my_post.reverse()
|
||||
self.createArchives(src, output, dom, self.hooks, my_post)
|
||||
self.nb_pages = 0
|
||||
self.cur_page = 0
|
||||
self.cur_post = 0
|
||||
|
||||
self.dirname = '/archive/' + str(self.cur_year)
|
||||
|
||||
self.generatePages(dom, my_post, src, output, 'archive', 'archive')
|
||||
self.cur_year = int(posts[i+1].creation_date.year)
|
||||
#print 'New year ' + str(self.cur_year)
|
||||
my_post = []
|
||||
|
|
|
@ -30,35 +30,13 @@ class Category(Index):
|
|||
'tags' : self.createTags,
|
||||
'replace' : self.createReplace}
|
||||
|
||||
self.blog = blog
|
||||
|
||||
if not os.path.exists(src + '/_category.html'):
|
||||
self.addError('No _category.html found, exiting')
|
||||
return self.report
|
||||
|
||||
try:
|
||||
dom = parse(src + '/_category.html')
|
||||
except xml.dom.DOMException as e:
|
||||
self.addError('Error parsing _category.html : ' + e)
|
||||
return self.report
|
||||
|
||||
if not os.path.exists(output + '/category'):
|
||||
os.mkdir(output + '/category')
|
||||
|
||||
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
||||
if not post_nodes is None:
|
||||
if post_nodes[0].hasAttribute("limit"):
|
||||
self.posts_per_page = int(post_nodes[0].getAttribute("limit"))
|
||||
else:
|
||||
self.posts_per_page = 5
|
||||
else:
|
||||
self.addError('No tag dyn:posts found')
|
||||
dom = self.parseTemplate(blog, src, output, 'category', 'category')
|
||||
if dom is None: return self.report
|
||||
|
||||
categories = Category.objects.all()
|
||||
|
||||
for category in categories:
|
||||
self.cur_category = category
|
||||
filename = self.filename + '.html'
|
||||
posts = Post.objects.filter(category__exact=category, published=True).order_by('-creation_date')
|
||||
|
||||
self.nb_pages = 0
|
||||
|
@ -67,31 +45,7 @@ class Category(Index):
|
|||
|
||||
self.dirname = '/category/' + category.name_slug
|
||||
|
||||
if posts.count() > self.posts_per_page:
|
||||
self.nb_pages = self.computeNbPages(posts.count(), self.posts_per_page)
|
||||
|
||||
if not os.path.exists(output + self.dirname):
|
||||
os.mkdir(output + self.dirname)
|
||||
|
||||
|
||||
while self.cur_page <= self.nb_pages:
|
||||
#print 'Generate ' + filename
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
nodes[0] = self.parse(src, self.hooks, posts, dom, nodes[0])
|
||||
self.writeIfNotTheSame(output + self.dirname + '/' + filename, nodes[0])
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = self.filename + str(self.cur_page) + '.html'
|
||||
dom = parse(src + '/_category.html')
|
||||
|
||||
while os.path.exists(filename):
|
||||
self.addReport('Removing unused ' + filename)
|
||||
os.unlink(filename)
|
||||
filename = filename + '.gz'
|
||||
if os.path.exists(filename):
|
||||
self.addReport('Removing unused ' + filename)
|
||||
os.unlink(filename)
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = output + self.dirname + '/' + self.filename + str(self.cur_page) + '.html'
|
||||
self.generatePages(dom, posts, src, output, 'category', 'category')
|
||||
|
||||
if not self.somethingWrote:
|
||||
self.addReport('Nothing changed')
|
||||
|
|
|
@ -8,7 +8,7 @@ from xml.dom.minidom import parse
|
|||
from xml.parsers.expat import *
|
||||
|
||||
class StrictUTF8Writer(codecs.StreamWriter):
|
||||
'''A StreamWriter for utf8 that requires written objects be unicode'''
|
||||
'''A StreamWriter for utf8'''
|
||||
encode = codecs.utf_8_encode
|
||||
value = ''
|
||||
|
||||
|
@ -34,10 +34,6 @@ class StrictUTF8Writer(codecs.StreamWriter):
|
|||
|
||||
def getvalue(self):
|
||||
return self.value
|
||||
#self.stream.write(object)
|
||||
# if not isinstance(object, unicode):
|
||||
# raise ValueError('write() requires unicode object')
|
||||
# return codecs.StreamWriter.write(self, object)
|
||||
|
||||
class DynastieGenerator:
|
||||
|
||||
|
@ -64,6 +60,7 @@ class DynastieGenerator:
|
|||
def addError(self, string):
|
||||
self.addReport(string, 'red')
|
||||
|
||||
# Virtual
|
||||
def generate(self, blog, src, output):
|
||||
return
|
||||
|
||||
|
@ -132,6 +129,7 @@ class DynastieGenerator:
|
|||
|
||||
return div
|
||||
|
||||
# Recursively transform <dyn:XXX> elements with the ones in values
|
||||
def simpleTransform(self, values, dom, elem, root):
|
||||
for node in root.childNodes:
|
||||
if node.prefix == 'dyn':
|
||||
|
|
|
@ -275,26 +275,21 @@ class Index(DynastieGenerator):
|
|||
|
||||
return code
|
||||
|
||||
def generate(self, blog, src, output):
|
||||
from dynastie.models import Post, Blog
|
||||
|
||||
self.hooks = {'posts' : self.createPosts,
|
||||
'navigation' : self.createNavigation,
|
||||
'recents' : self.createRecents,
|
||||
'tags' : self.createTags,
|
||||
'replace' : self.createReplace}
|
||||
|
||||
def parseTemplate(self, blog, src, output, name, directory=None):
|
||||
self.blog = blog
|
||||
|
||||
if not os.path.exists(src + '/_index.html'):
|
||||
self.addError('No _index.html found, exiting')
|
||||
return self.report
|
||||
if not os.path.exists(src + '/_%s.html' % name):
|
||||
self.addError('No _%s.html found, exiting' % name)
|
||||
return None
|
||||
|
||||
try:
|
||||
dom = parse(src + '/_index.html')
|
||||
dom = parse(src + '/_%s.html' % name)
|
||||
except xml.dom.DOMException as e:
|
||||
self.addError('Error parsing _index.html : ' + e)
|
||||
return self.report
|
||||
self.addError('Error parsing _%s.html : ' + e)
|
||||
return None
|
||||
|
||||
if not directory is None and not os.path.exists(output + '/' + directory):
|
||||
os.mkdir(output + '/' + directory)
|
||||
|
||||
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
||||
|
||||
|
@ -306,25 +301,25 @@ class Index(DynastieGenerator):
|
|||
else:
|
||||
self.addError('No tag dyn:posts found')
|
||||
|
||||
now = datetime.datetime.now()
|
||||
cur_year = now.year
|
||||
posts = Post.objects.filter(creation_date__year=cur_year, published=True, front_page=True).order_by('-creation_date')
|
||||
return dom
|
||||
|
||||
if posts.count() < self.posts_per_page:
|
||||
posts = Post.objects.all()[:self.posts_per_page]
|
||||
def generatePages(self, dom, posts, src, output, name, directory=None):
|
||||
if len(posts) > self.posts_per_page:
|
||||
self.nb_pages = self.computeNbPages(len(posts), self.posts_per_page)
|
||||
|
||||
if posts.count() > self.posts_per_page:
|
||||
self.nb_pages = self.computeNbPages(posts.count(), self.posts_per_page)
|
||||
if not directory is None and not os.path.exists(output + self.dirname):
|
||||
os.mkdir(output + self.dirname)
|
||||
|
||||
filename = self.dirname + self.filename + '.html'
|
||||
|
||||
filename = 'index.html'
|
||||
while self.cur_page <= self.nb_pages:
|
||||
#print 'Generate ' + filename
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
nodes[0] = self.parse(src, self.hooks, posts, dom, nodes[0])
|
||||
self.writeIfNotTheSame(output + '/' + filename, nodes[0])
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = 'index' + str(self.cur_page) + '.html'
|
||||
dom = parse(src + '/_index.html')
|
||||
filename = self.dirname + self.filename + str(self.cur_page) + '.html'
|
||||
dom = parse(src + '/_%s.html' % name)
|
||||
|
||||
filename = output + '/' + filename
|
||||
|
||||
|
@ -336,7 +331,30 @@ class Index(DynastieGenerator):
|
|||
self.addReport('Removing unused ' + filename)
|
||||
os.unlink(filename)
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = output + '/index' + str(self.cur_page) + '.html'
|
||||
filename = output + '/' + self.filename + str(self.cur_page) + '.html'
|
||||
|
||||
def generate(self, blog, src, output):
|
||||
from dynastie.models import Post, Blog
|
||||
|
||||
self.hooks = {'posts' : self.createPosts,
|
||||
'navigation' : self.createNavigation,
|
||||
'recents' : self.createRecents,
|
||||
'tags' : self.createTags,
|
||||
'replace' : self.createReplace}
|
||||
|
||||
|
||||
dom = self.parseTemplate(blog, src, output, 'index')
|
||||
if dom is None: return self.report
|
||||
|
||||
now = datetime.datetime.now()
|
||||
cur_year = now.year
|
||||
posts = Post.objects.filter(creation_date__year=cur_year, published=True, front_page=True).order_by('-creation_date')
|
||||
|
||||
if posts.count() < self.posts_per_page:
|
||||
posts = Post.objects.all()[:self.posts_per_page]
|
||||
|
||||
self.dirname = ''
|
||||
self.generatePages(dom, posts, src, output, 'index')
|
||||
|
||||
if not self.somethingWrote:
|
||||
self.addReport('Nothing changed')
|
||||
|
|
|
@ -146,18 +146,16 @@ class Post(Index):
|
|||
'tags' : self.createTags}
|
||||
|
||||
self.blog = blog
|
||||
name = 'post'
|
||||
|
||||
if not os.path.exists(src + '/_post.html'):
|
||||
self.addError('No _post.html found, exiting')
|
||||
if not os.path.exists(src + '/_%s.html' % name):
|
||||
self.addError('No _%s.html found, exiting' % name)
|
||||
return self.report
|
||||
|
||||
if not os.path.exists(output + '/post'):
|
||||
os.mkdir(output + '/post')
|
||||
|
||||
try:
|
||||
dom = parse(src + '/_post.html')
|
||||
dom = parse(src + '/_%s.html' % name)
|
||||
except xml.dom.DOMException as e:
|
||||
self.addError('Error parsing _post.html : ' + e)
|
||||
self.addError('Error parsing _%s.html : ' + e)
|
||||
return self.report
|
||||
|
||||
for post in posts:
|
||||
|
|
|
@ -30,35 +30,13 @@ class Tag(Index):
|
|||
'tags' : self.createTags,
|
||||
'replace' : self.createReplace}
|
||||
|
||||
self.blog = blog
|
||||
|
||||
if not os.path.exists(src + '/_tag.html'):
|
||||
self.addError('No _tag.html found, exiting')
|
||||
return self.report
|
||||
|
||||
try:
|
||||
dom = parse(src + '/_tag.html')
|
||||
except xml.dom.DOMException as e:
|
||||
self.addError('Error parsing _tag.html : ' + e)
|
||||
return self.report
|
||||
|
||||
if not os.path.exists(output + '/tag'):
|
||||
os.mkdir(output + '/tag')
|
||||
|
||||
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
||||
if not post_nodes is None:
|
||||
if post_nodes[0].hasAttribute("limit"):
|
||||
self.posts_per_page = int(post_nodes[0].getAttribute("limit"))
|
||||
else:
|
||||
self.posts_per_page = 5
|
||||
else:
|
||||
self.addError('No tag dyn:posts found')
|
||||
dom = self.parseTemplate(blog, src, output, 'tag', 'tag')
|
||||
if dom is None: return self.report
|
||||
|
||||
tags = Tag.objects.all()
|
||||
|
||||
for tag in tags:
|
||||
self.cur_tag = tag
|
||||
filename = self.filename + '.html'
|
||||
posts = Post.objects.filter(tags__in=[tag.id], published=True).order_by('-creation_date')
|
||||
|
||||
self.nb_pages = 0
|
||||
|
@ -67,31 +45,7 @@ class Tag(Index):
|
|||
|
||||
self.dirname = '/tag/' + tag.name_slug
|
||||
|
||||
if posts.count() > self.posts_per_page:
|
||||
self.nb_pages = self.computeNbPages(posts.count(), self.posts_per_page)
|
||||
|
||||
if not os.path.exists(output + self.dirname):
|
||||
os.mkdir(output + self.dirname)
|
||||
|
||||
|
||||
while self.cur_page <= self.nb_pages:
|
||||
#print 'Generate ' + filename
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
nodes[0] = self.parse(src, self.hooks, posts, dom, nodes[0])
|
||||
self.writeIfNotTheSame(output + self.dirname + '/' + filename, nodes[0])
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = self.filename + str(self.cur_page) + '.html'
|
||||
dom = parse(src + '/_tag.html')
|
||||
|
||||
while os.path.exists(filename):
|
||||
self.addReport('Removing unused ' + filename)
|
||||
os.unlink(filename)
|
||||
filename = filename + '.gz'
|
||||
if os.path.exists(filename):
|
||||
self.addReport('Removing unused ' + filename)
|
||||
os.unlink(filename)
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = output + self.dirname + '/' + self.filename + str(self.cur_page) + '.html'
|
||||
self.generatePages(dom, posts, src, output, 'tag', 'tag')
|
||||
|
||||
if not self.somethingWrote:
|
||||
self.addReport('Nothing changed')
|
||||
|
|
16
models.py
16
models.py
|
@ -8,7 +8,7 @@ from re import sub
|
|||
from datetime import datetime
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.signals import post_init, post_delete, post_save
|
||||
from django.db.models.signals import post_init, pre_delete, post_delete, post_save
|
||||
from django.dispatch import receiver
|
||||
from dynastie.generators import *
|
||||
|
||||
|
@ -177,7 +177,8 @@ class Category(models.Model):
|
|||
self.name_slug = slugify(self.name)
|
||||
super(Category, self).save()
|
||||
|
||||
def remove(self, blog):
|
||||
def remove(self):
|
||||
blog = Blog.objects.get(pk=self.blog)
|
||||
output = blog.output_path + '/category/' + self.name_slug
|
||||
if os.path.exists(output):
|
||||
shutil.rmtree(output)
|
||||
|
@ -192,7 +193,8 @@ class Tag(models.Model):
|
|||
self.name_slug = slugify(self.name)
|
||||
super(Tag, self).save()
|
||||
|
||||
def remove(self, blog):
|
||||
def remove(self):
|
||||
blog = Blog.objects.get(pk=self.blog)
|
||||
output = blog.output_path + '/tag/' + self.name_slug
|
||||
if os.path.exists(output):
|
||||
shutil.rmtree(output)
|
||||
|
@ -347,6 +349,14 @@ def init_blog_signal(sender, **kwargs):
|
|||
def delete_blog_signal(sender, **kwargs):
|
||||
kwargs['instance'].remove()
|
||||
|
||||
@receiver(pre_delete, sender=Category)
|
||||
def delete_category_signal(sender, **kwargs):
|
||||
kwargs['instance'].remove()
|
||||
|
||||
@receiver(pre_delete, sender=Tag)
|
||||
def delete_tag_signal(sender, **kwargs):
|
||||
kwargs['instance'].remove()
|
||||
|
||||
@receiver(post_delete, sender=Post)
|
||||
def delete_post_signal(sender, **kwargs):
|
||||
kwargs['instance'].remove()
|
||||
|
|
|
@ -67,6 +67,7 @@
|
|||
<div class="menu_content_header">Archives</div>
|
||||
<div class="menu_content_content">
|
||||
<ul>
|
||||
<li><a href="/archive/2012">2012</a></li>
|
||||
<li><a href="/archive/2011">2011</a></li>
|
||||
<li><a href="/archive/2010">2010</a></li>
|
||||
</ul>
|
||||
|
|
|
@ -69,6 +69,7 @@
|
|||
<div class="menu_content_header">Archives</div>
|
||||
<div class="menu_content_content">
|
||||
<ul>
|
||||
<li><a href="/archive/2012">2012</a></li>
|
||||
<li><a href="/archive/2011">2011</a></li>
|
||||
<li><a href="/archive/2010">2010</a></li>
|
||||
</ul>
|
||||
|
|
2
urls.py
2
urls.py
|
@ -5,7 +5,6 @@ from django.conf.urls import patterns, include, url
|
|||
# admin.autodiscover()
|
||||
|
||||
urlpatterns = patterns('',
|
||||
# Examples:
|
||||
url(r'^index$', 'dynastie.views.index', name='index'),
|
||||
url(r'^$', 'dynastie.views.index', name='index'),
|
||||
url(r'^disconnect$', 'dynastie.views.disconnect', name='disconnect'),
|
||||
|
@ -36,7 +35,6 @@ urlpatterns = patterns('',
|
|||
url(r'^tag/delete/(\d+)$', 'dynastie.views.delete_tag', name='delete_tag'),
|
||||
url(r'^search/generate/(\d+)$', 'dynastie.views.generate_search',name='generate_search'),
|
||||
url(r'^search/(\d+)$', 'dynastie.views.search', name='search'),
|
||||
# url(r'^dynastie/', include('dynastie.foo.urls')),
|
||||
|
||||
# Uncomment the admin/doc line below to enable admin documentation:
|
||||
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
|
||||
|
|
6
views.py
6
views.py
|
@ -214,7 +214,7 @@ def edit_category(request, category_id):
|
|||
form = CategoryForm(request.POST, instance=category)
|
||||
if form.is_valid():
|
||||
if request.POST['name'] != name:
|
||||
category.remove(b)
|
||||
category.remove()
|
||||
form.save()
|
||||
return HttpResponseRedirect('/category/' + str(b.id))
|
||||
else:
|
||||
|
@ -233,7 +233,6 @@ def delete_category(request, category_id):
|
|||
|
||||
b,_ = have_I_right(request, category.blog.id)
|
||||
|
||||
category.remove(b)
|
||||
category.delete()
|
||||
|
||||
return HttpResponseRedirect('/category/' + str(b.id))
|
||||
|
@ -265,7 +264,7 @@ def edit_tag(request, tag_id):
|
|||
form = TagForm(request.POST, instance=tag)
|
||||
if form.is_valid():
|
||||
if request.POST['name'] != name:
|
||||
tag.remove(b)
|
||||
tag.remove()
|
||||
form.save()
|
||||
return HttpResponseRedirect('/tag/' + str(b.id))
|
||||
else:
|
||||
|
@ -284,7 +283,6 @@ def delete_tag(request, tag_id):
|
|||
|
||||
b,_ = have_I_right(request, tag.blog.id)
|
||||
|
||||
tag.remove(b)
|
||||
tag.delete()
|
||||
|
||||
return HttpResponseRedirect('/tag/' + str(b.id))
|
||||
|
|
Loading…
Reference in New Issue
Block a user