Spaces:
Sleeping
Sleeping
File size: 17,121 Bytes
bd161ec |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
"""
Project management router for creating and managing projects.
"""
from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException, status
import logging
from models import User, Project, ProjectMember, ProjectRole, GPTModeSession
from schemas import (
ProjectCreate, ProjectUpdate, ProjectResponse, ProjectCreateResponse, ProjectInvite,
APIResponse
)
from dependencies import get_current_active_user
import uuid
from pydantic import BaseModel
# Removed unused imports - no longer needed in projects.py
from fastapi.responses import FileResponse
import tempfile
from docx import Document
import json
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from database import get_async_db
from datetime import datetime, timezone
# Removed chatbot service import - no longer needed in projects.py
logger = logging.getLogger(__name__)
router = APIRouter()
# Removed duplicate chatbot functionality - these are now handled in assistant.py
# --- Project Export Functions ---
@router.post("/", response_model=ProjectCreateResponse, status_code=201)
async def create_project(
project_data: ProjectCreate,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_async_db)
):
"""Create a new project (async)."""
try:
# Create project ID first
project_id = str(uuid.uuid4())
current_time = datetime.now(timezone.utc)
# Use raw SQL to avoid any ORM relationship issues
from sqlalchemy import text
await db.execute(
text("""
INSERT INTO projects (id, title, description, owner_id, is_active, created_at, updated_at)
VALUES (:id, :title, :description, :owner_id, :is_active, :created_at, :updated_at)
"""),
{
"id": project_id,
"title": project_data.title,
"description": project_data.description,
"owner_id": current_user.id,
"is_active": True,
"created_at": current_time,
"updated_at": current_time
}
)
await db.commit()
# Create the 14 phases for this project
await create_project_phases(db, project_id)
# Create response data without accessing any ORM objects
response_data = {
"id": project_id,
"title": project_data.title,
"description": project_data.description,
"owner": {
"id": current_user.id,
"email": current_user.email,
"full_name": current_user.full_name,
"role": current_user.role,
"is_active": current_user.is_active,
"created_at": current_user.created_at
},
"is_active": True,
"created_at": current_time,
"updated_at": current_time
}
logger.info(f"Project created: {project_data.title} by {current_user.email}")
return response_data
except Exception as e:
logger.error(f"Error creating project: {e}")
try:
await db.rollback()
except:
pass # Ignore rollback errors
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to create project"
)
async def create_project_phases(db: AsyncSession, project_id: str):
"""Create the 14 phases for a new project."""
try:
from models import Phase, PhaseStatus
from datetime import datetime, timezone
current_time = datetime.now(timezone.utc)
# Phase titles and descriptions
phase_data = [
(1, "Project Overview", "Create a comprehensive project overview"),
(2, "Target Audience Analysis", "Define and analyze target audience"),
(3, "Problem Statement", "Identify and articulate the problem"),
(4, "Solution Framework", "Develop solution framework"),
(5, "Value Proposition", "Define unique value proposition"),
(6, "Market Research", "Conduct market research and analysis"),
(7, "Competitive Analysis", "Analyze competitors and market positioning"),
(8, "Business Model", "Define business model and revenue streams"),
(9, "Marketing Strategy", "Develop comprehensive marketing strategy"),
(10, "Implementation Plan", "Create detailed implementation plan"),
(11, "Risk Assessment", "Identify and assess potential risks"),
(12, "Success Metrics", "Define key performance indicators"),
(13, "Timeline and Milestones", "Create project timeline and milestones"),
(14, "Final Review and Recommendations", "Final review and recommendations")
]
# Create phases using raw SQL to avoid ORM issues
from sqlalchemy import text
for phase_number, title, description in phase_data:
phase_id = str(uuid.uuid4())
await db.execute(
text("""
INSERT INTO phases (id, project_id, phase_number, title, description, status, created_at, updated_at)
VALUES (:id, :project_id, :phase_number, :title, :description, :status, :created_at, :updated_at)
"""),
{
"id": phase_id,
"project_id": project_id,
"phase_number": phase_number,
"title": title,
"description": description,
"status": PhaseStatus.NOT_STARTED.value,
"created_at": current_time,
"updated_at": current_time
}
)
await db.commit()
logger.info(f"Created 14 phases for project {project_id}")
except Exception as e:
logger.error(f"Error creating phases for project {project_id}: {e}")
await db.rollback()
raise
@router.get("/", response_model=list[ProjectResponse])
async def get_user_projects(
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_async_db)
):
"""Get all projects for the current user (async)."""
try:
result = await db.execute(
select(Project)
.where(Project.owner_id == current_user.id)
)
projects = result.scalars().all()
# Convert to response format manually to avoid greenlet issues
response_projects = []
for project in projects:
project_data = {
"id": project.id,
"title": project.title,
"description": project.description,
"owner": {
"id": current_user.id,
"email": current_user.email,
"full_name": current_user.full_name,
"role": current_user.role,
"is_active": current_user.is_active,
"created_at": current_user.created_at
},
"members": [], # Empty list for now
"is_active": project.is_active,
"created_at": project.created_at,
"updated_at": project.updated_at
}
response_projects.append(project_data)
return response_projects
except Exception as e:
logger.error(f"Error getting user projects: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to retrieve projects"
)
@router.get("/{project_id}", response_model=ProjectResponse)
async def get_project(
project_id: str,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_async_db)
):
"""Get a specific project (async)."""
try:
project_result = await db.execute(
select(Project).where(Project.id == project_id)
)
project = project_result.scalar_one_or_none()
if not project:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
# Check if user is owner
if project.owner_id != current_user.id:
# Check if user is a member
member_result = await db.execute(
select(ProjectMember).where(
ProjectMember.project_id == project_id,
ProjectMember.user_id == current_user.id
)
)
member = member_result.scalar_one_or_none()
if not member:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied to this project"
)
# Format response manually
response_data = {
"id": project.id,
"title": project.title,
"description": project.description,
"owner": {
"id": current_user.id,
"email": current_user.email,
"full_name": current_user.full_name,
"role": current_user.role,
"is_active": current_user.is_active,
"created_at": current_user.created_at
},
"members": [], # Empty list for now
"is_active": project.is_active,
"created_at": project.created_at,
"updated_at": project.updated_at
}
return response_data
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting project: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to retrieve project"
)
@router.put("/{project_id}", response_model=ProjectResponse)
async def update_project(
project_id: str,
project_data: ProjectUpdate,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_async_db)
):
project_result = await db.execute(select(Project).where(Project.id == project_id))
project = project_result.scalar()
if not project:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
if project.owner_id != current_user.id:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Only project owner can update project")
if project_data.title is not None:
project.title = project_data.title
if project_data.description is not None:
project.description = project_data.description
await db.commit()
await db.refresh(project)
# Format response manually
response_data = {
"id": project.id,
"title": project.title,
"description": project.description,
"owner": {
"id": current_user.id,
"email": current_user.email,
"full_name": current_user.full_name,
"role": current_user.role,
"is_active": current_user.is_active,
"created_at": current_user.created_at
},
"members": [], # Empty list for now
"is_active": project.is_active,
"created_at": project.created_at,
"updated_at": project.updated_at
}
return response_data
@router.delete("/{project_id}", response_model=APIResponse)
async def delete_project(
project_id: str,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_async_db)
):
project = await db.execute(select(Project).where(Project.id == project_id))
project = project.scalar()
if not project:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
if project.owner_id != current_user.id:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Only project owner can delete project")
project.is_active = False
await db.commit()
logger.info(f"Project deleted: {project.title} by {current_user.email}")
return APIResponse(success=True, message="Project deleted successfully")
@router.post("/{project_id}/invite", response_model=APIResponse)
async def invite_user_to_project(
project_id: str,
invite_data: ProjectInvite,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_async_db)
):
project = await db.execute(select(Project).where(Project.id == project_id))
project = project.scalar()
if not project:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
if project.owner_id != current_user.id:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Only project owner can invite users")
invited_user = await db.execute(select(User).where(User.email == invite_data.email))
invited_user = invited_user.scalar()
if not invited_user:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
existing_member = await db.execute(select(ProjectMember).where(
ProjectMember.project_id == project_id,
ProjectMember.user_id == invited_user.id
))
existing_member = existing_member.scalar()
if existing_member:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="User is already a member of this project")
db_member = ProjectMember(
project_id=project_id,
user_id=invited_user.id,
role=invite_data.role
)
db.add(db_member)
await db.commit()
logger.info(f"User {invited_user.email} invited to project {project.title}")
return APIResponse(success=True, message=f"User {invited_user.email} invited successfully")
@router.delete("/{project_id}/members/{user_id}", response_model=APIResponse)
async def remove_project_member(
project_id: str,
user_id: str,
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_async_db)
):
project = await db.execute(select(Project).where(Project.id == project_id))
project = project.scalar()
if not project:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
if project.owner_id != current_user.id:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Only project owner can remove members")
member = await db.execute(select(ProjectMember).where(
ProjectMember.project_id == project_id,
ProjectMember.user_id == user_id
))
member = member.scalar()
if not member:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Member not found")
await db.delete(member)
await db.commit()
logger.info(f"Member {user_id} removed from project {project.title}")
return APIResponse(success=True, message="Member removed successfully")
# --- GPT Mode Session Management ---
# Removed duplicate chatbot functionality - these are now handled in assistant.py
# Removed duplicate chatbot endpoints - these are now handled in assistant.py
@router.get("/{project_id}/export/pdf")
async def export_project_pdf(
project_id: str,
db: AsyncSession = Depends(get_async_db),
user: User = Depends(get_current_active_user)
):
# Gather all mode summaries for the project
sessions = await db.execute(select(GPTModeSession).where(GPTModeSession.project_id == project_id))
sessions = sessions.scalars().all()
doc = Document()
doc.add_heading(f"Project {project_id} Summary", 0)
for session in sessions:
doc.add_heading(session.mode_name, level=1)
for qnum, answer in session.answers.items():
doc.add_paragraph(f"Q{qnum}: {answer}")
if session.checkpoint_json.get('output_template'):
doc.add_paragraph("Output Template:")
doc.add_paragraph(session.checkpoint_json['output_template'])
with tempfile.NamedTemporaryFile(delete=False, suffix='.docx') as tmp:
doc.save(tmp.name)
tmp_path = tmp.name
return FileResponse(tmp_path, filename=f"project_{project_id}_summary.docx")
@router.get("/{project_id}/export/json")
async def export_project_json(
project_id: str,
db: AsyncSession = Depends(get_async_db),
user: User = Depends(get_current_active_user)
):
sessions = await db.execute(select(GPTModeSession).where(GPTModeSession.project_id == project_id))
sessions = sessions.scalars().all()
all_data = {}
for session in sessions:
all_data[session.mode_name] = {
"answers": session.answers,
"checkpoint_json": session.checkpoint_json
}
with tempfile.NamedTemporaryFile(delete=False, suffix='.json', mode='w', encoding='utf-8') as tmp:
json.dump(all_data, tmp, ensure_ascii=False, indent=2)
tmp_path = tmp.name
return FileResponse(tmp_path, filename=f"project_{project_id}_summary.json")
|