Set category and tag refers to a Blog
This commit is contained in:
parent
d30bb18e43
commit
76cda68611
2
forms.py
2
forms.py
|
@ -17,7 +17,7 @@ class PostForm(ModelForm):
|
|||
class CategoryForm(ModelForm):
|
||||
class Meta:
|
||||
model = Category
|
||||
exclude = ('parent', 'name_slug')
|
||||
exclude = ('parent', 'name_slug', 'blog')
|
||||
|
||||
class UserForm(ModelForm):
|
||||
class Meta:
|
||||
|
|
|
@ -42,7 +42,7 @@ class Post(Index):
|
|||
node.removeChild(node.childNodes[0])
|
||||
node.appendChild(dom.createTextNode(post.title))
|
||||
|
||||
def generate(self, blog, src, output, posts):
|
||||
def _generate(self, blog, src, output, posts):
|
||||
hooks = {'post' : self._createPost,
|
||||
'meta' : self.createMetas}
|
||||
|
||||
|
@ -81,7 +81,7 @@ class Post(Index):
|
|||
|
||||
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):
|
||||
now = datetime.datetime.now()
|
||||
|
|
|
@ -159,6 +159,7 @@ class Category(models.Model):
|
|||
name_slug = models.CharField(max_length=255)
|
||||
parent = models.ForeignKey('self', blank=True, null=True)
|
||||
description = models.TextField(max_length=255, blank=True)
|
||||
blog = models.ForeignKey(Blog)
|
||||
|
||||
def slugify(self):
|
||||
name = normalize('NFKD', self.name).encode('ascii', 'ignore').replace(' ', '-').lower()
|
||||
|
@ -181,6 +182,7 @@ class Category(models.Model):
|
|||
|
||||
class Tag(models.Model):
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
blog = models.ForeignKey(Blog)
|
||||
|
||||
class Post(models.Model):
|
||||
title = models.CharField(max_length=255)
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
{% block head %} {% endblock %}
|
||||
</head>
|
||||
<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 %}
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -7,12 +7,10 @@
|
|||
{% else %}
|
||||
<table>
|
||||
{% 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 %}
|
||||
</table>
|
||||
{% endif %}
|
||||
{% if user.is_superuser %}
|
||||
<li><a href="/category/add">Add a category</a></li>
|
||||
{% endif %}
|
||||
<li><a href="/category/add">Add a category</a></li>
|
||||
</table>
|
||||
{% endblock %}
|
||||
|
|
|
@ -4,10 +4,6 @@
|
|||
<form action="/category/edit/{{ category.id }}" method="post">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
{% if user.is_superuser %}
|
||||
<input type="submit" name="edit" value="Edit" />
|
||||
{% else %}
|
||||
<input type="submit" name="cancel" value="Cancel" />
|
||||
{% endif %}
|
||||
<input type="submit" name="edit" value="Edit" /> <input type="submit" name="cancel" value="Cancel" />
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
{% extends "templates/base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<a href="/category/{{ blog.id }}">Categories</a>
|
||||
{% if user.is_superuser %}
|
||||
<form action="/blog/edit/{{ blog.id }}" method="post">
|
||||
{% csrf_token %}
|
||||
|
@ -24,7 +25,9 @@
|
|||
{% else %}
|
||||
<table>
|
||||
{% 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>
|
||||
{% endwith %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</table>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
{% extends "templates/base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<a href="/category/{{ blog.id }}">Categories</a>
|
||||
{% if user.is_superuser %}
|
||||
<form action="/blog/edit/{{ blog.id }}" method="post">
|
||||
{% csrf_token %}
|
||||
|
|
14
urls.py
14
urls.py
|
@ -13,21 +13,21 @@ urlpatterns = patterns('',
|
|||
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/(\d+)$', 'dynastie.views.edit_user', name='view_user'),
|
||||
url(r'^category$', 'dynastie.views.category', name='category'),
|
||||
url(r'^category/add$', 'dynastie.views.add_category', name='add_category'),
|
||||
url(r'^category/(\d+)$', 'dynastie.views.category', name='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/delete/(\d+)$', 'dynastie.views.delete_category', name='delete_category'),
|
||||
url(r'^blog$', 'dynastie.views.blog', name='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/edit/(\d+)$', 'dynastie.views.edit_blog', name='edit_blog'),
|
||||
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/delete/(\d+)$','dynastie.views.delete_post', name='delete_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/delete/(\d+)$', 'dynastie.views.delete_post', name='delete_post'),
|
||||
url(r'^generate/(\d+)$', 'dynastie.views.generate', name='generate'),
|
||||
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/edit/(\d+)$', 'dynastie.views.tinymcelist_edit', 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'^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/delete/(\d+)$','dynastie.views.delete_comment',name='delete_comment'),
|
||||
|
|
65
views.py
65
views.py
|
@ -14,6 +14,7 @@ from django.template import Variable, VariableDoesNotExist
|
|||
|
||||
@register.filter
|
||||
def hash(object, attr):
|
||||
return None
|
||||
pseudo_context = { 'object' : object }
|
||||
try:
|
||||
value = Variable('object.%s' % attr).resolve(pseudo_context)
|
||||
|
@ -21,10 +22,13 @@ def hash(object, attr):
|
|||
value = None
|
||||
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
|
||||
p = None
|
||||
|
||||
if must_be_superuser and not request.user.is_superuser:
|
||||
raise Http404
|
||||
|
||||
if not post_id is None:
|
||||
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 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:
|
||||
b = Blog.objects.get(pk=post.blog.id)
|
||||
|
||||
if b is None:
|
||||
raise Http404
|
||||
|
||||
b = b
|
||||
|
||||
return (b, p)
|
||||
|
||||
def index(request):
|
||||
|
@ -143,28 +148,30 @@ def edit_user(request, user_id):
|
|||
return render(request, 'templates/edit_user.html', c)
|
||||
|
||||
@login_required
|
||||
def category(request):
|
||||
categories = Category.objects.all()
|
||||
def category(request, blog_id):
|
||||
b = have_I_right(request, blog_id)
|
||||
|
||||
categories = Category.objects.filter(blog_id=blog_id)
|
||||
|
||||
c = {'categories' : categories}
|
||||
|
||||
return render(request, 'templates/category.html', c)
|
||||
|
||||
@login_required
|
||||
def add_category(request):
|
||||
if not request.user.is_superuser:
|
||||
return HttpResponseRedirect('/category')
|
||||
def add_category(request, blog_id):
|
||||
b = have_I_right(request, blog_id)[0]
|
||||
|
||||
if request.method == 'POST': # If the form has been submitted...
|
||||
if 'add' in request.POST:
|
||||
form = CategoryForm(request.POST) # A form bound to the POST data
|
||||
form.blog = b
|
||||
if form.is_valid(): # All validation rules pass
|
||||
form = form.save()
|
||||
# Process the data in form.cleaned_data
|
||||
# ...
|
||||
return HttpResponseRedirect('/category') # Redirect after POST
|
||||
return HttpResponseRedirect('/category/' + str(b.id))
|
||||
else:
|
||||
return HttpResponseRedirect('/category') # Redirect after POST
|
||||
return HttpResponseRedirect('/category/' + str(b.id))
|
||||
else:
|
||||
form = CategoryForm() # An unbound form
|
||||
|
||||
|
@ -179,16 +186,19 @@ def edit_category(request, category_id):
|
|||
if category is None:
|
||||
raise Http404
|
||||
|
||||
b = have_I_right(request, category.blog.id)[0]
|
||||
|
||||
if request.method == 'POST': # If the form has been submitted...
|
||||
if 'cancel' in request.POST:
|
||||
return HttpResponseRedirect('/category')
|
||||
if 'edit' in request.POST and request.user.is_superuser:
|
||||
return HttpResponseRedirect('/category' + str(b.id))
|
||||
if 'edit' in request.POST:
|
||||
name = category.name
|
||||
form = CategoryForm(request.POST, instance=category) # A form bound to the POST data
|
||||
if form.is_valid(): # All validation rules pass
|
||||
if request.POST['name'] != name:
|
||||
category.remove()
|
||||
form.save()
|
||||
return HttpResponseRedirect('/category/' + str(b.id))
|
||||
else:
|
||||
form = CategoryForm(instance=category) # An unbound form
|
||||
|
||||
|
@ -198,18 +208,17 @@ def edit_category(request, category_id):
|
|||
|
||||
@login_required
|
||||
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)
|
||||
|
||||
if category is None:
|
||||
raise Http404
|
||||
|
||||
b = have_I_right(request, category.blog.id)[0]
|
||||
|
||||
category.remove()
|
||||
category.delete()
|
||||
|
||||
return HttpResponseRedirect('/category')
|
||||
return HttpResponseRedirect('/category/' + str(b.id))
|
||||
|
||||
@login_required
|
||||
def blog(request):
|
||||
|
@ -300,8 +309,7 @@ def edit_blog(request, blog_id):
|
|||
|
||||
@login_required
|
||||
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 'add' in request.POST:
|
||||
|
@ -401,7 +409,22 @@ def generate(request, blog_id):
|
|||
b = Blog.objects.get(pk=blog_id)
|
||||
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)
|
||||
|
||||
|
@ -530,7 +553,7 @@ def add_comment(request, post_id, parent_id):
|
|||
if inspect.isclass(obj) and obj.__module__.startswith("dynastie") \
|
||||
and obj.__module__.endswith("post"):
|
||||
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
|
||||
|
||||
# Send emails
|
||||
|
|
Loading…
Reference in New Issue
Block a user