feat: 优化用户资料构建过程
- 添加 LazerUserProfileSections 模型,用于自定义用户资料顺序 - 在 User 模型中添加 lazer_profile_order 关系 - 优化 utils.py 中的 build_user_profile 函数,使用数据库中的计数信息 -修复默认计数信息中的 recent_scores_count 和 socres_first_count 属性
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Float, Text, JSON, ForeignKey, Date, DECIMAL
|
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Float, Text, JSON, ForeignKey, Date, DECIMAL
|
||||||
|
from sqlalchemy.dialects.mysql import VARCHAR
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
@@ -61,6 +62,7 @@ class User(Base):
|
|||||||
lazer_profile = relationship("LazerUserProfile", back_populates="user", uselist=False, cascade="all, delete-orphan")
|
lazer_profile = relationship("LazerUserProfile", back_populates="user", uselist=False, cascade="all, delete-orphan")
|
||||||
lazer_statistics = relationship("LazerUserStatistics", back_populates="user", cascade="all, delete-orphan")
|
lazer_statistics = relationship("LazerUserStatistics", back_populates="user", cascade="all, delete-orphan")
|
||||||
lazer_achievements = relationship("LazerUserAchievement", back_populates="user", cascade="all, delete-orphan")
|
lazer_achievements = relationship("LazerUserAchievement", back_populates="user", cascade="all, delete-orphan")
|
||||||
|
lazer_profile_order=relationship("LazerUserProfileOrder", back_populates="user", cascade="all, delete-orphan")
|
||||||
statistics = relationship("LegacyUserStatistics", back_populates="user", cascade="all, delete-orphan")
|
statistics = relationship("LegacyUserStatistics", back_populates="user", cascade="all, delete-orphan")
|
||||||
achievements = relationship("LazerUserAchievement", back_populates="user", cascade="all, delete-orphan")
|
achievements = relationship("LazerUserAchievement", back_populates="user", cascade="all, delete-orphan")
|
||||||
team_membership = relationship("TeamMember", back_populates="user", cascade="all, delete-orphan")
|
team_membership = relationship("TeamMember", back_populates="user", cascade="all, delete-orphan")
|
||||||
@@ -126,6 +128,17 @@ class LazerUserProfile(Base):
|
|||||||
# 关联关系
|
# 关联关系
|
||||||
user = relationship("User", back_populates="lazer_profile")
|
user = relationship("User", back_populates="lazer_profile")
|
||||||
|
|
||||||
|
class LazerUserProfileSections(Base):
|
||||||
|
__tablename__ = "lazer_user_profile_sections"
|
||||||
|
|
||||||
|
user_id = Column(Integer, ForeignKey("users.id"), primary_key=True)
|
||||||
|
section_name = Column(VARCHAR(50))
|
||||||
|
display_order=Column(Integer)
|
||||||
|
|
||||||
|
created_at=Column(DateTime, default=datetime.utcnow)
|
||||||
|
updated_at=Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||||
|
|
||||||
|
user = relationship("User", back_populates="lazer_profile_order")
|
||||||
|
|
||||||
class LazerUserCountry(Base):
|
class LazerUserCountry(Base):
|
||||||
__tablename__ = "lazer_user_countries"
|
__tablename__ = "lazer_user_countries"
|
||||||
|
|||||||
59
app/utils.py
59
app/utils.py
@@ -19,7 +19,14 @@ def convert_db_user_to_api_user(db_user: DBUser, ruleset: str = "osu", db_sessio
|
|||||||
if not profile:
|
if not profile:
|
||||||
# 如果没有 lazer 资料,使用默认值
|
# 如果没有 lazer 资料,使用默认值
|
||||||
profile = create_default_profile(db_user)
|
profile = create_default_profile(db_user)
|
||||||
|
|
||||||
|
# 获取 Lazer 用户计数
|
||||||
|
|
||||||
|
lzrcnt=db_user.lazer_statistics
|
||||||
|
if not lzrcnt:
|
||||||
|
# 如果没有 lazer 计数,使用默认值
|
||||||
|
lzrcnt = create_default_counts()
|
||||||
|
|
||||||
# 获取指定模式的统计信息
|
# 获取指定模式的统计信息
|
||||||
user_stats = None
|
user_stats = None
|
||||||
for stat in db_user.lazer_statistics:
|
for stat in db_user.lazer_statistics:
|
||||||
@@ -252,37 +259,37 @@ def convert_db_user_to_api_user(db_user: DBUser, ruleset: str = "osu", db_sessio
|
|||||||
max_friends=profile.max_friends if profile and profile.max_friends else 500,
|
max_friends=profile.max_friends if profile and profile.max_friends else 500,
|
||||||
post_count=profile.post_count if profile and profile.post_count else 0,
|
post_count=profile.post_count if profile and profile.post_count else 0,
|
||||||
profile_hue=profile.profile_hue if profile and profile.profile_hue else None,
|
profile_hue=profile.profile_hue if profile and profile.profile_hue else None,
|
||||||
profile_order= ['me', 'recent_activity', 'top_ranks', 'medals', 'historical', 'beatmaps', 'kudosu'],
|
profile_order=profile.profile_order if profile and profile.profile_order else ['me', 'recent_activity', 'top_ranks', 'medals', 'historical', 'beatmaps', 'kudosu'],
|
||||||
title=None,
|
title=profile.title if profile else None,
|
||||||
title_url=None,
|
title_url=profile.title_url if profile else None,
|
||||||
twitter=None,
|
twitter=profile.twitter if profile else None,
|
||||||
website='https://gmoe.cc',
|
website=profile.website if profile else None,
|
||||||
session_verified=True,
|
session_verified=True,
|
||||||
support_level=0,
|
support_level=profile.support_level if profile else 0,
|
||||||
country=country,
|
country=country,
|
||||||
cover=cover,
|
cover=cover,
|
||||||
kudosu=kudosu,
|
kudosu=kudosu,
|
||||||
statistics=statistics,
|
statistics=statistics,
|
||||||
statistics_rulesets=statistics_rulesets,
|
statistics_rulesets=statistics_rulesets,
|
||||||
beatmap_playcounts_count=3306,
|
beatmap_playcounts_count=db_user.beatmap_playcounts_count if db_user.beatmap_playcounts_count is not None else 0,
|
||||||
comments_count=0,
|
comments_count=lzrcnt.comments_count if lzrcnt else 0,
|
||||||
favourite_beatmapset_count=0,
|
favourite_beatmapset_count=lzrcnt.favourite_beatmapset_count if lzrcnt else 0,
|
||||||
follower_count=0,
|
follower_count=lzrcnt.follower_count if lzrcnt else 0,
|
||||||
graveyard_beatmapset_count=0,
|
graveyard_beatmapset_count=lzrcnt.graveyard_beatmapset_count if lzrcnt else 0,
|
||||||
guest_beatmapset_count=0,
|
guest_beatmapset_count=lzrcnt.guest_beatmapset_count if lzrcnt else 0,
|
||||||
loved_beatmapset_count=0,
|
loved_beatmapset_count=lzrcnt.loved_beatmapset_count if lzrcnt else 0,
|
||||||
mapping_follower_count=0,
|
mapping_follower_count=lzrcnt.mapping_follower_count if lzrcnt else 0,
|
||||||
nominated_beatmapset_count=0,
|
nominated_beatmapset_count=lzrcnt.nominated_beatmapset_count if lzrcnt else 0,
|
||||||
pending_beatmapset_count=0,
|
pending_beatmapset_count=lzrcnt.pending_beatmapset_count if lzrcnt else 0,
|
||||||
ranked_beatmapset_count=0,
|
ranked_beatmapset_count=lzrcnt.ranked_beatmapset_count if lzrcnt else 0,
|
||||||
ranked_and_approved_beatmapset_count=0,
|
ranked_and_approved_beatmapset_count=lzrcnt.ranked_and_approved_beatmapset_count if lzrcnt else 0,
|
||||||
unranked_beatmapset_count=0,
|
unranked_beatmapset_count=lzrcnt.unranked_beatmapset_count if lzrcnt else 0,
|
||||||
scores_best_count=0,
|
scores_best_count=lzrcnt.scores_best_count if lzrcnt else 0,
|
||||||
scores_first_count=0,
|
scores_first_count=lzrcnt.socres_first_count if lzrcnt else 0,
|
||||||
scores_pinned_count=0,
|
scores_pinned_count=lzrcnt.scores_pinned_count,
|
||||||
scores_recent_count=0,
|
scores_recent_count=lzrcnt.recent_scores_count if lzrcnt else 0,
|
||||||
account_history=[],
|
account_history=[],
|
||||||
active_tournament_banner=None,
|
active_tournament_banner=0,
|
||||||
active_tournament_banners=[],
|
active_tournament_banners=[],
|
||||||
badges=[],
|
badges=[],
|
||||||
current_season_stats=None,
|
current_season_stats=None,
|
||||||
@@ -419,6 +426,8 @@ def create_default_counts():
|
|||||||
"""创建默认的计数信息"""
|
"""创建默认的计数信息"""
|
||||||
class MockCounts:
|
class MockCounts:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.recent_scores_count = None
|
||||||
|
self.socres_first_count = None
|
||||||
self.beatmap_playcounts_count = 0
|
self.beatmap_playcounts_count = 0
|
||||||
self.comments_count = 0
|
self.comments_count = 0
|
||||||
self.favourite_beatmapset_count = 0
|
self.favourite_beatmapset_count = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user