How to use the djangorestframework.status 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 paulmillr / chieftain / api / resources.py View on Github external
def delete(self, request, auth, *args, **kwargs):
        """Deletes post."""
        try:
            post = self.model.objects.get(id=kwargs['id'])
        except self.model.DoesNotExist:
            raise ResponseException(status.NOT_FOUND)
        key = tools.key(request.GET['password'])
        if post.password == key:
            post.remove()
        elif is_mod(request, post.section_slug()):
            mod_delete_post(request, post)
            post.remove()
        else:
            return Response(status.FORBIDDEN, content={
                'detail': u'{0}{1}. {2}'.format(
                    _('Error on deleting post #'), post.pid,
                    _('Password mismatch')
                )
            })
        return Response(status.NO_CONTENT)
github encode / django-rest-framework / djangorestframework / mixins.py View on Github external
def metadata(self, request, *args, **kwargs):
        content = {
            'name': self.get_name(),
            'description': self.get_description(),
            'renders': self._rendered_media_types,
            'parses': self._parsed_media_types,
        }
        #  TODO: Add 'fields', from serializer info.
        # form = self.get_bound_form()
        # if form is not None:
        #     field_name_types = {}
        #     for name, field in form.fields.iteritems():
        #         field_name_types[name] = field.__class__.__name__
        #     content['fields'] = field_name_types
        return Response(content, status=status.HTTP_200_OK)
github devilry / devilry-django / src / devilry_qualifiesforexam / devilry_qualifiesforexam / rest / status.py View on Github external
def _permissioncheck(self, period):
        if self.request.user.is_superuser:
            return
        if not Period.where_is_admin(self.request.user).filter(id=period.id).exists():
            raise ErrorResponse(status=statuscodes.HTTP_403_FORBIDDEN)
github srri / OpenRelay / apps / server_talk / views.py View on Github external
if not created:
                sibling.ip_address = sibling_data['ip_address']
                sibling.port = sibling_data['port']
                sibling.save()
            local_node = LocalNode.get()
            local_node_info = {
                'ip_address': IPADDRESS,
                'port': PORT,
                'uuid': local_node.uuid,
                'name': local_node.name,
                'email': local_node.email,
                'comment': local_node.comment,
            }
            return local_node_info
        else:
            return Response(status.PARTIAL_CONTENT)
github paulmillr / chieftain / api / resources.py View on Github external
def post(self, request, auth, content, *args, **kwargs):
        try:
            instance = validate_post(request)
        except forms.ValidationError as e:
            return Response(status.BAD_REQUEST, {'detail': e})
        # django sends date with microseconds. We don't want it.
        instance.date = instance.date.strftime('%Y-%m-%d %H:%M:%S')
        url = 'http://127.0.0.1:8888/api/streamp/{0}'
        data = urllib.urlencode({'html': instance.html.encode('utf-8')})
        try:
            urllib2.urlopen(url.format(instance.thread.id), data)
        except urllib2.URLError:
            raise ResponseException(status.INTERNAL_SERVER_ERROR, {
                'detail': u'{0}: {1}'.format(
                    _('Server error'), _('can\'t refresh messages')
                )
            })
        self.model.allowed_fields.append('html')
        return Response(status.CREATED, instance)
github devilry / devilry-django / devilry / apps / subjectadmin / rest / group.py View on Github external
def check_permission(self, user):
        if len(self.view.args) != 1:
            raise ErrorResponse(status.HTTP_403_FORBIDDEN,
                                {'detail': 'The IsAssignmentAdmin permission checker requires an assignmentid.'})
        assignmentid = self.view.args[0]

        # TODO: Should update assignmentadmin_required to raise ErrorResponse
        # TODO: Only check this here, not in create and list in the DAO
        try:
            assignmentadmin_required(user, "i18n.permissiondenied", assignmentid)
        except ForbiddenError, e:
            raise ErrorResponse(status.HTTP_403_FORBIDDEN,
                                {'detail': 'Permission denied.'})
github devilry / devilry-django / devilry / devilry_student / rest / errors.py View on Github external
def __init__(self, errormsg):
        super(NotFoundError, self).__init__(status.HTTP_404_NOT_FOUND,
                                            {'detail': errormsg})
github encode / django-rest-framework / djangorestframework / mixins.py View on Github external
def update(self, request, *args, **kwargs):
        self.object = self.get_object()
        serializer = self.get_serializer(data=request.DATA, instance=self.object)
        if serializer.is_valid():
            self.object = serializer.object
            self.object.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
github tranvictor / truongnha / api / views.py View on Github external
'homeTeacher': homeroom_class.teacher_id
                            }
                            if homeroom_class.teacher_id:
                                temp['homeTeacher'] = homeroom_class.teacher_id.full_name()
                            classes.append(temp)
                    result['teaching_class'] = classes

                except Exception as e:
                    print e
                    raise e
            elif user_position > 3:
                #TODO: return necessary information for school's admins
                return Response(status=status.HTTP_404_NOT_FOUND)
            return Response(status=status.HTTP_200_OK, content= result)
        else:
            return Response(status.HTTP_401_UNAUTHORIZED)
github encode / django-rest-framework / djangorestframework / mixins.py View on Github external
def destroy(self, request, *args, **kwargs):
        self.object = self.get_object()
        self.object.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)