class Poetry(pwdb.Model): # 推荐系统产出的诗歌id为idx idx = pw.IntegerField(unique=True) title = pw.CharField(max_length=32) title_tr = pw.CharField(max_length=32, default="") author = pw.CharField(max_length=32) author_tr = pw.CharField(max_length=32, default="") content = JSONCharField(max_length=4096, default=list) content_tr = JSONCharField(max_length=4096, default=list) background = pw.CharField(max_length=2048, default="") analysis = pw.TextField(default="") class Meta: table_name = "poetry" def to_dict(self): return { "id": self.id, "title": self.title, "author": self.author, "content": self.content } @classmethod def get_daily_poetry(cls, user_id): today_date = pendulum.today(tz=current_app.config["TZ"]).date() poetry = cls.recommend_a_poetry(user_id) return { "today_date": str(today_date), "week": today_date.day_of_week, "poetry": poetry.to_dict() } @classmethod def _random_fetch_a_poetry(cls, user_id): total = Poetry.select().count() pidx = random.randint(0, total - 1) poetry = Poetry.select().paginate(pidx, 1).first() return poetry @classmethod def recommend_a_poetry(cls, user_id): user_interest = UserInterest.get_user_interest(user_id) interests = [ i.text for i in Interest.select().where( Interest.id.in_(user_interest.interest_ids)) ] user_favorites = UserFavorite.select().where( UserFavorite.user_id == user_id).limit(30) poetries = Poetry.select().where( Poetry.id.in_([i.poetry_id for i in user_favorites])) favorite_poetry_idxs = [i.idx for i in poetries] idx = predict.predict(interests, favorite_poetry_idxs, []) return Poetry.get(idx=idx)
class UserInterest(pwdb.Model): user_id = pw.BigIntegerField(unique=True) interest_ids = JSONCharField(max_length=512, default=list) class Meta: table_name = "user_interest" @classmethod def check_have_interests(cls, user_id): ui = cls.select().where(cls.user_id == user_id).first() if ui and ui.interest_ids != []: return True return False @classmethod def add_interests(cls, user_id, interest_ids): cls.create(user_id=user_id, interest_ids=interest_ids) @classmethod def get_user_interest(cls, user_id): ui, _ = cls.get_or_create(user_id=user_id) return ui
class MyCategory(pwmysql.Model): id = peewee.AutoField() content = JSONCharField(max_length=128, default={})
class Category(pwdb.Model): id = peewee.AutoField() content = JSONCharField(max_length=128, default={}) remark = JSONCharField(max_length=128, null=True)
class MyCategory(pwmysql.Model): id = peewee.AutoField() content = JSONCharField(max_length=128, default={}) remark = JSONCharField(max_length=128, null=True) title = JSONCharField(max_length=128, null=True, ensure_ascii=False)