Spaces:
Sleeping
Sleeping
Download backend/presets.py from Success-dev/receipter-pro: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/spaces/Success-dev/receipter-pro/resolve/main/backend/presets.py
- Command line
-
hf download hf://spaces/Success-dev/receipter-pro/backend/presets.py
-
curl -L -o presets.py https://huggingface.co/spaces/Success-dev/receipter-pro/resolve/main/backend/presets.py
3.23 kB
| """ | |
| Saved email presets: a whole message (subject plus either a drag-and-drop design or raw HTML) | |
| that a sender can start the next campaign from. Built-in starter layouts live in the frontend; | |
| only the ones a sender saves are stored, per org at orgs/{org_id}/presets/{id}. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import uuid | |
| from datetime import datetime, timezone | |
| from backend import firestore_client as db | |
| from backend import orgs | |
| _MAX_NAME = 80 | |
| _MAX_SUBJECT = 300 | |
| # A stored document must stay under Firestore's 1 MB limit, images included. | |
| _MAX_CONTENT_BYTES = 900_000 | |
| _MODES = ("builder", "html") | |
| def _now() -> str: | |
| return datetime.now(timezone.utc).isoformat() | |
| def _prefix(org_id: str) -> str: | |
| return f"orgs/{org_id}/presets/" | |
| def _path(org_id: str, preset_id: str) -> str: | |
| return f"{_prefix(org_id)}{preset_id}" | |
| def _clean(name: str, subject: str, mode: str, design: dict | None, html: str) -> dict: | |
| name = name.strip() | |
| if not name or len(name) > _MAX_NAME: | |
| raise ValueError(f"Give the preset a name of at most {_MAX_NAME} characters") | |
| if len(subject) > _MAX_SUBJECT: | |
| raise ValueError("The subject is too long") | |
| if mode not in _MODES: | |
| raise ValueError("A preset is either a drag-and-drop design or HTML") | |
| if mode == "builder" and not isinstance(design, dict): | |
| raise ValueError("This preset has no design to save") | |
| size = len(json.dumps(design or {})) + len(html.encode()) | |
| if size > _MAX_CONTENT_BYTES: | |
| raise ValueError("This preset is too large to save. Use smaller images and try again") | |
| return {"name": name, "subject": subject, "mode": mode, "design": design if mode == "builder" else None, "html": html} | |
| def create_preset(org_id: str, actor_uid: str, name: str, subject: str, mode: str, design: dict | None, html: str) -> str: | |
| orgs.require_permission(org_id, actor_uid, "edit_emails") | |
| data = _clean(name, subject, mode, design, html) | |
| preset_id = uuid.uuid4().hex | |
| db.set_doc(_path(org_id, preset_id), data | {"created_at": _now(), "updated_at": _now()}) | |
| return preset_id | |
| def update_preset(org_id: str, actor_uid: str, preset_id: str, name: str, subject: str, mode: str, | |
| design: dict | None, html: str) -> None: | |
| orgs.require_permission(org_id, actor_uid, "edit_emails") | |
| get_preset(org_id, preset_id) | |
| db.update_doc(_path(org_id, preset_id), _clean(name, subject, mode, design, html) | {"updated_at": _now()}) | |
| def delete_preset(org_id: str, actor_uid: str, preset_id: str) -> None: | |
| orgs.require_permission(org_id, actor_uid, "edit_emails") | |
| get_preset(org_id, preset_id) | |
| db.delete_doc(_path(org_id, preset_id)) | |
| def get_preset(org_id: str, preset_id: str) -> dict: | |
| doc = db.get_doc(_path(org_id, preset_id)) | |
| if doc is None: | |
| raise orgs.NotFoundError(f"No preset {preset_id!r} in org {org_id!r}") | |
| return doc | |
| def list_presets(org_id: str, actor_uid: str) -> list[dict]: | |
| orgs.require_permission(org_id, actor_uid) | |
| prefix = _prefix(org_id) | |
| items = [data | {"preset_id": path[len(prefix):]} for path, data in db.query_prefix(prefix).items()] | |
| return sorted(items, key=lambda p: p.get("updated_at", ""), reverse=True) | |