示例#1
0
	def create(self, request, *args, **kwargs):

		# emit signal
		trigger(self.context+'.create:request',request,*args,**kwargs)

		# get a copy of the request data
		data = request.data.copy()
		trigger(self.context+'.create:before',data)

		# create new instance
		serializer = self.get_serializer(data=data)
		serializer.is_valid(raise_exception=True)
		instance = self.perform_create(serializer)
		trigger(self.context+'.create:success',instance)
		
		# serialize instance data
		serializer = self.get_serializer(instance)
		serialized_data = serializer.data
		trigger(self.context+'.retrieve:serialize',serialized_data)		

		# return a response
		headers = self.get_success_headers(serialized_data)
		response = Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
		trigger(self.context+'.create:response',response)
		return response
示例#2
0
    def create(self, request, *args, **kwargs):
        """ Perform file upload.
            
            Accepts multi-part form data to handle file uploads.
        """
        # trigger(self.context+'.create:request',request,*args,**kwargs)

        # data = request.data.copy()
        # trigger(self.context+'.create:before',data)

        # check for the existence of the file key in the request
        if not 'file' in request.FILES:

            # throw error if no file field supplied
            message = {
                'status': 'fail',
                'message': 'No field matching name "file" is present.'
            }
            response = Response({'status': 'fail'},
                                status=status.HTTP_400_BAD_REQUEST)
            trigger(self.context + '.create:fail', response)
            return response

        # store the file to the file system
        file = request.FILES['file']

        fs = FileSystemStorage()
        filename = fs.save(file.name, file)
        uploaded_file_url = fs.url(filename)

        # store the relative path to the file on disk
        request.data['path'] = filename

        # call the parent create method
        return super().create(request, *args, **kwargs)
示例#3
0
    def retrieve(self, request, *args, **kwargs):
        trigger(self.context + '.retrieve:request', request, *args, **kwargs)

        trigger(self.context + '.retrieve:before', kwargs['pk'])

        instance = self.get_object()
        trigger(self.context + '.retrieve:success', instance)

        serializer = self.get_serializer(instance)
        data = serializer.data
        trigger(self.context + '.retrieve:serialize', data)

        response = Response(data)
        trigger(self.context + '.retrieve:response', response)
        return response
示例#4
0
    def test_destroy_callback(self):

        scope = {'counter': 0}

        def callback(context, modifier):
            self.assertEqual(o.context, 'event.context:after')
            scope['counter'] = scope['counter'] + modifier

        o = on('event.context:after', callback)

        trigger('event.context:after', 1)
        self.assertEqual(scope['counter'], 1)

        o.destroy()
        trigger('event.context:after', 1)
        self.assertEqual(scope['counter'], 1)
示例#5
0
    def destroy(self, request, *args, **kwargs):
        trigger(self.context + '.destroy:request', request, *args, **kwargs)

        instance = self.get_object()
        trigger(self.context + '.destroy:before', instance)

        self.perform_destroy(instance)
        trigger(self.context + '.destroy:success', instance)

        response = Response(status=status.HTTP_204_NO_CONTENT)
        trigger(self.context + '.destroy:response', response)

        return response
示例#6
0
    def retrieve(self, request, *args, **kwargs):

        print('GET FILE')

        kwargs['pk'] = 1

        trigger(self.context + '.download:request', request, *args, **kwargs)

        trigger(self.context + '.download:before', kwargs['pk'])

        instance = File.objects.get(pk=kwargs['pk'])
        mimetype = mimetypes.guess_type(instance.full_path())

        # open the file
        filepointer = open(instance.full_path(), "r")

        # stream file to http response
        response = HttpResponse(filepointer, content_type=mimetype)
        response['Content-Disposition'] = 'attachment; filename={}'.format(
            instance.name)

        return response
示例#7
0
    def create(self, request, *args, **kwargs):
        trigger(self.context + '.create:request', request, *args, **kwargs)

        data = request.data
        trigger(self.context + '.create:before', data)

        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        instance = self.perform_create(serializer)
        trigger(self.context + '.create:success', instance)

        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers=headers)
示例#8
0
    def update(self, request, *args, **kwargs):
        trigger(self.context + '.update:request', request, *args, **kwargs)

        partial = kwargs.pop('partial', False)
        instance = self.get_object()
        trigger(self.context + '.update:retrieve', instance)

        data = request.data
        trigger(self.context + '.update:before', data)

        serializer = self.get_serializer(instance,
                                         data=request.data,
                                         partial=partial)
        serializer.is_valid(raise_exception=True)
        instance = self.perform_update(serializer)
        trigger(self.context + '.update:success', instance)

        data = serializer.data
        trigger(self.context + '.update:serialize', data)

        response = Response(data)
        trigger(self.context + '.update:response', response)
        return response
示例#9
0
    def create(self, request, *args, **kwargs):
        """ Perform account registration
            
        Accepts an email address and password as post data and creates a new user account.
        Sends an email to the user to activate the account before permitting login.
        """

        # create the user
        trigger(self.context + '.create:request', request, *args, **kwargs)

        data = request.data.copy()
        trigger(self.context + '.create:before', data)

        # check for existing account with given email
        try:
            duplicate = User.objects.get(email=request.data.get('email'))
            if duplicate:

                response = Response(
                    {
                        'status': 'Fail',
                        'message': 'This email is already in use.'
                    },
                    status=status.HTTP_409_CONFLICT)

                trigger(self.context + '.create:fail', response)
                return response

                return
        except:
            pass

        # set default user status
        data['status'] = 0 if AUTHENTICATION['REQUIRE_ACTIVATION'] else 1
        serializer = self.serializer_class(data=data)

        # validate supplied data
        if serializer.is_valid():

            # create the user account
            instance = serializer.save()
            trigger(self.context + '.create:success', instance)

            # if account requires activation
            if AUTHENTICATION['REQUIRE_ACTIVATION']:

                # create and send the activation link
                self.dispatch_activation_email(instance, request)

            # remove password data from response
            serializer = self.serializer_class(instance)
            serialized_data = serializer.data
            trigger(self.context + '.create:serialize', serialized_data,
                    instance)

            response = Response(serialized_data,
                                status=status.HTTP_201_CREATED)
            trigger(self.context + '.create:response', response)
            return response

        else:
            # data not valid, return bad request response
            response = Response(
                {
                    'status': 'Fail',
                    'message':
                    'Account could not be created with received data.'
                },
                status=status.HTTP_400_BAD_REQUEST)
            trigger(self.context + '.create:fail', response)
            return response