def view_by_id(request, tw_id): # Displays tweet by id number, serves as a permalink to tweets tweet = FavoriteWorst.objects.get(tweet_id = tw_id) return render(request, 'view_by_id.html', {'tweet': tweet, 'nice_term': nice_term()[0]})
def view_top(request): # Displays top 10 tweets from DB top_tweets=FavoriteWorst.objects.order_by('-upvote_count')[:10] nt = nice_term(10) to_render = zip(top_tweets, nt) return render(request, 'top_tweets.html', {'top_tweets': to_render})
def view_random(request): # Displays a random tweet from DB. Gets user's current profile pic and # post's current upvote count. try: new_upvote_count = request.session['_upvote_count'] del request.session['_upvote_count'] visible = True except KeyError: new_upvote_count = '' visible = False random_tweet = FavoriteWorst.objects.random(1)[0] return render(request, 'random_favorite.html', {'random_tweet': random_tweet, 'new_upvote_count': new_upvote_count, 'visible': visible, 'nice_term': nice_term()[0]})
def search_result(request, formatted_url): # Check if the user has already searched for this term (aka if they are # refreshing page or using "prev/next" buttons. # If they search term doesn't match the URL, a new search is performed # Based on the URL. search_term = formatted_url.replace('_', ' ') try: if search_term != request.session['_search_term']: request.session['_tweets'] = extreme_tweets(search_term) request.session['_tweet_index'] = 0 request.session['_search_term'] = search_term request.session['_is_favorite'] = False request.session['_favorite_indices'] = [] request.session['_newly_favorited'] = False except: request.session['_tweets'] = extreme_tweets(search_term) request.session['_tweet_index'] = 0 request.session['_search_term'] = search_term request.session['_is_favorite'] = False request.session['_favorite_indices'] = [] request.session['_newly_favorited'] = False tweets = request.session['_tweets'] if len(tweets) == 0: return redirect('/insufficient/') try: # Change page index based on whether user hit "next/prev" button if request.GET["next_prev"] == 'next' and \ request.session['_tweet_index'] + 1 < len(tweets): request.session['_tweet_index'] += 1 elif request.GET["next_prev"] == 'prev' and \ request.session['_tweet_index'] > 0: request.session['_tweet_index'] -= 1 except: pass tweet_index = request.session['_tweet_index'] try: favorite_count = FavoriteWorst.objects.get( tweet_id=tweets[tweet_index]["id"]).upvote_count except: favorite_count = 0 if tweet_index in request.session['_favorite_indices']: favorite_button = False else: favorite_button = True # Rules for displaying "next/prev" buttons if request.session['_tweet_index'] + 1 < len(tweets): next_button = True else: next_button = False if request.session['_tweet_index'] > 0: prev_button = True else: prev_button = False to_render = {'search_term': search_term, 'tweet': tweets[tweet_index], 'next_button_visible': next_button, 'prev_button_visible': prev_button, 'nice_term': nice_term()[0], 'favorite_button_visible': favorite_button, 'upvote_count': favorite_count, 'newly_favorited': request.session['_newly_favorited']} request.session['_newly_favorited'] = False return render(request, 'search_results.html', to_render,)