post_admin.py
60 lines
| 1.9 KiB
| text/x-python
|
PythonLexer
neko259
|
r566 | from django.shortcuts import render, get_object_or_404, redirect | ||
neko259
|
r730 | from boards.abstracts.settingsmanager import PERMISSION_MODERATE,\ | ||
get_settings_manager | ||||
neko259
|
r566 | from boards.views.base import BaseBoardView | ||
from boards.views.mixins import DispatcherMixin | ||||
from boards.models.post import Post | ||||
from boards.models.tag import Tag | ||||
from boards.forms import AddTagForm, PlainErrorList | ||||
neko259
|
r728 | |||
neko259
|
r566 | class PostAdminView(BaseBoardView, DispatcherMixin): | ||
def get(self, request, post_id, form=None): | ||||
neko259
|
r730 | settings_manager = get_settings_manager(request) | ||
neko259
|
r728 | if not settings_manager.has_permission(PERMISSION_MODERATE): | ||
neko259
|
r566 | redirect('index') | ||
post = get_object_or_404(Post, id=post_id) | ||||
if not form: | ||||
dispatch_result = self.dispatch_method(request, post) | ||||
if dispatch_result: | ||||
return dispatch_result | ||||
form = AddTagForm() | ||||
context = self.get_context_data(request=request) | ||||
context['post'] = post | ||||
context['tag_form'] = form | ||||
return render(request, 'boards/post_admin.html', context) | ||||
def post(self, request, post_id): | ||||
neko259
|
r730 | settings_manager = get_settings_manager(request) | ||
neko259
|
r728 | if not settings_manager.has_permission(PERMISSION_MODERATE): | ||
neko259
|
r566 | redirect('index') | ||
post = get_object_or_404(Post, id=post_id) | ||||
return self.dispatch_method(request, post) | ||||
def delete_tag(self, request, post): | ||||
tag_name = request.GET['tag'] | ||||
tag = get_object_or_404(Tag, name=tag_name) | ||||
post.remove_tag(tag) | ||||
return redirect('post_admin', post.id) | ||||
def add_tag(self, request, post): | ||||
form = AddTagForm(request.POST, error_class=PlainErrorList) | ||||
if form.is_valid(): | ||||
tag_name = form.cleaned_data['tag'] | ||||
tag, created = Tag.objects.get_or_create(name=tag_name) | ||||
post.add_tag(tag) | ||||
return redirect('post_admin', post.id) | ||||
else: | ||||
return self.get(request, post.id, form) | ||||