|
13 | 13 | BackgroundTasks,
|
14 | 14 | Depends,
|
15 | 15 | Query,
|
| 16 | + Form, |
16 | 17 | )
|
17 | 18 | from fastapi.responses import (
|
18 | 19 | HTMLResponse,
|
|
109 | 110 |
|
110 | 111 | from libreforms_fastapi.utils.pydantic_models import (
|
111 | 112 | CreateUserRequest,
|
| 113 | + HelpRequest, |
112 | 114 | get_form_config,
|
113 | 115 | get_form_names,
|
114 | 116 | )
|
@@ -1646,6 +1648,55 @@ async def api_auth_login(form_data: Annotated[OAuth2PasswordRequestForm, Depends
|
1646 | 1648 |
|
1647 | 1649 |
|
1648 | 1650 |
|
| 1651 | + |
| 1652 | +# This is a help route to submit a help request to the sysadmin |
| 1653 | +@app.post("/api/auth/help", dependencies=[Depends(api_key_auth)], response_class=JSONResponse) |
| 1654 | +async def api_auth_help( |
| 1655 | + request: Request, |
| 1656 | + background_tasks: BackgroundTasks, |
| 1657 | + help_request: HelpRequest, |
| 1658 | + session: SessionLocal = Depends(get_db), |
| 1659 | + key: str = Depends(X_API_KEY) |
| 1660 | +): |
| 1661 | + |
| 1662 | + if not config.HELP_PAGE_ENABLED or not config.SMTP_ENABLED: |
| 1663 | + raise HTTPException(status_code=404) |
| 1664 | + |
| 1665 | + # Get the requesting user details |
| 1666 | + user = session.query(User).filter_by(api_key=key).first() |
| 1667 | + |
| 1668 | + if not user: |
| 1669 | + raise HTTPException(status_code=404) |
| 1670 | + |
| 1671 | + time_str = datetime.now(config.TIMEZONE).strftime("%Y-%m-%d %H:%M:%S") |
| 1672 | + |
| 1673 | + # We escape and shorten the message contents as needed |
| 1674 | + safe_subject = escape(help_request.subject) |
| 1675 | + shortened_safe_subject = safe_subject[:50] |
| 1676 | + |
| 1677 | + safe_message = escape(help_request.message) |
| 1678 | + safe_category = escape(help_request.category) |
| 1679 | + |
| 1680 | + full_safe_subject = f"[{config.SITE_NAME}][{user.username}][{safe_category}] {shortened_safe_subject}" |
| 1681 | + |
| 1682 | + full_safe_message = f"You are receiving this message because a user has submitted a request for help at {config.DOMAIN}. " \ |
| 1683 | + f"You can see the request details below.\n\n****\nUser: {user.username}\nEmail: {user.email}\nTime of Submission:" \ |
| 1684 | + f"{time_str}\nCategory: {safe_category}\nSubject: {safe_subject}\nMessage: {safe_message}\n****\n\nYou may reply " \ |
| 1685 | + f"directly to the user who submitted this request by replying to this email." |
| 1686 | + |
| 1687 | + background_tasks.add_task( |
| 1688 | + mailer.send_mail, |
| 1689 | + subject=full_safe_subject, |
| 1690 | + content=full_safe_message, |
| 1691 | + to_address=config.HELP_EMAIL, |
| 1692 | + reply_to_addr=user.email |
| 1693 | + ) |
| 1694 | + |
| 1695 | + return JSONResponse( |
| 1696 | + status_code=202, |
| 1697 | + content={"status": "success"}, |
| 1698 | + ) |
| 1699 | + |
1649 | 1700 | ##########################
|
1650 | 1701 | ### API Routes - Admin
|
1651 | 1702 | ##########################
|
|
0 commit comments