Update PostModifyForm to use permissions to create its fields
Fields are only displayed via the PostModifyForm if the user has specific permissions to do things, like set stickies. Also, add PostModifySuccessView that will close the modify window when the process is complete. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -24,6 +24,7 @@ __all__ = (
|
||||
"BoardView",
|
||||
"PostCreateView",
|
||||
"PostModifyView",
|
||||
"PostModifySuccessView",
|
||||
"PostView",
|
||||
"PostSuccessView",
|
||||
"ReplyCreateView",
|
||||
@@ -35,6 +36,13 @@ __all__ = (
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
def can_modify(user):
|
||||
if not user:
|
||||
return False
|
||||
# TODO add more permissions as required
|
||||
return user.has_perm("board.set_sticky")
|
||||
|
||||
|
||||
class BannedView(TemplateView):
|
||||
template_name = "board/banned.html"
|
||||
|
||||
@@ -117,6 +125,7 @@ class BoardView(TemplateView):
|
||||
kwargs["pages"] = range(1, last_page + 1)
|
||||
kwargs["last_page"] = last_page
|
||||
kwargs["max_upload_size"] = settings.MAX_UPLOAD_SIZE
|
||||
kwargs["can_modify"] = can_modify(self.request.user)
|
||||
|
||||
return super(BoardView, self).get_context_data(**kwargs)
|
||||
|
||||
@@ -135,6 +144,7 @@ class PostCreateView(CreateView):
|
||||
context["capcodes"] = get_objects_for_user(
|
||||
get_user(self.request), "board.use_capcode"
|
||||
)
|
||||
kwargs["can_modify"] = can_modify(self.request.user)
|
||||
return context
|
||||
|
||||
def get_form_kwargs(self):
|
||||
@@ -152,19 +162,34 @@ class PostModifyView(PermissionRequiredMixin, edit.UpdateView):
|
||||
model = Post
|
||||
form_class = PostModifyForm
|
||||
template_name = "board/post_modify_view.html"
|
||||
success_url = reverse_lazy("board:post_modify_success")
|
||||
|
||||
def has_permission(self) -> bool:
|
||||
user = self.request.user
|
||||
if not user:
|
||||
return False
|
||||
# TODO add more permissions as required
|
||||
return user.has_perm("board.can_sticky")
|
||||
return can_modify(self.request.user)
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.post_obj = get_object_or_404(Post, id=kwargs["pk"])
|
||||
self.board = self.post_obj.board
|
||||
return super(PostModifyView, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_form_kwargs(self) -> Dict[str, Any]:
|
||||
kwargs = super(PostModifyView, self).get_form_kwargs()
|
||||
kwargs["user"] = self.request.user
|
||||
return kwargs
|
||||
|
||||
|
||||
class PostModifySuccessView(PermissionRequiredMixin, TemplateView):
|
||||
template_name = "board/post_modify_success.html"
|
||||
|
||||
def has_permission(self) -> bool:
|
||||
return can_modify(self.request.user)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context["window_timeout"] = settings.BAN_WINDOW_CLOSE_TIMEOUT
|
||||
context["can_modify"] = self.has_permission()
|
||||
return context
|
||||
|
||||
|
||||
class ReplyCreateView(CreateView):
|
||||
model = Post
|
||||
@@ -183,6 +208,7 @@ class ReplyCreateView(CreateView):
|
||||
context["capcodes"] = get_objects_for_user(
|
||||
get_user(self.request), "board.use_capcode"
|
||||
)
|
||||
kwargs["can_modify"] = can_modify(self.request.user)
|
||||
return context
|
||||
|
||||
def get_form_kwargs(self):
|
||||
@@ -262,7 +288,7 @@ class ReportSuccessView(TemplateView):
|
||||
class BanCreateView(PermissionRequiredMixin, edit.CreateView):
|
||||
model = Ban
|
||||
form_class = BanForm
|
||||
permission_required = "ban.create"
|
||||
permission_required = "board.add_ban"
|
||||
success_url = reverse_lazy("board:ban_success")
|
||||
|
||||
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
|
||||
|
||||
Reference in New Issue
Block a user