class Usuarios(FlaskDocument): tipo_id_usurio = db.StringField(required=True, max_length=10) id_usuario = db.StringField(primary_key=True) nombre_usuario = db.StringField(max_length=200) edad = db.IntField(default=0) genero = db.StringField(max_length=200) estado_vinculacion = db.BooleanField(default=True) tipo_afiliacion_usuario = db.StringField(required=True, max_length=100) categoria_usuario = db.StringField(required=True, max_length=10) tipo_id_trabajador = db.StringField(required=True, max_length=10) id_trabajador = db.IntField(default=0) tipo_afilacion_trabajador = db.StringField(required=True, max_length=100) fecha_alta = db.DateTimeField() fecha_modificacion = db.DateTimeField(default=datetime.datetime.now) meta = { 'indexes': [{ 'fields': [ '$nombre_usuario', "$tipo_afilacion_trabajador", "$tipo_afiliacion_usuario" ], 'default_language': 'Spanish', 'weights': { 'nombre_usuario': 10, 'tipo_afilacion_trabajador': 10, "tipo_afiliacion_usuario": 10 } }] }
class User(FlaskDocument): email = db.StringField(max_length=255) username = db.StringField(max_length=255, unique=True) firstname = db.StringField(max_length=255) lastname = db.StringField(max_length=255) password = db.StringField(max_length=255) active = db.BooleanField(default=True) confirmed_at = db.DateTimeField() is_admin = db.BooleanField(default=False) is_active = db.BooleanField(default=False) @staticmethod def generate_hash(password): return sha256.hash(password) @staticmethod def verify_hash(password, hash): return sha256.verify(password, hash)
class Merchant(db.Document): create_time = db.IntField(default=time.time()) update_time = db.IntField(default=time.time()) settled_time = db.IntField() # 入驻时间 create_user = db.ReferenceField(user.User, reverse_delete_rule=db.CASCADE) is_self = db.BooleanField(default=False) # 是否官方 name = db.StringField(null=True) brief = db.StringField(null=True) logo = db.StringField(null=True) htype = db.IntField() # 商家类型 1 地产商、2 中介,代理商 service = db.IntField() # 所需服务 1 推广获客、2 品牌推广 status = db.IntField() # 状态 1 申请中、2 已入驻 contacts = db.StringField() phone = db.StringField() meta = { 'collection': 'merchant', # 更改数据库表名 } def get_create_user_name(self): return self.create_user.name def get_id(self): return loads(dumps(self.id)).__str__() def to_json(self): return { "id": self.get_id(), "name": self.name, "brief": self.brief, "logo": self.logo, "htype": self.htype, "service": self.service, "status": self.status, "create_user": self.get_create_user_name(), "is_self": self.is_self, "contacts": self.contacts, "phone": self.phone, "settled_time": self.settled_time, "create_time": self.create_time, "update_time": self.update_time, } def __str__(self): return self.name
class Action(db.Document): create_time = db.IntField(default=time.time()) update_time = db.IntField(default=time.time()) user = db.ReferenceField(User, reverse_delete_rule=db.CASCADE) action_type_choice = ( (1, '阅读'), (2, '点赞'), (3, '收藏'), (4, '关注'), ) action_type = db.IntField(choices=action_type_choice) object_id = db.StringField(required=True) addition = db.StringField(null=True) is_del = db.BooleanField(default=False) meta = { "collection": "action", "indexes": ["#is_del", "#action_type", "#user", "#object_id"] } def get_id(self): return loads(dumps(self.id)).__str__() def get_user(self): return loads(dumps(self.user.id)).__str__() def to_json(self): return { "id": self.get_id(), "user": self.get_user(), "action_type": self.action_type, "object_id": self.object_id, "addition": self.addition, "create_time": self.create_time, "update_time": self.update_time, }
class User(db.Document): phone_area = db.StringField(default='+86') phone = db.StringField(required=True, unique=True) pwd = db.StringField() name = db.StringField(required=True) email = db.EmailField(null=True) icon = db.StringField(bull=True) nickname = db.StringField() signature = db.StringField(null=True) # 签名 gender_choice = ( (1, '男'), (2, '女'), ) gender = db.IntField(required=True, choices=gender_choice) # 性别 1 男 2 女 province = db.StringField(null=True) city = db.StringField(null=True) country = db.StringField(null=True) token = db.StringField() create_time = db.IntField(default=time.time()) update_time = db.IntField(default=time.time()) is_admin = db.BooleanField(default=False) merchants = db.ListField(db.EmbeddedDocumentField(UserMerchant), default=[]) meta = { 'collection': 'user', # 更改数据库表名 'indexes': [ '#phone', '#token', '#pwd' ] } def get_id(self): return loads(dumps(self.id)).__str__() def init_nick_name(self): nickname = '新用户_' for nick in random.sample('zyxwvutsrqponmlkjihgfedcbaABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 10): nickname += nick return nickname # 加密密码 def hash_password(self, password): return pwd_context.encrypt(password) # 密码验证 def verify_password(self, password): return pwd_context.verify(password, self.pwd) # 加密token,有效期1天 def generate_auth_token(self, expiration=EXPIRATION): s = Serializer(secret_key=SECRET_KEY, expires_in=expiration) return str(s.dumps({'id': str(self.id)}), encoding='utf-8') def HasMerchantRole(self, merchant_id, rol_int): for m in self.merchants: if m.merchant_id == merchant_id: if rol_int in m.roles: return True return False def get_merchant(self): return [merchant.to_json() for merchant in self.merchants] def to_json(self): return { "id": self.get_id(), "phone_area": self.phone_area, "phone": self.phone, "name": self.name, "email": self.email, "icon": self.icon, "nickname": self.nickname, "token": self.token, "signature": self.signature, "gender": self.gender, "province": self.province, "city": self.city, "country": self.country, "is_admin": self.is_admin, "merchants": self.get_merchant(), "create_time": self.create_time, "update_time": self.update_time, }