diff options
| author | 2021-03-16 17:01:48 +0300 | |
|---|---|---|
| committer | 2021-03-16 17:08:26 +0300 | |
| commit | 0947ade71504b08bf91d830b0ec872fca6ffaad8 (patch) | |
| tree | dfd3c32201eafe30a759cab7ab2d2a77c0ad149e | |
| parent | Adds Discord Context Helper (diff) | |
Adds DM Featuredm-message
Adds the form variables to allow sending DMs.
Signed-off-by: Hassan Abouelela <[email protected]>
Diffstat (limited to '')
| -rw-r--r-- | SCHEMA.md | 26 | ||||
| -rw-r--r-- | backend/constants.py | 1 | ||||
| -rw-r--r-- | backend/models/form.py | 33 | ||||
| -rw-r--r-- | backend/routes/forms/submit.py | 8 | 
4 files changed, 48 insertions, 20 deletions
@@ -12,16 +12,17 @@ In this document:  ## Form -| Field               | Type                                      | Description                                                                               | Example                                  | -| ------------------- | ----------------------------------------- | ----------------------------------------------------------------------------------------- | ---------------------------------------- | -| `id`                | Unique identifier                         | A user selected, unique, descriptive identifier (used in URL routes, so no spaces)        | `"ban-appeals"`                          | -| `features`          | List of [form features](#form-features)   | A list of features to change the behaviour of the form, described in the features section | `["OPEN", "COLLECT_EMAIL"]`              | -| `questions`         | List of [form questions](#form-question)  | The list of questions to render on a specific form                                        | Too long! See below                      | -| `name`              | String                                    | Name of the form                                                                          | `"Summer Code Jam 2100"`                 | -| `description`       | String                                    | Form description                                                                          | `"This is my amazing form description."` | -| `webhook`           | [Webhook object](#webhooks)               | An optional discord webhook.                                                              | See webhook documentation.               | -| `submitted_text`    | Optional[String]                          | An optional string for the response upon submitting.                                      | `"This is my amazing form response."`    | -| `discord_role`      | String (optional)                         | Discord role ID what will be assigned, required when `ASSIGN_ROLE` flag provided.         | `784467518298259466`                     | +| Field               | Type                                      | Description                                                                                                                                       | Example                                  | +| ------------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- | +| `id`                | Unique identifier                         | A user selected, unique, descriptive identifier (used in URL routes, so no spaces)                                                                | `"ban-appeals"`                          | +| `features`          | List of [form features](#form-features)   | A list of features to change the behaviour of the form, described in the features section                                                         | `["OPEN", "COLLECT_EMAIL"]`              | +| `questions`         | List of [form questions](#form-question)  | The list of questions to render on a specific form                                                                                                | Too long! See below                      | +| `name`              | String                                    | Name of the form                                                                                                                                  | `"Summer Code Jam 2100"`                 | +| `description`       | String                                    | Form description                                                                                                                                  | `"This is my amazing form description."` | +| `webhook`           | [Webhook object](#webhooks)               | An optional discord webhook.                                                                                                                      | See webhook documentation.               | +| `submitted_text`    | Optional[String]                          | An optional string for the response upon submitting.                                                                                              | `"This is my amazing form response."`    | +| `discord_role`      | String (optional)                         | Discord role ID what will be assigned, required when `ASSIGN_ROLE` flag provided.                                                                 | `784467518298259466`                     | +| `dm_message`        | Optional[String]                          | A message to send to the user after submitting a form. Can use [message variables](#message-variables). Required when `SEND_DM` flag is provided. | `"Hello world!"`                         |  ### Form features @@ -34,6 +35,7 @@ In this document:  | `DISABLE_ANTISPAM` | Disable the anti-spam checks from running on a form submission.               |  | `WEBHOOK_ENABLED`  | The form should notify the webhook. Has no effect if no webhook is set.       |  | `ASSIGN_ROLE`      | The form should assign role to user. Requires `REQUIRES_LOGIN`.               | +| `SEND_DM`          | The form should send a DM to the user. Requires `REQUIRES_LOGIN`.             |  ### Webhooks  Discord webhooks to send information upon form submission. @@ -41,10 +43,10 @@ Discord webhooks to send information upon form submission.  | Field     | Type   | Description                                                                                               |  | ----------| ------ | --------------------------------------------------------------------------------------------------------- |  | `url`     | String | Discord webhook URL.                                                                                      | -| `message` | String | An optional message to include before the embed. Can use certain [context variables](#webhook-variables). | +| `message` | String | An optional message to include before the embed. Can use certain [context variables](#message-variables). | -#### Webhook Variables +#### Message Variables  The following variables can be used in a webhook's message. The variables must be wrapped by braces (`{}`).  | Name          | Description                                                                  | diff --git a/backend/constants.py b/backend/constants.py index 8a32816..d33f205 100644 --- a/backend/constants.py +++ b/backend/constants.py @@ -73,6 +73,7 @@ class FormFeatures(Enum):      DISABLE_ANTISPAM = "DISABLE_ANTISPAM"      WEBHOOK_ENABLED = "WEBHOOK_ENABLED"      ASSIGN_ROLE = "ASSIGN_ROLE" +    SEND_DM = "SEND_DM"  class WebHook(Enum): diff --git a/backend/models/form.py b/backend/models/form.py index 30ae0e7..9970dce 100644 --- a/backend/models/form.py +++ b/backend/models/form.py @@ -14,7 +14,14 @@ PUBLIC_FIELDS = [      "name",      "description",      "submitted_text", -    "discord_role" +    "discord_role", +    "dm_message", +] + +REQUIRES_LOGIN_FEATURES = [ +    FormFeatures.COLLECT_EMAIL, +    FormFeatures.ASSIGN_ROLE, +    FormFeatures.SEND_DM,  ] @@ -43,6 +50,7 @@ class Form(BaseModel):      submitted_text: t.Optional[str] = None      webhook: _WebHook = None      discord_role: t.Optional[str] +    dm_message: t.Optional[str]      class Config:          allow_population_by_field_name = True @@ -57,13 +65,9 @@ class Form(BaseModel):              raise ValueError("Form features list contains one or more invalid values.")          if FormFeatures.REQUIRES_LOGIN.value not in value: -            if FormFeatures.COLLECT_EMAIL.value in value: -                raise ValueError( -                    "COLLECT_EMAIL feature require REQUIRES_LOGIN feature." -                ) - -            if FormFeatures.ASSIGN_ROLE.value in value: -                raise ValueError("ASSIGN_ROLE feature require REQUIRES_LOGIN feature.") +            for feature in REQUIRES_LOGIN_FEATURES: +                if feature.value in value: +                    raise ValueError(f"{feature.value} feature requires REQUIRES_LOGIN feature.")          return value @@ -80,6 +84,19 @@ class Form(BaseModel):          return values +    @root_validator +    def validate_dm_message(cls, values: dict[str, t.Any]) -> t.Optional[dict[str, t.Any]]: +        """Validates message provided when flag provided.""" +        if ( +            FormFeatures.SEND_DM.value in values.get("features", []) +            and not values.get("dm_message") +        ): +            raise ValueError( +                "dm_message field is required when SEND_DM flag is provided." +            ) + +        return values +      def dict(self, admin: bool = True, **kwargs: t.Any) -> dict[str, t.Any]:          """Wrapper for original function to exclude private data for public access."""          data = super().dict(**kwargs) diff --git a/backend/routes/forms/submit.py b/backend/routes/forms/submit.py index d0c7a87..f4b3174 100644 --- a/backend/routes/forms/submit.py +++ b/backend/routes/forms/submit.py @@ -197,6 +197,14 @@ class SubmitForm(Route):                      request_user=request.user                  ) +            if constants.FormFeatures.SEND_DM.value in form.features: +                tasks.add_task( +                    discord.send_direct_message, +                    form=form, +                    response=response_obj, +                    user=request.user +                ) +              return JSONResponse({                  "form": form.dict(admin=False),                  "response": response_obj.dict()  |