<s> from datetime import datetime , timedelta <EOL> from sqlalchemy . exc import IntegrityError <EOL> from grouper . constants import AUDIT_MANAGER <EOL> from grouper . email_util import send_email , send_async_email <EOL> from grouper . fe . forms import AuditCreateForm <EOL> from grouper . fe . settings import settings <EOL> from grouper . fe . util import GrouperHandler <EOL> from grouper . model_soup import Audit , AuditMember , GROUP_EDGE_ROLES , Group <EOL> from grouper . models . audit_log import AuditLogCategory , AuditLog <EOL> class AuditsCreate ( GrouperHandler ) : <EOL> def get ( self ) : <EOL> user = self . get_current_user ( ) <EOL> if not user . has_permission ( AUDIT_MANAGER ) : <EOL> return self . forbidden ( ) <EOL> self . render ( <EOL> "<STR_LIT>" , form = AuditCreateForm ( ) , <EOL> ) <EOL> def post ( self ) : <EOL> form = AuditCreateForm ( self . request . arguments ) <EOL> if not form . validate ( ) : <EOL> return self . render ( <EOL> "<STR_LIT>" , form = form , <EOL> alerts = self . get_form_alerts ( form . errors ) <EOL> ) <EOL> user = self . get_current_user ( ) <EOL> if not user . has_permission ( AUDIT_MANAGER ) : <EOL> return self . forbidden ( ) <EOL> open_audits = self . session . query ( Audit ) . filter ( <EOL> Audit . complete == False ) . all ( ) <EOL> if open_audits : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> ends_at = datetime . strptime ( form . data [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> audited_groups = [ ] <EOL> for groupname in self . graph . groups : <EOL> if not self . graph . get_group_details ( groupname ) [ "<STR_LIT>" ] : <EOL> continue <EOL> group = Group . get ( self . session , name = groupname ) <EOL> audit = Audit ( <EOL> group_id = group . id , <EOL> ends_at = ends_at , <EOL> ) <EOL> try : <EOL> audit . add ( self . session ) <EOL> self . session . flush ( ) <EOL> except IntegrityError : <EOL> self . session . rollback ( ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> audited_groups . append ( group ) <EOL> group . audit_id = audit . id <EOL> for member in group . my_members ( ) . values ( ) : <EOL> auditmember = AuditMember ( <EOL> audit_id = audit . id , edge_id = member . edge_id <EOL> ) <EOL> try : <EOL> auditmember . add ( self . session ) <EOL> except IntegrityError : <EOL> self . session . rollback ( ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . session . commit ( ) <EOL> AuditLog . log ( self . session , self . current_user . id , '<STR_LIT>' , <EOL> '<STR_LIT>' , category = AuditLogCategory . audit ) <EOL> schedule_times = [ ] <EOL> not_before = datetime . utcnow ( ) + timedelta ( <NUM_LIT:1> ) <EOL> for days_prior in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:3> , <NUM_LIT:1> ) : <EOL> email_time = ends_at - timedelta ( days_prior ) <EOL> email_time . replace ( hour = <NUM_LIT> , minute = <NUM_LIT:0> , second = <NUM_LIT:0> ) <EOL> if email_time > not_before : <EOL> schedule_times . append ( ( days_prior , email_time ) ) <EOL> for group in audited_groups : <EOL> mail_to = [ <EOL> member . name <EOL> for member in group . my_users ( ) <EOL> if GROUP_EDGE_ROLES [ member . role ] in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] <EOL> send_email ( self . session , mail_to , '<STR_LIT>' . format ( group . name ) , '<STR_LIT>' , <EOL> settings , { "<STR_LIT>" : group . name , "<STR_LIT>" : ends_at } ) <EOL> for days_prior , email_time in schedule_times : <EOL> send_async_email ( <EOL> self . session , <EOL> mail_to , <EOL> '<STR_LIT>' . format ( group . name , days_prior ) , <EOL> '<STR_LIT>' , <EOL> settings , <EOL> { <EOL> "<STR_LIT>" : group . name , <EOL> "<STR_LIT>" : ends_at , <EOL> "<STR_LIT>" : days_prior , <EOL> } , <EOL> email_time , <EOL> async_key = '<STR_LIT>' . format ( group . id ) , <EOL> ) <EOL> return self . redirect ( "<STR_LIT>" ) </s>
