<s> from django . core import serializers <EOL> from django . shortcuts import render , render_to_response , redirect <EOL> from django . http import HttpResponse , HttpResponseNotAllowed , HttpResponseForbidden , HttpResponseNotModified , HttpResponseNotFound , JsonResponse <EOL> from django . template import RequestContext <EOL> from gitorial . models import User , Tutorial , Step <EOL> import django . contrib . auth <EOL> import social . apps . django_app . views <EOL> import json <EOL> import requests <EOL> from config import settings <EOL> from datetime import datetime , timedelta <EOL> from . import diff <EOL> def index ( request ) : <EOL> return render_to_response ( '<STR_LIT>' , { } , <EOL> context_instance = RequestContext ( request ) ) <EOL> def logout ( request ) : <EOL> django . contrib . auth . logout ( request ) <EOL> return redirect ( '<STR_LIT:/>' ) <EOL> def callback ( request ) : <EOL> if ( request . user is not None and <EOL> request . user . is_authenticated ( ) ) : <EOL> return redirect ( '<STR_LIT>' + request . user . username ) <EOL> else : <EOL> return redirect ( '<STR_LIT:/>' ) <EOL> return <EOL> def session ( request ) : <EOL> if request . method == '<STR_LIT:GET>' : <EOL> if ( request . user is not None and <EOL> request . user . is_authenticated ( ) ) : <EOL> username = request . user . username <EOL> else : <EOL> username = '<STR_LIT>' <EOL> return HttpResponse ( json . dumps ( { <EOL> '<STR_LIT:username>' : username <EOL> } ) , <EOL> content_type = "<STR_LIT:application/json>" ) <EOL> else : <EOL> return HttpResponseNotAllowed ( [ '<STR_LIT:GET>' ] ) <EOL> def user_view ( request , username ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> user , is_new = User . objects . get_or_create ( username = username ) <EOL> if is_new : <EOL> api_r = requests . get ( '<STR_LIT>' % ( username , settings . SOCIAL_AUTH_GITHUB_KEY , settings . SOCIAL_AUTH_GITHUB_SECRET ) ) <EOL> print ( api_r . headers [ '<STR_LIT>' ] ) <EOL> response_json = api_r . json ( ) <EOL> try : <EOL> user . name = response_json [ '<STR_LIT:name>' ] <EOL> except : <EOL> pass <EOL> user . avatar_url = response_json [ '<STR_LIT>' ] <EOL> user . last_modified = datetime . now ( ) <EOL> user . save ( ) <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> else : <EOL> return HttpResponseForbidden ( ) <EOL> elif request . method == '<STR_LIT:GET>' : <EOL> try : <EOL> user = User . objects . get ( username = username ) <EOL> result = user . getDict ( ) <EOL> if request . user . username == username : <EOL> repo_r = requests . get ( '<STR_LIT>' % ( username , settings . SOCIAL_AUTH_GITHUB_KEY , settings . SOCIAL_AUTH_GITHUB_SECRET ) ) <EOL> repo_r_json = repo_r . json ( ) <EOL> result [ '<STR_LIT>' ] = [ { <EOL> '<STR_LIT:title>' : repo [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT:description>' : repo [ '<STR_LIT:description>' ] <EOL> } for repo in repo_r_json ] <EOL> result [ '<STR_LIT>' ] = [ { <EOL> '<STR_LIT:id>' : tutorial . id , <EOL> '<STR_LIT:title>' : tutorial . title , <EOL> '<STR_LIT:description>' : tutorial . description , <EOL> '<STR_LIT>' : tutorial . repo_url <EOL> } for tutorial in Tutorial . objects . filter ( owner = user ) ] <EOL> return HttpResponse ( json . dumps ( result ) , <EOL> content_type = "<STR_LIT:application/json>" ) <EOL> except Exception as e : <EOL> print ( e ) <EOL> return HttpResponseNotFound ( ) <EOL> elif request . method == '<STR_LIT>' : <EOL> try : <EOL> User . objects . get ( username = username ) . delete ( ) <EOL> return HttpResponse ( ) <EOL> except Exception as e : <EOL> print ( e ) <EOL> return HttpResponseNotFound ( ) <EOL> elif request . method == '<STR_LIT>' : <EOL> try : <EOL> user = User . objects . get ( username = username ) <EOL> if ( datetime . now ( ) - user . last_modified ) > timedelta ( hours = <NUM_LIT:1> ) : <EOL> api_r = requests . get ( '<STR_LIT>' % ( username , settings . SOCIAL_AUTH_GITHUB_KEY , settings . SOCIAL_AUTH_GITHUB_SECRET ) ) <EOL> print ( api_r . headers [ '<STR_LIT>' ] ) <EOL> response_json = api_r . json ( ) <EOL> try : <EOL> user . name = response_json [ '<STR_LIT:name>' ] <EOL> except : <EOL> pass <EOL> user . avatar_url = response_json [ '<STR_LIT>' ] <EOL> user . last_modified = datetime . now ( ) <EOL> user . save ( ) <EOL> return HttpResponse ( ) <EOL> else : <EOL> return HttpResponseNotModified ( ) <EOL> except Exception as e : <EOL> print ( e ) <EOL> return HttpResponseNotFound ( ) <EOL> else : <EOL> return HttpResponseNotAllowed ( [ '<STR_LIT:POST>' , '<STR_LIT:GET>' , '<STR_LIT>' ] ) <EOL> def build_tutorials ( user ) : <EOL> user_tutorials = Tutorial . objects . filter ( owner = user ) . values ( '<STR_LIT:title>' , '<STR_LIT:description>' , '<STR_LIT>' ) . order_by ( '<STR_LIT:id>' ) . reverse ( ) <EOL> return [ { '<STR_LIT:title>' : item [ '<STR_LIT:title>' ] , <EOL> '<STR_LIT:description>' : item [ '<STR_LIT:description>' ] , <EOL> '<STR_LIT:url>' : item [ '<STR_LIT>' ] } <EOL> for item in user_tutorials ] <EOL> def build_steps ( username , repo_name , tutorial , commits_data ) : <EOL> index = <NUM_LIT:0> <EOL> for commit_data in commits_data : <EOL> index += <NUM_LIT:1> <EOL> step , is_new = Step . objects . get_or_create ( index = index , tutorial = tutorial ) <EOL> if is_new : <EOL> step . title = commit_data [ '<STR_LIT:title>' ] <EOL> step . content_before = commit_data [ '<STR_LIT:message>' ] <EOL> step . content_after = '<STR_LIT>' <EOL> step . diff_url = commit_data [ '<STR_LIT>' ] <EOL> step . code_url = commit_data [ '<STR_LIT>' ] <EOL> api_r = requests . get ( <EOL> '<STR_LIT>' % ( <EOL> username , <EOL> repo_name , <EOL> commit_data [ '<STR_LIT>' ] , <EOL> settings . SOCIAL_AUTH_GITHUB_KEY , <EOL> settings . SOCIAL_AUTH_GITHUB_SECRET <EOL> ) , headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> step . files = json . dumps ( diff . parse ( api_r . text ) ) <EOL> step . save ( ) <EOL> def tutorial_new ( request , username , repo ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> user = User . objects . get ( username = username ) <EOL> repo_r_json = requests . get ( <EOL> '<STR_LIT>' % ( <EOL> username , <EOL> repo , <EOL> settings . SOCIAL_AUTH_GITHUB_KEY , <EOL> settings . SOCIAL_AUTH_GITHUB_SECRET ) <EOL> ) . json ( ) <EOL> tut_entry , is_new = Tutorial . objects . get_or_create ( id = repo_r_json [ '<STR_LIT:id>' ] , owner = user ) <EOL> tut_entry . title = repo_r_json [ '<STR_LIT:name>' ] <EOL> tut_entry . description = repo_r_json [ '<STR_LIT:description>' ] <EOL> tut_entry . repo_name = repo_r_json [ '<STR_LIT:name>' ] <EOL> tut_entry . repo_url = repo_r_json [ '<STR_LIT:url>' ] <EOL> tut_entry . owner = user <EOL> tut_entry . save ( ) <EOL> commits_r_json = requests . get ( <EOL> repo_r_json [ '<STR_LIT>' ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) + <EOL> ( '<STR_LIT>' % ( <EOL> settings . SOCIAL_AUTH_GITHUB_KEY , <EOL> settings . SOCIAL_AUTH_GITHUB_SECRET ) ) <EOL> ) . json ( ) <EOL> commits_data = [ ] <EOL> for commit in commits_r_json : <EOL> ( title_raw , _ , message_raw ) = commit [ '<STR_LIT>' ] [ '<STR_LIT:message>' ] . partition ( '<STR_LIT:\n>' ) <EOL> commits_data . insert ( <NUM_LIT:0> , { <EOL> '<STR_LIT>' : commit [ '<STR_LIT>' ] , <EOL> '<STR_LIT:title>' : title_raw [ : <NUM_LIT:50> ] , <EOL> '<STR_LIT:message>' : message_raw , <EOL> '<STR_LIT>' : commit [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( username , repo , commit [ '<STR_LIT>' ] ) <EOL> } ) <EOL> build_steps ( username , repo , tut_entry , commits_data ) <EOL> return JsonResponse ( { '<STR_LIT>' : tut_entry . id } ) <EOL> else : <EOL> return HttpResponseNotAllowed ( [ '<STR_LIT:POST>' ] ) <EOL> def tutorial ( request , username , tutnum ) : <EOL> if request . method == '<STR_LIT:GET>' : <EOL> tut_entry = Tutorial . objects . get ( id = tutnum ) <EOL> response = { <EOL> '<STR_LIT:id>' : tutnum , <EOL> '<STR_LIT:title>' : tut_entry . title , <EOL> '<STR_LIT:description>' : tut_entry . description , <EOL> '<STR_LIT>' : tut_entry . repo_name , <EOL> '<STR_LIT>' : tut_entry . repo_url , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:title>' : step . title , <EOL> '<STR_LIT>' : step . content_before , <EOL> '<STR_LIT>' : step . content_after , <EOL> '<STR_LIT>' : step . diff_url , <EOL> '<STR_LIT>' : step . code_url , <EOL> '<STR_LIT>' : json . loads ( step . files ) <EOL> } for step in Step . objects . filter ( tutorial = tutnum ) . order_by ( '<STR_LIT:index>' ) ] <EOL> } <EOL> return JsonResponse ( response ) <EOL> elif request . method == '<STR_LIT>' : <EOL> Tutorial . objects . get ( id = tutnum ) . delete ( ) <EOL> return HttpResponse ( ) <EOL> elif request . method == '<STR_LIT>' : <EOL> patch = json . loads ( request . body ) <EOL> try : <EOL> tut_entry = Tutorial . objects . get ( id = tutnum ) <EOL> tut_entry . title = patch [ '<STR_LIT:title>' ] <EOL> tut_entry . description = patch [ '<STR_LIT:description>' ] <EOL> tut_entry . save ( ) <EOL> index = <NUM_LIT:0> <EOL> for step_json in patch [ '<STR_LIT>' ] : <EOL> index += <NUM_LIT:1> <EOL> step = Step . objects . get ( tutorial = tut_entry , index = index ) <EOL> step . title = patch [ index - <NUM_LIT:1> ] [ '<STR_LIT:title>' ] <EOL> step . content_before = patch [ index - <NUM_LIT:1> ] [ '<STR_LIT>' ] <EOL> step . content_after = patch [ index - <NUM_LIT:1> ] [ '<STR_LIT>' ] <EOL> step . save ( ) <EOL> except Tutorial . DoesNotExist : <EOL> return HttpResponseNotFound ( ) <EOL> else : <EOL> return HttpResponseNotAllowed ( [ '<STR_LIT:POST>' , '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' ] ) </s>
