def ajax_timeline(request): g = request.GET if not request.user.is_authenticated() or 'twitter_tokens' not in request.session: return HttpResponseBadRequest('not authenticated') twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) tp = TwitterUserProfile.objects.get(id=twitter_tokens['user_id']) if not g.has_key(u'page'): return HttpResponseBadRequest('page number missing') page, feedtype = int(g[u'page']), g[u'timeline'] # Get maxid for certain types of timelines try: maxid = int(g[u'id']) except KeyError: maxid = None # Force cache fill if no statuses loaded originally if page == 1: cache_timeline_backfill(tp, twitter_tokens, None) if feedtype == 'normal': statuses = normal_timeline(api, tp, page, maxid) elif feedtype == 'reorder': statuses = reorder_timeline(api, tp, page) elif feedtype == 'filter': statuses = filter_timeline(api, tp, page, maxid) elif feedtype == 'predict': statuses = predict_timeline(api, tp, page, maxid) if len(statuses) == 0: return HttpResponse('Loading') Rating.appendTo(statuses, tp) return render_to_response('twitter/status_list.html', { 'statuses': statuses }, context_instance=RequestContext(request))
def get_rate_results(request, lex): results = {'success':'False'} u = request.user rating = lex[u'rating'] if(lex.has_key(u'status')): sid = int(lex[u'status']) try: status = Status.objects.get(id=sid) except Status.DoesNotExist: api = get_authorized_twython(request.session['twitter_tokens']) status_json = api.showStatus(id=lex[u'status']) status = Status.construct_from_dict(status_json) else: api = get_authorized_twython(request.session['twitter_tokens']) status_json = api.showStatus(id=lex[u'id']) status = Status.construct_from_dict(status_json) # Show user if tweet delivered from Search API, which does not have correct userid # TODO: a more elegant solution if not status.user.id: api = get_authorized_twython(request.session['twitter_tokens']) api_user = api.showUser(screen_name=status.user.screen_name) setattr(status, 'user', TwitterUserProfile.construct_from_dict(api_user)) tp = TwitterUserProfile.objects.get(id=request.session['twitter_tokens']['user_id']) prof = u.get_profile() status.save_with_user(is_cached=False) try: details = CachedStatus.objects.get(user=tp.id, status=status.id) details.prediction -= 2.0 details.save() except CachedStatus.DoesNotExist: pass if rating == 'up': rating_int = 1 elif rating == 'down': rating_int = -1 try: r = Rating.objects.get(status=status, user=tp) except: r = Rating(status=status, user=tp) # prof.whale.exp += 1 # if prof.whale.exp == prof.whale.species.evolution.minExp: prof.whale.species = prof.whale.species.evolution # prof.whale.save() r.rating = rating_int r.save() results['success'] = 'True' # results['exp'] = prof.whale.exp # results['min-exp'] = prof.whale.species.minExp # results['max-exp'] = prof.whale.species.evolution.minExp # results['species'] = prof.whale.species.img.url # results['speciesName'] = prof.whale.species.name return results
def timeline(request): user = request.user if not user.is_authenticated() or 'twitter_tokens' not in request.session: return render_to_response('landing.html') twitter_tokens = request.session['twitter_tokens'] tp = TwitterUserProfile.objects.get(id=twitter_tokens['user_id']) api = get_authorized_twython(twitter_tokens) statuses = Status.construct_from_dicts(api.getFriendsTimeline(include_rts=True)) cache_timeline_backfill.delay(tp, twitter_tokens, statuses) friends = api.getFriendsStatus() Rating.appendTo(statuses, tp) return render_to_response('twitter/timeline.html', { 'statuses': statuses, 'friends': friends, 'feedtype': 'normal' }, context_instance=RequestContext(request))
def search(request): if not request.user.is_authenticated() or 'twitter_tokens' not in request.session: return HttpResponseRedirect("/") term = request.GET.get('q') if term is not None: prof = request.user.get_profile() twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) tp = TwitterUserProfile.objects.get(id=twitter_tokens['user_id']) statuses = Status.construct_from_search_dicts(api.searchTwitter(q=term)[u'results']) friends = api.getFriendsStatus() Rating.appendTo(statuses, tp) return render_to_response('twitter/search_index.html', { # 'whale': prof.whale, 'friends': friends, 'statuses': statuses, 'term': term }, context_instance=RequestContext(request)) else: return HttpResponseRedirect("/")
def public_profile(request, username): if request.user.is_authenticated() and 'twitter_tokens' in request.session: twitter_tokens = request.session['twitter_tokens'] api = get_authorized_twython(twitter_tokens) else: # Require login return HttpResponseRedirect("/") friend = api.showUser(screen_name=username) friends = api.getFriendsStatus() prof = request.user.get_profile() tp = TwitterUserProfile.objects.get(user=prof) follow_request_sent = True is_true_friend = friend['following'] is_me = tp.id == friend['id'] if not is_true_friend: is_true_friend = False outgoing = api.friendshipsOutgoing() follow_request_sent = False if friend['id'] in outgoing['ids']: # if we have already requested to follow this person follow_request_sent = True if friend['protected'] and not is_true_friend: statuses = None else: try: statuses = Status.construct_from_dicts(api.getUserTimeline(screen_name=username)) Rating.appendTo(statuses, tp) except TwythonError: statuses = None return render_to_response('twitter/public_profile.html', { 'friends': friends, 'username': username, 'friend': friend, 'is_true_friend' : is_true_friend, 'is_me' : is_me, 'profile_protected' : friend['protected'], 'follow_request_sent': follow_request_sent, 'statuses' : statuses, }, context_instance=RequestContext(request))