parent
e4777fc696
commit
53181c750a
|
@ -0,0 +1 @@
|
|||
GET localhost:8000/api/files?b10a53f-5bd7-4988-a6c2-564edbcb7dfb
|
|
@ -36,6 +36,8 @@ DEBUG = env('DEBUG', default=False)
|
|||
|
||||
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', default=['*'])
|
||||
|
||||
REDIS_HOST = env.str('REDIS_HOST', default='localhost')
|
||||
REDIS_PORT = env.str('REDIS_PORT', default='6379')
|
||||
|
||||
# Application definition
|
||||
|
||||
|
@ -65,16 +67,16 @@ MIDDLEWARE = [
|
|||
|
||||
ROOT_URLCONF = 'safeshare.urls'
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_PERMISSION_CLASSES': (
|
||||
'rest_framework.permissions.IsAuthenticated',
|
||||
),
|
||||
'DEFAULT_AUTHENTICATION_CLASSES': (
|
||||
'rest_framework_simplejwt.authentication.JWTAuthentication',
|
||||
'rest_framework.authentication.SessionAuthentication',
|
||||
'rest_framework.authentication.BasicAuthentication',
|
||||
),
|
||||
}
|
||||
# REST_FRAMEWORK = {
|
||||
# 'DEFAULT_PERMISSION_CLASSES': (
|
||||
# 'rest_framework.permissions.IsAuthenticated',
|
||||
# ),
|
||||
# 'DEFAULT_AUTHENTICATION_CLASSES': (
|
||||
# 'rest_framework_simplejwt.authentication.JWTAuthentication',
|
||||
# 'rest_framework.authentication.SessionAuthentication',
|
||||
# 'rest_framework.authentication.BasicAuthentication',
|
||||
# ),
|
||||
# }
|
||||
SIMPLE_JWT = {
|
||||
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=10),
|
||||
'REFRESH_TOKEN_LIFETIME': timedelta(days=7),
|
||||
|
|
|
@ -15,8 +15,9 @@ Including another URLconf
|
|||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
from django.urls import path, include
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('api/', include('safeshare_app.urls')),
|
||||
]
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
# Generated by Django 4.2.5 on 2023-10-23 03:37
|
||||
|
||||
from django.db import migrations, models
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='File',
|
||||
fields=[
|
||||
('key', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('value', models.BinaryField()),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -1,3 +0,0 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
|
@ -0,0 +1,25 @@
|
|||
# import uuid
|
||||
#
|
||||
# from django.db import models
|
||||
# from django.core.cache import cache
|
||||
#
|
||||
#
|
||||
# class File(models.Model):
|
||||
# key = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
# value = models.BinaryField()
|
||||
#
|
||||
# REQUIRED_FIELDS = ['value']
|
||||
#
|
||||
# def __str__(self):
|
||||
# return self.key
|
||||
#
|
||||
# @classmethod
|
||||
# def get_or_set(cls, key, value=None, timeout=None):
|
||||
# data = cache.get(str(key))
|
||||
#
|
||||
# if data is None:
|
||||
# data = value
|
||||
#
|
||||
# cache.set(str(key), data, timeout=timeout)
|
||||
#
|
||||
# return data
|
|
@ -0,0 +1,7 @@
|
|||
# from rest_framework import serializers
|
||||
# from safeshare_app.models import File
|
||||
#
|
||||
# class FileSerializer(serializers.ModelSerializer):
|
||||
# class Meta:
|
||||
# model = File
|
||||
# fields = ('key', 'value')
|
|
@ -1,3 +0,0 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
|
@ -0,0 +1,11 @@
|
|||
from django.urls import path, include
|
||||
from rest_framework import routers
|
||||
from safeshare_app.views import manage_items, manage_item
|
||||
|
||||
# router = routers.SimpleRouter()
|
||||
# router.register(r'files', FileView, basename='file')
|
||||
|
||||
urlpatterns = [
|
||||
path('files/', manage_items, name="manage_items"),
|
||||
path('files/<str:key>/', manage_item, name="manage_item"),
|
||||
]
|
|
@ -1,3 +0,0 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
|
@ -0,0 +1 @@
|
|||
from .file import *
|
|
@ -0,0 +1,120 @@
|
|||
import json
|
||||
from django.conf import settings
|
||||
import redis
|
||||
import base64
|
||||
import uuid
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
|
||||
# from safeshare_app.models.file import File
|
||||
# from safeshare_app.serializers.file import FileSerializer
|
||||
|
||||
redis_instance = redis.StrictRedis(host=settings.REDIS_HOST,
|
||||
port=settings.REDIS_PORT, db=0)
|
||||
|
||||
|
||||
@api_view(['GET', 'POST'])
|
||||
def manage_items(request, *args, **kwargs):
|
||||
if request.method == 'GET':
|
||||
items = {}
|
||||
count = 0
|
||||
for key in redis_instance.keys("*"):
|
||||
items[key.decode("utf-8")] = redis_instance.get(key)
|
||||
count += 1
|
||||
response = {
|
||||
'count': count,
|
||||
'msg': f"Found {count} items.",
|
||||
'items': items
|
||||
}
|
||||
return Response(response, status=200)
|
||||
if request.method == 'POST':
|
||||
item = json.loads(request.body)
|
||||
key = uuid.uuid4().hex
|
||||
value = item['value']
|
||||
|
||||
# Modify value to binary object (base64)
|
||||
value = base64.b64encode(value.encode('utf-8'))
|
||||
redis_instance.set(key, value)
|
||||
response = {
|
||||
'msg': f"{key} successfully set to {value}"
|
||||
}
|
||||
return Response(response, 201)
|
||||
|
||||
|
||||
@api_view(['GET', 'PUT', 'DELETE'])
|
||||
def manage_item(request, *args, **kwargs):
|
||||
if request.method == 'GET':
|
||||
if kwargs['key']:
|
||||
value = redis_instance.get(kwargs['key'])
|
||||
if value:
|
||||
response = {
|
||||
'key': kwargs['key'],
|
||||
'value': value,
|
||||
'msg': 'success'
|
||||
}
|
||||
return Response(response, status=200)
|
||||
else:
|
||||
response = {
|
||||
'key': kwargs['key'],
|
||||
'value': None,
|
||||
'msg': 'Not found'
|
||||
}
|
||||
return Response(response, status=404)
|
||||
elif request.method == 'PUT':
|
||||
if kwargs['key']:
|
||||
request_data = json.loads(request.body)
|
||||
new_value = request_data['new_value']
|
||||
value = redis_instance.get(kwargs['key'])
|
||||
if value:
|
||||
redis_instance.set(kwargs['key'], new_value)
|
||||
response = {
|
||||
'key': kwargs['key'],
|
||||
'value': value,
|
||||
'msg': f"Successfully updated {kwargs['key']}"
|
||||
}
|
||||
return Response(response, status=200)
|
||||
else:
|
||||
response = {
|
||||
'key': kwargs['key'],
|
||||
'value': None,
|
||||
'msg': 'Not found'
|
||||
}
|
||||
return Response(response, status=404)
|
||||
|
||||
elif request.method == 'DELETE':
|
||||
if kwargs['key']:
|
||||
result = redis_instance.delete(kwargs['key'])
|
||||
if result == 1:
|
||||
response = {
|
||||
'msg': f"{kwargs['key']} successfully deleted"
|
||||
}
|
||||
return Response(response, status=404)
|
||||
else:
|
||||
response = {
|
||||
'key': kwargs['key'],
|
||||
'value': None,
|
||||
'msg': 'Not found'
|
||||
}
|
||||
return Response(response, status=404)
|
||||
|
||||
# class FileView(viewsets.ModelViewSet):
|
||||
# queryset = File.objects.all()
|
||||
# serializer_class = FileSerializer
|
||||
# permission_classes = ()
|
||||
#
|
||||
# def get_queryset(self):
|
||||
# # Only allow GET with a key
|
||||
# key = self.request.query_params.get('key', None)
|
||||
# if key is not None:
|
||||
# # Remove / from end of key
|
||||
# if key[-1] == '/':
|
||||
# key = key[:-1]
|
||||
#
|
||||
# print(key)
|
||||
# data = self.queryset.filter(key=key)
|
||||
# return data
|
||||
#
|
||||
# else:
|
||||
# # Return nothing if no key is provided
|
||||
# return File.objects.none()
|
Loading…
Reference in New Issue