<commit_before>#-*- coding: utf-8 -*-
#
# @license: MIT
#
# (c) 2013-2014 Wishtack
#
# $Id: fa6c3dc1c000ff952f7a2a4612c4e388ff1da7a6 $
#

import itertools
import logging

from synthetic import synthesize_constructor, synthesize_property
from tastypie.bundle import Bundle
from tastypie.exceptions import ImmediateHttpResponse, Unauthorized
from tastypie.fields import RelatedField
from tastypie.http import HttpForbidden
from tastypie.resources import Resource

from ..advanced_authorization import AdvancedAuthorization


logger = logging.getLogger(__name__)


@synthesize_property('resource', contract=Resource)
@synthesize_constructor()
class BundleWithResource(Bundle):
    """
    This class overrides `tastypie.bundle.Bundle` in order to add the 'resource' attribute that we use in to get
    the parent resources in `ICondition` implementations like `ConditionFriend` where we sometimes have to check
    that the wish's parent resource (which is the wish's owner) is a friend of the authenticated user.
    """
    pass


class AdvancedAuthorizationMixinError(Exception):
    pass

class AdvancedAuthorizationMixinInvalidAuthorizationClassError(AdvancedAuthorizationMixinError):

    def __init__(self):
        super(AdvancedAuthorizationMixinInvalidAuthorizationClassError, self).__init__(
            u"Meta.authorization must be an instance of `AdvancedAuthorization`."
        )

class AdvancedAuthorizationMixin(object):
    """
    Makes all resource's fields readyonly except those in 'editable_fields' Meta field.
    This effect can be disabled by setting 'editable_entirely' Meta field to 'True'.
    """

    _KEY_AUTHORIZATION = 'authorization'

    def __new__(cls, *args, **kwargs):

        # Generate api authorization methods instead of writing them manually. Thanks Guido ;).
        for method_name in AdvancedAuthorization.AUTHORIZATION_METHOD_LIST:
            setattr(cls, 'authorized_{}'.format(method_name), cls._make_authorized_method(method_name=method_name))

        return super(AdvancedAuthorizationMixin, cls).__new__(cls, *args, **kwargs)


    def __init__(self, *args, **kwargs):
        super(AdvancedAuthorizationMixin, self).__init__(*args, **kwargs)

        # Nothing to do if 'authorization' is not an instance of `AdvancedAuthorization`.
        advanced_authorization_meta = self._advanced_authorization_meta()
        if advanced_authorization_meta is None:
            raise AdvancedAuthorizationMixinInvalidAuthorizationClassError()

        setattr(self._meta, 'detail_allowed_methods', ['delete', 'get', 'patch', 'post'])
        setattr(self._meta, 'list_allowed_methods', ['delete', 'get', 'patch', 'post', 'put'])
<commit_msg>Allow 'put' mehod for resource detail.<commit_after>#-*- coding: utf-8 -*-
#
# @license: MIT
#
# (c) 2013-2014 Wishtack
#
# $Id: fa6c3dc1c000ff952f7a2a4612c4e388ff1da7a6 $
#

import itertools
import logging

from synthetic import synthesize_constructor, synthesize_property
from tastypie.bundle import Bundle
from tastypie.exceptions import ImmediateHttpResponse, Unauthorized
from tastypie.fields import RelatedField
from tastypie.http import HttpForbidden
from tastypie.resources import Resource

from ..advanced_authorization import AdvancedAuthorization


logger = logging.getLogger(__name__)


@synthesize_property('resource', contract=Resource)
@synthesize_constructor()
class BundleWithResource(Bundle):
    """
    This class overrides `tastypie.bundle.Bundle` in order to add the 'resource' attribute that we use in to get
    the parent resources in `ICondition` implementations like `ConditionFriend` where we sometimes have to check
    that the wish's parent resource (which is the wish's owner) is a friend of the authenticated user.
    """
    pass


class AdvancedAuthorizationMixinError(Exception):
    pass

class AdvancedAuthorizationMixinInvalidAuthorizationClassError(AdvancedAuthorizationMixinError):

    def __init__(self):
        super(AdvancedAuthorizationMixinInvalidAuthorizationClassError, self).__init__(
            u"Meta.authorization must be an instance of `AdvancedAuthorization`."
        )

class AdvancedAuthorizationMixin(object):
    """
    Makes all resource's fields readyonly except those in 'editable_fields' Meta field.
    This effect can be disabled by setting 'editable_entirely' Meta field to 'True'.
    """

    _KEY_AUTHORIZATION = 'authorization'

    def __new__(cls, *args, **kwargs):

        # Generate api authorization methods instead of writing them manually. Thanks Guido ;).
        for method_name in AdvancedAuthorization.AUTHORIZATION_METHOD_LIST:
            setattr(cls, 'authorized_{}'.format(method_name), cls._make_authorized_method(method_name=method_name))

        return super(AdvancedAuthorizationMixin, cls).__new__(cls, *args, **kwargs)


    def __init__(self, *args, **kwargs):
        super(AdvancedAuthorizationMixin, self).__init__(*args, **kwargs)

        # Nothing to do if 'authorization' is not an instance of `AdvancedAuthorization`.
        advanced_authorization_meta = self._advanced_authorization_meta()
        if advanced_authorization_meta is None:
            raise AdvancedAuthorizationMixinInvalidAuthorizationClassError()

        # Allow all methods.
        method_list = ['delete', 'get', 'patch', 'post', 'put']
        setattr(self._meta, 'detail_allowed_methods', method_list)
        setattr(self._meta, 'list_allowed_methods', method_list)

{'fraction_non_alphanumeric': 0.08371665133394664, 'fraction_numerical': 0.01140754369825207, 'mean_word_length': 4.6625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6199069', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1468, 'n_words': 425}