external_links.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. """API routes for external sidebar links."""
  2. import logging
  3. import uuid
  4. from pathlib import Path
  5. from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
  6. from fastapi.responses import FileResponse
  7. from sqlalchemy import select
  8. from sqlalchemy.ext.asyncio import AsyncSession
  9. from backend.app.core.config import settings as app_settings
  10. from backend.app.core.database import get_db
  11. from backend.app.models.external_link import ExternalLink
  12. from backend.app.schemas.external_link import (
  13. ExternalLinkCreate,
  14. ExternalLinkReorder,
  15. ExternalLinkResponse,
  16. ExternalLinkUpdate,
  17. )
  18. # Directory for storing custom icons
  19. ICONS_DIR = app_settings.base_dir / "icons"
  20. ALLOWED_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp", ".ico"}
  21. logger = logging.getLogger(__name__)
  22. router = APIRouter(prefix="/external-links", tags=["external-links"])
  23. @router.get("/", response_model=list[ExternalLinkResponse])
  24. async def list_external_links(db: AsyncSession = Depends(get_db)):
  25. """List all external links ordered by sort_order."""
  26. result = await db.execute(select(ExternalLink).order_by(ExternalLink.sort_order, ExternalLink.id))
  27. links = result.scalars().all()
  28. return links
  29. @router.post("/", response_model=ExternalLinkResponse)
  30. async def create_external_link(
  31. link_data: ExternalLinkCreate,
  32. db: AsyncSession = Depends(get_db),
  33. ):
  34. """Create a new external link."""
  35. # Get the highest sort_order to place new link at end
  36. result = await db.execute(select(ExternalLink).order_by(ExternalLink.sort_order.desc()).limit(1))
  37. last_link = result.scalar_one_or_none()
  38. next_order = (last_link.sort_order + 1) if last_link else 0
  39. link = ExternalLink(
  40. name=link_data.name,
  41. url=link_data.url,
  42. icon=link_data.icon,
  43. sort_order=next_order,
  44. )
  45. db.add(link)
  46. await db.commit()
  47. await db.refresh(link)
  48. logger.info(f"Created external link: {link.name} -> {link.url}")
  49. return link
  50. @router.get("/{link_id}", response_model=ExternalLinkResponse)
  51. async def get_external_link(
  52. link_id: int,
  53. db: AsyncSession = Depends(get_db),
  54. ):
  55. """Get a specific external link."""
  56. result = await db.execute(select(ExternalLink).where(ExternalLink.id == link_id))
  57. link = result.scalar_one_or_none()
  58. if not link:
  59. raise HTTPException(status_code=404, detail="External link not found")
  60. return link
  61. @router.patch("/{link_id}", response_model=ExternalLinkResponse)
  62. async def update_external_link(
  63. link_id: int,
  64. update_data: ExternalLinkUpdate,
  65. db: AsyncSession = Depends(get_db),
  66. ):
  67. """Update an external link."""
  68. result = await db.execute(select(ExternalLink).where(ExternalLink.id == link_id))
  69. link = result.scalar_one_or_none()
  70. if not link:
  71. raise HTTPException(status_code=404, detail="External link not found")
  72. # Update only provided fields
  73. update_dict = update_data.model_dump(exclude_unset=True)
  74. for key, value in update_dict.items():
  75. setattr(link, key, value)
  76. await db.commit()
  77. await db.refresh(link)
  78. logger.info(f"Updated external link: {link.name}")
  79. return link
  80. @router.delete("/{link_id}")
  81. async def delete_external_link(
  82. link_id: int,
  83. db: AsyncSession = Depends(get_db),
  84. ):
  85. """Delete an external link."""
  86. result = await db.execute(select(ExternalLink).where(ExternalLink.id == link_id))
  87. link = result.scalar_one_or_none()
  88. if not link:
  89. raise HTTPException(status_code=404, detail="External link not found")
  90. name = link.name
  91. await db.delete(link)
  92. await db.commit()
  93. logger.info(f"Deleted external link: {name}")
  94. return {"message": f"External link '{name}' deleted"}
  95. @router.put("/reorder", response_model=list[ExternalLinkResponse])
  96. async def reorder_external_links(
  97. reorder_data: ExternalLinkReorder,
  98. db: AsyncSession = Depends(get_db),
  99. ):
  100. """Update the sort order of external links."""
  101. # Update sort_order for each link based on position in the list
  102. for index, link_id in enumerate(reorder_data.ids):
  103. result = await db.execute(select(ExternalLink).where(ExternalLink.id == link_id))
  104. link = result.scalar_one_or_none()
  105. if link:
  106. link.sort_order = index
  107. await db.commit()
  108. # Return updated list
  109. result = await db.execute(select(ExternalLink).order_by(ExternalLink.sort_order, ExternalLink.id))
  110. links = result.scalars().all()
  111. logger.info(f"Reordered {len(reorder_data.ids)} external links")
  112. return links
  113. @router.post("/{link_id}/icon", response_model=ExternalLinkResponse)
  114. async def upload_icon(
  115. link_id: int,
  116. file: UploadFile = File(...),
  117. db: AsyncSession = Depends(get_db),
  118. ):
  119. """Upload a custom icon for an external link."""
  120. result = await db.execute(select(ExternalLink).where(ExternalLink.id == link_id))
  121. link = result.scalar_one_or_none()
  122. if not link:
  123. raise HTTPException(status_code=404, detail="External link not found")
  124. # Validate file extension
  125. if not file.filename:
  126. raise HTTPException(status_code=400, detail="No filename provided")
  127. ext = Path(file.filename).suffix.lower()
  128. if ext not in ALLOWED_EXTENSIONS:
  129. raise HTTPException(status_code=400, detail=f"File type not allowed. Allowed: {', '.join(ALLOWED_EXTENSIONS)}")
  130. # Create icons directory if it doesn't exist
  131. ICONS_DIR.mkdir(parents=True, exist_ok=True)
  132. # Delete old custom icon if exists
  133. if link.custom_icon:
  134. old_path = ICONS_DIR / link.custom_icon
  135. if old_path.exists():
  136. old_path.unlink()
  137. # Generate unique filename
  138. filename = f"{uuid.uuid4().hex}{ext}"
  139. filepath = ICONS_DIR / filename
  140. # Save file
  141. content = await file.read()
  142. with open(filepath, "wb") as f:
  143. f.write(content)
  144. # Update link
  145. link.custom_icon = filename
  146. await db.commit()
  147. await db.refresh(link)
  148. logger.info(f"Uploaded custom icon for link {link.name}: {filename}")
  149. return link
  150. @router.delete("/{link_id}/icon", response_model=ExternalLinkResponse)
  151. async def delete_icon(
  152. link_id: int,
  153. db: AsyncSession = Depends(get_db),
  154. ):
  155. """Delete the custom icon for an external link."""
  156. result = await db.execute(select(ExternalLink).where(ExternalLink.id == link_id))
  157. link = result.scalar_one_or_none()
  158. if not link:
  159. raise HTTPException(status_code=404, detail="External link not found")
  160. if link.custom_icon:
  161. filepath = ICONS_DIR / link.custom_icon
  162. if filepath.exists():
  163. filepath.unlink()
  164. link.custom_icon = None
  165. await db.commit()
  166. await db.refresh(link)
  167. logger.info(f"Deleted custom icon for link {link.name}")
  168. return link
  169. @router.get("/{link_id}/icon")
  170. async def get_icon(
  171. link_id: int,
  172. db: AsyncSession = Depends(get_db),
  173. ):
  174. """Get the custom icon for an external link."""
  175. result = await db.execute(select(ExternalLink).where(ExternalLink.id == link_id))
  176. link = result.scalar_one_or_none()
  177. if not link:
  178. raise HTTPException(status_code=404, detail="External link not found")
  179. if not link.custom_icon:
  180. raise HTTPException(status_code=404, detail="No custom icon set")
  181. filepath = ICONS_DIR / link.custom_icon
  182. if not filepath.exists():
  183. raise HTTPException(status_code=404, detail="Icon file not found")
  184. return FileResponse(filepath)