views.py
3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import mimetypes
import json
import os
import boto3
from django.contrib.auth.models import User
from django.core import serializers
from django.views.decorators.csrf import csrf_exempt
from rest_framework import viewsets
from rest_framework import permissions
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.parsers import JSONParser
from rest_framework.permissions import IsAuthenticated, AllowAny
from api.models import Item, SharedItem
from api.serializers import UserSerializer,GroupSerializer,ItemSerializer,SharedItemSerializer
from rest_framework import generics
from rest_framework import status
from annoying.functions import get_object_or_None
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated]
class ItemViewSet(viewsets.ModelViewSet):
queryset = Item.objects.all()
serializer_class = ItemSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
#IsOwnerOrReadOnly
]
# url: items/search
@action(methods=['GET'], detail=False, permission_classes=[AllowAny], url_path='search', url_name='search')
def search(self, request):
if request.method == 'GET':
keyword = request.GET.get('keyword', '')
item_list = Item.objects.filter(name__icontains = keyword)
data = serializers.serialize("json", item_list)
return Response({'data': {'list' : data}}, status=status.HTTP_200_OK)
class SharedItemViewSet(viewsets.ModelViewSet):
queryset = SharedItem.objects.all()
serializer_class = SharedItemSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly, permissions.AllowAny,
# IsOwnerOrReadOnly
]
# url: http://localhost:8000/items/1/share/
# 마지막 slash도 써주어야함
@csrf_exempt
@action(methods=['POST'], detail=True, permission_classes=[AllowAny], url_path='share', url_name='share')
def share(self, request, pk):
if request.method == 'POST':
password = request.POST.get('password', '')
expires = request.POST.get('expires', '')
sharedfile = get_object_or_None(SharedItem, item_id=pk)
if sharedfile != None:
# 서버는 정상이나 이미 공유객체로 등록된 파일임
return Response({'Message': 'This file is already shared'}, status=status.HTTP_200_OK)
sharedfile = SharedItem(item_id =pk, password=password, expires = expires)
sharedfile.save()
sharedfile = SharedItem.objects.get(item_id = pk)
# sf = serializers.serialize("json", sharedfile)
item = Item.objects.filter(item_id = pk)
item_json = serializers.serialize("json", item)
# data = serializers.serialize("json", item_list)
return Response({"shared": sharedfile.created_time , 'data': item_json}, status=status.HTTP_200_OK)