-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Be more defensive in the prompt view handling (#48)
- Loading branch information
Showing
4 changed files
with
116 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django import forms | ||
from django.core.exceptions import ValidationError | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
class PromptTextField(forms.CharField): | ||
default_error_messages = { | ||
"required": _( | ||
"No text provided - please enter some text before using AI features." | ||
), | ||
} | ||
|
||
|
||
class PromptUUIDField(forms.UUIDField): | ||
default_error_messages = { | ||
"required": _("Invalid prompt provided."), | ||
"invalid": _("Invalid prompt provided."), | ||
} | ||
|
||
|
||
class PromptForm(forms.Form): | ||
text = PromptTextField() | ||
prompt = PromptUUIDField() | ||
|
||
def clean_prompt(self): | ||
prompt_uuid = self.cleaned_data["prompt"] | ||
if prompt_uuid.version != 4: | ||
raise ValidationError( | ||
self.fields["prompt"].error_messages["invalid"], code="invalid" | ||
) | ||
|
||
return prompt_uuid | ||
|
||
def errors_for_json_response(self) -> str: | ||
errors_for_response = [] | ||
for _field, errors in self.errors.get_json_data().items(): | ||
for error in errors: | ||
errors_for_response.append(error["message"]) | ||
|
||
return " \n".join(errors_for_response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -219,3 +219,5 @@ | |
}, | ||
}, | ||
} | ||
|
||
FORMS_URLFIELD_ASSUME_HTTPS = True |