def home(request): """ Displays list of generated quotes from quotesondesign. """ if request.method == "GET": posts = request.GET.get('posts', 4) page = request.GET.get('page', 1) # response = requests.get("https://quotesondesign.com/wp-json/wp/v2/posts?filter[orderby]=rand&filter[posts_per_page]=30") response = requests.get( "https://quotesondesign.com/wp-json/wp/v2/posts/?orderby=rand&filter[posts_per_page]=30" ) data = response.json() quotes = [] for dict_item in data: quote = Quote() quote.content = list( map(lambda d: parse_content(d), [ value['rendered'] for key, value in dict_item.items() if key == "content" ]))[0] # temporary update after API v5.0 quote.author = dict_item['title'][ 'rendered'] # temporary update after API v5.0 check = Quote.objects.filter(content=quote.content).all( ) # returns Queryset if quote content exists if not check: quote.save() else: quote.id = check.values("id")[0][ 'id'] # If in database, store existing quote id into object sent for template rendering q = Quote.objects.get( id=quote.id) # fetches quote from database quote.no_user_likes = q.like_set.all().count( ) # generate number of user likes for particular quote quote.no_user_favourites = q.favourite_set.all().count( ) # generate number of user likes for particular quote quote.check_liked = check.values("check_liked")[0][ "check_liked"] # check if quote has been liked by logged in user quote.check_favourited = check.values("check_favourited")[0][ "check_favourited"] # check if quote has been favourited by logged in user quotes.append(quote) # Paginate quotes quotes = quotes_paginator(quotes, posts, page) return render( request, 'quotes/home.html', { 'quotes': quotes, 'posts': posts, 'page': page, 'max_posts': list(range(3, 6)) }) elif request.method == "POST": # URL queries are always fetched via GET, not POST; request.POST.get() is only for retrieving from request.body posts = request.GET.get('posts', 4) page = request.GET.get('page', 1) if request.user is not AnonymousUser and isinstance( request.POST.get('submit_user_like'), str): quote_id = request.POST.get("submit_user_like") Like.objects.get_or_create( user=request.user, quote_id=quote_id ) # Creates Like entry in table quotes_like. Outputs (Like object, boolean on whether created) quote = Quote.objects.get(id=quote_id) if (quote.check_liked == True): quote.check_liked = False quote.save() Like.objects.filter(quote=quote_id).delete() messages.success(request, "Unliked!") else: quote.check_liked = True quote.save() messages.success(request, "Liked!") elif request.user is not AnonymousUser and isinstance( request.POST.get('submit_user_favourite'), str): quote_id = request.POST.get("submit_user_favourite") Favourite.objects.get_or_create( user=request.user, quote_id=request.POST.get('submit_user_favourite')) quote = Quote.objects.get(id=quote_id) if (quote.check_favourited == True): quote.check_favourited = False quote.save() Favourite.objects.filter(quote=quote_id).delete() messages.success(request, "Removed from favourites!") else: quote.check_favourited = True quote.save() messages.success(request, "Favourited!") # Build redirect string back to 'quotes-home' with 'posts' and 'page' url query base_url = reverse('quotes-home') query_string = urlencode({'posts': posts, 'page': page}) url = '{}?{}'.format(base_url, query_string) return redirect(url)