def authenticate(email, password): session = Session() user = session.query(User).filter_by(email=email).first() session.close() if user and pbkdf2_sha256.verify(password, user.password): return user else: return None
def get_ticket_id(ticketId): session = Session() ticket_objects = session.query(Tickets).filter_by(id=ticketId).first() schema = TicketSchema() tickets = schema.dump(ticket_objects) # serializing as JSON session.close() return jsonify(tickets)
def get_users(): session = Session() user_objects = session.query(User).all() result = [{ "id": user.id, "name": (user.fname + " " + user.lname) } for user in user_objects] # serializing as JSON session.close() return jsonify(result)
def delete_opp(oppId): session = Session() opp = session.query(Opp).filter_by(id=oppId).first() session.delete(opp) session.commit() session.close() return '', 201
def delete_contact(contactId): session = Session() contact_obj = session.query(Contacts).filter_by(id=contactId).first() session.delete(contact_obj) session.commit() session.close() return '', 201
def delete_exam(examId): session = Session() exam = session.query(Exam).filter_by(id=examId).first() session.delete(exam) session.commit() session.close() return '', 201
def delete_ticket(ticketId): session = Session() ticket_obj = session.query(Tickets).filter_by(id=ticketId).first() session.delete(ticket_obj) session.commit() ticket_objects = session.query(Tickets).order_by( Tickets.status, Tickets.created_at.desc()).all() schema = TicketSchema(many=True) tickets = schema.dump(ticket_objects) session.close() return jsonify(tickets)
def get_exams(): # Fetch from DB session = Session() exam_objects = session.query(Exam).all() # Transform into json-serialized objects schema = ExamSchema(many=True) exams = schema.dump(exam_objects) # serializing as JSON session.close() return jsonify(exams)
def get_booked_time(): # fetching from the database session = Session() booked_time_objects = session.query(LoggedTime).filter(LoggedTime.task_id != NO_TASK).all() # transforming into JSON-serializable objects schema = LoggedTimeSchema(many=True) booked_time = schema.dump(booked_time_objects) # serializing as JSON session.close() return jsonify(booked_time)
def get_tasks(): # fetching from the database session = Session() task_objects = session.query(Task).all() # transforming into JSON-serializable objects schema = TaskSchema(many=True) tasks = schema.dump(task_objects) # serializing as JSON session.close() return jsonify(tasks)
def get_opps(): # fetching from the database session = Session() opp_objects = session.query(Opp).all() # transforming into JSON-serializable objects schema = OppSchema(many=True) opps = schema.dump(opp_objects) # serializing as JSON session.close() return jsonify(opps.data)
def get_exams(): # fetching from the database session = Session() exam_objects = session.query(Exam).all() # transforming into JSON-serializable objects schema = ExamSchema(many=True) exams = schema.dump(exam_objects) # serializing as JSON session.close() return jsonify(exams.data)
def get_tickets(): arg = request.args session = Session() # fetching from the database sort_arg = arg.get('sortedBy', default='created_at desc', type=str) builder = session.query(Tickets) ticket_objects = builder.order_by(Tickets.status, text(sort_arg)).all() schema = TicketSchema(many=True) tickets = schema.dump(ticket_objects) # serializing as JSON session.close() return jsonify(tickets)
def get_manufacturers(): # fetching from the database session = Session() manufacturer_objects = session.query(Manufacturer).all() # transforming into JSON-serializable objects schema = ManufacturerSchema(many=True) manufacturers = schema.dump(manufacturer_objects) # serializing as JSON session.close() return jsonify(manufacturers.data)
def get_contact_id(contactId): session = Session() contact_obj = session.query(Contacts).filter_by(id=contactId).first() result = { "id": contact_obj.id, "name": contact_obj.name, "email": contact_obj.email, "phone": contact_obj.phone, "company_name": contact_obj.company_name, } # serializing as JSON session.close() return jsonify(result)
def remove_task(): id_object = request.get_json() session = Session() session.query(Task).filter(Task.id == id_object['id']).delete() session.commit() session.close() return 'OK'
def update_ticket(ticketId): session = Session() posted_ticket = TicketSchema().load(request.get_json()) session.query(Tickets).filter_by(id=ticketId).update( posted_ticket, synchronize_session=False) session.commit() session.close() return posted_ticket, 201
def get_factory_by_id(): id = request.args.get('id') # fetching from the database session = Session() factory_object = session.query(Factory).get(id) # transforming into JSON-serializable objects schema = FactorySchema() factory = schema.dump(factory_object) # serializing as JSON session.close() return jsonify(factory.data)
def get_manufacturer_by_id(): id = request.args.get('id') # fetching from the database session = Session() manufacturer_object = session.query(Manufacturer).get(id) # transforming into JSON-serializable objects schema = ManufacturerSchema() manufacturer = schema.dump(manufacturer_object) # serializing as JSON session.close() return jsonify(manufacturer.data)
def get_manufacturers_factories(): id = request.args.get('id') # fetching from the database session = Session() factory_objects = session.query(Factory).filter( Factory.manufacturer_id == id) # transforming into JSON-serializable objects schema = FactorySchema(many=True) factories = schema.dump(factory_objects) # serializing as JSON session.close() return jsonify(factories.data)
def update_contact(contactId): session = Session() pc = request.get_json() # contact_obj = pc['name'],pc['email'],pc['phone'],pc['company_name']) session.query(Contacts).filter_by(id=contactId).update( pc, synchronize_session=False) session.commit() session.close() return jsonify(pc), 201
def add_contact(): pc = request.get_json() session = Session() contact = Contacts(pc['name'], pc['email'], pc['phone'], pc['company_name']) session.add(contact) session.commit() session.close() return jsonify({'ok': True, 'message': 'Contact created!'}), 201
def get_logged_time(): # fetching from the database session = Session() # group logged time by (application_name, window_title) logged_time_objects = \ session.query(LoggedTime.application_name, func.sum(LoggedTime.logged_time_seconds).label('logged_time_seconds'), func.min(LoggedTime.created_at).label('created_at'))\ .filter(LoggedTime.task_id==NO_TASK)\ .group_by(LoggedTime.application_name)\ .all() # transforming into JSON-serializable objects schema = LoggedTimeSchema(many=True) logged_time = schema.dump(logged_time_objects) # serializing as JSON session.close() return jsonify(logged_time)
def get_contacts(): # fetching from the database session = Session() contact_objects = session.query(Contacts).order_by( Contacts.created_at).all() results = [{ "id": contact.id, "name": contact.name, "email": contact.email, "phone": contact.phone, "company_name": contact.company_name } for contact in contact_objects] # schema = ContactSchema(many=True) # contacts = schema.dump(contact_objects) session.close() # return jsonify(contacts) return jsonify(results)
def create_task(): task = TaskSchema(only=('title', 'description', 'estimated_time_minutes', 'deadline')). \ load(request.get_json()) task_objects = Task(**task) session = Session() session.add(task_objects) session.commit() session.close() return 'OK'
def log_time(): logged_time = LoggedTimeSchema(only=('application_name', 'logged_time_seconds', 'window_title'), many=True) \ .load(request.get_json()) logged_time_objects = list(map(lambda x: LoggedTime(**x), logged_time)) session = Session() session.bulk_save_objects(logged_time_objects) session.commit() session.close() return 'OK'
def toggle_complete_task(): id_object = request.get_json() session = Session() task = session.query(Task).filter(Task.id == id_object['id']).first() task.is_finished = not task.is_finished session.commit() session.close() return 'OK'
def ticket_filter(): arg = request.args session = Session() # fetching from the database builder = session.query(Tickets) sort_arg = '' for key in arg: if hasattr(Tickets, key): vals = request.args.getlist(key) print(getattr(Tickets, key)) builder = builder.filter(getattr(Tickets, key).in_(vals)) elif key == 'sortedBy': sort_arg = arg.get('sortedBy', default='created_at desc', type=str) ticket_objects = builder.order_by(Tickets.status, text(sort_arg)).all() schema = TicketSchema(many=True) tickets = schema.dump(ticket_objects) # serializing as JSON session.close() return jsonify(tickets)
def add_user(): # posted_data = UserSchema(only=('email','password','fname','lname')).load(json.loads(request.get_json())) # posted_data = request.get_json() # user = User(**posted_data) pd = request.get_json() user = User(pd['email'], pd['password'], pd['fname'], pd['lname']) session = Session() if session.query(User).filter_by(email=user.email).first(): return jsonify( {'message': 'User {} already exists'.format(user.email)}), 401 user.password = pbkdf2_sha256.hash(user.password) session.add(user) session.commit() session.close() return jsonify({'ok': True, 'message': 'User created!'}), 201
def search_manufacturers(): term = request.args.get('search_term') # fetching from the database session = Session() search_term = '%{0}%'.format(term) search_clause = or_(Manufacturer.name.like(search_term), Address.city.like(search_term), Address.region.like(search_term), Address.zip_code.like(search_term)) manufacturer_objects = session.query(Manufacturer).join( Manufacturer.address).filter(search_clause) # transforming into JSON-serializable objects schema = ManufacturerSchema(many=True) manufacturers = schema.dump(manufacturer_objects) # serializing as JSON session.close() return jsonify(manufacturers.data)