def ReviewsForUser(User): # Getting the interests for the user interests=InterestController.GetInterests(User=User) # Getting the subscription for the user subscriptions=SubscriptionController.GetSubscriptions(User=User) # Making an empty list of reviews reviews=[] # Appending the reviews added by the user to the list # reviews.extend(list(ReviewController.GetReviewsByUser(User=User))) # Appending the reviews by the interest to the list for interest in interests: reviews.extend(list(ReviewController.GetReviewsByTopic(Topic=interest.topic))) # Appending the reviews by the subscriptions to the list for subscription in subscriptions: reviews.extend(list(ReviewController.ReviewsByUser(User=subscription.reviewer))) # Making a dictionary of reviews with ID as a key dictionary={} # Looping through each object in the review list for obj in reviews: dictionary[obj.pk]=obj # Returning unique reviews from the dictionary return dictionary.values()
def get(self, request, *args, **kwargs): # Checking if the user is authenticated if request.user.is_authenticated(): # Getting the interests for the user interest = InterestController.GetInterests(User=request.user) # Checking if the interests count is greater than zero if interest.count() > 0: # Getting the topics from the interests topics = interest.values_list('topic', flat=True) interest = [] # Looping through the topics received for topic_id in topics: # Getting the topic from ID topic = TopicController.GetTopic(ID=topic_id) # Checking if the topic is empty if topic is not None: # Appending the topic name to the interest interest.append(topic[0].name) else: # Emptying the interest list interest = [] # Returning the response with the interest list return JsonResponse({ 'InterestList': interest, 'UserAuthenticated': True }) else: # Returning the response without interest list return JsonResponse({'UserAuthenticated': False})
def post(self, request, topic_name, *args, **kwargs): # Getting the data from the request data = json.loads(request.body.decode('utf-8')) print(data) if request.user.is_authenticated(): if data.get('update_interest_status'): if InterestController.AddInterests(User=request.user, Data=[{ 'type': 'old', 'text': topic_name }]): return JsonResponse({ 'InterestStatus': True, 'UpdateInterestStatus': True, 'UserAuthenticated': True }) else: qs = InterestController.GetInterest( User=request.user, Topic=TopicController.GetTopic(Name=topic_name)) print(qs) if qs.count() == 1: return JsonResponse({ 'InterestStatus': True, 'UpdateInterestStatus': False, 'UserAuthenticated': True }) else: return JsonResponse({ 'InterestStatus': False, 'UpdateInterestStatus': False, 'UserAuthenticated': True }) else: InterestController.DeleteInterest( User=request.user, Topic=TopicController.GetTopic(Name=topic_name)) return JsonResponse({ 'InterestStatus': False, 'UpdateInterestStatus': True, 'UserAuthenticated': True }) else: return JsonResponse({'UserAuthenticated': False})
def post(self, request, *args, **kwargs): # Getting the data from the request data = json.loads(request.body.decode('utf-8')) # Checking if the user is authenticated if request.user.is_authenticated(): # Updating the interests for the user InterestController.UpdateInterests(request.user, data['interests']) # Returning the response with update status return JsonResponse({'UpdateInterestStatus': True}) else: # Returning the response with update status return JsonResponse({'UpdateInterestStatus': False})
def GetReviewsForUser(User): # Getting the interests for the user interests=InterestController.GetInterests(User=User) # Getting the subscription for the user subscriptions=SubscriptionController.GetSubscriptions(User=User) # Making an empty list of reviews reviews=[] # Appending the reviews added by the user to the list # reviews.extend(list(ReviewController.GetReviewsByUser(User=User))) # Appending the reviews by the interest to the list for interest in interests: reviews.extend(list(ReviewController.GetReviewsByTopic(Topic=interest.topic))) # Appending the reviews by the subscriptions to the list for subscription in subscriptions: reviews.extend(list(ReviewController.ReviewsByUser(User=subscription.reviewer))) # Making a dictionary of reviews with ID as a key dictionary={} # Looping through each object in the review list for obj in reviews: dictionary[obj.pk]=obj # Retrieving unique reviews from the dictionary reviews=dictionary.values() # Making a dictionary for the review and the rating information derived_list={} # Looping through each item in the unique review list for index, review_item in enumerate(sorted(reviews, key=lambda x: x.created)[::-1]): # Initializing the comment list comment_list = [] # Getting comments for comment in CommentController.GetCommments(Review=review_item): comment_list.append(CommentSerializer(comment).data) # Adding a dictionary object with the int as key and review and rating data as a part of another dictionary derived_list[index]={ 'review': ReviewSerializer(review_item).data, 'rating': RatingController.GetRating(Person=User, Review=review_item), 'topic_list': ReviewTopicController.GetTopics(Review=review_item), 'comment_list': comment_list } # Returning the derived list return derived_list
def get(self, request, topic_name, *args, **kwargs): if request.user.is_authenticated(): qs = InterestController.GetInterest( User=request.user, Topic=TopicController.GetTopic(Name=topic_name)) print(qs) if qs.count() == 1: return JsonResponse({ 'InterestStatus': True, 'UserAuthenticated': True }) else: return JsonResponse({ 'InterestStatus': False, 'UserAuthenticated': True }) else: return JsonResponse({'UserAuthenticated': False})
def AddReview(Data): # Creating a new review object and adding the parameters new_review=review.objects.create( added_by=Data.get('added_by'), caption=Data.get('caption'), briefing=Data.get('briefing'), # review_rating=Data.get('review_rating'), # pic=Data.get('pic') ) # Saving the new review new_review.save() print(Data) InterestController.AddInterests(Data.get('added_by'), Data.get('topic_list')) review_topics = [] for item in Data.get('topic_list'): review_topics.append(TopicController.GetTopic(Name=item.get('text'))[0]) print(review_topics) ReviewTopicController.AddTopics(new_review, review_topics) return True
def post(self, request, *args, **kwargs): # Getting the data from the request data = json.loads(request.body.decode('utf-8')) # Checking if the user is authenticated if request.user.is_authenticated(): # Making a dictionary for the review parameters new_topic = { 'name': data.get('name'), 'description': data.get('description'), 'added_by': request.user, } # Adding the new review into the database if TopicController.AddTopic(new_topic): InterestController.AddInterests(User=request.user, Data=[{ 'type': 'old', 'text': data.get('name') }]) # Returning the response with new review added return JsonResponse({'AddTopicStatus': True}) else: # Returning the response with unable add new review return JsonResponse({'AddTopicStatus': False})