external_links.py 7.5 KB

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