Esempio n. 1
0
    def download_file(cls, request, file_id):
        try:
            if not isinstance(file_id, uuid.UUID):
                file_id = uuid.UUID(file_id)
        except ValueError:
            raise api_exceptions.ValidationError400(
                {
                    'id': _('Not a valid UUID')
                }
            )
        file_object = Cache.get(
            str(f"file_id:{file_id}-user_id:{request.user.id}"),
        )

        if not file_object:
            try:
                file_object = File.objects.get(
                    file_id=file_id,
                    consumer=request.consumer,
                )
                Cache.set(
                    key=str(f"file_id:{file_id}-user_id:{request.user.id}"),
                    store_value=file_object,
                    expiry_time=settings.MAGPIE['CACHE_EXPIRY'],
                )
            except File.DoesNotExist:
                raise api_exceptions.NotFound404(
                    _('File does not exists or does not belongs to this user'),
                )
        path = file_object.file.path

        return (
            os.path.basename(path),
            os.path.dirname(path),
        )
Esempio n. 2
0
    def delete_file(cls, request, file_id):
        try:
            if not isinstance(file_id, uuid.UUID):
                file_id = uuid.UUID(file_id)
        except ValueError:
            raise api_exceptions.ValidationError400(
                {
                    'id': _('Not a valid UUID')
                }
            )

        try:
            file_object = File.objects.get(
                file_id=file_id,
                consumer=request.consumer,
            )
        except File.DoesNotExist:
            raise api_exceptions.NotFound404(
                _('File does not exists or does not belongs to this consumer'),
            )

        Cache.delete(
            key=str(f"file_id:{file_id}-user_id:{request.user.id}"),
        )

        file_object.file.delete()
        file_object.delete()

        return True
Esempio n. 3
0
 def revoke(cls, name):
     try:
         consumer = Consumer.objects.get(
             name=name,
         )
     except Consumer.DoesNotExist:
         raise api_exceptions.NotFound404(
             _("Token does not exits")
         )
     consumer.delete()
     return True
Esempio n. 4
0
    def get_one(cls, name):
        try:
            c = Consumer.objects.get(
                name=name,
            )
        except Consumer.DoesNotExist:
            raise api_exceptions.NotFound404(
                _("Consumer does not exits")
            )
        consumer = ConsumerSerializer(c)

        return consumer.data
Esempio n. 5
0
    def update(cls, name):
        try:
            consumer = Consumer.objects.get(
                name=name,
            )
        except Consumer.DoesNotExist:
            raise api_exceptions.NotFound404(
                _("Consumer does not exits")
            )
        consumer.token = Helper.generate_token()
        consumer.save()
        consumer = ConsumerSerializer(consumer)

        return consumer.data