<s> import StringIO <EOL> import logging <EOL> import zipfile <EOL> from django . core . cache import cache <EOL> from corehq . apps . hqmedia . models import CommCareImage , CommCareAudio , CommCareVideo <EOL> from django . utils . translation import ugettext as _ <EOL> from soil import DownloadBase <EOL> class BaseMultimediaStatusCache ( object ) : <EOL> upload_type = None <EOL> cache_expiry = <NUM_LIT> * <NUM_LIT> <EOL> def __init__ ( self , processing_id ) : <EOL> self . processing_id = processing_id <EOL> self . in_celery = False <EOL> self . complete = False <EOL> self . progress = <NUM_LIT:0> <EOL> self . errors = [ ] <EOL> if self . upload_type is None : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % { <EOL> '<STR_LIT>' : self . processing_id , <EOL> '<STR_LIT>' : self . progress , <EOL> } <EOL> def save ( self ) : <EOL> cache . set ( self . get_cache_key ( self . processing_id ) , self , self . cache_expiry ) <EOL> def mark_with_error ( self , error_str ) : <EOL> self . complete = True <EOL> self . errors . append ( error_str ) <EOL> self . save ( ) <EOL> def get_response ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT:type>' : self . upload_type , <EOL> '<STR_LIT>' : self . in_celery , <EOL> '<STR_LIT>' : self . complete , <EOL> '<STR_LIT>' : self . progress , <EOL> '<STR_LIT>' : self . errors , <EOL> '<STR_LIT>' : self . processing_id , <EOL> } <EOL> @ classmethod <EOL> def get_cache_key ( cls , processing_id ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ classmethod <EOL> def get ( cls , processing_id ) : <EOL> return cache . get ( cls . get_cache_key ( processing_id ) ) <EOL> class BulkMultimediaStatusCache ( BaseMultimediaStatusCache ) : <EOL> upload_type = "<STR_LIT>" <EOL> def __init__ ( self , processing_id ) : <EOL> super ( BulkMultimediaStatusCache , self ) . __init__ ( processing_id ) <EOL> self . skipped_files = [ ] <EOL> self . unmatched_files = [ ] <EOL> self . matched_files = dict ( ( m . __name__ , [ ] ) for m in self . allowed_media ) <EOL> self . total_files = None <EOL> self . processed_files = None <EOL> @ property <EOL> def allowed_media ( self ) : <EOL> return [ CommCareAudio , CommCareImage , CommCareVideo ] <EOL> def get_response ( self ) : <EOL> response = super ( BulkMultimediaStatusCache , self ) . get_response ( ) <EOL> response . update ( { <EOL> '<STR_LIT>' : self . unmatched_files , <EOL> '<STR_LIT>' : self . matched_files , <EOL> '<STR_LIT>' : self . total_files , <EOL> '<STR_LIT>' : self . processed_files , <EOL> '<STR_LIT>' : self . skipped_files , <EOL> } ) <EOL> return response <EOL> def update_progress ( self , num_files_processed ) : <EOL> if self . total_files is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . processed_files = num_files_processed <EOL> self . progress = int ( <NUM_LIT:100> * ( float ( self . processed_files ) / float ( self . total_files ) ) ) <EOL> if self . progress >= <NUM_LIT:100> : <EOL> self . complete = True <EOL> self . save ( ) <EOL> def add_skipped_path ( self , path , mimetype ) : <EOL> self . skipped_files . append ( { <EOL> '<STR_LIT:path>' : path , <EOL> '<STR_LIT>' : mimetype , <EOL> } ) <EOL> def add_unmatched_path ( self , path , reason ) : <EOL> self . unmatched_files . append ( { <EOL> '<STR_LIT:path>' : path , <EOL> '<STR_LIT>' : reason , <EOL> } ) <EOL> def add_matched_path ( self , media_class , media_info ) : <EOL> if media_class . __name__ in self . matched_files : <EOL> self . matched_files [ media_class . __name__ ] . append ( media_info ) <EOL> else : <EOL> self . add_unmatched_path ( media_info [ '<STR_LIT:path>' ] , <EOL> _ ( "<STR_LIT>" % media_class . __name__ ) ) <EOL> def _get_upload_file ( self ) : <EOL> saved_file = StringIO . StringIO ( ) <EOL> try : <EOL> saved_ref = DownloadBase . get ( self . processing_id ) <EOL> data = saved_ref . get_content ( ) <EOL> except Exception as e : <EOL> self . mark_with_error ( _ ( "<STR_LIT>" % e ) ) <EOL> return <EOL> saved_file . write ( data ) <EOL> saved_file . seek ( <NUM_LIT:0> ) <EOL> return saved_file <EOL> def get_upload_zip ( self ) : <EOL> saved_file = self . _get_upload_file ( ) <EOL> try : <EOL> uploaded_zip = zipfile . ZipFile ( saved_file ) <EOL> except Exception as e : <EOL> self . mark_with_error ( _ ( "<STR_LIT>" % e ) ) <EOL> return <EOL> if uploaded_zip . testzip ( ) : <EOL> self . mark_with_error ( _ ( "<STR_LIT>" ) ) <EOL> return <EOL> return uploaded_zip <EOL> @ classmethod <EOL> def get_cache_key ( cls , processing_id ) : <EOL> return "<STR_LIT>" % processing_id <EOL> class BulkMultimediaStatusCacheNfs ( BulkMultimediaStatusCache ) : <EOL> def __init__ ( self , processing_id , file_path ) : <EOL> super ( BulkMultimediaStatusCacheNfs , self ) . __init__ ( processing_id ) <EOL> self . file_path = file_path <EOL> def _get_upload_file ( self ) : <EOL> return self . file_path </s>
