<s> from galah . web import app <EOL> from flask . ext . login import current_user <EOL> from galah . web . auth import account_type_required <EOL> from bson . objectid import ObjectId <EOL> from galah . db . models import Class , Assignment , Submission <EOL> from flask import render_template , request , abort <EOL> import datetime <EOL> from mongoengine import Q <EOL> from galah . web . util import create_time_element , GalahWebAdapter <EOL> import logging <EOL> logger = GalahWebAdapter ( logging . getLogger ( "<STR_LIT>" ) ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ account_type_required ( ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> def browse_assignments ( ) : <EOL> classes = Class . objects ( id__in = current_user . classes ) . only ( "<STR_LIT:name>" ) <EOL> now = datetime . datetime . today ( ) <EOL> if "<STR_LIT>" in request . args : <EOL> assignments = list ( Assignment . objects ( <EOL> Q ( for_class__in = current_user . classes ) & <EOL> ( Q ( hide_until = None ) | Q ( hide_until__lt = now ) ) <EOL> ) . only ( "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> else : <EOL> personal_deadlines = ( current_user . personal_deadline . items ( ) + <EOL> current_user . personal_due_date . items ( ) ) <EOL> due_date_exceptions = set ( ) <EOL> for k , v in personal_deadlines : <EOL> if v > now - datetime . timedelta ( weeks = <NUM_LIT:1> ) : <EOL> due_date_exceptions . add ( ObjectId ( k ) ) <EOL> due_date_exceptions = list ( due_date_exceptions ) <EOL> assignments = list ( Assignment . objects ( <EOL> Q ( for_class__in = current_user . classes ) & <EOL> ( Q ( due__gt = now - datetime . timedelta ( weeks = <NUM_LIT:1> ) ) | <EOL> Q ( due_cutoff__gt = now - datetime . timedelta ( weeks = <NUM_LIT:1> ) ) | <EOL> Q ( id__in = due_date_exceptions ) ) & <EOL> ( Q ( hide_until = None ) | Q ( hide_until__lt = now ) ) <EOL> ) . only ( "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> assignments = [ i for i in assignments if <EOL> not i . hide_until or i . hide_until < now ] <EOL> all_assignments_count = Assignment . objects ( <EOL> Q ( for_class__in = current_user . classes ) & <EOL> ( Q ( hide_until = None ) | Q ( hide_until__lt = now ) ) <EOL> ) . count ( ) <EOL> submissions = list ( Submission . objects ( <EOL> user = current_user . email , <EOL> assignment__in = [ i . id for i in assignments ] , <EOL> most_recent = True <EOL> ) ) <EOL> for i in assignments : <EOL> try : <EOL> i . class_name = next ( ( j . name for j in classes if j . id == i . for_class ) ) <EOL> except StopIteration : <EOL> logger . error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( str ( i . id , i . for_class ) ) <EOL> ) <EOL> i . class_name = "<STR_LIT>" <EOL> i . apply_personal_deadlines ( current_user ) <EOL> submitted = next ( ( j for j in submissions if j . assignment == i . id ) , None ) <EOL> i . submitted = submitted <EOL> i . status = i . status_color = None <EOL> if submitted : <EOL> i . status = ( <EOL> "<STR_LIT>" + <EOL> create_time_element ( submitted . timestamp , now ) <EOL> ) <EOL> i . status_color = "<STR_LIT>" <EOL> elif now < i . due : <EOL> i . status = "<STR_LIT>" <EOL> i . status_color = "<STR_LIT>" <EOL> elif now > i . due and i . due_cutoff and now > i . due_cutoff : <EOL> i . status = "<STR_LIT>" <EOL> i . status_color = "<STR_LIT>" <EOL> elif now > i . due : <EOL> i . status = "<STR_LIT>" <EOL> i . status_color = "<STR_LIT>" <EOL> assignments . sort ( <EOL> key = lambda i : i . due_cutoff if i . due_cutoff else i . due , <EOL> reverse = True <EOL> ) <EOL> return render_template ( <EOL> "<STR_LIT>" , <EOL> assignments = assignments , <EOL> hidden_assignments = - <NUM_LIT:1> if "<STR_LIT>" in request . args <EOL> else all_assignments_count - len ( assignments ) , <EOL> create_time_element = create_time_element <EOL> ) </s>
