<s> import logging <EOL> import sys <EOL> from modularodm import Q <EOL> from website . app import init_app <EOL> from website . project . model import Comment <EOL> from framework . transactions . context import TokuTransaction <EOL> from scripts import utils as script_utils <EOL> logger = logging . getLogger ( __name__ ) <EOL> def migrate_status ( records ) : <EOL> for record in records : <EOL> if len ( record . reports ) > <NUM_LIT:0> : <EOL> record . spam_status = Comment . FLAGGED <EOL> else : <EOL> record . spam_status = Comment . UNKNOWN <EOL> record . save ( ) <EOL> logger . info ( '<STR_LIT>' . format ( record . _id ) ) <EOL> def migrate_latest ( records ) : <EOL> for record in records : <EOL> date = None <EOL> for user , report in record . reports . iteritems ( ) : <EOL> if date is None : <EOL> date = report . get ( '<STR_LIT:date>' ) <EOL> elif date < report . get ( '<STR_LIT:date>' ) : <EOL> date = report . get ( '<STR_LIT:date>' ) <EOL> record . date_last_reported = date <EOL> record . save ( ) <EOL> logger . info ( '<STR_LIT>' . format ( record . _id ) ) <EOL> def get_no_status_targets ( ) : <EOL> return Comment . find ( <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> ) <EOL> def get_no_latest_targets ( ) : <EOL> query = ( <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , None ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , Comment . UNKNOWN ) <EOL> ) <EOL> return Comment . find ( query ) <EOL> def main ( ) : <EOL> dry_run = False <EOL> status = True <EOL> latest = True <EOL> if '<STR_LIT>' in sys . argv : <EOL> dry_run = True <EOL> if '<STR_LIT>' in sys . argv : <EOL> status = False <EOL> if '<STR_LIT>' in sys . argv : <EOL> latest = False <EOL> if not dry_run : <EOL> script_utils . add_file_logger ( logger , __file__ ) <EOL> init_app ( set_backends = True , routes = False ) <EOL> with TokuTransaction ( ) : <EOL> if status : <EOL> migrate_status ( get_no_status_targets ( ) ) <EOL> if latest : <EOL> migrate_latest ( get_no_latest_targets ( ) ) <EOL> if dry_run : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
