def test_add(self): """add method adds function to patch object""" patch = ApiPatch() def test_function(): pass patch.add('test-add', test_function) self.assertEqual(len(patch._actions), 1) self.assertEqual(patch._actions[0]['op'], 'add') self.assertEqual(patch._actions[0]['path'], 'test-add') self.assertEqual(patch._actions[0]['handler'], test_function)
def test_add(self): """add method adds function to patch object""" patch = ApiPatch() def mock_function(): pass patch.add('test-add', mock_function) self.assertEqual(len(patch._actions), 1) self.assertEqual(patch._actions[0]['op'], 'add') self.assertEqual(patch._actions[0]['path'], 'test-add') self.assertEqual(patch._actions[0]['handler'], mock_function)
def test_dispatch_action(self): """dispatch_action calls specified actions""" patch = ApiPatch() mock_target = MockObject(13) def action_a(request, target, value): self.assertEqual(request, 'request') self.assertEqual(target, mock_target) return {'a': value * 2, 'b': 111} patch.replace('abc', action_a) def action_b(request, target, value): self.assertEqual(request, 'request') self.assertEqual(target, mock_target) return {'b': value * 10} patch.replace('abc', action_b) def action_fail(request, target, value): self.fail("unrequired action was called") patch.add('c', action_fail) patch.remove('c', action_fail) patch.replace('c', action_fail) patch_dict = {'id': 123} patch.dispatch_action( patch_dict, 'request', mock_target, { 'op': 'replace', 'path': 'abc', 'value': 5, } ) self.assertEqual(len(patch_dict), 3) self.assertEqual(patch_dict['id'], 123) self.assertEqual(patch_dict['a'], 10) self.assertEqual(patch_dict['b'], 50)
from ...utils import add_categories_to_threads from ...validators import validate_title thread_patch_dispatcher = ApiPatch() def patch_acl(request, thread, value): """useful little op that updates thread acl to current state""" if value: add_acl(request.user, thread) return {'acl': thread.acl} else: return {'acl': None} thread_patch_dispatcher.add('acl', patch_acl) def patch_title(request, thread, value): try: value_cleaned = six.text_type(value).strip() except (TypeError, ValueError): raise PermissionDenied(_("Invalid thread title.")) try: validate_title(value_cleaned) except ValidationError as e: raise PermissionDenied(e.args[0]) if not thread.acl.get('can_edit'): raise PermissionDenied(
UserModel = get_user_model() thread_patch_dispatcher = ApiPatch() def patch_acl(request, thread, value): """useful little op that updates thread acl to current state""" if value: add_acl(request.user, thread) return {'acl': thread.acl} else: return {'acl': None} thread_patch_dispatcher.add('acl', patch_acl) def patch_title(request, thread, value): try: value_cleaned = str(value).strip() except (TypeError, ValueError): raise PermissionDenied(_('Not a valid string.')) try: validate_title(value_cleaned) except ValidationError as e: raise PermissionDenied(e.args[0]) allow_edit_thread(request.user, thread)
from ...moderation import posts as moderation from ...permissions.threads import allow_approve_post, allow_hide_post, allow_protect_post, allow_unhide_post post_patch_dispatcher = ApiPatch() def patch_acl(request, post, value): """useful little op that updates post acl to current state""" if value: add_acl(request.user, post) return {'acl': post.acl} else: return {'acl': None} post_patch_dispatcher.add('acl', patch_acl) def patch_is_protected(request, post, value): allow_protect_post(request.user, post) if value: moderation.protect_post(request.user, post) else: moderation.unprotect_post(request.user, post) return {'is_protected': post.is_protected} post_patch_dispatcher.replace('is-protected', patch_is_protected) def patch_is_unapproved(request, post, value):
from misago.threads.permissions import allow_hide_event, allow_unhide_event event_patch_dispatcher = ApiPatch() def patch_acl(request, event, value): """useful little op that updates event acl to current state""" if value: add_acl(request.user, event) return {'acl': event.acl} else: return {'acl': None} event_patch_dispatcher.add('acl', patch_acl) def patch_is_hidden(request, event, value): if value: allow_hide_event(request.user, event) moderation.hide_post(request.user, event) else: allow_unhide_event(request.user, event) moderation.unhide_post(request.user, event) return {'is_hidden': event.is_hidden} event_patch_dispatcher.replace('is-hidden', patch_is_hidden)
from misago.core.apipatch import ApiPatch from ...moderation import posts as moderation event_patch_dispatcher = ApiPatch() def patch_acl(request, event, value): """useful little op that updates event acl to current state""" if value: add_acl(request.user, event) return {'acl': event.acl} else: return {'acl': None} event_patch_dispatcher.add('acl', patch_acl) def patch_is_hidden(request, event, value): if event.acl.get('can_hide'): if value: moderation.hide_post(request.user, event) else: moderation.unhide_post(request.user, event) return {'is_hidden': event.is_hidden} else: raise PermissionDenied( _("You don't have permission to hide this event."))
from misago.core.apipatch import ApiPatch from ...moderation import posts as moderation from ...permissions.threads import allow_approve_post, allow_hide_post, allow_protect_post, allow_unhide_post post_patch_dispatcher = ApiPatch() def patch_acl(request, post, value): """useful little op that updates post acl to current state""" if value: add_acl(request.user, post) return {'acl': post.acl} else: return {'acl': None} post_patch_dispatcher.add('acl', patch_acl) def patch_is_protected(request, post, value): allow_protect_post(request.user, post) if value: moderation.protect_post(request.user, post) else: moderation.unprotect_post(request.user, post) return {'is_protected': post.is_protected} post_patch_dispatcher.replace('is-protected', patch_is_protected) def patch_is_unapproved(request, post, value): if value is False: allow_approve_post(request.user, post)
def patch_top_category(request, thread, value): category_pk = get_int_or_404(value) root_category = get_object_or_404( Category.objects.all_categories(include_root=True), pk=category_pk ) categories = list(Category.objects.all_categories().filter( id__in=request.user.acl['visible_categories'] )) add_categories_to_threads(root_category, categories, [thread]) return {'top_category': CategorySerializer(thread.top_category).data} thread_patch_endpoint.add('top-category', patch_top_category) def patch_flatten_categories(request, thread, value): try: return { 'category': thread.category_id, 'top_category': thread.top_category.pk, } except AttributeError: return { 'category': thread.category_id, } thread_patch_endpoint.replace('flatten-categories', patch_flatten_categories)
from ...permissions import allow_start_thread from ...utils import add_categories_to_items from ...validators import validate_title thread_patch_dispatcher = ApiPatch() def patch_acl(request, thread, value): """useful little op that updates thread acl to current state""" if value: add_acl(request.user, thread) return {'acl': thread.acl} else: return {'acl': None} thread_patch_dispatcher.add('acl', patch_acl) def patch_title(request, thread, value): try: value_cleaned = six.text_type(value).strip() except (TypeError, ValueError): raise PermissionDenied(_("Invalid thread title.")) try: validate_title(value_cleaned) except ValidationError as e: raise PermissionDenied(e.args[0]) if not thread.acl.get('can_edit'): raise PermissionDenied(_("You don't have permission to edit this thread."))
UserModel = get_user_model() thread_patch_dispatcher = ApiPatch() def patch_acl(request, thread, value): """useful little op that updates thread acl to current state""" if value: add_acl(request.user, thread) return {'acl': thread.acl} else: return {'acl': None} thread_patch_dispatcher.add('acl', patch_acl) def patch_title(request, thread, value): try: value_cleaned = six.text_type(value).strip() except (TypeError, ValueError): raise PermissionDenied(_("Invalid thread title.")) try: validate_title(value_cleaned) except ValidationError as e: raise PermissionDenied(e.args[0]) allow_edit_thread(request.user, thread)
thread_patch_endpoint.replace('category', patch_move) def patch_top_category(request, thread, value): category_pk = get_int_or_404(value) root_category = get_object_or_404( Category.objects.all_categories(include_root=True), pk=category_pk ) categories = list(Category.objects.all_categories().filter( id__in=request.user.acl['visible_categories'] )) add_categories_to_threads(root_category, categories, [thread]) return {'top_category': CategorySerializer(thread.top_category).data} thread_patch_endpoint.add('top-category', patch_top_category) def patch_flatten_categories(request, thread, value): try: return { 'category': thread.category_id, 'top_category': thread.top_category.pk, } except AttributeError as e: return { 'category': thread.category_id, 'top_category': None } thread_patch_endpoint.replace('flatten-categories', patch_flatten_categories)
from ...serializers import ThreadParticipantSerializer from ...utils import add_categories_to_items from ...validators import validate_title thread_patch_dispatcher = ApiPatch() def patch_acl(request, thread, value): """useful little op that updates thread acl to current state""" if value: add_acl(request.user, thread) return {'acl': thread.acl} else: return {'acl': None} thread_patch_dispatcher.add('acl', patch_acl) def patch_title(request, thread, value): try: value_cleaned = six.text_type(value).strip() except (TypeError, ValueError): raise PermissionDenied(_("Invalid thread title.")) try: validate_title(value_cleaned) except ValidationError as e: raise PermissionDenied(e.args[0]) if not thread.acl.get('can_edit'): raise PermissionDenied(_("You don't have permission to edit this thread."))
from misago.threads.validators import validate_title UserModel = get_user_model() thread_patch_dispatcher = ApiPatch() def patch_acl(request, thread, value): """useful little op that updates thread acl to current state""" if value: add_acl(request.user, thread) return {'acl': thread.acl} else: return {'acl': None} thread_patch_dispatcher.add('acl', patch_acl) def patch_title(request, thread, value): try: value_cleaned = six.text_type(value).strip() except (TypeError, ValueError): raise PermissionDenied(_("Invalid thread title.")) try: validate_title(value_cleaned) except ValidationError as e: raise PermissionDenied(e.args[0]) allow_edit_thread(request.user, thread)