|
119 | 119 | RelationshipTypeModel,
|
120 | 120 | UserRelationshipModel,
|
121 | 121 | FormConfigUpdateRequest,
|
| 122 | + EmailConfigUpdateRequest, |
122 | 123 | SiteConfig,
|
123 | 124 | get_user_model,
|
124 | 125 | get_form_model,
|
|
139 | 140 | from libreforms_fastapi.utils.custom_tinydb import CustomEncoder
|
140 | 141 |
|
141 | 142 | # Import to render more powerful email content
|
142 |
| -from libreforms_fastapi.utils.jinja_emails import render_email_message_from_jinja |
| 143 | +from libreforms_fastapi.utils.jinja_emails import ( |
| 144 | + render_email_message_from_jinja, |
| 145 | + write_email_config_yaml, |
| 146 | + test_email_config, |
| 147 | + get_email_yaml, |
| 148 | +) |
143 | 149 |
|
144 | 150 | # Here we set the application config using the get_config
|
145 | 151 | # factory pattern defined in libreforms_fastapi.utis.config.
|
@@ -4552,7 +4558,7 @@ async def api_admin_get_form_config(
|
4552 | 4558 | raise HTTPException(status_code=404)
|
4553 | 4559 |
|
4554 | 4560 |
|
4555 |
| - _form_config = _get_form_config_yaml(config_path=config.FORM_CONFIG_PATH) |
| 4561 | + _form_config = get_form_config_yaml(config_path=config.FORM_CONFIG_PATH) |
4556 | 4562 |
|
4557 | 4563 | # Write this query to the TransactionLog
|
4558 | 4564 | if config.COLLECT_USAGE_STATISTICS:
|
@@ -4600,7 +4606,7 @@ async def api_admin_write_form_config(
|
4600 | 4606 | raise HTTPException(status_code=404)
|
4601 | 4607 |
|
4602 | 4608 |
|
4603 |
| - old_form_config_str = get_form_config_yaml(config_path=config.FORM_CONFIG_PATH).strip() |
| 4609 | + # old_form_config_str = get_form_config_yaml(config_path=config.FORM_CONFIG_PATH).strip() |
4604 | 4610 |
|
4605 | 4611 | try:
|
4606 | 4612 | write_form_config_yaml(
|
@@ -4634,6 +4640,115 @@ async def api_admin_write_form_config(
|
4634 | 4640 |
|
4635 | 4641 |
|
4636 | 4642 |
|
| 4643 | +# Get email yaml |
| 4644 | +@app.get( |
| 4645 | + "/api/admin/get_email_config", |
| 4646 | + dependencies=[Depends(api_key_auth)], |
| 4647 | + response_class=JSONResponse, |
| 4648 | +) |
| 4649 | +async def api_admin_get_email_config( |
| 4650 | + request: Request, |
| 4651 | + background_tasks: BackgroundTasks, |
| 4652 | + config = Depends(get_config_depends), |
| 4653 | + mailer = Depends(get_mailer), |
| 4654 | + session: SessionLocal = Depends(get_db), |
| 4655 | + key: str = Depends(X_API_KEY), |
| 4656 | + return_as_yaml_str: bool = False, |
| 4657 | +): |
| 4658 | + """ |
| 4659 | + Allows site administrators to view the site email config as yaml. This operation is logged for audit purposes. Set |
| 4660 | + `return_as_yaml_str` to True to receive back a string of the yaml config file. |
| 4661 | + """ |
| 4662 | + |
| 4663 | + # Get the requesting user details |
| 4664 | + user = session.query(User).filter_by(api_key=key).first() |
| 4665 | + |
| 4666 | + if not user or not user.site_admin: |
| 4667 | + raise HTTPException(status_code=404) |
| 4668 | + |
| 4669 | + email_config = get_email_yaml(config_path=config.EMAIL_CONFIG_PATH, return_as_yaml_str=return_as_yaml_str) |
| 4670 | + |
| 4671 | + # Write this query to the TransactionLog |
| 4672 | + if config.COLLECT_USAGE_STATISTICS: |
| 4673 | + |
| 4674 | + endpoint = request.url.path |
| 4675 | + remote_addr = request.client.host |
| 4676 | + |
| 4677 | + background_tasks.add_task( |
| 4678 | + write_api_call_to_transaction_log, |
| 4679 | + api_key=key, |
| 4680 | + endpoint=endpoint, |
| 4681 | + remote_addr=remote_addr, |
| 4682 | + query_params={}, |
| 4683 | + ) |
| 4684 | + |
| 4685 | + return JSONResponse( |
| 4686 | + status_code=200, |
| 4687 | + content={"status": "success", "content": email_config}, |
| 4688 | + ) |
| 4689 | + |
| 4690 | + |
| 4691 | +# Update email config string |
| 4692 | +@app.post( |
| 4693 | + "/api/admin/write_email_config", |
| 4694 | + dependencies=[Depends(api_key_auth)], |
| 4695 | + response_class=JSONResponse, |
| 4696 | +) |
| 4697 | +async def api_admin_write_email_config( |
| 4698 | + request: Request, |
| 4699 | + _email_config: EmailConfigUpdateRequest, |
| 4700 | + background_tasks: BackgroundTasks, |
| 4701 | + config = Depends(get_config_depends), |
| 4702 | + mailer = Depends(get_mailer), |
| 4703 | + session: SessionLocal = Depends(get_db), |
| 4704 | + key: str = Depends(X_API_KEY) |
| 4705 | +): |
| 4706 | + """ |
| 4707 | + Allows site administrators to update the site email config as yaml. This operation is logged for audit purposes. |
| 4708 | + """ |
| 4709 | + |
| 4710 | + # Get the requesting user details |
| 4711 | + user = session.query(User).filter_by(api_key=key).first() |
| 4712 | + |
| 4713 | + if not user or not user.site_admin: |
| 4714 | + raise HTTPException(status_code=404) |
| 4715 | + |
| 4716 | + # old_config_str = get_email_yaml(config_path=config.EMAIL_CONFIG_PATH, return_as_yaml_str=return_as_yaml_str).strip() |
| 4717 | + |
| 4718 | + |
| 4719 | + try: |
| 4720 | + write_email_config_yaml( |
| 4721 | + config_path=config.EMAIL_CONFIG_PATH, |
| 4722 | + email_config_str=_email_config.content, |
| 4723 | + env=config.ENVIRONMENT, |
| 4724 | + timezone=config.TIMEZONE, |
| 4725 | + config=config, user=user, # Add'l kwargs to validate with better data |
| 4726 | + ) |
| 4727 | + except Exception as e: |
| 4728 | + raise HTTPException(status_code=422, detail=f"{e}") |
| 4729 | + |
| 4730 | + # Write this query to the TransactionLog |
| 4731 | + if config.COLLECT_USAGE_STATISTICS: |
| 4732 | + |
| 4733 | + endpoint = request.url.path |
| 4734 | + remote_addr = request.client.host |
| 4735 | + |
| 4736 | + background_tasks.add_task( |
| 4737 | + write_api_call_to_transaction_log, |
| 4738 | + api_key=key, |
| 4739 | + endpoint=endpoint, |
| 4740 | + remote_addr=remote_addr, |
| 4741 | + query_params={}, |
| 4742 | + ) |
| 4743 | + |
| 4744 | + return JSONResponse( |
| 4745 | + status_code=200, |
| 4746 | + content={"status": "success"}, |
| 4747 | + ) |
| 4748 | + |
| 4749 | + |
| 4750 | + |
| 4751 | + |
4637 | 4752 | # Update form config string
|
4638 | 4753 | @app.post(
|
4639 | 4754 | "/api/admin/update_site_config",
|
|
0 commit comments