def add_rating(request, username, movie_id): token = request.data.get("token", None) user = User.objects.get(username=username) if user.refresh_token != token and not user.is_staff: return Response(data={"error": "권한 없음"}, status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION) response = verify_token(token) if response.status_code != 200: response = refresh_token(token) if response.status_code == 200: new_token = json.loads(response.text)["token"] user.refresh_token = new_token if response and response.status_code == 200: score = request.data.get("rating", None) if score: movie = get_object_or_404(Movie, id=movie_id) rating = Rating.objects.filter(movie=movie, user=user) if not rating: rating = Rating() rating.user = user rating.movie = movie else: rating = rating[0] rating.rating = score rating.save() return Response(status=status.HTTP_202_ACCEPTED) return Response(status=status.HTTP_204_NO_CONTENT) user.refresh_token = "" user.save() return Response(data={"error": "token"}, status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION)
def movie_followers(request, movie_id): movie = get_object_or_404(Movie, id=movie_id) if request.method == 'GET': serializer = UserSerializer(movie.followers, many=True) return Response(data=serializer.data, status=status.HTTP_200_OK) if request.method == 'POST': token = request.data.get("token", None) req_name = request.data.get("username", None) if req_name: user = get_object_or_404(User, username=req_name) if token == user.refresh_token: response = verify_token(token) if response.status_code != 200: response = refresh_token(token) if response.status_code == 200: new_token = json.loads(response.text)["token"] user.refresh_token = new_token if response and response.status_code == 200: if user in movie.followers.all(): movie.followers.remove(user) else: movie.followers.add(user) movie.save() return Response(status=status.HTTP_202_ACCEPTED) user.refresh_token = "" user.save() return Response(status=status.HTTP_401_UNAUTHORIZED) return Response(status=status.HTTP_204_NO_CONTENT)
def related_movies(request): movie_id = request.data.get('movieId', None) username = request.data.get("username", None) token = request.data.get("token", None) if not movie_id: return Response(data={"error": "정보 없음"}, status=status.HTTP_400_BAD_REQUEST) movie = get_object_or_404(Movie, id=movie_id) if username: user = get_object_or_404(User, username=username) if not user.subscribe and not user.is_staff: return Response( data={"error": "권한 없음"}, status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION) if user.refresh_token == token: response = verify_token(token) if response.status_code != 200: response = refresh_token(token) if response.status_code == 200: new_token = json.loads(response.text)["token"] user.refresh_token = new_token if response and response.status_code == 200: related_movies = Movie.objects.filter( Q(cluster__exact=movie.cluster) & ~Q(ratings__user__username=username)).order_by( "-avg_rating")[:10] serializer = MovieSerializer(related_movies, many=True) return Response(data=serializer.data, status=status.HTTP_202_ACCEPTED) user.refresh_token = "" user.save() auth_logout(request) return Response(data={"error": "token"}, status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION) # 메인 페이지의 영화 별 유사한 영화 추천 genres = movie.genres_array query = Q() for genre in genres: query.add(Q(genres__icontains=genre), query.OR) query.add(Q(cluster__exact=movie.cluster), query.AND) query.add(~Q(id__exact=movie.id), query.AND) related_movies = Movie.objects.filter(query).order_by("-avg_rating")[:10] serializer = MovieSerializer(related_movies, many=True) return Response(data=serializer.data, status=status.HTTP_202_ACCEPTED)
def movie_detail(request, movie_id): movie = get_object_or_404(Movie, id=movie_id) if request.method == 'GET': serializer = MovieSerializer(movie) return Response(data=serializer.data, status=status.HTTP_200_OK) if request.method == 'DELETE': username = request.data.get("username", None) token = request.data.get("token", None) user = User.objects.get(username=username) if not user.is_staff: return Response( data={"error": "권한 없음"}, status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION) if user.refresh_token == token: response = verify_token(token) if response.status_code != 200: response = refresh_token(token) if response.status_code == 200: new_token = json.loads(response.text)["token"] user.refresh_token = new_token if response and response.status_code == 200: movie.delete() return Response(status=status.HTTP_202_ACCEPTED) user.refresh_token = "" user.save() return Response(data={"error": "token"}, status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION) if request.method == 'PUT': username = request.data.get("username", None) token = request.data.get("token", None) user = User.objects.get(username=username) if not user.is_staff: return Response( data={"error": "권한 없음"}, status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION) if user.refresh_token == token: response = verify_token(token) if response.status_code != 200: response = refresh_token(token) if response.status_code == 200: new_token = json.loads(response.text)["token"] user.refresh_token = new_token if response and response.status_code == 200: movie_info = request.data.get("movie", None) if movie_info: movie.title = movie_info["title"] genres = movie_info["genres"] genres = '|'.join(genres) movie.genres = genres movie.save() return Response(status=status.HTTP_202_ACCEPTED) return Response(data={"error": "입력값이 없습니다."}, status=status.HTTP_204_NO_CONTENT) user.refresh_token = "" user.save() return Response(data={"error": "token"}, status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION)