def audit():
 tally = {u'total_files': 0, u'gdoc_count': 0, u'gdoc_types': {}, u'mime_types': {}, u'extensions': {}, u'error': {u'no_history': [], u'name_mime_mismatch': [], u'mime_history_change': [], u'path_history_change': [], u'path_mime_collision': [], u'unsupported_mime_type': [], u'recent_history_mismatch': []}}
 for model_tuple in MODELS:
  (model_type, model) = model_tuple
  google_files = model.find((Q(u'provider', u'eq', u'googledrive') & Q(u'is_file', u'eq', True)))
  for google_file in google_files:
   tally[u'total_files'] += 1
   current_path = google_file.path
   path_ext = _get_extension_from(current_path)
   _tally_extension(tally, u'path', path_ext)
   current_name = google_file.name
   name_ext = _get_extension_from(current_name)
   _tally_extension(tally, u'name', name_ext)
   file_id = u'{}|{}'.format(model_type, google_file._id)
   if (not len(google_file.history)):
    tally[u'error'][u'no_history'].append(u'{}: has no history. Extensions: path({}), name({})'.format(file_id, path_ext, name_ext))
    continue
   mime_type = (google_file.history[(-1)][u'contentType'] or u'')
   tally[u'mime_types'][mime_type] = (tally[u'mime_types'].get(mime_type, 0) + 1)
   if mime_type.startswith(GDOC_MIME_PREFIX):
    tally[u'gdoc_count'] += 1
    gdoc_type = mime_type.replace((GDOC_MIME_PREFIX + u'.'), u'')
    tally[u'gdoc_types'][gdoc_type] = (tally[u'gdoc_types'].get(gdoc_type, 0) + 1)
    gdoc_ext = EXTENSION_FOR.get(mime_type, None)
    if (gdoc_ext is None):
     tally[u'error'][u'unsupported_mime_type'].append(u'{}: Unsupported mime_type: {}'.format(file_id, mime_type))
    elif ((gdoc_ext in HAS_NAME_EXTENSION) and (gdoc_ext != name_ext)):
     tally[u'error'][u'name_mime_mismatch'].append(u"{}: mime type ({}) and name type ({}) don't match".format(file_id, mime_type, name_ext))
     if (path_ext is not u''):
      tally[u'error'][u'path_mime_collision'].append(u'{}: mime extension is ({}) and path extension is ({})'.format(file_id, gdoc_ext, path_ext))
   file_history = []
   for history in google_file.history:
    file_history.append([history[x] for x in (u'contentType', u'name', u'path')])
    if (history[u'contentType'] != mime_type):
     tally[u'error'][u'mime_history_change'].append(u'{}: mime type changed from {} to {}'.format(file_id, mime_type, history[u'contentType']))
    if (history[u'path'] != current_path):
     tally[u'error'][u'path_history_change'].append(u'{}: path changed from {} to {}'.format(file_id, current_path, history[u'path']))
   if ((path_ext != _get_extension_from(google_file.history[(-1)][u'path'])) or (current_name != google_file.history[(-1)][u'name'])):
    tally[u'error'][u'recent_history_mismatch'].append({u'file_id': file_id, u'path': current_path, u'name': current_name, u'history': file_history})
 print u'Tally:\n---'
 print json.dumps(tally)

def audit ( ) : tally = { u'total_files' : 0 , u'gdoc_count' : 0 , u'gdoc_types' : { } , u'mime_types' : { } , u'extensions' : { } , u'error' : { u'no_history' : [ ] , u'name_mime_mismatch' : [ ] , u'mime_history_change' : [ ] , u'path_history_change' : [ ] , u'path_mime_collision' : [ ] , u'unsupported_mime_type' : [ ] , u'recent_history_mismatch' : [ ] } } for model_tuple in MODELS : ( model_type , model ) = model_tuple google_files = model . find ( ( Q ( u'provider' , u'eq' , u'googledrive' ) & Q ( u'is_file' , u'eq' , True ) ) ) for google_file in google_files : tally [ u'total_files' ] += 1 current_path = google_file . path path_ext = _get_extension_from ( current_path ) _tally_extension ( tally , u'path' , path_ext ) current_name = google_file . name name_ext = _get_extension_from ( current_name ) _tally_extension ( tally , u'name' , name_ext ) file_id = u'{}|{}' . format ( model_type , google_file . _id ) if ( not len ( google_file . history ) ) : tally [ u'error' ] [ u'no_history' ] . append ( u'{}: has no history. Extensions: path({}), name({})' . format ( file_id , path_ext , name_ext ) ) continue mime_type = ( google_file . history [ ( - 1 ) ] [ u'contentType' ] or u'' ) tally [ u'mime_types' ] [ mime_type ] = ( tally [ u'mime_types' ] . get ( mime_type , 0 ) + 1 ) if mime_type . startswith ( GDOC_MIME_PREFIX ) : tally [ u'gdoc_count' ] += 1 gdoc_type = mime_type . replace ( ( GDOC_MIME_PREFIX + u'.' ) , u'' ) tally [ u'gdoc_types' ] [ gdoc_type ] = ( tally [ u'gdoc_types' ] . get ( gdoc_type , 0 ) + 1 ) gdoc_ext = EXTENSION_FOR . get ( mime_type , None ) if ( gdoc_ext is None ) : tally [ u'error' ] [ u'unsupported_mime_type' ] . append ( u'{}: Unsupported mime_type: {}' . format ( file_id , mime_type ) ) elif ( ( gdoc_ext in HAS_NAME_EXTENSION ) and ( gdoc_ext != name_ext ) ) : tally [ u'error' ] [ u'name_mime_mismatch' ] . append ( u"{}: mime type ({}) and name type ({}) don't match" . format ( file_id , mime_type , name_ext ) ) if ( path_ext is not u'' ) : tally [ u'error' ] [ u'path_mime_collision' ] . append ( u'{}: mime extension is ({}) and path extension is ({})' . format ( file_id , gdoc_ext , path_ext ) ) file_history = [ ] for history in google_file . history : file_history . append ( [ history [ x ] for x in ( u'contentType' , u'name' , u'path' ) ] ) if ( history [ u'contentType' ] != mime_type ) : tally [ u'error' ] [ u'mime_history_change' ] . append ( u'{}: mime type changed from {} to {}' . format ( file_id , mime_type , history [ u'contentType' ] ) ) if ( history [ u'path' ] != current_path ) : tally [ u'error' ] [ u'path_history_change' ] . append ( u'{}: path changed from {} to {}' . format ( file_id , current_path , history [ u'path' ] ) ) if ( ( path_ext != _get_extension_from ( google_file . history [ ( - 1 ) ] [ u'path' ] ) ) or ( current_name != google_file . history [ ( - 1 ) ] [ u'name' ] ) ) : tally [ u'error' ] [ u'recent_history_mismatch' ] . append ( { u'file_id' : file_id , u'path' : current_path , u'name' : current_name , u'history' : file_history } ) print u'Tally:\n---' print json . dumps ( tally )
restful crud controller for audit logs - used e .