portfolio.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from fastapi import APIRouter, Depends, HTTPException, Form, UploadFile, File
  2. import db
  3. import schemas
  4. import auth_utils
  5. import config
  6. import os
  7. import uuid
  8. import shutil
  9. from services.global_manager import global_manager
  10. from dependencies import require_admin
  11. from services.audit_service import audit_service
  12. router = APIRouter(tags=["portfolio"])
  13. @router.get("/portfolio")
  14. async def get_public_portfolio():
  15. query = """
  16. SELECT p.id, p.file_path, COALESCE(o.material_name, 'Showcase') as material_name, p.order_id
  17. FROM order_photos p
  18. LEFT JOIN orders o ON p.order_id = o.id
  19. WHERE p.is_public = TRUE AND (o.id IS NULL OR o.allow_portfolio = TRUE)
  20. ORDER BY p.created_at DESC
  21. """
  22. return db.execute_query(query)
  23. @router.get("/admin/all-photos")
  24. async def admin_get_all_photos(admin: dict = Depends(require_admin)):
  25. query = """
  26. SELECT p.id, p.file_path, p.is_public, p.order_id, o.allow_portfolio,
  27. o.first_name, o.last_name, COALESCE(o.material_name, 'Manual') as material_name
  28. FROM order_photos p
  29. JOIN orders o ON p.order_id = o.id
  30. ORDER BY p.created_at DESC
  31. """
  32. return db.execute_query(query)
  33. @router.post("/admin/orders/{order_id}/photos")
  34. async def admin_upload_order_photo(
  35. request: Request,
  36. order_id: int,
  37. is_public: bool = Form(False),
  38. file: UploadFile = File(...),
  39. admin: dict = Depends(require_admin)
  40. ):
  41. order = db.execute_query("SELECT allow_portfolio FROM orders WHERE id = %s", (order_id,))
  42. if not order: raise HTTPException(status_code=404, detail="Order not found")
  43. if is_public and not order[0]['allow_portfolio']:
  44. raise HTTPException(status_code=400, detail="Cannot make public: User did not consent to portfolio usage")
  45. if not file.filename: raise HTTPException(status_code=400, detail="Invalid file")
  46. unique_filename = f"{uuid.uuid4()}{os.path.splitext(file.filename)[1]}"
  47. disk_path = os.path.join(config.UPLOAD_DIR, unique_filename)
  48. db_file_path = f"uploads/{unique_filename}"
  49. with open(disk_path, "wb") as buffer:
  50. shutil.copyfileobj(file.file, buffer)
  51. query = "INSERT INTO order_photos (order_id, file_path, is_public) VALUES (%s, %s, %s)"
  52. photo_id = db.execute_commit(query, (order_id, db_file_path, is_public))
  53. await audit_service.log(
  54. user_id=admin['id'],
  55. action="upload_order_photo",
  56. target_type="order",
  57. target_id=order_id,
  58. details={"photo_id": photo_id, "is_public": is_public},
  59. request=request
  60. )
  61. # NOTIFY USER VIA WEBSOCKET
  62. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  63. if order_info:
  64. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  65. return {"id": photo_id, "file_path": db_file_path, "is_public": is_public}
  66. @router.patch("/admin/photos/{photo_id}")
  67. async def admin_update_photo_status(request: Request, photo_id: int, data: schemas.PhotoUpdate, admin: dict = Depends(require_admin)):
  68. query = "SELECT p.*, o.allow_portfolio FROM order_photos p JOIN orders o ON p.order_id = o.id WHERE p.id = %s"
  69. photo_data = db.execute_query(query, (photo_id,))
  70. if not photo_data: raise HTTPException(status_code=404, detail="Photo not found")
  71. if data.is_public and not photo_data[0]['allow_portfolio']:
  72. raise HTTPException(status_code=400, detail="Cannot make public: User did not consent to portfolio usage")
  73. db.execute_commit("UPDATE order_photos SET is_public = %s WHERE id = %s", (data.is_public, photo_id))
  74. await audit_service.log(
  75. user_id=admin['id'],
  76. action="update_photo_visibility",
  77. target_type="photo",
  78. target_id=photo_id,
  79. details={"is_public": data.is_public},
  80. request=request
  81. )
  82. # NOTIFY USER VIA WEBSOCKET
  83. order_id = photo_data[0]['order_id']
  84. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  85. if order_info:
  86. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  87. return {"id": photo_id, "is_public": data.is_public}
  88. @router.delete("/admin/photos/{photo_id}")
  89. async def admin_delete_photo(request: Request, photo_id: int, admin: dict = Depends(require_admin)):
  90. photo = db.execute_query("SELECT file_path, order_id FROM order_photos WHERE id = %s", (photo_id,))
  91. if not photo:
  92. raise HTTPException(status_code=404, detail="Photo not found")
  93. order_id = photo[0]['order_id']
  94. try:
  95. path = os.path.join(config.BASE_DIR, photo[0]['file_path'])
  96. if os.path.exists(path):
  97. os.remove(path)
  98. except Exception as e:
  99. print(f"Error deleting photo file: {e}")
  100. db.execute_commit("DELETE FROM order_photos WHERE id = %s", (photo_id,))
  101. await audit_service.log(
  102. user_id=admin['id'],
  103. action="delete_photo",
  104. target_type="photo",
  105. target_id=photo_id,
  106. details={"order_id": order_id},
  107. request=request
  108. )
  109. # NOTIFY USER VIA WEBSOCKET
  110. order_info = db.execute_query("SELECT user_id FROM orders WHERE id = %s", (order_id,))
  111. if order_info:
  112. await global_manager.notify_order_update(order_info[0]['user_id'], order_id)
  113. return {"id": photo_id, "status": "deleted"}