Improved Licensing

This commit is contained in:
2026-02-18 00:00:32 +01:00
parent a062383af0
commit af2c9e7fd8
17 changed files with 831 additions and 250 deletions

View File

@@ -1615,6 +1615,14 @@ async def post_detail_page(request: Request, post_id: str):
for item in post.media_items
]
# Check token limit for quick action buttons
limit_reached = False
limit_message = ""
if session.account_type in ("company", "employee") and session.company_id:
can_proceed, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
limit_reached = not can_proceed
limit_message = error_msg
return templates.TemplateResponse("post_detail.html", {
"request": request,
"page": "posts",
@@ -1627,7 +1635,9 @@ async def post_detail_page(request: Request, post_id: str):
"post_type_analysis": post_type_analysis,
"final_feedback": final_feedback,
"profile_picture_url": profile_picture_url,
"media_items_dict": media_items_dict
"media_items_dict": media_items_dict,
"limit_reached": limit_reached,
"limit_message": limit_message
})
except Exception as e:
import traceback
@@ -1643,11 +1653,11 @@ async def research_page(request: Request):
if not session:
return RedirectResponse(url="/login", status_code=302)
# Check research limit for companies/employees
# Check token limit for companies/employees
limit_reached = False
limit_message = ""
if session.account_type in ("company", "employee") and session.company_id:
can_create, error_msg = await db.check_company_research_limit(UUID(session.company_id))
can_create, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
limit_reached = not can_create
limit_message = error_msg
@@ -1672,11 +1682,11 @@ async def create_post_page(request: Request):
if not session:
return RedirectResponse(url="/login", status_code=302)
# Check post limit for companies/employees
# Check token limit for companies/employees
limit_reached = False
limit_message = ""
if session.account_type in ("company", "employee") and session.company_id:
can_create, error_msg = await db.check_company_post_limit(UUID(session.company_id))
can_create, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
limit_reached = not can_create
limit_message = error_msg
@@ -1703,6 +1713,14 @@ async def chat_create_page(request: Request):
user_id = UUID(session.user_id)
# Check token limit for companies/employees
limit_reached = False
limit_message = ""
if session.account_type in ("company", "employee") and session.company_id:
can_create, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
limit_reached = not can_create
limit_message = error_msg
# Get post types
post_types = await db.get_post_types(user_id)
if not post_types:
@@ -1724,7 +1742,9 @@ async def chat_create_page(request: Request):
"session": session,
"post_types": post_types,
"profile_picture": profile_picture,
"saved_posts": saved_posts
"saved_posts": saved_posts,
"limit_reached": limit_reached,
"limit_message": limit_message
})
@@ -1848,11 +1868,9 @@ async def start_research(request: Request, background_tasks: BackgroundTasks, po
if not session:
raise HTTPException(status_code=401, detail="Not authenticated")
# CHECK COMPANY RESEARCH LIMIT and capture company_id for quota tracking
quota_company_id = None
# CHECK COMPANY TOKEN LIMIT
if session.account_type in ("company", "employee") and session.company_id:
quota_company_id = UUID(session.company_id)
can_create, error_msg = await db.check_company_research_limit(quota_company_id)
can_create, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
if not can_create:
raise HTTPException(status_code=429, detail=error_msg)
@@ -1876,14 +1894,6 @@ async def start_research(request: Request, background_tasks: BackgroundTasks, po
post_type_id=UUID(post_type_id) if post_type_id else None
)
# INCREMENT COMPANY QUOTA after successful research
if quota_company_id:
try:
await db.increment_company_researches_quota(quota_company_id)
logger.info(f"Incremented research quota for company {quota_company_id}")
except Exception as quota_error:
logger.error(f"Failed to increment research quota: {quota_error}")
progress_store[task_id] = {"status": "completed", "message": f"{len(topics)} Topics gefunden!", "progress": 100, "topics": topics}
except Exception as e:
logger.exception(f"Research failed: {e}")
@@ -1994,11 +2004,9 @@ async def create_post(
if not session:
raise HTTPException(status_code=401, detail="Not authenticated")
# CHECK COMPANY POST LIMIT and capture company_id for quota tracking
quota_company_id = None
# CHECK COMPANY TOKEN LIMIT
if session.account_type in ("company", "employee") and session.company_id:
quota_company_id = UUID(session.company_id)
can_create, error_msg = await db.check_company_post_limit(quota_company_id)
can_create, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
if not can_create:
raise HTTPException(status_code=429, detail=error_msg)
@@ -2031,14 +2039,6 @@ async def create_post(
selected_hook=selected_hook
)
# INCREMENT COMPANY QUOTA after successful creation
if quota_company_id:
try:
await db.increment_company_posts_quota(quota_company_id)
logger.info(f"Incremented post quota for company {quota_company_id}")
except Exception as quota_error:
logger.error(f"Failed to increment post quota: {quota_error}")
progress_store[task_id] = {
"status": "completed", "message": "Post erstellt!", "progress": 100,
"result": {
@@ -2061,6 +2061,12 @@ async def get_post_suggestions(request: Request, post_id: str):
if not session:
raise HTTPException(status_code=401, detail="Not authenticated")
# Check token limit for companies/employees
if session.account_type in ("company", "employee") and session.company_id:
can_proceed, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
if not can_proceed:
raise HTTPException(status_code=429, detail=error_msg)
try:
post = await db.get_generated_post(UUID(post_id))
if not post:
@@ -2100,6 +2106,12 @@ async def revise_post(
if not session:
raise HTTPException(status_code=401, detail="Not authenticated")
# Check token limit for companies/employees
if session.account_type in ("company", "employee") and session.company_id:
can_proceed, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
if not can_proceed:
raise HTTPException(status_code=429, detail=error_msg)
try:
post = await db.get_generated_post(UUID(post_id))
if not post:
@@ -2886,11 +2898,11 @@ async def company_manage_research(request: Request, employee_id: str = None):
if not emp_user or str(emp_user.company_id) != session.company_id:
return RedirectResponse(url="/company/manage", status_code=302)
# Check research limit
# Check token limit
limit_reached = False
limit_message = ""
if session.company_id:
can_create, error_msg = await db.check_company_research_limit(UUID(session.company_id))
can_create, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
limit_reached = not can_create
limit_message = error_msg
@@ -2933,11 +2945,11 @@ async def company_manage_create(request: Request, employee_id: str = None):
if not emp_user or str(emp_user.company_id) != session.company_id:
return RedirectResponse(url="/company/manage", status_code=302)
# Check post limit
# Check token limit
limit_reached = False
limit_message = ""
if session.company_id:
can_create, error_msg = await db.check_company_post_limit(UUID(session.company_id))
can_create, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
limit_reached = not can_create
limit_message = error_msg
@@ -3308,6 +3320,12 @@ async def chat_generate_post(request: Request):
if not post_type_id:
return JSONResponse({"success": False, "error": "Post-Typ erforderlich"})
# Check token limit for companies/employees
if session.account_type in ("company", "employee") and session.company_id:
can_proceed, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
if not can_proceed:
return JSONResponse({"success": False, "token_limit_exceeded": True, "error": error_msg})
user_id = UUID(session.user_id)
# Get post type info
@@ -3341,6 +3359,11 @@ async def chat_generate_post(request: Request):
# Generate post using writer agent with user's content as primary focus
from src.agents.writer import WriterAgent
writer = WriterAgent()
writer.set_tracking_context(
operation='post_creation',
user_id=session.user_id,
company_id=session.company_id
)
# Create a topic structure from user's message
topic = {
@@ -3399,6 +3422,12 @@ async def chat_refine_post(request: Request):
if not post_type_id:
return JSONResponse({"success": False, "error": "Post-Typ erforderlich"})
# Check token limit for companies/employees
if session.account_type in ("company", "employee") and session.company_id:
can_proceed, error_msg, _, _ = await db.check_company_token_limit(UUID(session.company_id))
if not can_proceed:
return JSONResponse({"success": False, "token_limit_exceeded": True, "error": error_msg})
user_id = UUID(session.user_id)
# Get post type info
@@ -3442,6 +3471,11 @@ async def chat_refine_post(request: Request):
# Refine post using writer with feedback
from src.agents.writer import WriterAgent
writer = WriterAgent()
writer.set_tracking_context(
operation='post_creation',
user_id=session.user_id,
company_id=session.company_id
)
topic = {
"title": "Chat refinement",