<commit_before>#  -*- coding: utf-8 -*-
# resource.py ---
#
# Created: Mon Dec 12 12:10:00 2011 (+0200)
# Author: <NAME>
#


from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponse
import errors, datamapper
from http import codes


# todo: move and make configurable
REALM = "test"


# todo: move somewhere and add note about borrowing this from piston
def coerce_put_post(request):
    """
    Django doesn't particularly understand REST.
    In case we send data over PUT, Django won't
    actually look at the data and load it. We need
    to twist its arm here.

    The try/except abominiation here is due to a bug
    in mod_python. This should fix it.
    """
    if request.method.upper() == "PUT":
        # Bug fix: if _load_post_and_files has already been called, for
        # example by middleware accessing request.POST, the below code to
        # pretend the request is a POST instead of a PUT will be too late
        # to make a difference. Also calling _load_post_and_files will result
        # in the following exception:
        #   AttributeError: You cannot set the upload handlers after the upload has been processed.
        # The fix is to check for the presence of the _post field which is set
        # the first time _load_post_and_files is called (both by wsgi.py and
        # modpython.py). If it's set, the request has to be 'reset' to redo
        # the query value parsing in POST mode.
        if hasattr(request, '_post'):
            del request._post
            del request._files

        try:
            request.method = "POST"
            request._load_post_and_files()
            request.method = "PUT"
        except AttributeError:
            request.META['REQUEST_METHOD'] = 'POST'
            request._load_post_and_files()
            request.META['REQUEST_METHOD'] = 'PUT'

        request.PUT = request.POST


class Resource(object):
    """ Base class for resources exposed by an API.

    Derive this class to create a resource. At minimum, (in the
    derived class) you need to define one method named after the
    corresponding http method (i.e. post, get, put or delete).

    The response returned by `get()` can be one of the following:
      - django's HttpResponse
      - drest's Response
      - dictionary
      - plaintext

    Additionally, `HttpStatusCodeError`s are caught and converted to
    corresponding http responses.
    """

    # configuration parameters
    # todo: add parameter for default_mapper

    access_controller = None
    allow_empty_data = False
    authentication = None
    representation = None
    parameters = None
    formatter = None
    mapper = None

    def __call__(self, request, *args, **kw):
        """ Entry point for HTTP requests. """

        coerce_put_post(request) #django-fix
        try:
            return self._handle_request(request, *args, **kw)
        except errors.HttpStatusCodeError, e:
            return self._get_error_response(e)

    def name(self):
        """ Return resource's name.

        This is used mainly by the permission module to determine the
        name of a permission.<commit_msg>Removed unused parameters from Resource<commit_after>#  -*- coding: utf-8 -*-
# resource.py ---
#
# Created: Mon Dec 12 12:10:00 2011 (+0200)
# Author: <NAME>
#


from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponse
import errors, datamapper
from http import codes


# todo: move and make configurable
REALM = "test"


# todo: move somewhere and add note about borrowing this from piston
def coerce_put_post(request):
    """
    Django doesn't particularly understand REST.
    In case we send data over PUT, Django won't
    actually look at the data and load it. We need
    to twist its arm here.

    The try/except abominiation here is due to a bug
    in mod_python. This should fix it.
    """
    if request.method.upper() == "PUT":
        # Bug fix: if _load_post_and_files has already been called, for
        # example by middleware accessing request.POST, the below code to
        # pretend the request is a POST instead of a PUT will be too late
        # to make a difference. Also calling _load_post_and_files will result
        # in the following exception:
        #   AttributeError: You cannot set the upload handlers after the upload has been processed.
        # The fix is to check for the presence of the _post field which is set
        # the first time _load_post_and_files is called (both by wsgi.py and
        # modpython.py). If it's set, the request has to be 'reset' to redo
        # the query value parsing in POST mode.
        if hasattr(request, '_post'):
            del request._post
            del request._files

        try:
            request.method = "POST"
            request._load_post_and_files()
            request.method = "PUT"
        except AttributeError:
            request.META['REQUEST_METHOD'] = 'POST'
            request._load_post_and_files()
            request.META['REQUEST_METHOD'] = 'PUT'

        request.PUT = request.POST


class Resource(object):
    """ Base class for resources exposed by an API.

    Derive this class to create a resource. At minimum, (in the
    derived class) you need to define one method named after the
    corresponding http method (i.e. post, get, put or delete).

    The response returned by `get()` can be one of the following:
      - django's HttpResponse
      - drest's Response
      - dictionary
      - plaintext

    Additionally, `HttpStatusCodeError`s are caught and converted to
    corresponding http responses.
    """

    # configuration parameters
    # todo: add parameter for default_mapper

    access_controller = None
    allow_empty_data = False
    authentication = None
    representation = None
    mapper = None

    def __call__(self, request, *args, **kw):
        """ Entry point for HTTP requests. """

        coerce_put_post(request) #django-fix
        try:
            return self._handle_request(request, *args, **kw)
        except errors.HttpStatusCodeError, e:
            return self._get_error_response(e)

    def name(self):
        """ Return resource's name.

        This is used mainly by the permission module to determine the
        name of a permission.
{'fraction_non_alphanumeric': 0.07254870901314747, 'fraction_numerical': 0.005385712022810074, 'mean_word_length': 2.5332960268606604, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '3990144', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1650, 'n_words': 701}