def add_iata_to_db(iata): if db_session.query(IATA).filter_by(code=iata).count() == 0: iata_model = IATA(code=iata) db_session.add(iata_model) db_session.commit() return iata_model else: return db_session.query(IATA).filter_by(code=iata)[0]
def queue_partner_emails(): try: users = db_session.query(Users).filter( Users.wants_emails == True).all() for user in users: print("Sending email to {}...".format(user.email)) partner = get_next_partner(user.uuid) if not partner: print("Reseting prayers for {}!".format(user.email)) db_session.query(Prayers).filter( Prayers.user_id == str(user.uuid)).filter( Prayers.partner_id != None).delete( synchronize_session=False) db_session.commit() partner = get_next_partner(user.uuid) message = Mail(from_email=From('*****@*****.**', '3ABC Prayer Reminders'), to_emails=user.email) message.dynamic_template_data = { 'user_name': user.name, 'email': partner.email, 'name': partner.name, 'type': partner.type, 'image': partner.image_url if partner.image_url is not None else None, 'description': partner.description if partner.description is not None else None, 'location': partner.location if partner.location is not None else None, } message.template_id = 'd-5f8116bd407849d5a06e66d586354bdb' sg = SendGridAPIClient( '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' ) response = sg.send(message) print(response.status_code) new_prayer = Prayers(member_id=None, partner_id=partner.ccbid, user_id=user.uuid) db_session.add(new_prayer) db_session.commit() return 200 except Exception as e: print('Failed sending partner emails') print(e) return 500 finally: db_session.close()
def queue_emails(): try: users = db_session.query(Users).filter( Users.wants_emails == True).all() for user in users: print("Sending email to {}...".format(user.email)) member = get_next_member(user.uuid) if not member: print("Reseting prayers for {}!".format(user.email)) db_session.query(Prayers).filter( Prayers.user_id == str(user.uuid)).filter( Prayers.member_id != None).delete( synchronize_session=False) db_session.commit() member = get_next_member(user.uuid) message = Mail(from_email=From('*****@*****.**', '3ABC Prayer Reminders'), to_emails=user.email) member_since = member.member_since.strftime('%B %Y') message.dynamic_template_data = { 'email': member.email, 'name': member.name, 'member_since': member_since, 'image': member.image_url if member.image_url is not None else None, 'username': user.name if user.name is not None else user.username } message.template_id = 'd-0a35a380ad214eb0b6152ddaa961f420' sg = SendGridAPIClient( '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' ) response = sg.send(message) print(response.status_code) new_prayer = Prayers(member_id=member.ccbid, partner_id=None, user_id=user.uuid) db_session.add(new_prayer) db_session.commit() return 200 except Exception as e: print('Failed sending all emails') print(e) return 500 finally: db_session.close()
def refresh_partner_urls(): try: print('Refreshing partner images...') req = { 'username': os.getenv("CCB_USER"), 'password': os.getenv("CCB_PASS"), 'keep_me_logged_in': True } session = requests.Session() login_res = session.post('https://thirdave.ccbchurch.com/api/login', json=req) if login_res.status_code == 200: partners = db_session.query(Partners).all() for partner in partners: res = session.get( 'https://thirdave.ccbchurch.com/api/individuals/%s' % partner.ccbid) ccb_partner = json.loads(res.text) partner.image_url = get_image(ccb_partner.get('images')) db_session.commit() else: return 200 else: print( 'Failed to authenticate with CCB while refreshing partner images' ) return 401 except Exception as e: db_session.close() print(e) print('Failed to refresh partner images') return 500 finally: db_session.close()
def get_next_partner(userId): try: # return next unprayed for third ave partner prayers = db_session.query(Prayers).filter( Prayers.user_id == str(userId)).all() prayerIds = list(map(lambda x: x.partner_id, prayers)) partners = db_session.query(Partners).all() for partner in partners: if partner.ccbid not in prayerIds: return partner break else: return None except Exception as e: print(e) print('Failed getting next partner for emails') return 500 finally: db_session.close()
def get_next_member(userId): try: # return next unprayed for third ave member prayers = db_session.query(Prayers).filter( Prayers.user_id == str(userId)).all() prayerIds = list(map(lambda x: x.member_id, prayers)) members = db_session.query(Members).all() for member in members: if member.ccbid not in prayerIds and member.image_url: return member break else: return None except Exception as e: print('Failed getting next member') print(e) return 500 finally: db_session.close()
def caching(): logger.info(f"Start, caching time {datetime.now()}") for iata_from, iata_to in iatas: iata_from_model = add_iata_to_db(iata_from) iata_to_model = add_iata_to_db(iata_to) iterator_date = date.today() date_end = iterator_date + relativedelta(months=+1) logger.info(f"{iata_from} to {iata_to} start") while iterator_date != date_end: logger.info(f"{iterator_date} start caching") if ( db_session.query(Ticket) .filter_by( date_search=iterator_date, fly_from=iata_from_model, fly_to=iata_to_model, ) .count() ): iterator_date += relativedelta(days=1) continue data = make_request(iata_from, iata_to, iterator_date) data = data["data"] flag = False chipest_ticket = {} for ticket in data: main_data_in_ticket = { "booking_token": ticket["booking_token"], "price": ticket["price"], "flyFrom": ticket["flyFrom"], "flyTo": ticket["flyTo"], "date_from": datetime.utcfromtimestamp(int(ticket["dTime"])), "date_to": datetime.utcfromtimestamp(int(ticket["aTime"])), } if not flag: chipest_ticket = main_data_in_ticket flag = True else: if int(ticket["price"]) < int(chipest_ticket["price"]): chipest_ticket = ticket add_ticket_to_db( ticket=chipest_ticket, date_search=iterator_date, iata_from_model=iata_from_model, iata_to_model=iata_to_model, ) print(iterator_date) iterator_date += relativedelta(days=1) logger.info(f"{iterator_date} end caching") logger.info("End, caching")
def get_current_user(token: str): try: payload = jwt.decode(token, config.SECRET_KEY, algorithms=[ALGORITHM]) token_data = TokenPayload(**payload) user = db_session.query(Users).filter( Users.uuid == token_data.user_uuid).first() if not user: return None return user except Exception as e: db_session.rollback() return None finally: db_session.close()
def reload_cache(): tickets = db_session.query(Ticket).all() for ticket in tickets: db_session.delete(ticket) db_session.commit() caching()
def getByName(name: str): query = db_session.query(Hotels) return query.filter(Hotels.name == name).first()
def getById(item_id: int): query = db_session.query(Hotels) return query.filter(Hotels.id == item_id).first()
def get(): return db_session.query(Hotels).all()