<s> from tapiriik . database import db <EOL> from tapiriik . auth import User <EOL> from tapiriik . services import Service <EOL> import datetime <EOL> import logging <EOL> import json <EOL> from bson . objectid import ObjectId <EOL> logger = logging . getLogger ( __name__ ) <EOL> class RollbackTask : <EOL> def __new__ ( cls , dbRec ) : <EOL> if not dbRec : <EOL> return None <EOL> return super ( RollbackTask , cls ) . __new__ ( cls ) <EOL> def __init__ ( self , dbRec ) : <EOL> self . __dict__ . update ( dbRec ) <EOL> def _create ( user ) : <EOL> logger . info ( "<STR_LIT>" % user [ "<STR_LIT>" ] ) <EOL> conns = User . GetConnectionRecordsByUser ( user ) <EOL> my_services = [ conn . Service . ID for conn in conns ] <EOL> my_ext_ids = [ conn . ExternalID for conn in conns ] <EOL> logger . info ( "<STR_LIT>" % ( my_services , my_ext_ids ) ) <EOL> uploads = db . uploaded_activities . find ( { "<STR_LIT>" : { "<STR_LIT>" : my_services } , "<STR_LIT>" : { "<STR_LIT>" : my_ext_ids } } ) <EOL> pending_deletions = { } <EOL> for upload in uploads : <EOL> svc = upload [ "<STR_LIT>" ] <EOL> upload_id = upload [ "<STR_LIT>" ] <EOL> svc_ext_id = upload [ "<STR_LIT>" ] <EOL> if my_services . index ( svc ) != my_ext_ids . index ( svc_ext_id ) : <EOL> continue <EOL> if svc not in pending_deletions : <EOL> pending_deletions [ svc ] = [ ] <EOL> pending_deletions [ svc ] . append ( upload_id ) <EOL> return RollbackTask ( { "<STR_LIT>" : pending_deletions } ) <EOL> def Create ( user ) : <EOL> task = RollbackTask . _create ( user ) <EOL> uid = db . rollback_tasks . insert ( { "<STR_LIT>" : task . PendingDeletions , "<STR_LIT>" : datetime . datetime . utcnow ( ) , "<STR_LIT>" : user [ "<STR_LIT>" ] } ) <EOL> logger . info ( "<STR_LIT>" % uid ) <EOL> task . _id = uid <EOL> return task <EOL> def Get ( id ) : <EOL> dbRec = db . rollback_tasks . find_one ( { "<STR_LIT>" : ObjectId ( id ) } ) <EOL> if not dbRec : <EOL> return <EOL> return RollbackTask ( dbRec ) <EOL> def json ( self ) : <EOL> self . ActivityURLs = { svc : { } for svc in self . PendingDeletions . keys ( ) } <EOL> for svc_id , urls in self . ActivityURLs . items ( ) : <EOL> svc = Service . FromID ( svc_id ) <EOL> for upload in self . PendingDeletions [ svc_id ] : <EOL> try : <EOL> urls [ upload ] = svc . UserUploadedActivityURL ( upload ) <EOL> except NotImplementedError : <EOL> pass <EOL> self . PendingDeletionCount = sum ( [ len ( v ) for k , v in self . PendingDeletions . items ( ) ] ) <EOL> dthandler = lambda obj : obj . isoformat ( ) if isinstance ( obj , datetime . datetime ) or isinstance ( obj , datetime . date ) else str ( obj ) <EOL> return json . dumps ( self . __dict__ , default = dthandler ) <EOL> def Schedule ( self ) : <EOL> db . rollback_tasks . update ( { "<STR_LIT>" : self . _id } , { "<STR_LIT>" : { "<STR_LIT>" : datetime . datetime . utcnow ( ) } } ) <EOL> from rollback_worker import schedule_rollback_task <EOL> schedule_rollback_task ( str ( self . _id ) ) <EOL> def Execute ( self ) : <EOL> logger . info ( "<STR_LIT>" % self . _id ) <EOL> deletion_status = { } <EOL> user = User . Get ( self . UserID ) <EOL> for svc_id , upload_ids in self . PendingDeletions . items ( ) : <EOL> svcrec = User . GetConnectionRecord ( user , svc_id ) <EOL> deletion_status [ svc_id ] = { } <EOL> if not svcrec . Service . SupportsActivityDeletion : <EOL> continue <EOL> for upload_id in upload_ids : <EOL> logger . info ( "<STR_LIT>" % ( upload_id , svc_id ) ) <EOL> try : <EOL> svcrec . Service . DeleteActivity ( svcrec , upload_id ) <EOL> except Exception as e : <EOL> deletion_status [ svc_id ] [ str ( upload_id ) ] = False <EOL> logger . exception ( "<STR_LIT>" % e ) <EOL> else : <EOL> deletion_status [ svc_id ] [ str ( upload_id ) ] = True <EOL> db . rollback_tasks . update ( { "<STR_LIT>" : self . _id } , { "<STR_LIT>" : { "<STR_LIT>" : deletion_status } } ) <EOL> logger . info ( "<STR_LIT>" % self . _id ) </s>
