How to use the djangorestframework.response.ErrorResponse function in djangorestframework

To help you get started, we’ve selected a few djangorestframework examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github devilry / devilry-django / devilry / devilry_student / rest / add_delivery.py View on Github external
def _raise_error_response(self, statuscode, message):
        if self.CONTENT['respond_with_200_status_on_error']:
            statuscode = 200 # This is required for IE
        raise ErrorResponse(statuscode, {'detail': unicode(message),
                                         'success': False})
github devilry / devilry-django / devilry / devilry_student / rest / errors.py View on Github external
from djangorestframework.response import ErrorResponse
from djangorestframework import status


class NotFoundError(ErrorResponse):
    """
    Raised to signal that an item was not found
    """
    def __init__(self, errormsg):
        super(NotFoundError, self).__init__(status.HTTP_404_NOT_FOUND,
                                            {'detail': errormsg})

class BadRequestError(ErrorResponse):
    def __init__(self, errormsg):
        super(BadRequestError, self).__init__(status.HTTP_400_BAD_REQUEST,
                                              {'detail': errormsg})
github boosh / pwlocker / pwlocker / apps / api / views.py View on Github external
def put(self, request, *args, **kwargs):
        """
        Only allow the creating user to modify an instance.
        """
        model = self.resource.model
        query_kwargs = self.get_query_kwargs(request, *args, **kwargs)

        try:
            self.model_instance = self.get_instance(**query_kwargs)

            if self.model_instance.created_by == self.user:
                return super(RestrictPasswordToUserMixin, self).put(request, *args, **kwargs)
        except model.DoesNotExist:
            pass
        
        raise ErrorResponse(status.HTTP_401_UNAUTHORIZED, None, {})
github paulmillr / chieftain / api1 / views.py View on Github external
def mod_delete_post(request, post):
    if request.GET.get("ban_ip"):
        reason = request.GET.get("ban_reason")
        if not reason:
            raise ErrorResponse(status.BAD_REQUEST,
                {"detail": _("You need to enter ban reason")}
            )
        ip = DeniedIP(ip=post.ip, reason=r, by=request.user)
        ip.save()

    if request.GET.get("delete_all"):
        posts = post.section().posts().filter(ip=post.ip)
        op = posts.filter(is_op_post=True).values("pid", "thread")
        t = models.Thread.objects.filter(id__in=[i["thread"] for i in op])
        t.update(is_deleted=True)

        for p in posts:
            p.remove()
github devilry / devilry-django / src / devilry_subjectadmin / devilry_subjectadmin / rest / mixins.py View on Github external
detail = {}

            # Add any non-field errors
            if bound_form.non_field_errors():
                detail[u'errors'] = bound_form.non_field_errors()

            # Add standard field errors
            field_errors = dict((key, map(unicode, val))
                                for (key, val) in bound_form.errors.iteritems()
                                if not key.startswith('__'))

            if field_errors:
                detail[u'field_errors'] = field_errors
            from djangorestframework import status
            from djangorestframework.response import ErrorResponse
            raise ErrorResponse(status.HTTP_400_BAD_REQUEST, detail)
github devilry / devilry-django / src / devilry_usersearch / devilry_usersearch / rest.py View on Github external
def check_permission(self, user):
        if not user_is_admin_or_superadmin(user):
            raise ErrorResponse(status.HTTP_403_FORBIDDEN,
                                {'detail': 'Only administrators have permission to query the user database.'})
github devilry / devilry-django / src / devilry_qualifiesforexam / devilry_qualifiesforexam / rest / status.py View on Github external
def _get_instance(self, id):
        try:
            qry = Period.objects.select_related('parentnode')
            period = qry.get(id=id)
        except Period.DoesNotExist:
            raise ErrorResponse(statuscodes.HTTP_404_NOT_FOUND,
                {'detail': 'The period with ID {id} does not exist'.format(id=id)})

        self._permissioncheck(period)

        statusQry = period.qualifiedforexams_status.all()
        if statusQry.count() == 0:
            raise ErrorResponse(statuscodes.HTTP_404_NOT_FOUND,
                {'detail': 'The period has no statuses'})
        statusQry = statusQry.select_related(
                'period', 'user', 'user__devilryuserprofile')

        grouper = GroupsGroupedByRelatedStudentAndAssignment(period)
        out = self._serialize_period(period)
        out.update({
            'perioddata': grouper.serialize(),
            'statuses': [self._serialize_status(status) for status in statusQry]
github boosh / pwlocker / pwlocker / apps / api / views.py View on Github external
def delete(self, request, *args, **kwargs):
        """
        Deletes shares from Passwords when a PasswordContact is deleted
        """
        model = self.resource.model
        query_kwargs = self.get_query_kwargs(request, *args, **kwargs)

        try:
            instance = self.get_instance(**query_kwargs)
        except model.DoesNotExist:
            raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {})

        # remove any shares from any passwords shared with this contact
        password_contacts = PasswordContact.objects.filter(from_user=self.user,
            to_user=instance.to_user)

        for password_contact in password_contacts:
            password_contact.delete()

        instance.delete()
        return