def create_user(data): time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') db_session = DBSession() print(data) user = db_session.query(User).filter( User.email == data['email']).first() if user: return {'status': 'error', 'message': 'message'} password = sha512(data['password'].encode('utf-8')).hexdigest() new_user = User( username=data['username'], password=password, email=data['email'], confirmed_at=time, is_active=True, is_admin=data['is_admin']) try: db_session.add(new_user) db_session.commit() result = {'status': 'OK', 'user': data['username']} except: db_session.rollback() result = {'status': 'error'} db_session.close() return result
def update_user(user_id, data): data = {k: data[k] for k in data if k in ['username', 'password', 'new_password', 'email']} db_session = DBSession() user = db_session.query(User).get(user_id) password = sha512(data['password'].encode('utf-8')).hexdigest() if (not user or user.password != password): return {'status': 'error'} if (user.password == password and data['new_password'] != ''): new_password = sha512( data['new_password'].encode('utf-8')).hexdigest() user.username = data['username'] user.email = data['email'] user.password = new_password elif (user.password == data['password'] and data['new_password'] == ''): user.username = data['username'] user.email = data['email'] user.password = data['password'] else: return {'status': 'error'} db_session.commit() db_session.close() return {'status': 'OK', 'user': user_id}
def get_books(): db_session = DBSession() books = db_session.query(Books).order_by(desc(Books.id)).all() for b in books: yield {'id': b.id, 'name': b.name} db_session.close()
def get_types(): db_session = DBSession() types = db_session.query(Types).order_by(desc(Types.id)).all() for i in types: yield {'id': i.id, 'name': i.name} db_session.close()
def get_authors(): db_session = DBSession() authors = db_session.query(Authors).order_by(desc(Authors.id)).all() for i in authors: yield {'id': i.id, 'fullname': i.fullname} db_session.close()
def delete_type(type_id): if not (type_id): return {'status': 'error'} db_session = DBSession() db_session.query(Types).filter(Types.id == type_id).delete() db_session.commit() db_session.close() return {'status': 'OK'}
def delete_outhor(author_id): if not (author_id): return {'status': 'error'} print(type(author_id)) db_session = DBSession() outhor = db_session.query(Authors).get(author_id) db_session.delete(outhor) db_session.commit() db_session.close() return {'status': 'OK'}
def delete_book(book_id): if not (book_id): return {'status': 'error'} db_session = DBSession() book = db_session.query(Books).get(book_id) db_session.delete(book) db_session.commit() db_session.close() return {'status': 'OK'}
def get_users(): db_session = DBSession() users = db_session.query(User).order_by(desc(User.id)).all() for i in users: yield {'id': i.id, 'username': i.username, 'email': i.email, 'is_admin': i.is_admin, 'confirmed_at': str(i.confirmed_at), 'is_active': i.is_active} db_session.close()
def get_user(user_id): if not user_id: return {'status': 'Error'} else: db_session = DBSession() user = db_session.query(User).filter(User.id == user_id).all() if not user: return {'message': 'user not found'} user = [d.to_dict() for d in user] db_session.close() return {'user': user}
def get_type(type_id): if not type_id: return {'status': 'Error'} else: db_session = DBSession() book_type = db_session.query(Types).filter(Types.id == type_id).all() if not book_type: return {'message': 'type not found'} book_type = [d.to_dict() for d in book_type] db_session.close() return {'type': book_type}
def delete_user(user_id, data): if not user_id: return{'status': 'error'} db_session = DBSession() user = db_session.query(User).get(user_id) password = sha512(data['password'].encode('utf-8')).hexdigest() if (not user or data['username'] != user.username or password != user.password): return{'status': 'error'} db_session.query(User).filter(User.id == user_id).delete() db_session.commit() db_session.close() return {'status': 'OK'}
def get_book(book_id): if not book_id: return {'status': 'Error'} else: db_session = DBSession() book = db_session.query(Books).filter(Books.id == book_id).all() if not book: return {'message': 'book not found'} book = [d.to_dict() for d in book] db_session.close() return {'book': book}
def create_author(data): db_session = DBSession() new_author = Authors(**data) try: db_session.add(new_author) db_session.commit() result = {'status': 'OK', 'author': data['fullname']} except: db_session.rollback() result = {'status': 'error'} db_session.close() return result
def get_author(author_id): if not author_id: return {'status': 'Error'} else: db_session = DBSession() book_author = db_session.query(Authors).filter( Authors.id == author_id).all() if not book_author: return {'message': 'type not found'} book_author = [d.to_dict() for d in book_author] db_session.close() return {'author': book_author}
def create_book(data): db_session = DBSession() new_book = Books(name=data['name'], type_id=data['type_id'], author_id=data['author_id'], book_translator=data['book_translator']) try: db_session.add(new_book) db_session.commit() result = {'status': 'OK', 'book': data['name']} except: db_session.rollback() result = {'status': 'error'} db_session.close() return result
def create_type(data): db_session = DBSession() book_type = db_session.query(Types).filter( Types.name == data['name']).first() if book_type: return {'status': 'error', 'message': 'message'} new_type = Types(name=data['name']) try: db_session.add(new_type) db_session.commit() result = {'status': 'OK', 'type': data['name']} except: db_session.rollback() result = {'status': 'error'} db_session.close() return result