def rate_movie(self, request, pk=None): if 'stars' in request.data: movie = Movie.objects.get(id=pk) stars = request.data['stars'] user = request.user try: rating = Rating.objects.get(user=user.id, movie=movie.id) rating.stars = stars rating.save() serializer = RatingSerializer(rating, many=False) response = { 'message': 'Rating updated', 'result': serializer.data } return Response(response, status.HTTP_200_OK) except: rating = Rating.objects.create(user=user, movie=movie, stars=stars) serializer = RatingSerializer(rating, many=False) response = { 'message': 'Rating created', 'result': serializer.data } return Response(response, status.HTTP_200_OK) else: response = {'message': 'You need to provide stars'} return Response(response, status.HTTP_400_BAD_REQUEST)
def rate_movie(self, request, pk=None): if 'stars' in request.data: movie = Movie.objects.get(pk=pk) stars = request.data['stars'] # user = User.objects.get(id=1) user = request.user try: rating = Rating.objects.create(movie=movie, user=user, stars=stars) serializer = RatingSerializer(rating, many=False) response = { 'message': 'Rating created!', 'result': serializer.data } return Response(response) except IntegrityError as exc: if 'UNIQUE' in str(exc): rating = Rating.objects.get(movie=movie, user=user) rating.stars = stars rating.save() serializer = RatingSerializer(rating, many=False) response = { 'message': 'Rating updated!', 'result': serializer.data } return Response(response) return Response({'message': 'You need to provide stars'})
def rate_movie(self,request,pk=None): if 'stars' in request.data: movie = Movie.objects.get(id=pk) stars =request.data['stars'] User= User.objects.get(id=1) print('movie title',movie.title) print('user',user.username) try: rating =Rating.objects.get(user=user.id,movie=movie.id) rating.stars =stars rating.save() serializer = RatingSerializer(rating,many=False) response={'message':'rating updated','result':serializer.data} return Response(response,status=status.HTTP_200_OK) except: Rating.objects.create(user=user,movie=movie,stars=stars) serializer = RatingSerializer(rating,many=False) response={'message':'rating updated','result':serializer.data} return Response(response,status=status.HTTP_200_OK) else: response = {"mes":"you need to provide stars"} return Response(response,status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, rating_pk): ratings = get_object_or_404(Ratings, pk=rating_pk) serializer = RatingSerializer(instance=ratings, data=request.data, partial=True) if serializer.is_valid(): serializer.save(rating_date=round(datetime.now().timestamp())) return Response(serializer.data, status=status.HTTP_202_ACCEPTED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def user_ratings(request, username): user = get_object_or_404(User, username=username) id = request.GET.get("movieId", None) if id: movie = get_object_or_404(Movie, id=id) rating = user.ratings.filter(movie=movie) serializer = RatingSerializer(rating, many=True) return Response(data=serializer.data, status=status.HTTP_202_ACCEPTED) serializer = RatingSerializer(user.ratings.all(), many=True) return Response(data=serializer.data, status=status.HTTP_202_ACCEPTED)
def ratings(request): if request.method == 'GET': ratings = Rating.objects.all() serializer = RatingSerializer(ratings, many=True) return Response(data=serializer.data, status=status.HTTP_200_OK) if request.method == 'DELETE': Rating.objects.all() Rating.delete() return Response(status=status.HTTP_200_OK) if request.method == 'POST': ratings = request.data.get('ratings', None) for rating in ratings: user = rating.get('user', None) movieid = rating.get('movieid', None) score = rating.get('score', None) timestamp = rating.get('timestamp', None) Rating(user=User.objects.get(pk=user), movie_id=Movie.objects.get(pk=movieid), rate=rate, timestamp=timestamp).save() return Response(status=status.HTTP_200_OK) #.save() ?
def rate(self, request, pk=None): # Update user's rating of a post vote = request.data['vote'] if vote is None: return Response(status=status.HTTP_400_BAD_REQUEST) vote = int(vote) if not (vote == Rating.LIKE_VALUE) \ and not (vote == Rating.DISLIKE_VALUE) \ and not (vote == Rating.DELETE_RATING_VALUE): return Response(status=status.HTTP_400_BAD_REQUEST) comment = get_object_or_404(Comment, id=pk) rating = comment.get_current_user_vote(request.user) if rating is None: rating = Rating.objects.create(content_object=comment, value=vote, user = request.user) else: if vote == Rating.DELETE_RATING_VALUE: rating.delete() return Response(status=status.HTTP_200_OK) rating.value = request.data['vote'] rating.save() serializer_rating = RatingSerializer(rating) return Response(serializer_rating.data, status=status.HTTP_200_OK)
def rating_list(request): if request.method == 'GET': raitings = Rating.objects.all() serializer = RatingSerializer(raitings, many=True) return Response(data=serializer.data, status=status.HTTP_200_OK) if request.method == 'POST': ratings = request.data.get('ratings', None) if ratings: for item in ratings: username = item.get('username', None) user = get_object_or_404(User, username=username) movie_id = item.get('movie_id', None) movie = get_object_or_404(Movie, id=movie_id) rating = item.get('rating', None) timestamp = item.get('timestamp', None) if not (movie_id and rating and timestamp): continue time = datetime.utcfromtimestamp( int(timestamp)).strftime("%Y-%m-%d %H:%M:%S") Rating.objects.create(user=user, movie_id=movie_id, rating=rating, timestamp=time) movie.total_rating += int(rating) movie.rating_count += 1 movie.avg_rating = round( movie.total_rating / movie.rating_count, 2) movie.save() return Response(status=status.HTTP_200_OK)
def get(self, request): ratings = Ratings.objects.all() site_pk = request.GET.get('site_pk') user_pk = request.GET.get('user_pk') if site_pk is not None: ratings = ratings.filter(apiSite__pk=site_pk) if user_pk is not None: ratings = ratings.filter(user__pk=user_pk) serializer = RatingSerializer(ratings, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def list(self, request, *args, **kargs): queryset = Food.objects.all() serializer = FoodSerializer(queryset, many=True, context={'request': request}) key_set = set(request.GET.keys()) print request.GET if "tops" in request.GET: tops = request.GET["tops"] #print foodDB.objects.get(rating=2).rating #g = foodDB.objects.get(rating=2) #g.rating = 0 #g.save() #print "Qwe" queryset = Food.objects.all() #serializer = FoodSerializer(queryset, many=True, context={'request':request}) topTen = [] all = [(item['name'], item['rating']) for item in serializer.data] all.sort(key=lambda x: x[1], reverse=True) if len(all) >= tops: all = all[0:tops - 1] for item in all: topTen.append(highestRating(item[0], item[1])) topTenSer = RatingSerializer(topTen, many=True) #print c.data return Response(topTenSer.data) if len(serializer.data) > 0: temp_set = key_set.intersection(serializer.data[0].keys()) temp = [] for food in serializer.data: flag = True for key in list(temp_set): if str(food[key]) != str(request.GET[key]): flag = False if flag: temp.append(food) return Response(temp) return Response({'status': status.HTTP_400_BAD_REQUEST})
def movie_ratings(request, movie_id): movie = get_object_or_404(Movie, id=movie_id) if request.method == 'GET': total = len(movie.ratings.all()) start = request.data.get('start', 0) limit = request.data.get('limit', 8) raitings = movie.ratings.all()[start:start + limit] serializer = RatingSerializer(raitings, many=True) return Response(data={ "total": total, "start": start, "limit": limit, "data": serializer.data }, status=status.HTTP_200_OK)
def ratings(request): if request.method == 'GET': movieid = request.GET.get('movieid', None) ratings = Rating.objects.all() if movieid: ratings = ratings.filter(movieid=movieid) serializer = RatingSerializer(ratings, many=True) return Response(data=serializer.data, status=status.HTTP_200_OK) if request.method == 'DELETE': rating = Rating.objects.all() rating.delete() return Response(status=status.HTTP_200_OK) if request.method == 'POST': ratings = request.data.get('ratings', None) for item in ratings: userid = item.get('userid', None) movieid = item.get('movieid', None) rating = item.get('rating', None) timestamp = item.get('timestamp', None) if not (userid and movieid and rating and timestamp): continue Rating(userid_id=userid, movieid_id=movieid, rating=rating, timestamp=timestamp).save() return Response(status=status.HTTP_200_OK)\ if request.method == 'PUT': rating = Rating.objects.get(id=request.data.get('id')) serializer = RatingSerializer(rating, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def rating_detail(request, rating_id): rating = get_object_or_404(Rating, id=rating_id) if request.method == 'GET': serializer = RatingSerializer(rating) return Response(data=serializer.data, status=status.HTTP_200_OK) if request.method == 'DELETE': rating.delete() return Response(status=status.HTTP_200_OK) if request.method == 'PUT': rating = request.data.get('rating', None) if rating: rating.rating = rating rating.timestamp = datetime.now() rating.save() return Response(status=status.HTTP_202_ACCEPTED) else: return Response(status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = RatingSerializer(request.data) if serializer.is_valid(): serializer.save(rating_date=round(datetime.now().timestamp())) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def rating(request): if request.method == "POST": user = request.data['user'] movie = request.data['movie'] if Rating.objects.filter(user=user, movie=movie): return Response(status=status.HTTP_400_BAD_REQUEST) else: serializer = RatingSerializer(data=request.data) if serializer.is_valid(): rating = serializer.save(user=User.objects.get(id=user), movie=Movie.objects.get(id=movie), rating_date=round( datetime.now().timestamp())) return Response(RatingSerializer(rating).data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) if request.method == "PATCH": user = request.data.get('user') movie = request.data.get('movie') if Rating.objects.filter(user=user, movie=movie): rating = get_object_or_404(Rating, user=user, movie=movie) serializer = RatingSerializer(rating, data=request.data, partial=True) if serializer.is_valid(): rating = serializer.save( rating_date=round(datetime.now().timestamp())) return Response(RatingSerializer(rating).data) else: serializer = RatingSerializer(data=request.data) if serializer.is_valid(): rating = serializer.save(user=User.objects.get(id=user), movie=Movie.objects.get(id=movie), rating_date=round( datetime.now().timestamp())) return Response(RatingSerializer(rating).data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def ratings(request): if request.method == 'GET': mode = request.GET.get('mode', None) movieid = request.GET.get('movieid', None) userid = request.GET.get('userid', None) ratings = Rating.objects.all() if mode == 'user': #영화를 본 유저 뽑기 if movieid: result = user_movieid(movieid) return Response(data=result, status=status.HTTP_200_OK) if userid: movieids = ratings.filter(userid__exact=userid) serializer = RatingSerializer(movieids, many=True) return Response(data=serializer.data, status=status.HTTP_200_OK) if mode == 'usermovie': #특정 유저가 본 영화 제목 뽑기 if userid: userid = int(userid) result = usermovie_sql(userid) return Response(data=result, status=status.HTTP_200_OK) if mode == 'mostview': #특정기준으로 많이 본 영화 리스트 뽑기 profiles = Profile.objects.all() select = request.GET.get('select', None) where = request.GET.get('where', None) result = mostview_sql(select, where) return Response(data=result, status=status.HTTP_200_OK) else: #평점 구하기 if movieid: ratings = ratings.filter(movieid__exact=movieid) query_set = ratings.aggregate(Avg('rating')) return Response(data=query_set, status=status.HTTP_200_OK) #전체 뽑아오기 else: serializer = RatingSerializer(ratings, many=True) return Response(data=serializer.data, status=status.HTTP_200_OK) if request.method == 'DELETE': userid = request.GET.get('userid', None) ratings = Rating.objects.all() if userid: ratings = ratings.filter(userid__icontains=userid) ratings.delete() return Response(status=status.HTTP_200_OK) if request.method == 'POST': ratings = request.data.get('ratings', None) params = request.data.get('params', None) if ratings: for cur_rating in ratings: userid = cur_rating.get('userid', None) userid = Profile.objects.get(id=userid) movieid = cur_rating.get('movieid', None) rating = cur_rating.get('rating', None) date = cur_rating.get('date', None) if not (userid and movieid and rating and date): continue print(userid, movieid, rating, date) Rating(userid=userid, movieid=movieid, rating=rating, date=date).save() if params: print(params) userid = params.get('userid', None) movieid = params.get('movieid', None) rating = params.get('rating', None) dt = datetime.now() date = dt.strftime('%Y-%m-%d %H:%M:%S') print(userid, movieid, rating, date) Rating(userid=userid, movieid=movieid, rating=rating, date=date).save() return Response(status=status.HTTP_200_OK)