def get_book_by_id(id): with get_session() as session: book = session.query(Book).filter_by(id=id).first() if not book: raise NotFoundError("That book doesn't exist.") return json.loads(str(book))
def delete_book_by_id(id): with get_session() as session: book = session.query(Book).filter_by(id=id).first() if not book: raise NotFoundError("That book doesn't exist.") session.delete(book) session.commit()
def execute_reminders(): date = pytz.utc.localize(datetime.datetime.utcnow()) with get_session() as session: reminders = session.query(Reminder).filter(Reminder.execution_time <= date,Reminder.status==PENDING).all() for reminder in reminders: try: notify_helper(reminder.user_id,reminder.template_key,reminder.mail_data,reminder.role,reminder.sms_data) reminder.status = PROCESSED session.commit() except Exception as e: print(e) reminder.status = FAILED session.commit()
def update_book_by_id(id, request): with get_session() as session: existing_book = session.query(Book).filter_by(id=id).first() if not existing_book: raise NotFoundError("That book doesn't exist.") try: existing_book.title = request.json_body['title'] existing_book.author = request.json_body['author'] existing_book.read = request.json_body['read'] session.commit() except Exception as e: raise BadRequestError(e)
def add_reminder(reminder): with get_session() as session: new_reminder = Reminder( user_id = reminder['user_id'], appointment_id = reminder['appointment_id'], execution_time = reminder['execution_time'], status = PENDING, role = reminder['role'], template_key = reminder['template_key'], mail_data = reminder['mail_data'], sms_data = reminder['sms_data'] ) session.add(new_reminder) session.flush()
def create_book(request): # explain that this is just a simple catch-all try: title = request.json_body['title'] author = request.json_body['author'] read = request.json_body['read'] with get_session() as session: new_book = Book(title=title, author=author, read=read) session.add(new_book) session.commit() return except Exception as e: raise BadRequestError(e)
def notify_helper(user_id, template_id, data, role, sms_data): with get_session() as session: if role == "mentee": details = session.execute('SELECT * FROM mentee WHERE id = :id', { "id": user_id }).fetchall() else: details = session.execute('SELECT * FROM mentor WHERE id = :id', { "id": user_id }).fetchall() isSMS = False message = Templates(template_id, sms_data) for details in details: if details.phone_number != '' and details.isSMSEnabled and message != '': isSMS = True body = { "number": details.phone_number, "email_id": details.email, "message": message, "data": data, "template_id": template_id } # data = json.dumps({ # "isSMS": isSMS, # "isMAil": True, # "credentials": [ # { # "number": details.phone_number, # "email-id": details.email, # "message": message # } # ] # }) print('notify') print(body) send_mail(body) if (isSMS): send_sms(body)
def get_earned_by_id(): with get_session() as session: x = session.execute("select * from testing where name='miku';").fetchone()[0] return str(x)
def get_all_books(): with get_session() as session: books = session.query(Book).all() return [json.loads(str(b)) for b in books]
def decline_reminder(appointment_id): with get_session() as session: reminders = session.query(Reminder).filter_by(appointment_id=appointment_id,status=PENDING).all() for reminder in reminders: reminder.status = DECLINED session.commit()