def removeItem(request, list_id): if request.method == 'POST': json_data = json.loads(request.body) # Prevent empty movie ID movie_id = json_data["movie_id"] if not movie_id: return Response.badRequest(message="Movie ID is Required!") # Prevent delete others movie user_id = getUserID(request) find_movie = ListMovies.objects.filter(user_id=user_id, list_id=list_id, movie_id=movie_id).first() if not find_movie: return Response.badRequest(message="Selected movie is not found.") ref_id = find_movie.id find_movie.delete() log.logger(user_id=user_id, ref='list_item', ref_id=ref_id, action='remove') return Response.ok(message="Movie deleted from the list.") else: return Response.badRequest()
def updateList(request, list_id): user_id = getUserID(request) # Prevent update others list find_list = Lists.objects.filter(user_id=user_id, id=list_id).first() if not find_list: return Response.badRequest(message="Could not find the list.") json_data = json.loads(request.body) # Prevent empty list name if not json_data['name']: return Response.badRequest(message="List name is required!") find_list.name = json_data['name'] find_list.note = json_data['note'] find_list.updated_at = timezone.now() find_list.save() log.logger(user_id=user_id, ref='list', ref_id=find_list.id, action='update') return Response.ok(message="List successfully updated.")
def retrieveList(request): user_id = getUserID(request) # Retrieve all list belongs by user find_list = Lists.objects.filter(user_id=user_id).all() results = transformer.transform(find_list) return Response.ok(values=results)
def popular(request): if request.method == 'GET': movie = Movie() popular = movie.popular() results = transformer.transform(popular) return Response.ok(results) else: return Response.badRequest(message="Invalid request")
def top_rated(request): if request.method == 'GET': movie = Movie() latest = movie.top_rated() results = transformer.transform(latest) return Response.ok(results) else: return Response.badRequest(message="invalid request")
def clearItem(request, list_id): if request.method == 'POST': # Clear movies belongs by list find_movie = ListMovies.objects.filter(list_id=list_id).delete() user_id = getUserID(request) log.logger(user_id=user_id, ref='list', ref_id=list_id, action='clear') return Response.ok(message="List successfully cleared.") else: return Response.badRequest()
def deleteList(request, list_id): user_id = getUserID(request) # Prevent delete others list find_list = Lists.objects.filter(user_id=user_id, id=list_id).first() if not find_list: return Response.badRequest(message="Could not find the list.") # Clear movie belonging by the list delete_movie = ListMovies.objects.filter(list_id=list_id).delete() # Delete list find_list.delete() log.logger(user_id=user_id, ref='list', ref_id=list_id, action='delete') return Response.ok(message="List successfully deleted.")
def register(request): if request.method == 'POST': json_data = json.loads(request.body) user = Users() user.name = json_data['name'] user.email = json_data['email'] user.password = make_password(password=json_data['password']) try: user.save() except IntegrityError: return Response.badRequest(message="Failed to register!") return Response.ok(values=transformer.singleTransform(user), message="Successfully Registered!") else: return Response.badRequest(message="Invalid request.")
def auth(request): if request.method == 'POST': json_data = json.loads(request.body) email = json_data['email'] user = Users.objects.filter(email=email).first() if not user: return Response.badRequest(message="Email is not registered!") if not check_password(json_data['password'], user.password): return Response.badRequest(message="Wrong password!") user = transformer.singleTransform(user) jwt = JWTAuth() user['token'] = jwt.encode({"id": user['id']}) return Response.ok(values=user, message="Login successfully!") else: return Response.badRequest(message="Invalid Request.")
def addList(request): json_data = json.loads(request.body) # Prevent empty list name if not json_data["name"]: return Response.badRequest(message="List name is required!") user_id = getUserID(request) # Insert new list insert_list = Lists() insert_list.user_id = user_id insert_list.name = json_data["name"] insert_list.note = json_data["note"] insert_list.save() log.logger(user_id=user_id, ref='list', ref_id=insert_list.id, action='add') return Response.ok(values=transformer.singleTransform(insert_list), message="List successfully added.")
def addItem(request, list_id): if request.method == 'POST': json_data = json.loads(request.body) movie_id = json_data["movie_id"] # Validate to make sure list is exists find_list = Lists.objects.filter(id=list_id).first() if not find_list: return Response.badRequest(message="Could not found the list.") # Validate to prevent duplicated movie in list find_movie = ListMovies.objects.filter(list_id=list_id, movie_id=movie_id).first() if find_movie: return Response.badRequest(message="Movie is already added.") user_id = getUserID(request) # Insert new movie into list model = ListMovies() model.user_id = user_id model.list_id = list_id model.movie_id = movie_id model.title = json_data["title"] model.poster_path = json_data["poster_path"] model.save() # log log.logger(user_id=user_id, ref='list_item', ref_id=model.id, action='add') return Response.ok(values=transformer.singleMovie(model), message="Movie successfully added.") else: return Response.badRequest()