from django.shortcuts import render, redirect from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_protect from boards.abstracts.settingsmanager import get_settings_manager from boards.models.attachment import AttachmentSticker, Attachment from boards.views.base import BaseBoardView CONTEXT_GLOBAL_STICKERS = 'global_stickers' CONTEXT_LOCAL_STICKERS = 'local_stickers' TEMPLATE = 'boards/aliases.html' class AliasesView(BaseBoardView): @method_decorator(csrf_protect) def get(self, request, category=None): result = self._process_creation(request) if result: return result params = dict() if category: params[CONTEXT_GLOBAL_STICKERS] = AttachmentSticker.objects.filter( name__startswith=(category + '/')) else: params[CONTEXT_GLOBAL_STICKERS] = AttachmentSticker.objects.all() params[CONTEXT_LOCAL_STICKERS] = get_settings_manager(request).get_stickers() return render(request, TEMPLATE, params) def _process_creation(self, request): action = request.GET.get('action') if action == 'add' and 'name' in request.GET and 'id' in request.GET: name = request.GET['name'] id = request.GET['id'] attachment = Attachment.objects.get(id=id) get_settings_manager(request).add_attachment_alias(name, attachment) return redirect('stickers') if action == 'remove' and 'name' in request.GET: name = request.GET['name'] get_settings_manager(request).remove_attachment_alias(name) return redirect('stickers')