django キャッシュ設定
- 公開日:
- 更新日:
- 文字数:4840文字
from django.shortcuts import render,get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status,viewsets
from django.core.cache import cache
Create your views here.
from .serializers import ProjectSerializer,ProjectMediaStatusSerializer
from .models import Project,ProjectMediaStatus
import json
from django.http import JsonResponse
from django.utils.timezone import make_aware
from datetime import datetime
import sys
API経由から一覧表示
class ProjectListView(viewsets.ModelViewSet):
queryset = Project.objects.filter(status=1).order_by('-created_at')[:200]
queryset = Project.objects.order_by('-created_at')[:10]
serializer_class = ProjectSerializer
def list(self, request, *args, **kwargs):
# キャッシュキーの設定
cache_key = 'project_list'
# キャッシュからデータを取得
cached_data = cache.get(cache_key)
if cached_data is not None:
# キャッシュされたデータがあればそれを返す
return Response(cached_data)
# キャッシュされていない場合は、データを取得してキャッシュに保存
queryset = self.filter_queryset(self.get_queryset())
serializer = self.get_serializer(queryset, many=True)
data = serializer.data
cache.set(cache_key, data, timeout=3600) # 12時間
return Response(data)
プロジェクトのファイルのステータスを変更
class MediaStatusView(APIView):
def post(self, request):
media_id = request.data.get('media_id', None)
project_id = request.data.get('project_id', None)
status_id = request.data.get('status', None)
try:
instance = ProjectMediaStatus.objects.get(project=project_id,media=media_id)
instance.status = status_id
instance.save()
except ProjectMediaStatus.DoesNotExist:
instance = ProjectMediaStatus.objects.create(
status=status_id,
media_id=media_id,
project_id=project_id
)
serializer = ProjectMediaStatusSerializer(instance)
return Response(serializer.data, status=status.HTTP_201_CREATED)
プロジェクトのファイルのステータスを取得
class ProjectFileStatsulView(APIView):
def get(self, request, pk):
try:
project_status = ProjectMediaStatus.objects.filter(project=pk,status=99)[:100]
except ProjectMediaStatus.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = ProjectMediaStatusSerializer(project_status,many=True)
return Response(serializer.data, status=status.HTTP_201_CREATED)
詳細情報を取得
class ProjectDetailView(APIView):
def get(self, request, pk):
project = Project.objects.get(pk=pk)
serializer = ProjectSerializer(project)
# serializer_1 = ProjectMediaStatusSerializer(project_data)
return Response(serializer.data)
class ProjectChangeStatusView(APIView):
def get(self, request, pk):
instance = Project.objects.get(pk=pk)
instance.status = 99
instance.save()
return Response({'message': 'Delete successfully'}, status=status.HTTP_200_OK)
class ProjectDeleteView(APIView):
def get(self, request):
instance = Project.objects.filter(status=99)
Project.objects.filter(status=99).delete()
# 取得したデータを削除
#if request.method == 'DELETE':
# instance.delete()
#return Response(serializer.data,status=status.HTTP_200_OK)
return Response({'message': '削除しました。'}, status=status.HTTP_200_OK)
class ProjectInsertUpdateView(APIView):
def post(self, request):
json_data_str = request.data.get('data', None)
print(json_data_str)
json_object_arr = json.loads(json_data_str)
#ひとつだけ登録
instance = Project.objects.get(id=json_object_arr['id'])
instance.name = json_object_arr['name'] # 新しい説明を設定
instance.desc = ''
if 'desc' in json_object_arr:
instance.desc = json_object_arr['desc'] # 新しい説明を設定
news_files = request.data.get('news_files', None)
parsed_list = []
if news_files:
parsed_list = list(set(map(int, news_files.split(','))))
instance.file.set(parsed_list)
instance.save()
return Response({'message': 'Updated successfully'}, status=status.HTTP_200_OK)
class ProjectInsertView(APIView):
def post(self, request):
json_data_str = request.data.get('data', None)
print(json_data_str)
json_object_arr = json.loads(json_data_str)
name = json_object_arr['name'] # 新しい説明を設定
desc = '' # 新しい説明を設定
if 'desc' in json_object_arr:
desc = json_object_arr['desc'] # 新しい説明を設定
user_id = request.data.get('user_id', None)
news_files = request.data.get('news_files', None)
parsed_list = []
if news_files:
parsed_list = list(set(map(int, news_files.split(','))))
updated_at = datetime.now()
updated_at = make_aware(updated_at)
mymodel = Project.objects.create(
name=name,
desc=desc,
user_id = user_id,
updated_at=updated_at
)
# ManyToManyFieldに新しいタグを追加
mymodel.file.set(parsed_list)
serializer = ProjectSerializer(mymodel)
# キャッシュを削除
cache_key = 'project_list'
cache.delete(cache_key)
return Response(serializer.data, status=status.HTTP_201_CREATED)