def add_by_user_loyalty_id(user_id, loyalty_id): answer = -1 loyalty = PaymentLoyalty.query.filter_by( id=loyalty_id).first() if not loyalty: return False user_wallets = PaymentWallet.query.filter_by(user_id=user_id).all() for wallet in user_wallets: wl = WalletLoyalty.query.filter_by( loyalty_id=loyalty_id, status=WalletLoyalty.STATUS_ON, wallet_id=wallet.id).first() if not wl: continue person = Person.query.filter_by( firm_id=loyalty.firm_id, payment_id=wallet.payment_id).first() if not person: person = Person() person.name = 'Участник промо-кампании' person.firm_id = loyalty.firm_id person.hard_id = wallet.hard_id person.payment_id = wallet.payment_id person.save() if not loyalty.terms_id: continue timeout = PersonEvent.LIKE_TIMEOUT if loyalty.timeout: timeout = loyalty.timeout terms = json.loads(loyalty.terms_id) for term in terms: event = PersonEvent.query.filter_by( person_id=person.id, term_id=term, event_id=loyalty.event_id, firm_id=loyalty.firm_id).first() if event: if event.timeout > timeout: event.status = PersonEvent.STATUS_BANNED event.save() else: event = PersonEvent() event.person_id = person.id event.term_id = term event.event_id = loyalty.event_id event.firm_id = loyalty.firm_id event.timeout = PersonEvent.LIKE_TIMEOUT if loyalty.timeout: event.timeout = loyalty.timeout event.save() answer = event.id return answer