<s> import datetime <EOL> from django . apps import apps <EOL> from django . conf import settings <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . auth . views import login <EOL> from django . core . cache import cache <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import Q <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import render , redirect <EOL> from aristotle_mdr import forms as MDRForms <EOL> from aristotle_mdr import models as MDR <EOL> from aristotle_mdr . views . utils import paginated_list , paginated_workgroup_list <EOL> def friendly_redirect_login ( request ) : <EOL> if request . user . is_authenticated ( ) : <EOL> if '<STR_LIT>' in request . GET : <EOL> return HttpResponseRedirect ( request . GET . get ( '<STR_LIT>' ) ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> else : <EOL> return login ( request ) <EOL> @ login_required <EOL> def home ( request ) : <EOL> from reversion . models import Revision <EOL> recent = Revision . objects . filter ( user = request . user ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> : <NUM_LIT:10> ] <EOL> page = render ( request , "<STR_LIT>" , { "<STR_LIT>" : request . user , '<STR_LIT>' : recent } ) <EOL> return page <EOL> @ login_required <EOL> def roles ( request ) : <EOL> page = render ( request , "<STR_LIT>" , { "<STR_LIT>" : request . user } ) <EOL> return page <EOL> @ login_required <EOL> def recent ( request ) : <EOL> from reversion . models import Revision <EOL> from aristotle_mdr . views . utils import paginated_reversion_list <EOL> items = Revision . objects . filter ( user = request . user ) . order_by ( '<STR_LIT>' ) <EOL> context = { } <EOL> return paginated_reversion_list ( request , items , "<STR_LIT>" , context ) <EOL> @ login_required <EOL> def inbox ( request , folder = None ) : <EOL> if folder is None : <EOL> folder = '<STR_LIT>' <EOL> folder = folder . lower ( ) <EOL> if folder == '<STR_LIT>' : <EOL> notices = request . user . notifications . unread ( ) . all ( ) <EOL> elif folder == "<STR_LIT:all>" : <EOL> notices = request . user . notifications . all ( ) <EOL> page = render ( <EOL> request , <EOL> "<STR_LIT>" , <EOL> { "<STR_LIT>" : request . user , "<STR_LIT>" : notices , '<STR_LIT>' : folder } <EOL> ) <EOL> return page <EOL> @ login_required <EOL> def admin_tools ( request ) : <EOL> if not request . user . is_superuser : <EOL> raise PermissionDenied <EOL> aristotle_apps = getattr ( settings , '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , [ ] ) <EOL> aristotle_apps += [ "<STR_LIT>" ] <EOL> from django . contrib . contenttypes . models import ContentType <EOL> models = ContentType . objects . filter ( app_label__in = aristotle_apps ) . all ( ) <EOL> model_stats = { } <EOL> for m in models : <EOL> if issubclass ( m . model_class ( ) , MDR . _concept ) and not m . model . startswith ( "<STR_LIT:_>" ) : <EOL> app_models = model_stats . get ( m . app_label , { '<STR_LIT>' : None , '<STR_LIT>' : [ ] } ) <EOL> if app_models [ '<STR_LIT>' ] is None : <EOL> app_models [ '<STR_LIT>' ] = getattr ( apps . get_app_config ( m . app_label ) , '<STR_LIT>' ) <EOL> app_models [ '<STR_LIT>' ] . append ( <EOL> ( <EOL> m . model_class ( ) , <EOL> get_cached_object_count ( m ) , <EOL> reverse ( "<STR_LIT>" % ( m . app_label , m . model ) ) <EOL> ) <EOL> ) <EOL> model_stats [ m . app_label ] = app_models <EOL> page = render ( <EOL> request , <EOL> "<STR_LIT>" , <EOL> { "<STR_LIT>" : request . user , "<STR_LIT>" : model_stats } <EOL> ) <EOL> return page <EOL> @ login_required <EOL> def admin_stats ( request ) : <EOL> if not request . user . is_superuser : <EOL> raise PermissionDenied <EOL> aristotle_apps = getattr ( settings , '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , [ ] ) <EOL> aristotle_apps += [ "<STR_LIT>" ] <EOL> from django . contrib . contenttypes . models import ContentType <EOL> models = ContentType . objects . filter ( app_label__in = aristotle_apps ) . all ( ) <EOL> model_stats = { } <EOL> t7 = datetime . date . today ( ) - datetime . timedelta ( days = <NUM_LIT:7> ) <EOL> t30 = datetime . date . today ( ) - datetime . timedelta ( days = <NUM_LIT:30> ) <EOL> mod_counts = [ ] <EOL> use_cache = True <EOL> for m in models : <EOL> if issubclass ( m . model_class ( ) , MDR . _concept ) and not m . model . startswith ( "<STR_LIT:_>" ) : <EOL> app_models = model_stats . get ( m . app_label , { '<STR_LIT>' : None , '<STR_LIT>' : [ ] } ) <EOL> if app_models [ '<STR_LIT>' ] is None : <EOL> app_models [ '<STR_LIT>' ] = getattr ( apps . get_app_config ( m . app_label ) , '<STR_LIT>' ) <EOL> if use_cache : <EOL> total = get_cached_query_count ( <EOL> qs = m . model_class ( ) . objects , <EOL> key = model_to_cache_key ( m ) + "<STR_LIT>" , <EOL> ttl = <NUM_LIT> <EOL> ) <EOL> t7_val = get_cached_query_count ( <EOL> qs = m . model_class ( ) . objects . filter ( created__gte = t7 ) , <EOL> key = model_to_cache_key ( m ) + "<STR_LIT>" , <EOL> ttl = <NUM_LIT> <EOL> ) <EOL> t30_val = get_cached_query_count ( <EOL> qs = m . model_class ( ) . objects . filter ( created__gte = t30 ) , <EOL> key = model_to_cache_key ( m ) + "<STR_LIT>" , <EOL> ttl = <NUM_LIT> <EOL> ) <EOL> else : <EOL> total = m . model_class ( ) . objects . count ( ) <EOL> t7_val = m . model_class ( ) . objects . filter ( created__gte = t7 ) . count ( ) <EOL> t30_val = m . model_class ( ) . objects . filter ( created__gte = t30 ) . count ( ) <EOL> mod_counts . append ( total ) <EOL> app_models [ '<STR_LIT>' ] . append ( <EOL> ( <EOL> m . model_class ( ) , <EOL> { <EOL> '<STR_LIT>' : total , <EOL> '<STR_LIT>' : t7_val , <EOL> '<STR_LIT>' : t30_val <EOL> } , <EOL> reverse ( "<STR_LIT>" % ( m . app_label , m . model ) ) <EOL> ) <EOL> ) <EOL> model_stats [ m . app_label ] = app_models <EOL> page = render ( <EOL> request , "<STR_LIT>" , <EOL> { "<STR_LIT>" : request . user , "<STR_LIT>" : model_stats , '<STR_LIT>' : max ( mod_counts ) } <EOL> ) <EOL> return page <EOL> def get_cached_query_count ( qs , key , ttl ) : <EOL> count = cache . get ( key , None ) <EOL> if not count : <EOL> count = qs . count ( ) <EOL> cache . set ( key , count , ttl ) <EOL> return count <EOL> def model_to_cache_key ( model_type ) : <EOL> return '<STR_LIT>' % ( model_type . app_label , model_type . model ) <EOL> def get_cached_object_count ( model_type ) : <EOL> CACHE_KEY = model_to_cache_key ( model_type ) <EOL> query = model_type . model_class ( ) . objects <EOL> return get_cached_query_count ( query , CACHE_KEY , <NUM_LIT> * <NUM_LIT> * <NUM_LIT:12> ) <EOL> @ login_required <EOL> def edit ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = MDRForms . UserSelfEditForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> request . user . first_name = form . cleaned_data [ '<STR_LIT>' ] <EOL> request . user . last_name = form . cleaned_data [ '<STR_LIT>' ] <EOL> request . user . email = form . cleaned_data [ '<STR_LIT:email>' ] <EOL> request . user . save ( ) <EOL> return redirect ( reverse ( '<STR_LIT>' , ) ) <EOL> else : <EOL> form = MDRForms . UserSelfEditForm ( { <EOL> '<STR_LIT>' : request . user . first_name , <EOL> '<STR_LIT>' : request . user . last_name , <EOL> '<STR_LIT:email>' : request . user . email , <EOL> } ) <EOL> return render ( request , "<STR_LIT>" , { "<STR_LIT>" : form } ) <EOL> @ login_required <EOL> def favourites ( request ) : <EOL> items = request . user . profile . favourites . select_subclasses ( ) <EOL> context = { <EOL> '<STR_LIT>' : request . GET . get ( "<STR_LIT>" , False ) , <EOL> '<STR_LIT>' : request . GET . get ( "<STR_LIT>" , False ) <EOL> } <EOL> return paginated_list ( request , items , "<STR_LIT>" , context ) <EOL> @ login_required <EOL> def registrar_tools ( request ) : <EOL> if not request . user . profile . is_registrar : <EOL> raise PermissionDenied <EOL> page = render ( request , "<STR_LIT>" ) <EOL> return page <EOL> @ login_required <EOL> def review_list ( request ) : <EOL> if not request . user . profile . is_registrar : <EOL> raise PermissionDenied <EOL> items = MDR . _concept . objects . visible ( request . user ) . filter ( readyToReview = True , statuses = None ) . select_subclasses ( ) <EOL> return paginated_list ( request , items , "<STR_LIT>" , { '<STR_LIT>' : items } ) <EOL> @ login_required <EOL> def workgroups ( request ) : <EOL> text_filter = request . GET . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> workgroups = request . user . profile . myWorkgroups <EOL> if text_filter : <EOL> workgroups = workgroups . filter ( Q ( name__icontains = text_filter ) | Q ( definition__icontains = text_filter ) ) <EOL> context = { '<STR_LIT>' : text_filter } <EOL> return paginated_workgroup_list ( request , workgroups , "<STR_LIT>" , context ) <EOL> @ login_required <EOL> def workgroup_archives ( request ) : <EOL> text_filter = request . GET . get ( '<STR_LIT>' , None ) <EOL> workgroups = request . user . profile . workgroups . filter ( archived = True ) <EOL> if text_filter : <EOL> workgroups = workgroups . filter ( Q ( name__icontains = text_filter ) | Q ( definition__icontains = text_filter ) ) <EOL> context = { '<STR_LIT>' : text_filter } <EOL> return paginated_workgroup_list ( request , workgroups , "<STR_LIT>" , context ) </s>
