<s> from django . http import HttpResponse , HttpResponseRedirect <EOL> from django . core . urlresolvers import reverse <EOL> from django . shortcuts import render , get_object_or_404 <EOL> from django . views . decorators . csrf import csrf_protect <EOL> from django . contrib . auth . decorators import login_required <EOL> import messages_shim as messages <EOL> from django . contrib . sites . models import Site <EOL> from django . contrib . auth . models import Group <EOL> from skills . models import Skill , TrainingBit , Topic , Image , Comment , Project <EOL> from allauth . account . models import EmailAddress <EOL> from global_change_lab . models import User , GCLFlatPage <EOL> from global_change_lab . forms import UserInfoForm <EOL> from django . db . models import Q <EOL> from datetime import datetime , timedelta <EOL> import csv , json <EOL> def front_page ( request ) : <EOL> if request . user . is_authenticated ( ) : <EOL> return profile ( request ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : Skill . objects . all ( ) , <EOL> } ) <EOL> def new_user ( request ) : <EOL> return render ( request , '<STR_LIT>' , { <EOL> } ) <EOL> @ csrf_protect <EOL> def new_user_input_details ( request ) : <EOL> form = None <EOL> if request . POST : <EOL> form = UserInfoForm ( request . POST , instance = request . user ) <EOL> if form . is_valid ( ) : <EOL> userinfo = form . save ( ) <EOL> request . user . has_been_welcomed = True <EOL> request . user . save ( ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> else : <EOL> form = UserInfoForm ( instance = request . user ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' ) , <EOL> } ) <EOL> def new_user_topics ( request ) : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : Topic . objects . all ( ) , <EOL> } ) <EOL> @ csrf_protect <EOL> def new_user_suggestions ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> topic_ids = [ int ( pk ) for pk in request . POST . getlist ( '<STR_LIT>' ) ] <EOL> topics = Topic . objects . filter ( id__in = topic_ids ) <EOL> trainingbits = [ ] <EOL> skills = [ ] <EOL> for topic in topics [ : <NUM_LIT:4> ] : <EOL> trainingbit_pks = [ t . pk for t in trainingbits ] <EOL> trainingbits += topic . trainingbits . exclude ( pk__in = trainingbit_pks ) . filter ( is_draft = False ) [ : <NUM_LIT:2> ] <EOL> skill_pks = [ s . pk for s in skills ] <EOL> skills += topic . skills . exclude ( pk__in = skill_pks ) . filter ( is_draft = False ) [ : <NUM_LIT:2> ] <EOL> tb_pks = [ tb . pk for tb in trainingbits ] <EOL> if len ( trainingbits ) < <NUM_LIT:4> : <EOL> missing_count = <NUM_LIT:4> - len ( trainingbits ) <EOL> trainingbits += TrainingBit . objects . filter ( is_draft = False ) . exclude ( pk__in = tb_pks ) [ : missing_count ] <EOL> skill_pks = [ s . pk for s in skills ] <EOL> if len ( skills ) < <NUM_LIT:4> : <EOL> missing_count = <NUM_LIT:4> - len ( skills ) <EOL> skills += Skill . objects . filter ( is_draft = False ) . exclude ( pk__in = skill_pks ) [ : missing_count ] <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : trainingbits [ : <NUM_LIT:4> ] , <EOL> '<STR_LIT>' : skills [ : <NUM_LIT:4> ] , <EOL> } ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> def shares ( request ) : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : None , <EOL> } ) <EOL> from django . contrib import comments <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . conf import settings <EOL> def trainer_dashboard ( request ) : <EOL> all_trainingbits = request . user . trainingbit_set . all ( ) <EOL> trainingbits_public = list ( filter ( lambda t : not t . is_draft , all_trainingbits ) ) <EOL> trainingbits_drafts = list ( filter ( lambda t : t . is_draft , all_trainingbits ) ) <EOL> all_skills = request . user . skill_set . all ( ) <EOL> skills_public = list ( filter ( lambda s : not s . is_draft , all_skills ) ) <EOL> skills_drafts = list ( filter ( lambda s : s . is_draft , all_skills ) ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : trainingbits_public , <EOL> '<STR_LIT>' : trainingbits_drafts , <EOL> '<STR_LIT>' : skills_public , <EOL> '<STR_LIT>' : skills_drafts , <EOL> '<STR_LIT>' : Topic . objects . all ( ) , <EOL> '<STR_LIT>' : GCLFlatPage . objects . all ( ) , <EOL> } ) <EOL> def statistics ( request ) : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : TrainingBit . objects . filter ( is_draft = False ) , <EOL> '<STR_LIT>' : Skill . objects . filter ( is_draft = False ) , <EOL> } ) <EOL> def get_user_statistics ( ) : <EOL> one_week_ago = datetime . now ( ) - timedelta ( days = <NUM_LIT:7> ) <EOL> one_month_ago = datetime . now ( ) - timedelta ( days = <NUM_LIT:30> ) <EOL> one_year_ago = datetime . now ( ) - timedelta ( days = <NUM_LIT> ) <EOL> num_users_last_week = len ( User . objects . filter ( datetime_joined__gt = one_week_ago ) . distinct ( ) ) <EOL> num_users_last_month = len ( User . objects . filter ( datetime_joined__gt = one_month_ago ) . distinct ( ) ) <EOL> num_users_last_year = len ( User . objects . filter ( datetime_joined__gt = one_year_ago ) . distinct ( ) ) <EOL> return { <EOL> "<STR_LIT>" : num_users_last_week , <EOL> "<STR_LIT>" : num_users_last_month , <EOL> "<STR_LIT>" : num_users_last_year , <EOL> } <EOL> def admin_dashboard ( request ) : <EOL> num_users = len ( User . objects . distinct ( ) ) <EOL> template_dict = { <EOL> "<STR_LIT>" : num_users , <EOL> } <EOL> template_dict . update ( get_user_statistics ( ) ) <EOL> return render ( request , '<STR_LIT>' , template_dict ) <EOL> def admin_flagged_comments ( request ) : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : Comment . objects . filter ( is_deleted = False , is_flagged = True , project__is_deleted = False ) , <EOL> } ) <EOL> def admin_flagged_shares ( request ) : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : Project . objects . filter ( is_deleted = False , is_flagged = True ) , <EOL> } ) <EOL> def admin_users_csv ( request ) : <EOL> response = HttpResponse ( content_type = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> fields_to_save = { <EOL> "<STR_LIT:username>" : lambda u : u . username , <EOL> "<STR_LIT>" : lambda u : u . datetime_joined , <EOL> "<STR_LIT:email>" : lambda u : u . email , <EOL> "<STR_LIT:description>" : lambda u : u . description , <EOL> "<STR_LIT>" : lambda u : u . userinfo . get_sex_display ( ) , <EOL> "<STR_LIT>" : lambda u : u . userinfo . country . name , <EOL> "<STR_LIT>" : lambda u : u . userinfo . birthdate , <EOL> "<STR_LIT>" : lambda u : u . userinfo . get_organization_display ( ) , <EOL> "<STR_LIT>" : lambda u : '<STR_LIT:;>' . join ( map ( lambda s : s . name , u . skills_completed . all ( ) ) ) , <EOL> "<STR_LIT>" : lambda u : '<STR_LIT:;>' . join ( map ( lambda s : s . name , u . skills_in_progress . all ( ) ) ) , <EOL> "<STR_LIT>" : lambda u : '<STR_LIT:;>' . join ( map ( lambda s : s . name , u . trainingbits_completed . all ( ) ) ) , <EOL> "<STR_LIT>" : lambda u : '<STR_LIT:;>' . join ( map ( lambda s : s . name , u . trainingbits_in_progress . all ( ) ) ) , <EOL> } <EOL> writer = csv . writer ( response ) <EOL> writer . writerow ( list ( fields_to_save . keys ( ) ) ) <EOL> for user in User . objects . distinct ( ) : <EOL> l = [ ] <EOL> for field in fields_to_save . values ( ) : <EOL> l . append ( field ( user ) ) <EOL> writer . writerow ( l ) <EOL> return response <EOL> def admin_statistics_csv ( request ) : <EOL> response = HttpResponse ( content_type = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> writer = csv . writer ( response ) <EOL> writer . writerow ( [ "<STR_LIT>" , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> num_users = len ( User . objects . distinct ( ) ) <EOL> d = get_user_statistics ( ) <EOL> writer . writerow ( [ num_users , d [ '<STR_LIT>' ] , d [ '<STR_LIT>' ] , d [ '<STR_LIT>' ] ] ) <EOL> return response <EOL> @ login_required <EOL> @ csrf_protect <EOL> def upload_picture ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> image = Image ( ) <EOL> image . image = request . FILES [ '<STR_LIT>' ] <EOL> image . identifier = request . POST [ '<STR_LIT>' ] <EOL> image . author = request . user <EOL> image . save ( ) <EOL> result = [ <EOL> { <EOL> "<STR_LIT:url>" : image . image . url , <EOL> } , <EOL> ] <EOL> response_data = json . dumps ( result ) <EOL> return HttpResponse ( response_data , mimetype = '<STR_LIT:application/json>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT:/>' ) ) <EOL> def upload_profile_picture ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> request . user . image = request . FILES [ '<STR_LIT>' ] <EOL> request . user . save ( ) <EOL> result = [ <EOL> { <EOL> "<STR_LIT:url>" : request . user . image . url , <EOL> } , <EOL> ] <EOL> response_data = json . dumps ( result ) <EOL> return HttpResponse ( response_data , mimetype = '<STR_LIT:application/json>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> def profile ( request , user_id = None ) : <EOL> form = None <EOL> if request . POST : <EOL> form = UserInfoForm ( request . POST , instance = request . user ) <EOL> if form . is_valid ( ) : <EOL> userinfo = form . save ( ) <EOL> primary_emailaddress = EmailAddress . objects . get_primary ( userinfo ) <EOL> if primary_emailaddress and primary_emailaddress . email != userinfo . email : <EOL> primary_emailaddress . change ( request , userinfo . email ) <EOL> if user_id is None : <EOL> profile_user = request . user <EOL> form = UserInfoForm ( instance = request . user ) <EOL> else : <EOL> profile_user = get_object_or_404 ( User , pk = user_id ) <EOL> newest_skills = profile_user . skills_completed . all ( ) [ : <NUM_LIT:3> ] <EOL> other_skills_count = profile_user . skills_completed . count ( ) - <NUM_LIT:3> <EOL> if request . user == profile_user : <EOL> own_profile = True <EOL> trainingbits_completed = request . user . trainingbits_completed . values_list ( '<STR_LIT>' , flat = True ) <EOL> skills_in_progress_w_percentage = [ ] <EOL> skills_in_progress = request . user . skills_in_progress . filter ( is_draft = False ) . prefetch_related ( '<STR_LIT>' ) <EOL> for skill in skills_in_progress : <EOL> tbs = skill . trainingbits . all ( ) <EOL> incompleted_count = len ( set ( [ t . pk for t in tbs ] ) - set ( trainingbits_completed ) ) <EOL> total_count = len ( tbs ) <EOL> if total_count == <NUM_LIT:0> : <EOL> percentage = <NUM_LIT:0> <EOL> else : <EOL> percentage = <NUM_LIT:1.0> - ( incompleted_count / total_count ) <EOL> percentage *= <NUM_LIT:100> <EOL> skill . percentage = percentage <EOL> skills_in_progress_w_percentage . append ( ( skill , int ( percentage ) ) ) <EOL> trainingbits_in_progress = request . user . trainingbits_in_progress . filter ( is_draft = False ) <EOL> in_progress_dict = { <EOL> '<STR_LIT>' : skills_in_progress_w_percentage , <EOL> '<STR_LIT>' : skills_in_progress , <EOL> '<STR_LIT>' : trainingbits_in_progress , <EOL> } <EOL> else : <EOL> own_profile = False <EOL> in_progress_dict = { } <EOL> projects = list ( profile_user . project_set . filter ( is_deleted = False , trainingbit__is_draft = False ) . order_by ( '<STR_LIT>' ) . select_related ( '<STR_LIT>' ) . prefetch_related ( '<STR_LIT>' ) [ : <NUM_LIT:12> ] ) <EOL> project_list0 = projects [ <NUM_LIT:0> : : <NUM_LIT:3> ] <EOL> project_list1 = projects [ <NUM_LIT:1> : : <NUM_LIT:3> ] <EOL> project_list2 = projects [ <NUM_LIT:2> : : <NUM_LIT:3> ] <EOL> template_dict = { <EOL> '<STR_LIT>' : own_profile , <EOL> '<STR_LIT>' : profile_user , <EOL> '<STR_LIT>' : newest_skills , <EOL> '<STR_LIT>' : other_skills_count , <EOL> '<STR_LIT>' : project_list0 , <EOL> '<STR_LIT>' : project_list1 , <EOL> '<STR_LIT>' : project_list2 , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' ) , <EOL> } <EOL> template_dict . update ( in_progress_dict ) <EOL> return render ( request , '<STR_LIT>' , template_dict ) <EOL> def user_list ( request ) : <EOL> new_users = User . objects . all ( ) . order_by ( '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : new_users , <EOL> } ) <EOL> def user_delete ( request , user_id ) : <EOL> user = User . objects . get ( id__exact = user_id ) <EOL> if user == request . user : <EOL> user . delete ( ) <EOL> messages . success ( request , '<STR_LIT>' ) <EOL> else : <EOL> messages . error ( request , '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> def user_upgrade_to_trainer ( request , user_id ) : <EOL> user = User . objects . get ( id__exact = user_id ) <EOL> if request . user . is_admin : <EOL> g = Group . objects . get ( name = '<STR_LIT>' ) <EOL> g . user_set . add ( user ) <EOL> messages . success ( request , '<STR_LIT>' % user . username ) <EOL> else : <EOL> messages . error ( request , '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = [ user_id ] ) ) <EOL> @ csrf_protect <EOL> def page_new ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> flatpage = GCLFlatPage ( ) <EOL> flatpage . url = '<STR_LIT:/>' + request . POST [ '<STR_LIT:title>' ] . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:->' ) . lower ( ) + '<STR_LIT:/>' <EOL> flatpage . title = request . POST [ '<STR_LIT:title>' ] <EOL> flatpage . content = request . POST [ '<STR_LIT:content>' ] <EOL> flatpage . save ( ) <EOL> flatpage . sites . add ( * Site . objects . all ( ) ) <EOL> messages . success ( request , '<STR_LIT>' % flatpage . title ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = [ flatpage . url ] ) ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> @ csrf_protect <EOL> def page_edit ( request , page_pk ) : <EOL> flatpage = get_object_or_404 ( GCLFlatPage , pk = page_pk ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> flatpage . content = request . POST [ '<STR_LIT:content>' ] <EOL> flatpage . save ( ) <EOL> messages . success ( request , '<STR_LIT>' % flatpage . title ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = [ flatpage . url ] ) ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> @ csrf_protect <EOL> def page_delete ( request , page_pk ) : <EOL> page = get_object_or_404 ( GCLFlatPage , pk = page_pk ) <EOL> page . delete ( ) <EOL> messages . success ( request , '<STR_LIT>' % page . title ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) </s>
