class UserModel(db.Model): __tablename__ = 'users' p_id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) phone = db.Column(db.String(20), nullable=False) gender = db.Column(db.String(20), nullable=False) designation = db.Column(db.String(50), nullable=False) role = db.Column(db.String(50), nullable=False) def save_to_db(self): db.session.add(self) db.session.commit() def db_to_delete(self): db.session.delete(self) db.session.commit() def db_to_commit(self): db.session.commit() def update_data(self, old_data, new_data): old_data.name = new_data['name'] old_data.email = new_data['email'] old_data.password = new_data['password'] old_data.phone = new_data['phone'] old_data.gender = new_data['gender'] old_data.designation = new_data['designation'] old_data.role = new_data['role'] return old_data @staticmethod def to_json(data): return { 'name': data.name, 'email': data.email, 'password': data.password, 'phone': data.phone, 'gender': data.gender, 'designation': data.designation, 'role': data.role } @staticmethod def generate_hash(password): return sha256.hash(password) @staticmethod def verify_hash(password, hash): return sha256.verify(password, hash) @classmethod def find_by_email(cls, email): return cls.query.filter_by(email=email).first() @classmethod def find_by_id(cls, p_id): return cls.query.filter_by(p_id=p_id).first() @classmethod def return_all(cls): return { 'users': list(map(lambda user: cls.to_json(user), UserModel.query.all())) }
class SubjectModel(db.Model): __tablename__ = 'subjects' p_id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, nullable=False) code = db.Column(db.String(120), unique=True, nullable=False) class_id = db.Column(db.Integer, db.ForeignKey('classes.p_id'), nullable=False) department_id = db.Column(db.Integer, db.ForeignKey('departments.p_id'), nullable=False) classes = db.relationship("ClassModel", backref=db.backref("sub_class", uselist=False)) departmentes = db.relationship("DepartmentModel", backref=db.backref("sub_class", uselist=False)) def save_to_db(self): db.session.add(self) db.session.commit() def db_to_delete(self): db.session.delete(self) db.session.commit() def db_to_commit(self): db.session.commit() def update_data(self, old_data, new_data): old_data.name = new_data['name'] old_data.code = new_data['code'] old_data.class_id = new_data['class_id'] old_data.department_id = new_data['department_id'] return old_data #FOR CONVERT DATA INTO JSON FORMAT @staticmethod def to_json(data): return { 'name': data.name, 'code': data.code, 'class': data.classes.name, 'department': data.departmentes.name } @classmethod def find_by_id(cls, p_id): return cls.query.filter_by(p_id=p_id).first() @classmethod def find_by_code(cls, code): return cls.query.filter_by(code=code).first() @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() @classmethod def return_all(cls): return { 'subjects': list(map(lambda x: cls.to_json(x), SubjectModel.query.all())) }
class UserModel(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) name = db.Column(db.String(120), nullable=False) surname = db.Column(db.String(120), nullable=False) current_balance = db.Column(db.Integer, nullable=False) purchases = db.Column(MutableDict.as_mutable(PickleType)) def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() @classmethod def find_by_email(cls, email): return cls.query.filter_by(email=email).first() @classmethod def find_by_id(cls, id): return cls.query.filter_by(id=id).first() @classmethod def return_all(cls): def to_json(x): return { 'username': x.username, 'id': x.id, 'email': x.email, 'name': x.name, 'surname': x.surname, 'balance': x.current_balance } return { 'users': list(map(lambda x: to_json(x), UserModel.query.all())) } @classmethod def return_user_by_id(cls, id): def to_json(x): return { 'username': x.username, 'id': x.id, 'email': x.email, 'name': x.name, 'surname': x.surname, 'balance': x.current_balance } return {'user': to_json(cls.find_by_id(id))} @classmethod def delete_all(cls): try: num_rows_deleted = db.session.query(cls).delete() db.session.commit() return {'message': '{} row(s) deleted'.format(num_rows_deleted)} except: return {'message': 'Something went wrong'} @staticmethod def generate_hash(password): return sha256.hash(password) @staticmethod def verify_hash(password, hash): return sha256.verify(password, hash) def change_password(self, new_password): db.session.query(UserModel).filter(UserModel.username == self.username).\ update({UserModel.password: new_password}, synchronize_session=False) db.session.commit() def change_balance(self, new_balance): db.session.query(UserModel).filter(UserModel.id == self.id).\ update({UserModel.current_balance: new_balance}, synchronize_session=False) db.session.commit() def get_reset_password_token(self, expires_in=60000): return jwt.encode({ 'id': self.id, 'exp': time() + expires_in }, app.config['SECRET_KEY'], algorithm='HS256') @staticmethod def verify_reset_password_token(token): try: id = jwt.decode(token, app.config['SECRET_KEY'])['id'] except: return return db.session.query(UserModel).filter(UserModel.id == id).first() @staticmethod def send_password_reset_email(user): token = user.get_reset_password_token() msg = Message('Reset your password', sender='Слайдовалюта', recipients=[user.email]) link = 'https://slide-wallet.firebaseapp.com/auth/restore-password?token=' + \ str(token)[2:-1] msg.body = render_template('reset_password.txt', user=user, link=link) msg.html = render_template('reset_password.html', user=user, link=link) mail.send(msg) @staticmethod def send_support_email(body, user): msg = Message('Technical Support message', sender='Слайдовалюта', recipients=[app.config['MAIL_USERNAME']]) msg.body = render_template('technical_support.txt', user=user, body=body) msg.html = render_template('technical_support.html', user=user, body=body) mail.send(msg) def get_own_purchases_list(self): if len(self.purchases) == 0: return {'message': 'list is empty'} return self.purchases
class User(db.Model): __tablename__ = "user_table" user_id = db.Column(db.Integer, primary_key=True, nullable=False) # pylint: disable=no-member user_email = db.Column(db.String(128), unique=True, nullable=False) # pylint: disable=no-member user_password = db.Column(db.String(128), unique=False, nullable=True) # pylint: disable=no-member user_name = db.Column(db.String(128), unique=True, nullable=False) # pylint: disable=no-member user_phone = db.Column(db.Integer, unique=False, nullable=True) # pylint: disable=no-member @staticmethod def generate_hash(password): return pwd_context.hash(password) @staticmethod def verify_hash(password, hash): return pwd_context.verify(password, hash) @classmethod def find_by_uid(cls, uid): return cls.query.filter_by(user_id=uid).first() @classmethod def find_by_email(cls, email): return cls.query.filter_by(user_email=email).first() @classmethod def find_by_username(cls, username): return cls.query.filter_by(user_name=username).first() @classmethod def number_of_users(cls): records = db.session.query(cls).all() # pylint: disable=no-member number = len(records) return number def save_to_db(self): db.session.add(self) # pylint: disable=no-member db.session.commit() # pylint: disable=no-member def commit(self): db.session.commit() # pylint: disable=no-member # TODO: Remove this method before production! @classmethod def return_all(cls): def to_json(x): return { 'uid': x.user_id, 'email': x.user_email, 'user_name': x.user_name, 'password': x.user_password, 'phone': x.user_phone, } return {'users': list(map(lambda x: to_json(x), User.query.all()))} # TODO: Remove this method before production! @classmethod def delete_all(cls): try: num_rows_deleted = db.session.query(cls).delete() # pylint: disable=no-member db.session.commit() # pylint: disable=no-member return {'message': '{} row(s) deleted'.format(num_rows_deleted)} except Exception as err: return {'message': 'Something went wrong', "error": str(err)}
class BabyAction(db.Model): __tablename__ = 'baby_action' id = db.Column(db.Integer, primary_key=True, autoincrement=True) action_name = db.Column(db.String(500))
class UserModel(db.Model): __tablename__ = 't_users' user_id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(120), unique = True, nullable = False) password = db.Column(db.String(120), nullable = False) email = db.Column(db.String(255), unique=True) registered_on = db.Column(db.DateTime,default=datetime.datetime.utcnow) admin = db.Column(db.Boolean, nullable=False,default=False) first_name = db.Column(db.String(100)) last_name = db.Column(db.String(100)) phone_number = db.Column(db.String(50)) latitude = db.Column(db.String(50)) longitude = db.Column(db.String(50)) area = db.Column(db.Integer, default=30) def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def find_by_username(cls, username): return cls.query.filter_by(username = username).first() @classmethod def return_all(cls): def to_json(x): return { 'user_id': x.user_id, 'username': x.username, 'password': x.password, 'email': x.email, 'admin': x.admin, 'first_name': x.first_name, 'last_name': x.last_name, 'phone_number': x.phone_number, 'latitude': x.latitude, 'longitude': x.longitude, 'area': x.area, } return {'users': list(map(lambda x: to_json(x), cls.query.all()))} @classmethod def del_by_username(cls, username): try: num_rows_deleted = db.session.query(cls).filter(cls.username == username).delete() db.session.commit() return {'message': '{} row(s) deleted'.format(num_rows_deleted)} except: return {'message': 'Something went wrong'} @classmethod def delete_all(cls): try: num_rows_deleted = db.session.query(cls).delete() db.session.commit() return {'message': '{} row(s) deleted'.format(num_rows_deleted)} except: return {'message': 'Something went wrong'} @staticmethod def generate_hash(password): return sha256.hash(password) @staticmethod def verify_hash(password, hash): return sha256.verify(password, hash)
class NoteModel(db.Model): __tablename__ = 'notes' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, nullable=False, name='user_id') title = db.Column(db.String(30), nullable=False) body = db.Column(db.String(300), nullable=False) datetime = db.Column(db.String(30), nullable=True) edited = db.Column(db.Integer, nullable=False) def add(self): db.session.add(self) db.session.commit() @classmethod def find_by_ids(cls, id): return db.session.query(cls).filter(cls.id == id).first() @classmethod def find_by_date(cls, date, username): def to_json(x): return { 'id': x.id, 'title': x.title, 'body': x.body, 'datetime': x.datetime, 'edited' : x.edited } user_id = UserModel.find_by_username(username).id return { "{}'s notes for {}".format(username, date): list( map(lambda x: to_json(x), db.session.query(cls).filter(cls.datetime.contains(date), cls.user_id == user_id).all()) ) } @classmethod def return_all(cls, username): def to_json(x): return { 'id': x.id, 'title': x.title, 'body': x.body, 'datetime': x.datetime, 'edited' : x.edited } user_id = UserModel.find_by_username(username).id return { "{}'s notes".format(username): list( map(lambda x: to_json(x), db.session.query(cls).filter(cls.user_id == user_id).all()) ) } @classmethod def delete_note(cls, id): note = db.session.query(cls).filter(cls.id == id).first() db.session.delete(note) db.session.commit() return {'message': 'Note was successfully deleted'} @classmethod def edit_note(cls, id, title, text): db.session.query(cls).filter(cls.id == id).\ update({'title': title, 'body': text, 'edited': 1}) db.session.commit() return {'message': 'Note was successfully edited'}
class UserModel(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() @classmethod def return_all(cls): def to_json(x): return {'id': x.id, 'username': x.username} return { 'users': list(map(lambda x: to_json(x), UserModel.query.all())) } @classmethod def delete_all(cls): try: num_rows_deleted = db.session.query(cls).delete() db.session.commit() return {'message': '{} row(s) deleted'.format(num_rows_deleted)} except: return {'message': 'Something went wrong'} @staticmethod def generate_hash(password): return sha256.hash(password) @staticmethod def verify_hash(password, hash): return sha256.verify(password, hash) @classmethod def find_by_id(cls, id): return cls.query.filter_by(id=id).first() @classmethod def delete_user(cls, id): try: num_rows_deleted = db.session.query(cls).filter_by(id=id).delete() db.session.commit() return {'message': 'User {} deleted'.format(id)} except: return {'message': 'Something went wrong'} @classmethod def update_user(cls, id, username, password): try: user = cls.query.filter_by(id=id).first() db.session.query(cls).filter_by(id=id).update({ "username": username, "password": password }) db.session.commit() return {'message': 'User {} updated'.format(user.username)} except: return {'message': 'Something went wrong'}
class UserModel(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) content = db.relationship('ContentModel', backref='user') def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() @classmethod def find_by_id(cls, user_id): return cls.query.filter_by(id=user_id).first() @classmethod def return_all(cls): def to_json(x): return { 'username': x.username, 'password': x.password, 'contents': UserModel.return_all_post(x) } return { 'users': list(map(lambda x: to_json(x), UserModel.query.all())) } @classmethod def return_one(cls, user_id): def to_json(x): return { 'id': x.id, 'username': x.username, 'password': x.password, 'contents': UserModel.return_all_post(x) } return { 'users': list( map(lambda x: to_json(x), UserModel.query.filter_by(id=user_id).all())) } def return_all_post(y): def to_json_post(x): return {'text': x.text, 'user': x.user_id} return list( map(lambda x: to_json_post(x), ContentModel.query.filter_by(user_id=y.id).all())) @classmethod def edit_user(cls, data, user_id): user = UserModel.find_by_id(user_id).first() try: user.username = data['username'] user.password = UserModel.generate_hash(data['password']) db.session.commit() return {'message': '{} edit success'.format(user.username)} except: return {'message': 'Something went wrong!'} @classmethod def delete_all(cls): try: num_rows_deleted = db.session.query(cls).delete() db.session.commit() return {'message': '{} row(s) deleted!'.format(num_rows_deleted)} except: return {'message': 'Something went wrong'} @classmethod def delete_one(cls, user_id): user = UserModel.find_by_id(user_id) #post = ContentModel.query.filter(user_id == current_user.id).all() try: #db.session.query(ContentModel).filter(user_id = current_user.id).delete() db.session.delete(user) db.session.commit() return {'message': '{} deleted!'.format(user.username)} except: return {'message': 'Something went wrong'} @staticmethod def generate_hash(password): return sha256.hash(password) @staticmethod def verify_hash(password, hash): return sha256.verify(password, hash)
class HabitModel(db.Model): __tablename__ = 'habits' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, nullable=False, name='user_id') title = db.Column(db.String(30), nullable=False) datetime = db.Column(db.String(30), nullable=False) completed = db.Column(db.Text, nullable=False) def add(self): db.session.add(self) db.session.commit() @classmethod def find_by_creation_date(cls, date, username): def to_json(x): return { 'id': x.id, 'title': x.title, 'datetime': x.datetime, 'completed': x.completed } user_id = UserModel.find_by_username(username).id return { "{}'s created habits for {}".format(username, date): list( map(lambda x: to_json(x), db.session.query(cls).filter(cls.datetime.contains(date), cls.user_id == user_id).all()) ) } @classmethod def find_by_completion_date(cls, date, username): def to_json(x): return { 'id': x.id, 'title': x.title, 'datetime': x.datetime, 'completed': x.completed } user_id = UserModel.find_by_username(username).id list = [] for habit in db.session.query(cls).filter(cls.user_id == user_id).all(): completed = json.loads(habit.completed) if date in completed: list.append(to_json(habit)) jsonify(list) return { "{}'s completed habits for {}".format(username, date): list } @classmethod def find_by_ids(cls, id): return db.session.query(cls).filter(cls.id == id).first() @classmethod def return_all(cls, username): def to_json(x): return { 'id': x.id, 'title': x.title, 'datetime': x.datetime, 'completed': x.completed } user_id = UserModel.find_by_username(username).id return { "{}'s habits".format(username): list( map(lambda x: to_json(x), db.session.query(cls).filter(cls.user_id == user_id).all()) ) } @classmethod def add_completed_date(cls, id, date): db.session.query(cls).filter(cls.id == id).update({'completed': date}) db.session.commit() return {'message': 'Habit was completed'} @classmethod def delete_habit(cls, id): habit = db.session.query(cls).filter(cls.id == id).first() db.session.delete(habit) db.session.commit() return {'message': 'Note was successfully deleted'}
class ContentModel(db.Model): __tablename__ = 'contents' id = db.Column(db.Integer, primary_key=True) text = db.Column(db.String(120)) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def find_by_id(cls, content_id): return cls.query.get(int(content_id)) @classmethod def return_all(cls): def to_json(x): return { 'id': x.id, 'text': x.text, 'user': x.user_id, } return { 'contents': list(map(lambda x: to_json(x), ContentModel.query.all())) } @classmethod def return_one(cls, content_id): def to_json(x): return { 'text': x.text, 'user': x.user_id, } return { 'contents': list( map(lambda x: to_json(x), ContentModel.query.filter_by(id=content_id).all())) } @classmethod def delete_all(cls): try: num_rows_deleted = db.session.query(cls).delete() db.session.commit() return {'message': '{} row(s) deleted!'.format(num_rows_deleted)} except: return {'message': 'Something went wrong'} @classmethod def edit_content(cls, data, content_id, post): try: post.text = data['text'] db.session.commit() return {'message': 'Post edit success!'} except: return {'message': 'Went_wrong!'} @classmethod def delete_one(cls, content_id, post): try: db.session.delete(post) db.session.commit() return {'message': 'Post deleted!'} except: return {'message': 'went wrong'}
class Account(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(60)) pswd = db.Column(db.String(60)) isAdmin = db.Column(db.Boolean)
class Evento(db.Model): eventoId = db.Column(db.Integer, primary_key=True) nombre = db.Column(db.String(60), nullable=False) fecha = db.Column(db.Date, nullable=False) hora = db.Column(db.Time, nullable=False) descripcion = db.Column(db.String(500), nullable=False) imagen = db.Column(db.String(70), nullable=False) tipo = db.Column(db.String(15), nullable=False) lugar = db.Column(db.String(60), nullable=False) usuario = db.relationship("Usuario", back_populates="eventos") comentarios = db.relationship("Comentario", back_populates="evento", cascade="all, delete-orphan") usuarioId = db.Column(db.Integer, db.ForeignKey('usuario.usuarioId'), nullable=False) aprobado = db.Column(db.Boolean, nullable=False, default=False) def to_json(self): evento_json = { 'eventoId': url_for('apiGetEventoById', id=self.eventoId, _external=True), 'nombre': self.nombre, 'fecha': self.fecha, 'tipo': self.tipo, 'lugar': self.lugar, 'imagen': self.imagen, 'descripcion': self.descripcion } return evento_json @staticmethod def from_json(evento_json): nombre = evento_json.get('nombre') fecha = evento_json.get('fecha') tipo = evento_json.get('tipo') lugar = evento_json.get('lugar') imagen = evento_json.get('imagen') descripcion = evento_json.get('descripcion') return Evento(nombre=nombre, tipo=tipo, lugar=lugar, imagen=imagen, descripcion=descripcion, fecha=fecha)
class UserAccountInfoModel(db.Model): __tablename__ = 'accountinfo' username = db.Column(db.String(255), db.ForeignKey('users.username'), primary_key=True) spreadsheet = db.Column(db.String(255), nullable=True) spreadsheet_target = db.Column(db.String(255), nullable=True) model = db.Column(db.String(255), nullable=True) @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() @classmethod def find_spreadsheet_by_username(cls, username): return cls.query.filter_by(username=username).first().spreadsheet @classmethod def find_target_by_username(cls, username): return cls.query.filter_by( username=username).first().spreadsheet_target @classmethod def find_model_by_username(cls, username): return cls.query.filter_by(username=username).first().model @classmethod def user_has_model(cls, username): print(cls.query.filter_by(username=username).first().model) if cls.query.filter_by(username=username).first().model == None: return False else: return True @classmethod def return_all(cls): def to_json(x): return { 'username': x.username, 'spreadsheet': x.spreadsheet, 'spreadsheet_target': x.spreadsheet_target, 'model': x.model } return { 'accountinfo': list(map(lambda x: to_json(x), UserAccountInfoModel.query.all())) } @classmethod def delete_all(cls): try: num_rows_deleted = db.session.query(cls).delete() db.session.commit() return {'message': '{} row(s) deleted'.format(num_rows_deleted)} except: return {'message': 'Something went wrong'} @classmethod def change_target(cls, username, spreadsheet_target): user = UserAccountInfoModel.query.filter_by(username=username).first() user.spreadsheet_target = spreadsheet_target db.session.commit() @classmethod def change_spreadsheet(cls, username, spreadsheet): user = UserAccountInfoModel.query.filter_by(username=username).first() user.spreadsheet = spreadsheet db.session.commit() @classmethod def add_model(cls, username, model): user = UserAccountInfoModel.query.filter_by(username=username).first() user.model = model db.session.commit() def save_to_db(self): db.session.add(self) db.session.commit()
class BabyInfo(db.Model): __tablename__ = 'baby_info' # 宝宝id id = db.Column(db.Integer, primary_key=True, autoincrement=True) baby_name = db.Column(db.String(500))
class Grade(db.Model): __tablename__ = 'grades' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(20), nullable=False) # 关系, backref=‘grade’给Student类中添加属性grade, uselist=True, 一对多关系, 一个对象对应一个列表; students = db.relationship('Student', backref="grade", uselist=True)
class Type(db.Model): __tablename__ = 'type' id = db.Column(db.Integer, primary_key=True) article_type = db.Column(db.String(255)) status = db.Column(db.Integer) createtime = db.Column(db.DateTime, default=datetime.datetime.now)