<s> import shutil <EOL> import subprocess <EOL> import os <EOL> import datetime <EOL> import os . path <EOL> from bson import ObjectId <EOL> from galah . db . models import Submission , CSV , TestResult , User , Assignment <EOL> from galah . base . config import load_config <EOL> config = load_config ( "<STR_LIT>" ) <EOL> import logging <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def _create_assignment_csv ( csv_id , requester , assignment ) : <EOL> csv_id = ObjectId ( csv_id ) <EOL> csv_file = temp_directory = "<STR_LIT>" <EOL> deleted_files = [ ] <EOL> for i in CSV . objects ( expires__lt = datetime . datetime . today ( ) ) : <EOL> deleted_files . append ( i . file_location ) <EOL> if i . file_location : <EOL> try : <EOL> os . remove ( i . file_location ) <EOL> except OSError as e : <EOL> logger . warning ( <EOL> "<STR_LIT>" , <EOL> i . file_location , str ( e ) <EOL> ) <EOL> i . delete ( ) <EOL> if deleted_files : <EOL> logger . info ( "<STR_LIT>" , str ( deleted_files ) ) <EOL> new_csv = CSV ( <EOL> id = csv_id , <EOL> requester = requester <EOL> ) <EOL> temp_directory = csv_file = None <EOL> try : <EOL> assn = Assignment . objects . get ( id = ObjectId ( assignment ) ) <EOL> users = list ( <EOL> User . objects ( <EOL> account_type = "<STR_LIT>" , <EOL> classes = assn . for_class <EOL> ) <EOL> ) <EOL> query = { <EOL> "<STR_LIT>" : ObjectId ( assignment ) , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : [ i . id for i in users ] <EOL> } <EOL> submissions = list ( Submission . objects ( ** query ) ) <EOL> csv_file = open ( os . path . join ( config [ "<STR_LIT>" ] , str ( csv_id ) ) , "<STR_LIT:w>" ) <EOL> for i in submissions : <EOL> score = "<STR_LIT:None>" <EOL> if i . test_results : <EOL> test_result = TestResult . objects . get ( id = i . test_results ) <EOL> score = str ( test_result . score ) <EOL> print >> csv_file , "<STR_LIT>" % ( i . user , score , i . timestamp . strftime ( "<STR_LIT>" ) ) <EOL> csv_file . close ( ) <EOL> new_csv . file_location = os . path . join ( config [ "<STR_LIT>" ] , str ( csv_id ) ) <EOL> new_csv . expires = datetime . datetime . today ( ) + config [ "<STR_LIT>" ] <EOL> new_csv . save ( force_insert = True ) <EOL> except Exception as e : <EOL> new_csv . file_location = None <EOL> os . remove ( os . path . join ( config [ "<STR_LIT>" ] , str ( csv_id ) ) ) <EOL> new_csv . error_string = str ( e ) <EOL> new_csv . save ( force_insert = True ) <EOL> raise </s>
