Exemplo n.º 1
0
    def get_field_detail(self, request, **kwargs):
        """Given the id's of a SubmissionSet (kwargs['pk']) and
        a DocumentationField (kwargs['fieldpk']), get the
        DocumentationFieldSubmissionResource that matches.
        """
        # Need to check
        # DocumentationFieldSubmissionResource.Meta.allowed_methods
        # explicitly because the URL fiddling done above bypasses the
        # usual check:
        if (request.method.lower() not in
                DocumentationFieldSubmissionResource.Meta.allowed_methods):
            return HttpMethodNotAllowed()
        self.is_authenticated(request)
        for field_resource_type in (
                NumericSubmissionResource, TextSubmissionResource,
                NumericSubmissionResource, TextSubmissionResource,
                LongTextSubmissionResource, DateSubmissionResource,
                URLSubmissionResource, UploadSubmissionResource,
                BooleanSubmissionResource, ChoiceSubmissionResource,
                MultiChoiceSubmissionResource):
            resources = field_resource_type().obj_get_list(
                request, submissionset_id=kwargs['pk'])
            try:
                resources.get(documentation_field__id=kwargs['fieldpk'])
                break
            except ObjectDoesNotExist:
                pass
        else:
            return HttpGone()

        kwargs['submissionset_id'] = kwargs.pop('pk')
        field_submission_resource = field_resource_type()
        detail = field_submission_resource.get_detail(request, **kwargs)
        return detail
Exemplo n.º 2
0
    def get_credit_detail(self, request, **kwargs):
        """Get the CreditSubmissionResource that matches the Credit
        where id = kwargs['credpk'] and the SubmissionSet where
        id = kwargs['pk'].
        """
        # Need to check CreditSubmissionResource.Meta.allowed_methods
        # explicitly because the URL fiddling done above
        # bypasses the usual check:
        if (request.method.lower()
                not in CreditSubmissionResource.Meta.allowed_methods):
            return HttpMethodNotAllowed()
        self.is_authenticated(request)
        credit_id = kwargs.pop('credpk')
        basic_bundle = self.build_bundle(request=request)
        try:
            submissionset = self.cached_obj_get(
                bundle=basic_bundle, **self.remove_api_resource_names(kwargs))
        except ObjectDoesNotExist:
            return HttpGone()
        except MultipleObjectsReturned:
            return HttpMultipleChoices(
                "More than one resource is found at this URI.")

        kwargs.pop('pk')
        kwargs['credpk'] = credit_id
        kwargs['submissionset'] = submissionset
        credit_submission_resource = CreditSubmissionResource()
        detail = credit_submission_resource.get_detail(request, **kwargs)
        return detail
Exemplo n.º 3
0
    def get_subcategory_detail(self, request, **kwargs):
        """Get the SubcategorySubmission for the Subcategory where
        id = kwargs['subcatpk']."""
        # Need to check SubcategorySubmissionResource.Meta.allowed_methods
        # explicitly because the URL fiddling done above
        # bypasses the usual check:
        if (request.method.lower()
                not in SubcategorySubmissionResource.Meta.allowed_methods):
            return HttpMethodNotAllowed()
        self.is_authenticated(request)
        subcategory_id = kwargs.pop('subcatpk')
        # Make sure the submission set is valid:
        basic_bundle = self.build_bundle(request=request)
        try:
            obj = self.cached_obj_get(bundle=basic_bundle,
                                      **self.remove_api_resource_names(kwargs))
        except ObjectDoesNotExist:
            return HttpGone()
        except MultipleObjectsReturned:
            return HttpMultipleChoices(
                "More than one resource is found at this URI.")

        kwargs['subcatpk'] = subcategory_id
        kwargs['submissionset'] = obj
        return SubcategorySubmissionResource().get_detail(request, **kwargs)
Exemplo n.º 4
0
 def test_various_statuses(self):
     created = HttpCreated(location='http://example.com/thingy/1/')
     self.assertEqual(created.status_code, 201)
     self.assertEqual(created['Location'], 'http://example.com/thingy/1/')
     # Regression.
     created_2 = HttpCreated()
     self.assertEqual(created_2.status_code, 201)
     self.assertEqual(created_2['Location'], '')
     accepted = HttpAccepted()
     self.assertEqual(accepted.status_code, 202)
     no_content = HttpNoContent()
     self.assertEqual(no_content.status_code, 204)
     see_other = HttpSeeOther()
     self.assertEqual(see_other.status_code, 303)
     not_modified = HttpNotModified()
     self.assertEqual(not_modified.status_code, 304)
     bad_request = HttpBadRequest()
     self.assertEqual(bad_request.status_code, 400)
     unauthorized = HttpUnauthorized()
     self.assertEqual(unauthorized.status_code, 401)
     not_found = HttpNotFound()
     self.assertEqual(not_found.status_code, 404)
     not_allowed = HttpMethodNotAllowed()
     self.assertEqual(not_allowed.status_code, 405)
     conflict = HttpConflict()
     self.assertEqual(conflict.status_code, 409)
     gone = HttpGone()
     self.assertEqual(gone.status_code, 410)
     toomanyrequests = HttpTooManyRequests()
     self.assertEqual(toomanyrequests.status_code, 429)
     not_implemented = HttpNotImplemented()
     self.assertEqual(not_implemented.status_code, 501)
Exemplo n.º 5
0
    def method_check(self, request, allowed=None):
        """
        Check for an OPTIONS request. If so return the Allow- headers
        """
        if allowed is None:
            allowed = []

        request_method = request.method.lower()
        allows = ','.join(map(lambda s: s.upper(), allowed))

        if request_method == 'options':
            response = HttpResponse(allows)
            response['Access-Control-Allow-Origin'] = '*'
            response[
                'Access-Control-Allow-Headers'] = 'Content-Type, Authorization, X-Requested-With'
            response['Access-Control-Allow-Methods'] = "GET, PUT, POST, PATCH"
            response['Allow'] = allows
            raise ImmediateHttpResponse(response=response)

        if not request_method in allowed:
            response = HttpMethodNotAllowed(allows)
            response['Allow'] = allows
            raise ImmediateHttpResponse(response=response)

        return request_method
Exemplo n.º 6
0
def event_receiver(request):
    """
    This actually does nothing, called for testing purpose only
    """
    if request.method == 'POST':
        data = json.loads(get_raw_post_data(request))

        logger.debug(u'got event notification (%s)' % data)

        digest = generate_hmac_digest(
            settings('HMAC_KEY', DEV_HMAC_KEY), '%s%s' % (
                data['event'],
                data['ctime'],
            ))

        logger.debug(u'hmac digest (%s)' % digest)

        if digest == data['hmac']:
            logger.debug(u'the notification has been processed normally.')

            return HttpResponse(u'OK')
        else:
            logger.exception(u'invalid notifcation detected.')

            return HttpBadRequest(u'Invalid notification detected')

    return HttpMethodNotAllowed()
Exemplo n.º 7
0
    def get_category_list(self, request, **kwargs):
        """Get a list of categories for the SubmissionSet with
        id = kwargs['pk']."""
        # Need to check CategorySubmissionResource.Meta.allowed_methods
        # explicitly because the URL fiddling done above
        # bypasses the usual check:
        if (request.method.lower()
                not in CategorySubmissionResource.Meta.allowed_methods):
            return HttpMethodNotAllowed()
        self.is_authenticated(request)
        basic_bundle = self.build_bundle(request=request)
        try:
            obj = self.cached_obj_get(bundle=basic_bundle,
                                      **self.remove_api_resource_names(kwargs))
        except ObjectDoesNotExist:
            return HttpGone()
        except MultipleObjectsReturned:
            return HttpMultipleChoices(
                "More than one resource is found at this URI.")

        category_submission_resource = CategorySubmissionResource()
        return category_submission_resource.get_list(request,
                                                     submissionset=obj.pk)
Exemplo n.º 8
0
 def not_supported(cls, msg):
     msg_json = '{"status": "error", "message": "%s" }' % msg
     return HttpMethodNotAllowed(msg_json, content_type='application/json')