<s> from django . shortcuts import render_to_response , get_object_or_404 <EOL> from django . http import HttpResponseRedirect <EOL> from django . template import RequestContext <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . conf import settings <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from microblogging . utils import twitter_account_for_user , twitter_verify_credentials <EOL> from microblogging . models import Tweet , TweetInstance , Following <EOL> from microblogging . forms import TweetForm <EOL> if "<STR_LIT>" in settings . INSTALLED_APPS : <EOL> from notification import models as notification <EOL> else : <EOL> notification = None <EOL> def personal ( request , form_class = TweetForm , <EOL> template_name = "<STR_LIT>" , success_url = None ) : <EOL> """<STR_LIT>""" <EOL> twitter_account = twitter_account_for_user ( request . user ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = form_class ( request . user , request . POST ) <EOL> if form . is_valid ( ) : <EOL> text = form . cleaned_data [ '<STR_LIT:text>' ] <EOL> form . save ( ) <EOL> if request . POST . get ( "<STR_LIT>" , False ) : <EOL> twitter_account . PostUpdate ( text ) <EOL> if success_url is None : <EOL> success_url = reverse ( '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( success_url ) <EOL> reply = None <EOL> else : <EOL> reply = request . GET . get ( "<STR_LIT>" , None ) <EOL> form = form_class ( ) <EOL> if reply : <EOL> form . fields [ '<STR_LIT:text>' ] . initial = u"<STR_LIT>" % reply <EOL> tweets = TweetInstance . objects . tweets_for ( request . user ) . order_by ( "<STR_LIT>" ) <EOL> return render_to_response ( template_name , { <EOL> "<STR_LIT>" : form , <EOL> "<STR_LIT>" : reply , <EOL> "<STR_LIT>" : tweets , <EOL> "<STR_LIT>" : twitter_verify_credentials ( twitter_account ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> personal = login_required ( personal ) <EOL> def public ( request , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> tweets = Tweet . objects . all ( ) . order_by ( "<STR_LIT>" ) <EOL> return render_to_response ( template_name , { <EOL> "<STR_LIT>" : tweets , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> def single ( request , id , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> tweet = get_object_or_404 ( Tweet , id = id ) <EOL> return render_to_response ( template_name , { <EOL> "<STR_LIT>" : tweet , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> def _follow_list ( request , other_user , follow_list , template_name ) : <EOL> return render_to_response ( template_name , { <EOL> "<STR_LIT>" : other_user , <EOL> "<STR_LIT>" : follow_list , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> def followers ( request , username , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> other_user = get_object_or_404 ( User , username = username ) <EOL> users_followers = Following . objects . filter ( followed_object_id = other_user . id , followed_content_type = ContentType . objects . get_for_model ( other_user ) ) <EOL> follow_list = [ u . follower_content_object for u in users_followers ] <EOL> return _follow_list ( request , other_user , follow_list , template_name ) <EOL> def following ( request , username , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> other_user = get_object_or_404 ( User , username = username ) <EOL> following = Following . objects . filter ( follower_object_id = other_user . id , follower_content_type = ContentType . objects . get_for_model ( other_user ) ) <EOL> follow_list = [ u . followed_content_object for u in following ] <EOL> return _follow_list ( request , other_user , follow_list , template_name ) <EOL> def toggle_follow ( request , username ) : <EOL> """<STR_LIT>""" <EOL> other_user = get_object_or_404 ( User , username = username ) <EOL> if request . user == other_user : <EOL> is_me = True <EOL> else : <EOL> is_me = False <EOL> if request . user . is_authenticated ( ) and request . method == "<STR_LIT:POST>" and not is_me : <EOL> if request . POST [ "<STR_LIT:action>" ] == "<STR_LIT>" : <EOL> Following . objects . follow ( request . user , other_user ) <EOL> request . user . message_set . create ( message = _ ( "<STR_LIT>" ) % { '<STR_LIT>' : other_user } ) <EOL> if notification : <EOL> notification . send ( [ other_user ] , "<STR_LIT>" , { "<STR_LIT:user>" : request . user } ) <EOL> elif request . POST [ "<STR_LIT:action>" ] == "<STR_LIT>" : <EOL> Following . objects . unfollow ( request . user , other_user ) <EOL> request . user . message_set . create ( message = _ ( "<STR_LIT>" ) % { '<STR_LIT>' : other_user } ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ other_user ] ) ) </s>
