feat: add project code generation + remove issues/milestones from nav
This commit is contained in:
@@ -139,6 +139,11 @@ def update_issue(issue_id: int, issue_update: schemas.IssueUpdate, db: Session =
|
||||
check_project_role(db, current_user.id, issue.project_id, min_role="dev")
|
||||
if not issue:
|
||||
raise HTTPException(status_code=404, detail="Issue not found")
|
||||
update_data = issue_update.model_dump(exclude_unset=True)
|
||||
if "issue_type" in update_data or issue_subtype in update_data:
|
||||
new_type = update_data.get("issue_type", issue.issue_type)
|
||||
new_subtype = update_data.get("issue_subtype", issue.issue_subtype)
|
||||
_validate_issue_type_subtype(new_type, new_subtype, require_subtype="issue_type" in update_data)
|
||||
for field, value in update_data.items():
|
||||
setattr(issue, field, value)
|
||||
db.commit()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Projects router with RBAC."""
|
||||
import re
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -11,10 +12,116 @@ from app.api.rbac import check_project_role
|
||||
|
||||
router = APIRouter(prefix="/projects", tags=["Projects"])
|
||||
|
||||
WORD_SEGMENT_RE = re.compile(r"[A-Za-z]+")
|
||||
CAMEL_RE = re.compile(r"[A-Z]+(?=[A-Z][a-z])|[A-Z]?[a-z]+|[A-Z]+")
|
||||
|
||||
|
||||
def _split_words(name: str):
|
||||
segments = WORD_SEGMENT_RE.findall(name or '')
|
||||
words = []
|
||||
for seg in segments:
|
||||
parts = CAMEL_RE.findall(seg)
|
||||
for part in parts:
|
||||
if part.isupper() and len(part) > 1:
|
||||
words.extend(list(part))
|
||||
else:
|
||||
words.append(part)
|
||||
return words
|
||||
|
||||
|
||||
def _code_exists(db, code: str) -> bool:
|
||||
return db.query(models.Project).filter(models.Project.project_code == code).first() is not None
|
||||
|
||||
|
||||
def _next_counter(db, prefix: str, width: int) -> str:
|
||||
if width <= 0:
|
||||
return ''
|
||||
counter = db.query(models.ProjectCodeCounter).filter(models.ProjectCodeCounter.prefix == prefix).first()
|
||||
if not counter:
|
||||
counter = models.ProjectCodeCounter(prefix=prefix, next_value=0)
|
||||
db.add(counter)
|
||||
db.flush()
|
||||
value = counter.next_value
|
||||
counter.next_value += 1
|
||||
db.flush()
|
||||
return format(value, 'x').upper().zfill(width)
|
||||
|
||||
|
||||
def _generate_with_counter(db, prefix: str, width: int) -> str:
|
||||
if prefix.upper() == 'UN':
|
||||
prefix = 'UN'
|
||||
while True:
|
||||
suffix = _next_counter(db, prefix, width)
|
||||
code = (prefix + suffix).upper()
|
||||
if not _code_exists(db, code):
|
||||
return code
|
||||
|
||||
|
||||
def _generate_project_code(db, name: str) -> str:
|
||||
words = _split_words(name)
|
||||
if not words:
|
||||
return _generate_with_counter(db, 'UN', 4)
|
||||
|
||||
if len(words) == 1:
|
||||
letters = ''.join([c for c in words[0] if c.isalpha()]).upper()
|
||||
if not letters:
|
||||
return _generate_with_counter(db, 'UN', 4)
|
||||
if len(letters) >= 6:
|
||||
code = letters[:6]
|
||||
if _code_exists(db, code):
|
||||
raise HTTPException(status_code=400, detail='Project code collision')
|
||||
return code
|
||||
prefix = letters
|
||||
width = 6 - len(prefix)
|
||||
return _generate_with_counter(db, prefix, width)
|
||||
|
||||
total_letters = sum(len(w) for w in words)
|
||||
if len(words) > 6:
|
||||
code = ''.join([w[0] for w in words[:6]]).upper()
|
||||
if _code_exists(db, code):
|
||||
raise HTTPException(status_code=400, detail='Project code collision')
|
||||
return code
|
||||
|
||||
if total_letters < 6:
|
||||
prefix = ''.join(words).upper()
|
||||
width = 6 - len(prefix)
|
||||
return _generate_with_counter(db, prefix, width)
|
||||
|
||||
if total_letters == 6:
|
||||
code = ''.join(words).upper()
|
||||
if _code_exists(db, code):
|
||||
raise HTTPException(status_code=400, detail='Project code collision')
|
||||
return code
|
||||
|
||||
word_count = len(words)
|
||||
needed = 6 - word_count
|
||||
for idx in range(word_count - 1, -1, -1):
|
||||
extra_letters = list(words[idx][1:])
|
||||
if needed > len(extra_letters):
|
||||
continue
|
||||
indices = list(range(len(extra_letters)))
|
||||
def combos(start, depth, path):
|
||||
if depth == 0:
|
||||
yield path
|
||||
return
|
||||
for i in range(start, len(indices) - depth + 1):
|
||||
yield from combos(i + 1, depth - 1, path + [indices[i]])
|
||||
for combo in combos(0, needed, []):
|
||||
pieces = []
|
||||
for wi, w in enumerate(words):
|
||||
pieces.append(w[0])
|
||||
if wi == idx:
|
||||
pieces.extend([extra_letters[i] for i in combo])
|
||||
code = ''.join(pieces)[:6].upper()
|
||||
if not _code_exists(db, code):
|
||||
return code
|
||||
raise HTTPException(status_code=400, detail='Project code collision')
|
||||
|
||||
@router.post("", response_model=schemas.ProjectResponse, status_code=status.HTTP_201_CREATED)
|
||||
def create_project(project: schemas.ProjectCreate, db: Session = Depends(get_db)):
|
||||
db_project = models.Project(**project.model_dump())
|
||||
payload = project.model_dump()
|
||||
payload["project_code"] = _generate_project_code(db, project.name)
|
||||
db_project = models.Project(**payload)
|
||||
db.add(db_project)
|
||||
db.commit()
|
||||
db.refresh(db_project)
|
||||
|
||||
Reference in New Issue
Block a user