Rework on categories : add name in url

This commit is contained in:
Grégory Soutadé 2012-08-04 21:21:04 +02:00
parent 3f8390be16
commit 99c5a9e6e9
3 changed files with 14 additions and 28 deletions

View File

@ -10,7 +10,7 @@ class Category(Index):
nb_pages = 0 nb_pages = 0
cur_article = 0 cur_article = 0
articles_per_page = 0 articles_per_page = 0
filename = 'category' filename = 'index'
cur_category = None cur_category = None
def createCategory(self, articles, dom, root, node): def createCategory(self, articles, dom, root, node):
@ -52,20 +52,24 @@ class Category(Index):
for category in categories: for category in categories:
self.cur_category = category self.cur_category = category
self.filename = category.name_slug
filename = self.filename + '.html' filename = self.filename + '.html'
articles = Article.objects.filter(category__exact=category).order_by('-creation_date') articles = Article.objects.filter(category__exact=category).order_by('-creation_date')
self.nb_pages = 0 self.nb_pages = 0
self.cur_page = 0 self.cur_page = 0
self.cur_article = 0
if articles.count() > self.articles_per_page: if articles.count() > self.articles_per_page:
self.nb_pages = articles.count() / self.articles_per_page self.nb_pages = articles.count() / self.articles_per_page
if not os.path.exists(output + '/category/' + category.name_slug):
os.mkdir(output + '/category/' + category.name_slug)
while self.cur_page <= self.nb_pages: while self.cur_page <= self.nb_pages:
#print 'Generate ' + filename #print 'Generate ' + filename
nodes = dom.getElementsByTagName("*") nodes = dom.getElementsByTagName("*")
self.parse(hooks, articles, dom, nodes[0]) self.parse(hooks, articles, dom, nodes[0])
self.writeIfNotTheSame(output + '/category/' + filename, nodes[0].toxml('utf8')) self.writeIfNotTheSame(output + '/category/' + category.name_slug + '/' + filename, nodes[0].toxml('utf8'))
self.cur_page = self.cur_page + 1 self.cur_page = self.cur_page + 1
filename = self.filename + str(self.cur_page) + '.html' filename = self.filename + str(self.cur_page) + '.html'
dom = parse(src + '/_category.html') dom = parse(src + '/_category.html')
@ -78,7 +82,7 @@ class Category(Index):
self.addReport('Removing unused ' + filename) self.addReport('Removing unused ' + filename)
os.unlink(filename) os.unlink(filename)
self.cur_page = self.cur_page + 1 self.cur_page = self.cur_page + 1
filename = output + '/category/' + self.filename + str(self.cur_page) + '.html' filename = output + '/category/' + category.name_slug + '/' + self.filename + str(self.cur_page) + '.html'
if not self.somethingWrote: if not self.somethingWrote:
self.addReport('Nothing changed') self.addReport('Nothing changed')

View File

@ -148,29 +148,8 @@ class Category(models.Model):
def remove(self, blog): def remove(self, blog):
blog.create_paths() blog.create_paths()
output = blob.output_path output = blog.output_path + '/category/' + self.name_slug
cur_cat = 0 shutil.rmtree(output)
while True:
found = False
filename = output + '/category/'
if cur_cat == 0:
filename = filename + self.name_slug + '.html'
else:
filename = filename + self.name_slug + str(cur_cat) + '.html'
if os.path.exists(filename):
os.unlink(filename)
found = True
filename = filename + '.gz'
if os.path.exists(filename):
os.unlink(filename)
found = True
if not found:
break
cur_cat = cur_cat + 1
filename = output + '/category/'
if len(os.listdir(filename)) == 0:
os.rmdir(filename)
class Tag(models.Model): class Tag(models.Model):

View File

@ -139,8 +139,11 @@ def edit_category(request, category_id):
if 'cancel' in request.POST: if 'cancel' in request.POST:
return HttpResponseRedirect('/category') return HttpResponseRedirect('/category')
if 'edit' in request.POST and request.user.is_superuser: if 'edit' in request.POST and request.user.is_superuser:
name = category.name
form = CategoryForm(request.POST, instance=category) # A form bound to the POST data form = CategoryForm(request.POST, instance=category) # A form bound to the POST data
if form.is_valid(): # All validation rules pass if form.is_valid(): # All validation rules pass
if category.name != name:
category.remove()
form.save() form.save()
else: else:
form = CategoryForm(instance=category) # An unbound form form = CategoryForm(instance=category) # An unbound form
@ -295,7 +298,7 @@ def edit_article(request, article_id):
if 'edit' in request.POST: if 'edit' in request.POST:
form = ArticleForm(request.POST, instance=article) # A form bound to the POST data form = ArticleForm(request.POST, instance=article) # A form bound to the POST data
if form.is_valid(): # All validation rules pass if form.is_valid(): # All validation rules pass
if title != article.title: if title != form.title:
article.remove() article.remove()
form.save() form.save()
# Process the data in form.cleaned_data # Process the data in form.cleaned_data