Do things in a more python way
This commit is contained in:
parent
5e1007e7da
commit
7aa99e0cbe
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1,4 +1,6 @@
|
|||
*~
|
||||
*.pyc
|
||||
*_*
|
||||
*.bdd
|
||||
*.bdd
|
||||
*.gz
|
||||
*.bak
|
|
@ -17,17 +17,11 @@
|
|||
You should have received a copy of the GNU General Public License
|
||||
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
import os
|
||||
from xml.dom.minidom import parse, parseString
|
||||
from dynastie.generators.generator import DynastieGenerator
|
||||
from dynastie.generators.index import Index
|
||||
from django.db import models
|
||||
|
||||
class AllPosts(Index):
|
||||
|
||||
filename = 'all_posts'
|
||||
cur_post = 0
|
||||
|
||||
def createPosts(self, posts, dom, root, node):
|
||||
posts_elem = self.createElement(dom, 'posts')
|
||||
|
||||
|
@ -46,7 +40,7 @@ class AllPosts(Index):
|
|||
|
||||
def createMonth(self, posts, dom, root, node):
|
||||
date_format = node.getAttribute('format')
|
||||
if date_format == '':
|
||||
if not date_format:
|
||||
date_format = '%B'
|
||||
|
||||
self.cur_month = posts[0].creation_date.month
|
||||
|
@ -57,11 +51,9 @@ class AllPosts(Index):
|
|||
prev_month = None
|
||||
for p in posts:
|
||||
if p.creation_date.month == self.cur_month:
|
||||
cur_posts.append(p)
|
||||
cur_posts.insert(0, p)
|
||||
continue
|
||||
|
||||
cur_posts.reverse()
|
||||
|
||||
month_elem = self.createElement(dom, 'month')
|
||||
month_def = dom.createElement('month')
|
||||
month_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
|
||||
|
@ -81,7 +73,7 @@ class AllPosts(Index):
|
|||
cur_posts.append(p)
|
||||
|
||||
# Last month
|
||||
if len(cur_posts) != 0:
|
||||
if cur_posts:
|
||||
cur_posts.reverse()
|
||||
month_elem = self.createElement(dom, 'month')
|
||||
month_def = dom.createElement('month')
|
||||
|
@ -161,6 +153,8 @@ class AllPosts(Index):
|
|||
def generate(self, blog, src, output):
|
||||
from dynastie.models import Post, Blog, Category
|
||||
|
||||
self.filename = 'all_posts'
|
||||
|
||||
self.hooks['year'] = self.createYear
|
||||
self.hooks['month'] = self.createMonth
|
||||
self.hooks['posts'] = self.createPosts
|
||||
|
|
|
@ -17,23 +17,12 @@
|
|||
You should have received a copy of the GNU General Public License
|
||||
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
import os
|
||||
from datetime import datetime
|
||||
from xml.dom.minidom import parse, parseString
|
||||
from dynastie.generators.generator import DynastieGenerator
|
||||
from dynastie.generators.index import Index
|
||||
from django.db import models
|
||||
|
||||
class Archive(Index):
|
||||
|
||||
cur_page = 0
|
||||
nb_pages = 0
|
||||
cur_post = 0
|
||||
posts_per_page = 0
|
||||
filename = 'index'
|
||||
dirname = ''
|
||||
cur_year = 0
|
||||
|
||||
def createArchive(self, posts, dom, root, node):
|
||||
if node.hasAttribute('year'):
|
||||
self.replaceByText(dom, root, node, str(self.cur_year))
|
||||
|
@ -54,7 +43,7 @@ class Archive(Index):
|
|||
return self.report
|
||||
|
||||
first_post = posts[0]
|
||||
last_post = Post.objects.filter(published=True, front_page=True).order_by('-creation_date')[0]
|
||||
last_post = posts[len(posts)-1]
|
||||
|
||||
start_year = first_post.creation_date.year
|
||||
end_year = last_post.creation_date.year
|
||||
|
@ -65,11 +54,9 @@ class Archive(Index):
|
|||
|
||||
self.cur_year = i
|
||||
|
||||
posts = Post.objects.filter(published=True, front_page=True, creation_date__gt=datetime(i, 1, 1), creation_date__lt=datetime(i+1, 1, 1)).order_by('-creation_date')
|
||||
posts = Post.objects.filter(published=True, creation_date__gt=datetime(i, 1, 1), creation_date__lt=datetime(i+1, 1, 1)).order_by('-creation_date')
|
||||
|
||||
self.nb_pages = 0
|
||||
self.cur_page = 0
|
||||
self.cur_post = 0
|
||||
self.resetCounters()
|
||||
|
||||
self.dirname = '/archive/' + str(i)
|
||||
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
import os
|
||||
import datetime
|
||||
import xml
|
||||
from dynastie.generators.generator import DynastieGenerator
|
||||
from dynastie.generators.rss import RSS
|
||||
from xml.dom.minidom import getDOMImplementation
|
||||
from django.db import models
|
||||
|
|
|
@ -17,22 +17,11 @@
|
|||
You should have received a copy of the GNU General Public License
|
||||
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
import os
|
||||
from xml.dom.minidom import parse, parseString
|
||||
from dynastie.generators.generator import DynastieGenerator
|
||||
from dynastie.generators.index import Index
|
||||
from django.db import models
|
||||
|
||||
class Category(Index):
|
||||
|
||||
cur_page = 0
|
||||
nb_pages = 0
|
||||
cur_post = 0
|
||||
posts_per_page = 0
|
||||
filename = 'index'
|
||||
dirname = ''
|
||||
cur_category = None
|
||||
|
||||
def createCategory(self, posts, dom, root, node):
|
||||
if node.hasAttribute('name'):
|
||||
self.replaceByText(dom, root, node, self.cur_category.name)
|
||||
|
@ -43,6 +32,8 @@ class Category(Index):
|
|||
def generate(self, blog, src, output):
|
||||
from dynastie.models import Post, Blog, Category
|
||||
|
||||
self.cur_category = None
|
||||
|
||||
self.hooks['category'] = self.createCategory
|
||||
|
||||
dom = self.parseTemplate(blog, src, output, 'category', 'category')
|
||||
|
@ -54,9 +45,7 @@ class Category(Index):
|
|||
self.cur_category = category
|
||||
posts = Post.objects.filter(category__exact=category, published=True).order_by('-creation_date')
|
||||
|
||||
self.nb_pages = 0
|
||||
self.cur_page = 0
|
||||
self.cur_post = 0
|
||||
self.resetCounters()
|
||||
|
||||
self.dirname = '/category/' + category.name_slug
|
||||
|
||||
|
|
|
@ -56,8 +56,6 @@ class StrictUTF8Writer(codecs.StreamWriter):
|
|||
class DynastieGenerator:
|
||||
|
||||
URI = "http://indefero.soutade.fr/p/dynastie"
|
||||
report = ''
|
||||
somethingWrote = False
|
||||
|
||||
def __init__(self, hash_posts=None, hash_posts_content=None):
|
||||
self.report = ''
|
||||
|
@ -67,11 +65,11 @@ class DynastieGenerator:
|
|||
|
||||
def addReport(self, string, color=''):
|
||||
if string in self.report: return
|
||||
if color != '':
|
||||
if color:
|
||||
self.report = self.report + '<span style="color:' + color + '">'
|
||||
self.report = self.report + '<b>' + self.__class__.__name__ + '</b> : '
|
||||
self.report = self.report + string
|
||||
if color != '':
|
||||
if color:
|
||||
self.report = self.report + '</span>'
|
||||
self.report = self.report + '<br/>\n'
|
||||
|
||||
|
@ -105,8 +103,8 @@ class DynastieGenerator:
|
|||
else:
|
||||
cache_obj = cache_objs[0]
|
||||
|
||||
if not cache_obj is None or os.path.exists(filename):
|
||||
if cache_obj is None:
|
||||
if cache_obj or os.path.exists(filename):
|
||||
if not cache_obj:
|
||||
src_md5 = hashlib.md5()
|
||||
f = open(filename,'rb')
|
||||
src_md5.update(f.read())
|
||||
|
@ -125,7 +123,8 @@ class DynastieGenerator:
|
|||
f.write(content)
|
||||
f.close()
|
||||
return
|
||||
os.unlink(filename)
|
||||
if os.path.exists(filename):
|
||||
os.unlink(filename)
|
||||
|
||||
if cache_obj is None:
|
||||
cache_obj = FileOutputCache(name=filename, hash=dst_md5.hexdigest())
|
||||
|
@ -161,20 +160,20 @@ class DynastieGenerator:
|
|||
|
||||
def createElement(self, dom, name='', content='', subtree=None):
|
||||
div = dom.createElement('div')
|
||||
if name != '':
|
||||
if name:
|
||||
div.setAttribute('class', name)
|
||||
if content != '':
|
||||
if content:
|
||||
div.appendChild(dom.createTextNode(content))
|
||||
if not subtree is None:
|
||||
if subtree:
|
||||
self.cloneSubtree(div, subtree)
|
||||
|
||||
return div
|
||||
|
||||
def createMeta(self, dom, name='', content=''):
|
||||
div = dom.createElement('meta')
|
||||
if name != '':
|
||||
if name:
|
||||
div.setAttribute('name', name)
|
||||
if content != '':
|
||||
if content:
|
||||
div.setAttribute('content', content)
|
||||
|
||||
return div
|
||||
|
@ -185,7 +184,7 @@ class DynastieGenerator:
|
|||
if node.prefix == 'dyn':
|
||||
if node.localName in values:
|
||||
content = values[node.localName]
|
||||
if type(content) == unicode or type(content) == str:
|
||||
if isinstance(content, basestring):
|
||||
new_elem = self.createElement(dom, node.localName, content)
|
||||
else:
|
||||
new_elem = self.createElement(dom, node.localName)
|
||||
|
@ -204,16 +203,15 @@ class DynastieGenerator:
|
|||
|
||||
def _parse(self, hooks, posts, dom, root):
|
||||
for node in root.childNodes:
|
||||
if node.prefix == 'dyn':
|
||||
if node.localName in hooks:
|
||||
node = hooks[node.localName](posts, dom, root, node)
|
||||
if not node is None and node.hasChildNodes():
|
||||
if node.prefix == 'dyn' and node.localName in hooks:
|
||||
node = hooks[node.localName](posts, dom, root, node)
|
||||
if node and node.hasChildNodes():
|
||||
self._parse(hooks, posts, dom, node)
|
||||
|
||||
def parse(self, src, hooks, posts, dom, root):
|
||||
bases = dom.getElementsByTagNameNS(self.URI, 'base')
|
||||
|
||||
if len(bases) == 0:
|
||||
if not bases:
|
||||
self._parse(hooks, posts, dom, root)
|
||||
return root
|
||||
|
||||
|
@ -233,7 +231,7 @@ class DynastieGenerator:
|
|||
return root
|
||||
|
||||
target_blocks = base.getElementsByTagNameNS(self.URI, 'block')
|
||||
if len(target_blocks) == 0:
|
||||
if not target_blocks:
|
||||
self.addError('No \'block\' defined in ' + src + '/' + filename)
|
||||
return root
|
||||
|
||||
|
|
|
@ -19,24 +19,17 @@
|
|||
"""
|
||||
import os
|
||||
import datetime
|
||||
import hashlib
|
||||
import xml
|
||||
from xml.parsers.expat import *
|
||||
import xml.parsers.expat
|
||||
from xml.dom.minidom import parse, parseString
|
||||
from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer
|
||||
from django.db import models
|
||||
from dynastie.generators import markdown2
|
||||
|
||||
class Index(DynastieGenerator):
|
||||
|
||||
cur_page = 0
|
||||
nb_pages = 0
|
||||
cur_post = 0
|
||||
cur_post_obj = None
|
||||
posts_per_page = 0
|
||||
filename = 'index'
|
||||
dirname = ''
|
||||
blog = None
|
||||
|
||||
def __init__(self, hash_posts=None, hash_posts_content=None):
|
||||
DynastieGenerator.__init__(self, hash_posts, hash_posts_content)
|
||||
|
||||
|
@ -55,6 +48,19 @@ class Index(DynastieGenerator):
|
|||
|
||||
self.first_try = True
|
||||
|
||||
self.posts_per_page = 5
|
||||
self.filename = 'index'
|
||||
self.dirname = ''
|
||||
self.blog = None
|
||||
|
||||
self.resetCounters()
|
||||
|
||||
def resetCounters(self):
|
||||
self.nb_pages = 0
|
||||
self.cur_page = 0
|
||||
self.cur_post = 0
|
||||
self.cur_post_obj = None
|
||||
|
||||
def createReplace(self, posts, dom, root, replace_elem):
|
||||
if not replace_elem.hasAttribute('div_name'):
|
||||
self.addError('No attribute div_name for a replace tag')
|
||||
|
@ -68,7 +74,7 @@ class Index(DynastieGenerator):
|
|||
|
||||
value = value.replace('dyn:blog_id', str(self.blog.id))
|
||||
|
||||
if not self.cur_post_obj is None:
|
||||
if self.cur_post_obj:
|
||||
url = self.cur_post_obj.getPath()
|
||||
full_url = self.cur_post_obj.blog.name + url
|
||||
value = value.replace('dyn:post_url', url)
|
||||
|
@ -96,10 +102,10 @@ class Index(DynastieGenerator):
|
|||
self.replaceByText(dom, root, node, category)
|
||||
|
||||
def createNavigation(self, posts, dom, root, node):
|
||||
if self.nb_pages == 0 or self.nb_pages == 1:
|
||||
if 0 <= self.nb_pages <= 1:
|
||||
return None
|
||||
|
||||
if self.dirname != '':
|
||||
if self.dirname:
|
||||
if self.dirname.startswith('/'):
|
||||
href = '<a href="' + self.dirname + '/' + self.filename
|
||||
else:
|
||||
|
@ -189,13 +195,13 @@ class Index(DynastieGenerator):
|
|||
end = code.find('</dyn:code>')
|
||||
|
||||
if end < start:
|
||||
self.addError('Invalid <dyn:code> tags in ' + filename)
|
||||
self.addError('Invalid <dyn:code> tags in ' + self.filename)
|
||||
break
|
||||
|
||||
try:
|
||||
dom = parseString(code[start:end+11])
|
||||
except xml.dom.DOMException as e:
|
||||
self.addError('Error parsing ' + filename)
|
||||
self.addError('Error parsing ' + self.filename)
|
||||
break
|
||||
|
||||
res = self.createCode(dom, dom.firstChild)
|
||||
|
@ -211,12 +217,11 @@ class Index(DynastieGenerator):
|
|||
node = self.hash_posts[post.id]
|
||||
return node.cloneNode(0)
|
||||
|
||||
values = {}
|
||||
values = {'post_content': '', 'author': 'Unknown'}
|
||||
try:
|
||||
values['author'] = post.author.first_name + ' ' + post.author.last_name
|
||||
except:
|
||||
values['author'] = 'Unknown'
|
||||
values['post_content'] = ''
|
||||
pass
|
||||
|
||||
blog = post.blog
|
||||
blog.create_paths()
|
||||
|
@ -232,7 +237,6 @@ class Index(DynastieGenerator):
|
|||
post_content = f.read()
|
||||
f.close()
|
||||
if post.content_format == Post.CONTENT_TEXT:
|
||||
from dynastie.generators import markdown2
|
||||
post_content = markdown2.markdown(post_content)
|
||||
self.hash_posts_content[filename] = post_content
|
||||
else:
|
||||
|
@ -252,8 +256,6 @@ class Index(DynastieGenerator):
|
|||
content_node.appendChild(new_node)
|
||||
|
||||
if post.id in self.hash_posts:
|
||||
import hashlib
|
||||
|
||||
# Here, we are in first_try, check that computed
|
||||
# post has the same result than the one in cache
|
||||
self.first_try = False
|
||||
|
@ -276,8 +278,9 @@ class Index(DynastieGenerator):
|
|||
# If not, clear cache
|
||||
if md5_1.digest() != md5_2.digest():
|
||||
self.hash_posts = {}
|
||||
|
||||
self.hash_posts[post.id] = post_elem.cloneNode(0)
|
||||
self.hash_posts[post.id] = post_elem.cloneNode(0)
|
||||
else:
|
||||
self.hash_posts[post.id] = post_elem.cloneNode(0)
|
||||
|
||||
return post_elem
|
||||
|
||||
|
@ -293,11 +296,11 @@ class Index(DynastieGenerator):
|
|||
else:
|
||||
post_elem = self.createElement(dom, '', '<b>No posts yet</b>')
|
||||
self.cur_post_obj = None
|
||||
if not post_elem is None:
|
||||
if post_elem:
|
||||
posts_elem.appendChild(post_elem)
|
||||
|
||||
# Parse inner HTML
|
||||
self._parse(self.hooks, posts, dom, post_elem)
|
||||
# Parse inner HTML
|
||||
self._parse(self.hooks, posts, dom, post_elem)
|
||||
|
||||
self.cur_post = self.cur_post + 1
|
||||
if self.cur_post == len(posts):
|
||||
|
@ -347,7 +350,7 @@ class Index(DynastieGenerator):
|
|||
else:
|
||||
cur_post = None
|
||||
|
||||
if not cur_post is None:
|
||||
if cur_post:
|
||||
for tag in cur_post.tags.all():
|
||||
if create_link:
|
||||
tag_elem = self.createElement(dom, 'tag')
|
||||
|
@ -442,18 +445,16 @@ class Index(DynastieGenerator):
|
|||
self.addError('Error parsing _%s.html : ' + e)
|
||||
return None
|
||||
|
||||
if not directory is None and not os.path.exists(output + '/' + directory):
|
||||
if directory and not os.path.exists(output + '/' + directory):
|
||||
os.mkdir(output + '/' + directory)
|
||||
|
||||
if not parsePostsTag: return dom
|
||||
|
||||
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
||||
|
||||
if not post_nodes is None and len(post_nodes) > 0:
|
||||
if post_nodes:
|
||||
if post_nodes[0].hasAttribute("limit"):
|
||||
self.posts_per_page = int(post_nodes[0].getAttribute("limit"))
|
||||
else:
|
||||
self.posts_per_page = 5
|
||||
else:
|
||||
self.addWarning('No tag dyn:posts found')
|
||||
|
||||
|
|
|
@ -18,22 +18,13 @@
|
|||
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
import os
|
||||
from xml.dom.minidom import parse, parseString
|
||||
from xml.dom.minidom import parse
|
||||
import xml.parsers.expat
|
||||
from dynastie.generators.generator import DynastieGenerator
|
||||
from dynastie.generators.index import Index
|
||||
from django.db import models
|
||||
|
||||
class LJDC(Index):
|
||||
|
||||
cur_page = 0
|
||||
nb_pages = 0
|
||||
cur_post = 0
|
||||
posts_per_page = 20
|
||||
filename = 'index'
|
||||
dirname = '/ljdc'
|
||||
cur_category = None
|
||||
|
||||
def createPost(self, posts, dom, post_elem, root):
|
||||
new_elem = self.createElement(dom, 'ljdc')
|
||||
|
||||
|
@ -73,6 +64,9 @@ class LJDC(Index):
|
|||
return srcdom
|
||||
|
||||
def generate(self, blog, src, output):
|
||||
self.posts_per_page = 20
|
||||
self.dirname = '/ljdc'
|
||||
|
||||
srcdom = self._load_references(src)
|
||||
|
||||
if srcdom is None: return None
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
"""
|
||||
import datetime
|
||||
import os
|
||||
import xml
|
||||
from xml.dom.minidom import parse, parseString
|
||||
from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer
|
||||
from dynastie.generators.index import Index
|
||||
|
@ -27,9 +28,6 @@ from dynastie.tree import TreeNode
|
|||
|
||||
class Post(Index):
|
||||
|
||||
cur_comment = None
|
||||
comment_index = {}
|
||||
|
||||
def createReplace(self, post, dom, root, replace_elem):
|
||||
if not replace_elem.hasAttribute('div_name'):
|
||||
self.addError('No attribute div_name for a replace tag')
|
||||
|
@ -43,13 +41,10 @@ class Post(Index):
|
|||
value = value.replace('dyn:post_id', str(post.id))
|
||||
if self.cur_comment is None:
|
||||
value = value.replace('dyn:comment_index', '0')
|
||||
else:
|
||||
value = value.replace('dyn:comment_index', str(self.comment_index[self.cur_comment.id]))
|
||||
if self.cur_comment is None:
|
||||
value = value.replace('dyn:comment_id', '0')
|
||||
else:
|
||||
value = value.replace('dyn:comment_index', str(self.comment_index[self.cur_comment.id]))
|
||||
value = value.replace('dyn:comment_id', str(self.cur_comment.id))
|
||||
|
||||
value = value.replace('dyn:blog_id', str(self.blog.id))
|
||||
url = post.getPath()
|
||||
full_url = post.blog.name + url
|
||||
|
@ -86,8 +81,8 @@ class Post(Index):
|
|||
|
||||
comments = Comment.objects.filter(post=post).order_by('date')
|
||||
|
||||
cur_comment = None
|
||||
comment_index = {}
|
||||
self.cur_comment = None
|
||||
self.comment_index = {}
|
||||
index = 1
|
||||
|
||||
rootNode = TreeNode('', '')
|
||||
|
@ -146,9 +141,12 @@ class Post(Index):
|
|||
self.addError('name attribute \'' + name + '\' unknown for dyn:meta' )
|
||||
return None
|
||||
|
||||
def _createPost(self, post, dom, post_elem, root):
|
||||
import sys, traceback
|
||||
def createPostTitle(self, post, dom, root, node):
|
||||
value = post.title.replace('"', '“')
|
||||
self.replaceByText(dom, root, node, value)
|
||||
return None
|
||||
|
||||
def _createPost(self, post, dom, post_elem, root):
|
||||
self.cur_post_obj = post
|
||||
posts = [post]
|
||||
self.createPost(posts, dom, post_elem, root)
|
||||
|
@ -169,12 +167,16 @@ class Post(Index):
|
|||
return node
|
||||
|
||||
def _generate(self, blog, src, output, posts):
|
||||
import xml
|
||||
from dynastie.search import Search
|
||||
|
||||
self.cur_comment = None
|
||||
self.comment_index = {}
|
||||
|
||||
self.hooks['post'] = self._createPost
|
||||
self.hooks['meta'] = self.createMetas
|
||||
self.hooks['comments'] = self.createComments
|
||||
self.hooks['replace'] = self.createReplace
|
||||
self.hooks['post_title'] = self.createPostTitle
|
||||
del self.hooks['navigation']
|
||||
del self.hooks['recents']
|
||||
del self.hooks['posts']
|
||||
|
@ -183,6 +185,7 @@ class Post(Index):
|
|||
if dom is None: return self.report
|
||||
|
||||
impl = xml.dom.getDOMImplementation()
|
||||
s = Search()
|
||||
for post in posts:
|
||||
filename = output + '/post/'
|
||||
filename = filename + post.creation_date.strftime("%Y") + '/' + post.creation_date.strftime("%m") + '/'
|
||||
|
@ -192,11 +195,9 @@ class Post(Index):
|
|||
|
||||
if not post.published:
|
||||
if os.path.exists(filename):
|
||||
from dynastie.search import *
|
||||
os.unlink(filename)
|
||||
os.unlink(filename + '.gz')
|
||||
self.addReport('Remove ' + filename)
|
||||
s = Search()
|
||||
s.delete_post(blog, post.id)
|
||||
continue
|
||||
#print 'Generate ' + filename
|
||||
|
|
|
@ -30,7 +30,7 @@ class RSS(DynastieGenerator):
|
|||
|
||||
def appendElement(self, dom, root, name='', content='', attributes=None):
|
||||
elem = dom.createElement(name)
|
||||
if not attributes is None:
|
||||
if attributes:
|
||||
for k, v in attributes.iteritems():
|
||||
elem.setAttribute(k, v)
|
||||
if content != '':
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
import os
|
||||
from datetime import datetime
|
||||
from xml.dom.minidom import parse, parseString
|
||||
from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer
|
||||
from dynastie.generators.index import Index
|
||||
|
@ -47,7 +46,7 @@ class Search(Index):
|
|||
return self.report
|
||||
|
||||
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
||||
if not post_nodes is None:
|
||||
if post_nodes:
|
||||
if post_nodes[0].hasAttribute("limit"):
|
||||
self.posts_per_page = int(post_nodes[0].getAttribute("limit"))
|
||||
else:
|
||||
|
@ -64,7 +63,7 @@ class Search(Index):
|
|||
post = Post.objects.get(pk=post_id)
|
||||
except:
|
||||
continue
|
||||
if not post is None:
|
||||
if post:
|
||||
posts.append(post)
|
||||
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
|
|
|
@ -17,22 +17,11 @@
|
|||
You should have received a copy of the GNU General Public License
|
||||
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
import os
|
||||
from xml.dom.minidom import parse, parseString
|
||||
from dynastie.generators.generator import DynastieGenerator
|
||||
from dynastie.generators.index import Index
|
||||
from django.db import models
|
||||
|
||||
class Tag(Index):
|
||||
|
||||
cur_page = 0
|
||||
nb_pages = 0
|
||||
cur_post = 0
|
||||
posts_per_page = 0
|
||||
filename = 'index'
|
||||
dirname = ''
|
||||
cur_tag = None
|
||||
|
||||
def createTag(self, posts, dom, root, node):
|
||||
if node.hasAttribute('name'):
|
||||
self.replaceByText(dom, root, node, self.cur_tag.name)
|
||||
|
@ -43,6 +32,7 @@ class Tag(Index):
|
|||
def generate(self, blog, src, output):
|
||||
from dynastie.models import Post, Blog, Tag
|
||||
|
||||
self.cur_tag = None
|
||||
self.hooks['tag'] = self.createTag
|
||||
|
||||
dom = self.parseTemplate(blog, src, output, 'tag', 'tag')
|
||||
|
@ -54,9 +44,7 @@ class Tag(Index):
|
|||
self.cur_tag = tag
|
||||
posts = Post.objects.filter(tags__in=[tag.id], published=True).order_by('-creation_date')
|
||||
|
||||
self.nb_pages = 0
|
||||
self.cur_page = 0
|
||||
self.cur_post = 0
|
||||
self.resetCounters()
|
||||
|
||||
self.dirname = '/tag/' + tag.name_slug
|
||||
|
||||
|
|
|
@ -84,7 +84,8 @@ class Blog(models.Model):
|
|||
engine = line.strip()
|
||||
if not engine in globals():
|
||||
print 'Engine ' + engine + ' doesn\'t exists'
|
||||
self.engines.append(globals()[engine])
|
||||
else:
|
||||
self.engines.append(globals()[engine])
|
||||
f.close()
|
||||
else:
|
||||
self.engines.append(globals()['post'])
|
||||
|
|
|
@ -205,15 +205,11 @@ class Search:
|
|||
for key in hashtable.keys():
|
||||
if reg.match(key):
|
||||
for post in hashtable[key]:
|
||||
if not post[0] in res:
|
||||
res[post[0]] = post[1]
|
||||
else:
|
||||
res[post[0]] += post[1]
|
||||
res[post[0]] = res.get(post[0],0) + post[1]
|
||||
|
||||
sorted_res = sorted(res.iteritems(), key=operator.itemgetter(1))
|
||||
sorted_res.reverse()
|
||||
|
||||
res = []
|
||||
for i in range(len(sorted_res)):
|
||||
res .append(sorted_res[i][0])
|
||||
res = [sorted_res[i][0] for i in range(len(sorted_res))]
|
||||
|
||||
return res
|
||||
|
|
|
@ -53,7 +53,7 @@ def have_I_right(request, blog_id=None, post_id=None, must_be_superuser=False, c
|
|||
if must_be_superuser and not request.user.is_superuser:
|
||||
raise Http404
|
||||
|
||||
if not post_id is None:
|
||||
if post_id:
|
||||
p = cls.objects.get(pk=post_id)
|
||||
|
||||
if p is None:
|
||||
|
@ -61,10 +61,10 @@ def have_I_right(request, blog_id=None, post_id=None, must_be_superuser=False, c
|
|||
|
||||
blog_id = p.blog.id
|
||||
|
||||
if not blog_id is None:
|
||||
if blog_id:
|
||||
if not request.user.is_superuser:
|
||||
b = Blog.objects.filter(pk=blog_id, writers=request.user.id)
|
||||
if len(b) == 0:
|
||||
if not b:
|
||||
raise Http404
|
||||
b = b[0]
|
||||
else:
|
||||
|
@ -128,14 +128,11 @@ def index(request):
|
|||
|
||||
def disconnect(request):
|
||||
logout(request)
|
||||
c = {'login_failed' : False}
|
||||
return HttpResponseRedirect('/')
|
||||
|
||||
@login_required
|
||||
def user(request):
|
||||
users = User.objects.all()
|
||||
|
||||
c = {'users' : users}
|
||||
c = {'users' : User.objects.all()}
|
||||
|
||||
return render(request, 'user.html', c)
|
||||
|
||||
|
@ -286,6 +283,7 @@ def edit_tag(request, tag_id):
|
|||
return HttpResponseRedirect('/tag/' + str(b.id))
|
||||
if 'edit' in request.POST:
|
||||
name = tag.name
|
||||
name = name.strip()
|
||||
form = TagForm(request.POST, instance=tag)
|
||||
if form.is_valid():
|
||||
if request.POST['name'] != name:
|
||||
|
@ -351,10 +349,7 @@ def view_blog(request, blog_id):
|
|||
if 'page' in request.GET:
|
||||
cur_page = int(request.GET['page'])
|
||||
else:
|
||||
if 'cur_page' in request.session:
|
||||
cur_page = request.session['cur_page']
|
||||
else:
|
||||
cur_page = 0
|
||||
cur_page = request.session.get('cur_page',0)
|
||||
|
||||
if cur_page <= 0:
|
||||
drafts = Draft.objects.filter(blog=b).order_by('-creation_date')
|
||||
|
@ -374,15 +369,13 @@ def view_blog(request, blog_id):
|
|||
end = start + 50 + drafts.count()
|
||||
|
||||
orig_posts = orig_posts.order_by('-creation_date')[start:end]
|
||||
posts = []
|
||||
for p in orig_posts:
|
||||
found = False
|
||||
for d in drafts:
|
||||
if d.id == p.id:
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
posts.append(p)
|
||||
|
||||
# Select post without drafts
|
||||
if drafts:
|
||||
drafts_id = [draft.id for draft in drafts]
|
||||
posts = [p for p in orig_posts if not p.id in drafts_id]
|
||||
else:
|
||||
posts = orig_posts
|
||||
|
||||
form = BlogForm(instance=b)
|
||||
|
||||
|
@ -390,10 +383,7 @@ def view_blog(request, blog_id):
|
|||
dict_comments = {}
|
||||
for comment in comments:
|
||||
key = comment.post.id
|
||||
if not key in dict_comments:
|
||||
dict_comments[key] = 1
|
||||
else:
|
||||
dict_comments[key] = dict_comments[key] + 1
|
||||
dict_comments[key] = dict_comments.get(key, 0) + 1
|
||||
|
||||
navigation_bar = createNavigationBar(b.id, cur_page, nb_pages)
|
||||
|
||||
|
@ -461,10 +451,7 @@ def search_blog(request, blog_id):
|
|||
dict_comments = {}
|
||||
for comment in comments:
|
||||
key = comment.post.id
|
||||
if not key in dict_comments:
|
||||
dict_comments[key] = 1
|
||||
else:
|
||||
dict_comments[key] = dict_comments[key] + 1
|
||||
dict_comments[key] = dict_comments.get(key, 0) + 1
|
||||
|
||||
c = {'blog' : b, 'posts' : posts, 'comments' : dict_comments}
|
||||
|
||||
|
@ -552,9 +539,7 @@ def edit_post(request, post_id):
|
|||
content = 'Empty post'
|
||||
|
||||
comments = Comment.objects.filter(post=post).order_by('date')
|
||||
comment_list = []
|
||||
for comment in comments:
|
||||
comment_list.append(comment)
|
||||
comment_list = [comment for comment in comments]
|
||||
|
||||
return render(request, 'edit_post.html', {
|
||||
'form': form, 'post_id' : post_id, 'content' : content,
|
||||
|
@ -652,10 +637,7 @@ def _generate(request, blog_id, report):
|
|||
dict_comments = {}
|
||||
for comment in comments:
|
||||
key = comment.post.id
|
||||
if not key in dict_comments:
|
||||
dict_comments[key] = 1
|
||||
else:
|
||||
dict_comments[key] = dict_comments[key] + 1
|
||||
dict_comments[key] = dict_comments.get(key, 0) + 1
|
||||
|
||||
navigation_bar = createNavigationBar(b.id, 0, nb_pages)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user