Skip to content

Commit

Permalink
Drop authorization code for hooksets
Browse files Browse the repository at this point in the history
  • Loading branch information
khchine5 committed Apr 17, 2016
1 parent a3530fe commit e1ae325
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 43 deletions.
25 changes: 0 additions & 25 deletions pinax/comments/authorization.py

This file was deleted.

11 changes: 4 additions & 7 deletions pinax/comments/conf.py
@@ -1,4 +1,4 @@
from __future__ import unicode_literals
from __future__ import absolute_import

from appconf import AppConf
from django.conf import settings # noqa
Expand All @@ -21,11 +21,8 @@ def load_path_attr(path):


class CommentsAppConf(AppConf):
CAN_DELETE_CALLABLE = "pinax.comments.authorization.default_can_delete"
CAN_EDIT_CALLABLE = "pinax.comments.authorization.default_can_edit"

def configure_can_delete_callable(self, value):
return load_path_attr(value)
HOOKSET = "pinax.comments.hooks.CommentsDefaultHookSet"

def configure_can_edit_callable(self, value):
return load_path_attr(value)
def configure_hookset(self, value):
return load_path_attr(value)()
17 changes: 17 additions & 0 deletions pinax/comments/hooks.py
@@ -0,0 +1,17 @@
class CommentsDefaultHookSet(object):
def load_can_delete(self, user, comment):
if user.is_superuser:
return True
return user == comment.author

def load_can_edit(self, user, comment):
return user == comment.author


class HookProxy(object):
def __getattr__(self, attr):
from .conf import settings
return getattr(settings.COMMENTS_HOOKSET, attr)


hookset = HookProxy()
8 changes: 3 additions & 5 deletions pinax/comments/templatetags/pinax_comments_tags.py
Expand Up @@ -5,23 +5,21 @@

from django.contrib.contenttypes.models import ContentType

from ..authorization import load_can_delete, load_can_edit
from ..hooks import hookset
from ..forms import CommentForm
from ..models import Comment

can_delete = load_can_delete()
can_edit = load_can_edit()
register = template.Library()


@register.filter
def can_edit_comment(comment, user):
return can_edit(user, comment)
return hookset.load_can_edit(user, comment)


@register.filter
def can_delete_comment(comment, user):
return can_delete(user, comment)
return hookset.load_can_delete(user, comment)


@register.simple_tag
Expand Down
1 change: 0 additions & 1 deletion pinax/comments/tests/tests.py
Expand Up @@ -21,7 +21,6 @@ def getajax(self, url_name, *args, **kwargs):
HTTP_X_REQUESTED_WITH="XMLHttpRequest")

def post_comment_2(self, url_name, *args, **kwargs):
# data = kwargs.get("data", {})
url_name = "pinax_comments:" + url_name
return self.post(url_name, args=args, kwargs=kwargs)

Expand Down
7 changes: 2 additions & 5 deletions pinax/comments/views.py
Expand Up @@ -13,10 +13,7 @@
from .forms import CommentForm
from .models import Comment
from .signals import commented, comment_updated
from .conf import settings

can_delete = settings.COMMENTS_CAN_EDIT_CALLABLE
can_edit = settings.COMMENTS_CAN_EDIT_CALLABLE
from .hooks import hookset


class CommentSecureRedirectToMixin(object):
Expand Down Expand Up @@ -123,7 +120,7 @@ class CommentDeleteView(LoginRequiredMixin, CommentSecureRedirectToMixin, Delete
def post(self, request, *args, **kwargs):
self.object = self.get_object()
success_url = self.get_secure_redirect_to()
if can_delete(request.user, self.object):
if hookset.load_can_delete(request.user, self.object):
self.object.delete()
if request.is_ajax():
return JsonResponse({"status": "OK"})
Expand Down

0 comments on commit e1ae325

Please sign in to comment.