<s> from django . contrib . auth import authenticate , login as auth_login <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . comments . signals import comment_was_posted <EOL> from django . http import HttpResponse , HttpResponseRedirect <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . shortcuts import render_to_response <EOL> from tagging . models import Tag , TaggedItem <EOL> from django . template import RequestContext <EOL> from snipt . snippet . models import Snippet , Referer <EOL> from django . utils . html import escape <EOL> from django . utils import simplejson <EOL> from tagging . utils import get_tag <EOL> from snipt . snippet . utils import * <EOL> from django . http import Http404 <EOL> from settings import DEBUG <EOL> from snipt . forms import * <EOL> import md5 <EOL> from pygments import highlight <EOL> from pygments . lexers import get_lexer_by_name <EOL> from pygments . formatters import HtmlFormatter <EOL> def dispatcher ( request , user , slug , snipt_id ) : <EOL> if not user and not slug and not snipt_id : <EOL> return home_page ( request ) <EOL> elif user and snipt_id : <EOL> if snipt_id == '<STR_LIT>' : <EOL> return user_page ( request , user , slug , True ) <EOL> else : <EOL> return snipt_page ( request , user , snipt_id ) <EOL> else : <EOL> return user_page ( request , user , slug ) <EOL> def search ( request ) : <EOL> query_string = '<STR_LIT>' <EOL> found_snipts = None <EOL> if ( '<STR_LIT:q>' in request . GET ) and request . GET [ '<STR_LIT:q>' ] . strip ( ) : <EOL> query_string = request . GET [ '<STR_LIT:q>' ] <EOL> entry_query = get_query ( query_string , [ '<STR_LIT:code>' , '<STR_LIT:description>' , '<STR_LIT>' , ] ) <EOL> snipts = Snippet . objects . filter ( entry_query ) . filter ( Q ( public = <NUM_LIT:1> ) | Q ( user = request . user . id ) ) . order_by ( '<STR_LIT>' ) <EOL> disable_wrap = request . session . get ( '<STR_LIT>' ) <EOL> disable_message = request . session . get ( '<STR_LIT>' ) <EOL> snipts_count = snipts . count ( ) <EOL> search = True <EOL> return render_to_response ( '<STR_LIT>' , locals ( ) , context_instance = RequestContext ( request ) ) <EOL> def feed ( request , user , slug ) : <EOL> return render_to_response ( '<STR_LIT>' , locals ( ) , context_instance = RequestContext ( request ) ) <EOL> def embed ( request , snipt ) : <EOL> try : <EOL> snipt = Snippet . objects . get ( key = snipt ) <EOL> snipt . code_stylized = highlight ( snipt . code . replace ( "<STR_LIT>" , "<STR_LIT>" ) , get_lexer_by_name ( snipt . lexer , encoding = '<STR_LIT>' ) , HtmlFormatter ( style = "<STR_LIT>" , noclasses = True , prestyles = "<STR_LIT>" ) ) <EOL> snipt . code_stylized = snipt . code_stylized . split ( '<STR_LIT:\n>' ) <EOL> snipt . referer = request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> i = <NUM_LIT:0> ; <EOL> for sniptln in snipt . code_stylized : <EOL> snipt . code_stylized [ i ] = snipt . code_stylized [ i ] . replace ( "<STR_LIT>" , "<STR_LIT>" ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> i = i + <NUM_LIT:1> <EOL> except Snippet . DoesNotExist : <EOL> raise Http404 <EOL> if ( snipt . referer ) : <EOL> try : <EOL> Referer . objects . get ( url = snipt . referer , snippet = snipt . id ) <EOL> except Referer . DoesNotExist : <EOL> referrer = Referer ( None , snipt . referer , snipt . id ) <EOL> referrer . save ( ) <EOL> return render_to_response ( '<STR_LIT>' , locals ( ) , context_instance = RequestContext ( request ) , mimetype = "<STR_LIT>" ) <EOL> def password_reset ( request ) : <EOL> request . session [ '<STR_LIT>' ] = "<STR_LIT>" ; <EOL> if request . POST : <EOL> try : <EOL> email = request . POST [ '<STR_LIT:email>' ] <EOL> user = User . objects . get ( email = email ) <EOL> from random import choice <EOL> new_password = '<STR_LIT>' . join ( [ choice ( '<STR_LIT>' ) for i in range ( <NUM_LIT:16> ) ] ) <EOL> user . set_password ( new_password ) <EOL> user . save ( ) <EOL> from django . core . mail import send_mail <EOL> subject = '<STR_LIT>' <EOL> message = '<STR_LIT>' % new_password <EOL> send_mail ( subject , message , '<STR_LIT>' , [ email ] , fail_silently = False ) <EOL> request . session [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> except User . DoesNotExist : <EOL> request . session [ '<STR_LIT>' ] = "<STR_LIT>" % request . POST [ '<STR_LIT:email>' ] <EOL> return render_to_response ( '<STR_LIT>' , locals ( ) , context_instance = RequestContext ( request ) ) <EOL> def home_page ( request ) : <EOL> if request . user . is_authenticated ( ) : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' + request . user . username ) <EOL> else : <EOL> is_home = True <EOL> return render_to_response ( '<STR_LIT>' , locals ( ) , context_instance = RequestContext ( request ) ) <EOL> def all_public_tags ( request ) : <EOL> tags = Tag . objects . usage_for_queryset ( Snippet . objects . filter ( public = '<STR_LIT:1>' ) , counts = True ) <EOL> return render_to_response ( '<STR_LIT>' , locals ( ) , context_instance = RequestContext ( request ) ) <EOL> from django . contrib . comments . signals import comment_was_posted <EOL> def email_owner_on_comment ( sender , comment , request , * args , ** kwargs ) : <EOL> from django . core . mail import EmailMultiAlternatives <EOL> snipt = Snippet . objects . get ( id = request . POST [ '<STR_LIT>' ] ) <EOL> if snipt . user . username != comment . name : <EOL> text_content = """<STR_LIT>""" % ( comment . name , snipt . description , snipt . get_absolute_url ( ) , comment . id ) <EOL> html_content = """<STR_LIT>""" % ( comment . name , comment . name , snipt . get_absolute_url ( ) , comment . id , snipt . description ) <EOL> msg = EmailMultiAlternatives ( """<STR_LIT>""" % snipt . description , text_content , '<STR_LIT>' , [ snipt . user . email ] ) <EOL> msg . attach_alternative ( html_content , "<STR_LIT>" ) <EOL> msg . send ( ) <EOL> comment_was_posted . connect ( email_owner_on_comment ) <EOL> def snipt_page ( request , user , snipt_id ) : <EOL> try : <EOL> snipt = Snippet . objects . get ( slug = snipt_id ) <EOL> if '<STR_LIT:c>' in request . GET : <EOL> return HttpResponseRedirect ( snipt . get_absolute_url ( ) + '<STR_LIT>' + request . GET [ '<STR_LIT:c>' ] ) <EOL> except : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' + user + '<STR_LIT>' + snipt_id ) <EOL> context_user = User . objects . get ( id = snipt . user . id ) <EOL> if request . user . id == context_user . id : <EOL> mine = True <EOL> else : <EOL> mine = False <EOL> if not snipt . public and not mine : <EOL> try : <EOL> if request . GET [ '<STR_LIT:key>' ] != snipt . key : <EOL> raise Http404 ( ) <EOL> else : <EOL> key = True <EOL> except : <EOL> raise Http404 ( ) <EOL> disable_wrap = request . session . get ( '<STR_LIT>' ) <EOL> disable_message = request . session . get ( '<STR_LIT>' ) <EOL> return render_to_response ( '<STR_LIT>' , locals ( ) , context_instance = RequestContext ( request ) ) <EOL> def user_page ( request , user , slug , feed = False ) : <EOL> if request . user . username != user : <EOL> mine = False <EOL> try : <EOL> context_user = User . objects . get ( username = user ) <EOL> if context_user . username == '<STR_LIT>' : <EOL> public = True <EOL> else : <EOL> public = False <EOL> except : <EOL> raise Http404 ( ) <EOL> else : <EOL> mine = True <EOL> context_user = request . user <EOL> if slug is not None : <EOL> slug = slug . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> try : <EOL> tag = Tag . objects . get ( name = slug ) <EOL> except : <EOL> tag = None <EOL> if tag is not None : <EOL> if mine : <EOL> snipts = TaggedItem . objects . get_by_model ( Snippet . objects . filter ( user = context_user . id ) . order_by ( '<STR_LIT>' ) , tag ) <EOL> from snipt . favsnipt . models import FavSnipt <EOL> favsnipts = FavSnipt . objects . filter ( user = context_user . id ) <EOL> favrd = [ ] <EOL> for fs in favsnipts : <EOL> fs . snipt . favrd = True <EOL> fs . snipt . created = fs . created <EOL> if str ( tag ) in fs . snipt . tags : <EOL> favrd . append ( fs . snipt ) <EOL> from operator import attrgetter <EOL> from itertools import chain <EOL> snipts = sorted ( <EOL> chain ( snipts , favrd ) , <EOL> key = attrgetter ( '<STR_LIT>' ) , reverse = True ) <EOL> elif not public : <EOL> snipts = TaggedItem . objects . get_by_model ( Snippet . objects . filter ( user = context_user . id , public = '<STR_LIT:1>' ) . order_by ( '<STR_LIT>' ) , tag ) <EOL> else : <EOL> snipts = TaggedItem . objects . get_by_model ( Snippet . objects . filter ( public = '<STR_LIT:1>' ) . order_by ( '<STR_LIT>' ) , tag ) <EOL> else : <EOL> raise Http404 ( ) <EOL> if len ( snipts ) == <NUM_LIT:0> : <EOL> raise Http404 ( ) <EOL> else : <EOL> if mine : <EOL> snipts = Snippet . objects . filter ( user = context_user . id ) . order_by ( '<STR_LIT>' ) <EOL> from snipt . favsnipt . models import FavSnipt <EOL> favsnipts = FavSnipt . objects . filter ( user = context_user . id ) <EOL> favrd = [ ] <EOL> for fs in favsnipts : <EOL> fs . snipt . favrd = True <EOL> fs . snipt . created = fs . created <EOL> favrd . append ( fs . snipt ) <EOL> from operator import attrgetter <EOL> from itertools import chain <EOL> snipts = sorted ( <EOL> chain ( snipts , favrd ) , <EOL> key = attrgetter ( '<STR_LIT>' ) , reverse = True ) <EOL> elif not public : <EOL> snipts = Snippet . objects . filter ( user = context_user . id , public = '<STR_LIT:1>' ) . order_by ( '<STR_LIT>' ) <EOL> else : <EOL> snipts = Snippet . objects . filter ( public = '<STR_LIT:1>' ) . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:100> ] <EOL> if mine : <EOL> user_tags_list = Tag . objects . usage_for_queryset ( Snippet . objects . filter ( user = context_user . id ) . order_by ( '<STR_LIT>' ) , counts = True ) <EOL> elif not public : <EOL> user_tags_list = Tag . objects . usage_for_queryset ( Snippet . objects . filter ( user = context_user . id , public = '<STR_LIT:1>' ) . order_by ( '<STR_LIT>' ) , counts = True ) <EOL> else : <EOL> user_tags_list = Tag . objects . usage_for_queryset ( Snippet . objects . filter ( public = '<STR_LIT:1>' ) , counts = True ) <EOL> if not DEBUG : <EOL> user_tags_list . sort ( key = lambda x : x . count , reverse = True ) <EOL> user_tags_list = user_tags_list [ : <NUM_LIT> ] <EOL> for usertag in user_tags_list : <EOL> usertag . slug = str ( usertag ) <EOL> disable_wrap = request . session . get ( '<STR_LIT>' ) <EOL> disable_message = request . session . get ( '<STR_LIT>' ) <EOL> if mine : <EOL> total_count = Snippet . objects . filter ( user = context_user . id ) . count ( ) <EOL> elif not public : <EOL> total_count = Snippet . objects . filter ( user = context_user . id , public = '<STR_LIT:1>' ) . count ( ) <EOL> else : <EOL> total_count = Snippet . objects . filter ( public = '<STR_LIT:1>' ) . count ( ) <EOL> if total_count > <NUM_LIT:0> : <EOL> if mine : <EOL> last_lexer = Snippet . objects . filter ( user = context_user . id ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . lexer <EOL> snipts_count = len ( snipts ) <EOL> if mine : <EOL> request_tag = request . path . replace ( '<STR_LIT:/>' + request . user . username + '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> request_tag = request . path . replace ( '<STR_LIT:/>' + context_user . username + '<STR_LIT>' , '<STR_LIT>' ) <EOL> if feed : <EOL> try : <EOL> snipts = TaggedItem . objects . get_by_model ( Snippet . objects . filter ( user = context_user . id , public = '<STR_LIT:1>' ) . order_by ( '<STR_LIT>' ) , tag ) <EOL> except : <EOL> snipts = Snippet . objects . filter ( user = context_user . id , public = '<STR_LIT:1>' ) . order_by ( '<STR_LIT>' ) <EOL> real_path = request . path . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in request . path : <EOL> snipts = snipts . filter ( ) [ : <NUM_LIT:20> ] <EOL> return render_to_response ( '<STR_LIT>' , locals ( ) , context_instance = RequestContext ( request ) , mimetype = "<STR_LIT>" ) <EOL> else : <EOL> return render_to_response ( '<STR_LIT>' , locals ( ) , context_instance = RequestContext ( request ) ) <EOL> def signup ( request ) : <EOL> """<STR_LIT>""" <EOL> signup_form = UserCreationForm ( request . POST ) <EOL> if signup_form . is_valid ( ) : <EOL> signup_form . save ( ) <EOL> user = authenticate ( username = request . POST [ '<STR_LIT:username>' ] , password = request . POST [ '<STR_LIT>' ] ) <EOL> auth_login ( request , user ) <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> else : <EOL> return render_to_response ( "<STR_LIT>" , { <EOL> '<STR_LIT>' : signup_form , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:data>' : request . POST <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ login_required <EOL> def tags ( request ) : <EOL> """<STR_LIT>""" <EOL> user_tags_list = Tag . objects . usage_for_queryset ( Snippet . objects . filter ( user = request . user . id ) . order_by ( '<STR_LIT>' ) , counts = True ) <EOL> """<STR_LIT>""" <EOL> tags_list = [ ] <EOL> for tag in user_tags_list : <EOL> tags_list . append ( { <EOL> '<STR_LIT:id>' : tag . id , <EOL> '<STR_LIT>' : escape ( str ( tag ) ) , <EOL> '<STR_LIT:count>' : escape ( str ( tag . count ) ) <EOL> } ) <EOL> data = { <EOL> '<STR_LIT>' : tags_list <EOL> } <EOL> return HttpResponse ( simplejson . dumps ( data ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> def save ( request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> request . POST [ '<STR_LIT:id>' ] <EOL> except : <EOL> raise Http404 ( ) <EOL> if request . POST [ '<STR_LIT:id>' ] != '<STR_LIT:0>' : <EOL> submitted_snippet = SnippetForm ( request . POST , instance = Snippet . objects . get ( id = request . POST [ '<STR_LIT:id>' ] , user = request . user . id ) ) <EOL> else : <EOL> submitted_snippet = SnippetForm ( request . POST ) <EOL> if submitted_snippet . is_valid ( ) : <EOL> submitted_snippet = submitted_snippet . save ( commit = False ) <EOL> submitted_snippet . user = request . user <EOL> if request . POST [ '<STR_LIT:id>' ] == '<STR_LIT:0>' : <EOL> submitted_snippet . slug = SlugifyUniquely ( submitted_snippet . description , Snippet ) <EOL> submitted_snippet . key = md5 . new ( submitted_snippet . slug ) . hexdigest ( ) <EOL> submitted_snippet . save ( ) <EOL> """<STR_LIT>""" <EOL> submitted_snippet . tags_list = [ ] <EOL> tags_list = submitted_snippet . get_tags ( ) <EOL> for tag in tags_list : <EOL> submitted_snippet . tags_list . append ( { <EOL> '<STR_LIT:id>' : tag . id , <EOL> '<STR_LIT>' : escape ( str ( tag ) ) , <EOL> } ) <EOL> data = { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:id>' : submitted_snippet . id , <EOL> '<STR_LIT:code>' : escape ( submitted_snippet . code ) , <EOL> '<STR_LIT>' : stylize ( submitted_snippet . code , submitted_snippet . lexer ) , <EOL> '<STR_LIT:description>' : escape ( submitted_snippet . description ) , <EOL> '<STR_LIT>' : escape ( submitted_snippet . tags ) , <EOL> '<STR_LIT>' : submitted_snippet . tags_list , <EOL> '<STR_LIT>' : submitted_snippet . lexer , <EOL> '<STR_LIT>' : submitted_snippet . public , <EOL> '<STR_LIT:key>' : submitted_snippet . key , <EOL> '<STR_LIT>' : submitted_snippet . slug , <EOL> '<STR_LIT:username>' : request . user . username , <EOL> '<STR_LIT>' : submitted_snippet . created . strftime ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : submitted_snippet . created . strftime ( "<STR_LIT>" ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> } <EOL> else : <EOL> data = { <EOL> '<STR_LIT:success>' : False <EOL> } <EOL> return HttpResponse ( simplejson . dumps ( data ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def stylize ( code , lexer ) : <EOL> return highlight ( code , get_lexer_by_name ( lexer , encoding = '<STR_LIT>' ) , HtmlFormatter ( ) ) <EOL> def wrap ( request ) : <EOL> if request . session . get ( '<STR_LIT>' ) == True : <EOL> request . session [ '<STR_LIT>' ] = False <EOL> else : <EOL> request . session [ '<STR_LIT>' ] = True <EOL> data = { <EOL> '<STR_LIT>' : str ( request . session [ '<STR_LIT>' ] ) <EOL> } <EOL> return HttpResponse ( simplejson . dumps ( data ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def message ( request ) : <EOL> if request . session . get ( '<STR_LIT>' ) == True : <EOL> request . session [ '<STR_LIT>' ] = False <EOL> else : <EOL> request . session [ '<STR_LIT>' ] = True <EOL> data = { <EOL> '<STR_LIT>' : str ( request . session [ '<STR_LIT>' ] ) <EOL> } <EOL> return HttpResponse ( simplejson . dumps ( data ) , mimetype = '<STR_LIT:application/json>' ) <EOL> @ login_required <EOL> def delete ( request ) : <EOL> """<STR_LIT>""" <EOL> deleting = Snippet . objects . get ( id = request . POST [ '<STR_LIT:id>' ] ) <EOL> if deleting . user_id == request . user . id : <EOL> delid = deleting . id <EOL> deleting . delete ( ) <EOL> data = { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT:id>' : delid <EOL> } <EOL> else : <EOL> data = { <EOL> '<STR_LIT:success>' : False <EOL> } <EOL> return HttpResponse ( simplejson . dumps ( data ) , mimetype = '<STR_LIT:application/json>' ) </s>
