Set category and tag refers to a Blog

This commit is contained in:
Grégory Soutadé 2012-10-09 20:47:12 +02:00
parent d30bb18e43
commit 76cda68611
10 changed files with 64 additions and 41 deletions

View File

@ -17,7 +17,7 @@ class PostForm(ModelForm):
class CategoryForm(ModelForm): class CategoryForm(ModelForm):
class Meta: class Meta:
model = Category model = Category
exclude = ('parent', 'name_slug') exclude = ('parent', 'name_slug', 'blog')
class UserForm(ModelForm): class UserForm(ModelForm):
class Meta: class Meta:

View File

@ -42,7 +42,7 @@ class Post(Index):
node.removeChild(node.childNodes[0]) node.removeChild(node.childNodes[0])
node.appendChild(dom.createTextNode(post.title)) node.appendChild(dom.createTextNode(post.title))
def generate(self, blog, src, output, posts): def _generate(self, blog, src, output, posts):
hooks = {'post' : self._createPost, hooks = {'post' : self._createPost,
'meta' : self.createMetas} 'meta' : self.createMetas}
@ -81,7 +81,7 @@ class Post(Index):
posts = Post.objects.all() posts = Post.objects.all()
return self.generate(blog, src, output, hooks, dom, posts) return self._generate(blog, src, output, posts)
def createPreview(self, values, dom, root, node): def createPreview(self, values, dom, root, node):
now = datetime.datetime.now() now = datetime.datetime.now()

View File

@ -159,6 +159,7 @@ class Category(models.Model):
name_slug = models.CharField(max_length=255) name_slug = models.CharField(max_length=255)
parent = models.ForeignKey('self', blank=True, null=True) parent = models.ForeignKey('self', blank=True, null=True)
description = models.TextField(max_length=255, blank=True) description = models.TextField(max_length=255, blank=True)
blog = models.ForeignKey(Blog)
def slugify(self): def slugify(self):
name = normalize('NFKD', self.name).encode('ascii', 'ignore').replace(' ', '-').lower() name = normalize('NFKD', self.name).encode('ascii', 'ignore').replace(' ', '-').lower()
@ -181,6 +182,7 @@ class Category(models.Model):
class Tag(models.Model): class Tag(models.Model):
name = models.CharField(max_length=255, unique=True) name = models.CharField(max_length=255, unique=True)
blog = models.ForeignKey(Blog)
class Post(models.Model): class Post(models.Model):
title = models.CharField(max_length=255) title = models.CharField(max_length=255)

View File

@ -4,7 +4,7 @@
{% block head %} {% endblock %} {% block head %} {% endblock %}
</head> </head>
<body> <body>
<a href="/user">Users</a> <a href="/blog">Blogs</a> <a href="/category">Categories</a> <a href="/disconnect">Disconnect</a><br/><br/> <a href="/user">Users</a> <a href="/blog">Blogs</a> <a href="/disconnect">Disconnect</a><br/><br/>
{% block content %} {% endblock %} {% block content %} {% endblock %}
</body> </body>
</html> </html>

View File

@ -7,12 +7,10 @@
{% else %} {% else %}
<table> <table>
{% for category in categories %} {% for category in categories %}
<tr><td>{{ category.id }}</td><td>{{ category.name }}</td><td><a href="/category/edit/{{ category.id }}">Edit</a></td>{% if user.is_superuser %}<td><a href="/category/delete/{{ category.id }}">Delete</a></td>{% endif %}</tr> <tr><td>{{ category.id }}</td><td>{{ category.name }}</td><td><a href="/category/edit/{{ category.id }}">Edit</a></td><td><a href="/category/delete/{{ category.id }}">Delete</a></td></tr>
{% endfor %} {% endfor %}
</table> </table>
{% endif %} {% endif %}
{% if user.is_superuser %} <li><a href="/category/add">Add a category</a></li>
<li><a href="/category/add">Add a category</a></li>
{% endif %}
</table> </table>
{% endblock %} {% endblock %}

View File

@ -4,10 +4,6 @@
<form action="/category/edit/{{ category.id }}" method="post"> <form action="/category/edit/{{ category.id }}" method="post">
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{ form.as_p }}
{% if user.is_superuser %} <input type="submit" name="edit" value="Edit" /> <input type="submit" name="cancel" value="Cancel" />
<input type="submit" name="edit" value="Edit" />
{% else %}
<input type="submit" name="cancel" value="Cancel" />
{% endif %}
</form> </form>
{% endblock %} {% endblock %}

View File

@ -1,6 +1,7 @@
{% extends "templates/base.html" %} {% extends "templates/base.html" %}
{% block content %} {% block content %}
<a href="/category/{{ blog.id }}">Categories</a>
{% if user.is_superuser %} {% if user.is_superuser %}
<form action="/blog/edit/{{ blog.id }}" method="post"> <form action="/blog/edit/{{ blog.id }}" method="post">
{% csrf_token %} {% csrf_token %}
@ -24,7 +25,9 @@
{% else %} {% else %}
<table> <table>
{% for post in posts %} {% for post in posts %}
{% with post.id as cur_id %}
<tr><td><a href="/post/edit/{{ post.id }}">{{ post.id }}</a></td><td>{{ post.title }}</td><td>{{ post.category.name }}</td><td>{{ post.creation_date }}</td><td>{{ post.modification_date }}</td><td>{{ post.published }}</td><td>{{ post.front_page }}</td><td>{{ comments|hash:cur_id|default_if_none:"0" }} comment{{ comments|hash:cur_id|pluralize }}</td><td><a href="/post/delete/{{ post.id }}">Delete</a></td></tr> <tr><td><a href="/post/edit/{{ post.id }}">{{ post.id }}</a></td><td>{{ post.title }}</td><td>{{ post.category.name }}</td><td>{{ post.creation_date }}</td><td>{{ post.modification_date }}</td><td>{{ post.published }}</td><td>{{ post.front_page }}</td><td>{{ comments|hash:cur_id|default_if_none:"0" }} comment{{ comments|hash:cur_id|pluralize }}</td><td><a href="/post/delete/{{ post.id }}">Delete</a></td></tr>
{% endwith %}
{% endfor %} {% endfor %}
{% endif %} {% endif %}
</table> </table>

View File

@ -1,6 +1,7 @@
{% extends "templates/base.html" %} {% extends "templates/base.html" %}
{% block content %} {% block content %}
<a href="/category/{{ blog.id }}">Categories</a>
{% if user.is_superuser %} {% if user.is_superuser %}
<form action="/blog/edit/{{ blog.id }}" method="post"> <form action="/blog/edit/{{ blog.id }}" method="post">
{% csrf_token %} {% csrf_token %}

14
urls.py
View File

@ -13,21 +13,21 @@ urlpatterns = patterns('',
url(r'^user/add$', 'dynastie.views.add_user', name='add_user'), url(r'^user/add$', 'dynastie.views.add_user', name='add_user'),
url(r'^user/edit/(\d+)$', 'dynastie.views.edit_user', name='edit_user'), url(r'^user/edit/(\d+)$', 'dynastie.views.edit_user', name='edit_user'),
url(r'^user/(\d+)$', 'dynastie.views.edit_user', name='view_user'), url(r'^user/(\d+)$', 'dynastie.views.edit_user', name='view_user'),
url(r'^category$', 'dynastie.views.category', name='category'), url(r'^category/(\d+)$', 'dynastie.views.category', name='category'),
url(r'^category/add$', 'dynastie.views.add_category', name='add_category'), url(r'^category/add/(\d+)$', 'dynastie.views.add_category', name='add_category'),
url(r'^category/edit/(\d+)$', 'dynastie.views.edit_category', name='edit_category'), url(r'^category/edit/(\d+)$', 'dynastie.views.edit_category', name='edit_category'),
url(r'^category/delete/(\d+)$', 'dynastie.views.delete_category', name='delete_category'), url(r'^category/delete/(\d+)$', 'dynastie.views.delete_category', name='delete_category'),
url(r'^blog$', 'dynastie.views.blog', name='blog'), url(r'^blog$', 'dynastie.views.blog', name='blog'),
url(r'^blog/add$', 'dynastie.views.add_blog', name='add_blog'), url(r'^blog/add$', 'dynastie.views.add_blog', name='add_blog'),
url(r'^blog/(\d+)$', 'dynastie.views.view_blog', name='view_blog'), url(r'^blog/(\d+)$', 'dynastie.views.view_blog', name='view_blog'),
url(r'^blog/edit/(\d+)$', 'dynastie.views.edit_blog', name='edit_blog'), url(r'^blog/edit/(\d+)$', 'dynastie.views.edit_blog', name='edit_blog'),
url(r'^post/add/(\d+)$', 'dynastie.views.add_post', name='add_post'), url(r'^post/add/(\d+)$', 'dynastie.views.add_post', name='add_post'),
url(r'^post/edit/(\d+)$', 'dynastie.views.edit_post', name='edit_post'), url(r'^post/edit/(\d+)$', 'dynastie.views.edit_post', name='edit_post'),
url(r'^post/delete/(\d+)$','dynastie.views.delete_post', name='delete_post'), url(r'^post/delete/(\d+)$', 'dynastie.views.delete_post', name='delete_post'),
url(r'^generate/(\d+)$', 'dynastie.views.generate', name='generate'), url(r'^generate/(\d+)$', 'dynastie.views.generate', name='generate'),
url(r'^preview/(\d+)$', 'dynastie.views.preview', name='preview'), url(r'^preview/(\d+)$', 'dynastie.views.preview', name='preview'),
url(r'^tinyMCEExternalList/post/add/(\d+)$', 'dynastie.views.tinymcelist_add', name='tinymce'), url(r'^tinyMCEExternalList/post/add/(\d+)$', 'dynastie.views.tinymcelist_add', name='tinymce'),
url(r'^tinyMCEExternalList/post/edit/(\d+)$', 'dynastie.views.tinymcelist_edit', name='tinymce'), url(r'^tinyMCEExternalList/post/edit/(\d+)$', 'dynastie.views.tinymcelist_edit', name='tinymce'),
url(r'^comment/add/(\d+)/(\d+)$', 'dynastie.views.add_comment', name='add_comment'), url(r'^comment/add/(\d+)/(\d+)$', 'dynastie.views.add_comment', name='add_comment'),
url(r'^comment/edit/(\d+)$', 'dynastie.views.edit_comment', name='edit_comment'), url(r'^comment/edit/(\d+)$', 'dynastie.views.edit_comment', name='edit_comment'),
url(r'^comment/delete/(\d+)$','dynastie.views.delete_comment',name='delete_comment'), url(r'^comment/delete/(\d+)$','dynastie.views.delete_comment',name='delete_comment'),

View File

@ -14,6 +14,7 @@ from django.template import Variable, VariableDoesNotExist
@register.filter @register.filter
def hash(object, attr): def hash(object, attr):
return None
pseudo_context = { 'object' : object } pseudo_context = { 'object' : object }
try: try:
value = Variable('object.%s' % attr).resolve(pseudo_context) value = Variable('object.%s' % attr).resolve(pseudo_context)
@ -21,10 +22,13 @@ def hash(object, attr):
value = None value = None
return value return value
def have_I_right(request, blog_id=None, post_id=None): def have_I_right(request, blog_id=None, post_id=None, must_be_superuser=False):
b = None b = None
p = None p = None
if must_be_superuser and not request.user.is_superuser:
raise Http404
if not post_id is None: if not post_id is None:
p = Post.objects.filter(pk=post_id) p = Post.objects.filter(pk=post_id)
@ -37,15 +41,16 @@ def have_I_right(request, blog_id=None, post_id=None):
if not blog_id is None: if not blog_id is None:
if not request.user.is_superuser: if not request.user.is_superuser:
b = Blog.objects.filter(pk=blog_id).filter(writers=request.user.id)[0] b = Blog.objects.filter(pk=blog_id).filter(writers=request.user.id)
if len(b) == 0:
raise Http404
b = b[0]
else: else:
b = Blog.objects.get(pk=post.blog.id) b = Blog.objects.get(pk=post.blog.id)
if b is None: if b is None:
raise Http404 raise Http404
b = b
return (b, p) return (b, p)
def index(request): def index(request):
@ -143,28 +148,30 @@ def edit_user(request, user_id):
return render(request, 'templates/edit_user.html', c) return render(request, 'templates/edit_user.html', c)
@login_required @login_required
def category(request): def category(request, blog_id):
categories = Category.objects.all() b = have_I_right(request, blog_id)
categories = Category.objects.filter(blog_id=blog_id)
c = {'categories' : categories} c = {'categories' : categories}
return render(request, 'templates/category.html', c) return render(request, 'templates/category.html', c)
@login_required @login_required
def add_category(request): def add_category(request, blog_id):
if not request.user.is_superuser: b = have_I_right(request, blog_id)[0]
return HttpResponseRedirect('/category')
if request.method == 'POST': # If the form has been submitted... if request.method == 'POST': # If the form has been submitted...
if 'add' in request.POST: if 'add' in request.POST:
form = CategoryForm(request.POST) # A form bound to the POST data form = CategoryForm(request.POST) # A form bound to the POST data
form.blog = b
if form.is_valid(): # All validation rules pass if form.is_valid(): # All validation rules pass
form = form.save() form = form.save()
# Process the data in form.cleaned_data # Process the data in form.cleaned_data
# ... # ...
return HttpResponseRedirect('/category') # Redirect after POST return HttpResponseRedirect('/category/' + str(b.id))
else: else:
return HttpResponseRedirect('/category') # Redirect after POST return HttpResponseRedirect('/category/' + str(b.id))
else: else:
form = CategoryForm() # An unbound form form = CategoryForm() # An unbound form
@ -179,16 +186,19 @@ def edit_category(request, category_id):
if category is None: if category is None:
raise Http404 raise Http404
b = have_I_right(request, category.blog.id)[0]
if request.method == 'POST': # If the form has been submitted... if request.method == 'POST': # If the form has been submitted...
if 'cancel' in request.POST: if 'cancel' in request.POST:
return HttpResponseRedirect('/category') return HttpResponseRedirect('/category' + str(b.id))
if 'edit' in request.POST and request.user.is_superuser: if 'edit' in request.POST:
name = category.name 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 request.POST['name'] != name: if request.POST['name'] != name:
category.remove() category.remove()
form.save() form.save()
return HttpResponseRedirect('/category/' + str(b.id))
else: else:
form = CategoryForm(instance=category) # An unbound form form = CategoryForm(instance=category) # An unbound form
@ -198,18 +208,17 @@ def edit_category(request, category_id):
@login_required @login_required
def delete_category(request, category_id): def delete_category(request, category_id):
if not request.user.is_superuser:
return HttpResponseRedirect('/category/' + str(category_id))
category = Category.objects.get(pk=category_id) category = Category.objects.get(pk=category_id)
if category is None: if category is None:
raise Http404 raise Http404
b = have_I_right(request, category.blog.id)[0]
category.remove() category.remove()
category.delete() category.delete()
return HttpResponseRedirect('/category') return HttpResponseRedirect('/category/' + str(b.id))
@login_required @login_required
def blog(request): def blog(request):
@ -300,8 +309,7 @@ def edit_blog(request, blog_id):
@login_required @login_required
def add_post(request, blog_id): def add_post(request, blog_id):
if not request.user.is_superuser: (b,) = have_I_right(request, blog_id)
(b,) = have_I_right(request, blog_id)
if request.method == 'POST': # If the form has been submitted... if request.method == 'POST': # If the form has been submitted...
if 'add' in request.POST: if 'add' in request.POST:
@ -401,7 +409,22 @@ def generate(request, blog_id):
b = Blog.objects.get(pk=blog_id) b = Blog.objects.get(pk=blog_id)
form = BlogForm(instance=b) form = BlogForm(instance=b)
c = {'blog' : b, 'posts' : posts, 'form' : form, 'report': report} comments = Comment.objects.filter(post=post).order_by('date')
comment_list_list = []
for comment in comments:
try:
if comment.parent.id is None:
comment_list_list.append([comment])
else:
for comment_list in comment_list_list:
if comment_list[0] == comment.parent:
comment_list[0].append(comment)
break
except Comment.DoesNotExist:
comment_list_list.append([comment])
c = {'blog' : b, 'posts' : posts, 'form' : form, 'report': report, 'comments' : comment_list_list}
return render(request, 'templates/generate.html', c) return render(request, 'templates/generate.html', c)
@ -530,7 +553,7 @@ def add_comment(request, post_id, parent_id):
if inspect.isclass(obj) and obj.__module__.startswith("dynastie") \ if inspect.isclass(obj) and obj.__module__.startswith("dynastie") \
and obj.__module__.endswith("post"): and obj.__module__.endswith("post"):
e = obj() e = obj()
content = e.generate(blog, b.src_path, b.output_path, post) content = e._generate(blog, b.src_path, b.output_path, post)
break break
# Send emails # Send emails