<s> from . provider_base import ExternalPaymentProvider <EOL> from tapiriik . database import db <EOL> from tapiriik . settings import MOTIVATO_PREMIUM_USERS_LIST_URL <EOL> import requests <EOL> class MotivatoExternalPaymentProvider ( ExternalPaymentProvider ) : <EOL> ID = "<STR_LIT>" <EOL> def RefreshPaymentStateForExternalIDs ( self , external_ids ) : <EOL> from tapiriik . services import Service , ServiceRecord <EOL> external_ids = [ str ( x ) for x in external_ids ] <EOL> connections = [ ServiceRecord ( x ) for x in db . connections . find ( { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : { "<STR_LIT>" : external_ids } } ) ] <EOL> users = db . users . find ( { "<STR_LIT>" : { "<STR_LIT>" : [ x . _id for x in connections ] } } ) <EOL> for user in users : <EOL> my_connection = [ x for x in connections if x . _id in [ y [ "<STR_LIT>" ] for y in user [ "<STR_LIT>" ] ] ] [ <NUM_LIT:0> ] <EOL> state = Service . FromID ( "<STR_LIT>" ) . _getPaymentState ( my_connection ) <EOL> self . ApplyPaymentState ( user , state , my_connection . ExternalID , duration = None ) <EOL> def RefreshPaymentState ( self ) : <EOL> from tapiriik . services import ServiceRecord <EOL> from tapiriik . payments import Payments <EOL> from tapiriik . auth import User <EOL> external_ids = requests . get ( MOTIVATO_PREMIUM_USERS_LIST_URL ) . json ( ) <EOL> connections = [ ServiceRecord ( x ) for x in db . connections . find ( { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : { "<STR_LIT>" : external_ids } } ) ] <EOL> users = list ( db . users . find ( { "<STR_LIT>" : { "<STR_LIT>" : [ x . _id for x in connections ] } } ) ) <EOL> payments = [ ] <EOL> for user in users : <EOL> my_connection = [ x for x in connections if x . _id in [ y [ "<STR_LIT>" ] for y in user [ "<STR_LIT>" ] ] ] [ <NUM_LIT:0> ] <EOL> pmt = Payments . EnsureExternalPayment ( self . ID , my_connection . ExternalID , duration = None ) <EOL> payments . append ( pmt ) <EOL> User . AssociateExternalPayment ( user , pmt , skip_deassoc = True ) <EOL> db . users . update ( { "<STR_LIT>" : { "<STR_LIT>" : [ x [ "<STR_LIT>" ] for x in users ] } } , { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : [ x [ "<STR_LIT>" ] for x in payments ] } } } } , multi = True ) <EOL> ExternalPaymentProvider . Register ( MotivatoExternalPaymentProvider ( ) ) </s>
