Skip to content

Commit

Permalink
fix: 🐛 Solve issue reordering onboarding pages
Browse files Browse the repository at this point in the history
  • Loading branch information
albinmedoc committed Aug 1, 2024
1 parent e6e9d68 commit 2f6ca87
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions apps/wizarr-backend/wizarr_backend/api/routes/onboarding_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,34 @@ def put(self, onboarding_id: int):
if not page:
return {"error": "Onboarding page not found"}, 404

if(value is not None):
if value is not None:
page.value = value
if(enabled is not None):
if enabled is not None:
page.enabled = enabled in ["true", "True", "1"]

if order is not None and page.order != order:
step = 1 if page.order > order else -1
start, end = sorted([page.order, order])

# Update orders of affected pages
# Temporarily set the order of the target page to a value that won't conflict
max_order = OnboardingDB.select(fn.MAX(OnboardingDB.order)).scalar() or 0
page.order = max_order + 1
page.save()

# Shift orders of affected pages
affected_pages = OnboardingDB.select().where(
OnboardingDB.id != onboarding_id,
OnboardingDB.order >= start,
OnboardingDB.order <= end,
)
).order_by(OnboardingDB.order.asc() if step > 0 else OnboardingDB.order.desc())

for p in affected_pages:
p.order += step
p.save() # Save each affected page
p.save()

# Update the target page
# Finally, set the target page to its new order
page.order = order
page.save() # Save the target page

try:
transaction.commit() # Commit the transaction
except Exception as e:
transaction.rollback() # Rollback in case of error
return {"error": str(e)}, 500
return loads(dumps(model_to_dict(page), indent=4, sort_keys=True, default=str)), 200

@api.doc(description="Delete a single onboarding page")
Expand Down

0 comments on commit 2f6ca87

Please # to comment.