Add BoardMixin, update can_modify
* BoardMixin adds a cached property that gets the current viewed board - this assumes that there is a self.kwargs["url"] value available. * can_modify now checks for some more permissions. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
|
import functools
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth import get_user_model, get_user
|
from django.contrib.auth import get_user
|
||||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.http import Http404, HttpResponseRedirect
|
from django.http import Http404, HttpResponseRedirect
|
||||||
@@ -33,14 +34,15 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
User = get_user_model()
|
|
||||||
|
|
||||||
|
|
||||||
def can_modify(user):
|
def can_modify(user):
|
||||||
if not user:
|
if not user:
|
||||||
return False
|
return False
|
||||||
# TODO add more permissions as required
|
# TODO add more permissions as required
|
||||||
return user.has_perm("board.set_sticky")
|
return (
|
||||||
|
user.has_perm("board.set_sticky")
|
||||||
|
or user.has_perm("board.set_bump")
|
||||||
|
or user.has_perm("board.set_lock")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class BannedView(TemplateView):
|
class BannedView(TemplateView):
|
||||||
@@ -60,7 +62,13 @@ class BannedView(TemplateView):
|
|||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
class CreateView(edit.CreateView):
|
class BoardMixin:
|
||||||
|
@functools.cached_property
|
||||||
|
def board(self) -> Board:
|
||||||
|
return get_object_or_404(Board, url=self.kwargs["url"]) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
class CreateView(BoardMixin, edit.CreateView):
|
||||||
"""
|
"""
|
||||||
Helper class that sets a few variables for posts and check against bans.
|
Helper class that sets a few variables for posts and check against bans.
|
||||||
This should not be used by itself.
|
This should not be used by itself.
|
||||||
@@ -76,9 +84,6 @@ class CreateView(edit.CreateView):
|
|||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
# Set the board on this object
|
|
||||||
self.board = get_object_or_404(Board, url=kwargs["url"])
|
|
||||||
|
|
||||||
# Check for bans
|
# Check for bans
|
||||||
ip = get_client_ip(request)
|
ip = get_client_ip(request)
|
||||||
if request.method == "POST" and is_banned(ip, self.board):
|
if request.method == "POST" and is_banned(ip, self.board):
|
||||||
@@ -87,7 +92,7 @@ class CreateView(edit.CreateView):
|
|||||||
return super(CreateView, self).dispatch(request, *args, **kwargs)
|
return super(CreateView, self).dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class BoardView(TemplateView):
|
class BoardView(BoardMixin, TemplateView):
|
||||||
model = Board
|
model = Board
|
||||||
slug_field = "url"
|
slug_field = "url"
|
||||||
slug_url_kwarg = "url"
|
slug_url_kwarg = "url"
|
||||||
@@ -101,10 +106,6 @@ class BoardView(TemplateView):
|
|||||||
)
|
)
|
||||||
return super(BoardView, self).get(request, *args, **kwargs)
|
return super(BoardView, self).get(request, *args, **kwargs)
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
|
||||||
self.board = get_object_or_404(Board, url=kwargs["url"])
|
|
||||||
return super(BoardView, self).dispatch(request, *args, **kwargs)
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
page = self.kwargs["page"]
|
page = self.kwargs["page"]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user