Skip to content

Commit

Permalink
fix bug ???
Browse files Browse the repository at this point in the history
  • Loading branch information
leafty committed Nov 1, 2023
1 parent 825765f commit 47c6602
Showing 1 changed file with 33 additions and 35 deletions.
68 changes: 33 additions & 35 deletions components/renku_data_services/user_preferences/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,38 @@ async def get_user_preferences(

async def add_pinned_project(self, user: base_models.APIUser, project_slug: str) -> models.UserPreferences:
"""Adds a new pinned project to the user's preferences."""
async with self.session_maker() as session, session.begin():
if not user.is_authenticated:
raise errors.Unauthorized(message="Anonymous users cannot have user preferences.")

res = await session.execute(
select(schemas.UserPreferencesORM).where(schemas.UserPreferencesORM.user_id == user.id)
)
user_preferences = res.one_or_none()
user_preferences = user_preferences[0] if user_preferences is not None else None
async with self.session_maker() as session:
async with session.begin():
if not user.is_authenticated:
raise errors.Unauthorized(message="Anonymous users cannot have user preferences.")

if user_preferences is None:
new_preferences = models.UserPreferences(
user_id=cast(str, user.id), pinned_projects=models.PinnedProjects(project_slugs=[project_slug])
res = await session.execute(
select(schemas.UserPreferencesORM).where(schemas.UserPreferencesORM.user_id == user.id)
)
user_preferences = schemas.UserPreferencesORM.load(new_preferences)
session.add(user_preferences)
model = user_preferences.dump()
return model

project_slugs: List[str]
project_slugs = user_preferences.pinned_projects.get("project_slugs", [])

exists = False
for slug in project_slugs:
if project_slug.lower() == slug.lower():
exists = True
break

if exists:
model = user_preferences.dump()
return model

project_slugs.append(project_slug)
pinned_projects = models.PinnedProjects(project_slugs=project_slugs).model_dump()
setattr(user_preferences, "pinned_projects", pinned_projects)
model = user_preferences.dump()
return model
user_preferences = res.one_or_none()
user_preferences = user_preferences[0] if user_preferences is not None else None

if user_preferences is None:
new_preferences = models.UserPreferences(
user_id=cast(str, user.id), pinned_projects=models.PinnedProjects(project_slugs=[project_slug])
)
user_preferences = schemas.UserPreferencesORM.load(new_preferences)
session.add(user_preferences)
return user_preferences.dump()

project_slugs: List[str]
project_slugs = user_preferences.pinned_projects.get("project_slugs", [])

exists = False
for slug in project_slugs:
if project_slug.lower() == slug.lower():
exists = True
break

if exists:
return user_preferences.dump()

project_slugs.append(project_slug)
pinned_projects = models.PinnedProjects(project_slugs=project_slugs).model_dump()
setattr(user_preferences, "pinned_projects", pinned_projects)
return user_preferences.dump()

0 comments on commit 47c6602

Please sign in to comment.