Skip to content

Commit

Permalink
Raise form validation error if site ID/title/server name is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
homeworkprod committed Apr 21, 2024
1 parent bbdf0c5 commit 72e25d7
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 6 deletions.
70 changes: 69 additions & 1 deletion byceps/blueprints/admin/site/forms.py
Expand Up @@ -8,13 +8,14 @@

from flask_babel import lazy_gettext, pgettext
from wtforms import BooleanField, SelectField, StringField
from wtforms.validators import InputRequired, Length, Optional
from wtforms.validators import InputRequired, Length, Optional, ValidationError

from byceps.services.board import board_service
from byceps.services.news import news_channel_service
from byceps.services.party import party_service
from byceps.services.shop.shop import shop_service
from byceps.services.shop.storefront import storefront_service
from byceps.services.site import site_service
from byceps.util.forms import MultiCheckboxField
from byceps.util.l10n import LocalizedForm

Expand Down Expand Up @@ -79,10 +80,77 @@ class CreateForm(_BaseForm):
lazy_gettext('ID'), validators=[InputRequired(), Length(min=1, max=40)]
)

@staticmethod
def validate_id(form, field):
site_id = field.data.strip()

if site_service.find_site(site_id):
raise ValidationError(
lazy_gettext(
'This value is not available. Please choose another.'
)
)

@staticmethod
def validate_title(form, field):
title = field.data.strip()

if not site_service.is_title_available(title):
raise ValidationError(
lazy_gettext(
'This value is not available. Please choose another.'
)
)

@staticmethod
def validate_server_name(form, field):
server_name = field.data.strip()

if not site_service.is_server_name_available(server_name):
raise ValidationError(
lazy_gettext(
'This value is not available. Please choose another.'
)
)


class UpdateForm(_BaseForm):
archived = BooleanField(lazy_gettext('archived'))

def __init__(
self, current_title: str, current_server_name: str, *args, **kwargs
):
super().__init__(*args, **kwargs)
self._current_title = current_title
self._current_server_name = current_server_name

@staticmethod
def validate_title(form, field):
title = field.data.strip()

if title != form._current_title and not site_service.is_title_available(
title
):
raise ValidationError(
lazy_gettext(
'This value is not available. Please choose another.'
)
)

@staticmethod
def validate_server_name(form, field):
server_name = field.data.strip()

if (
server_name != form._current_server_name
and not site_service.is_server_name_available(server_name)
):
raise ValidationError(
lazy_gettext(
'This value is not available. Please choose another.'
)
)


class AssignNewsChannelsForm(LocalizedForm):
news_channel_ids = MultiCheckboxField(
Expand Down
Expand Up @@ -23,8 +23,8 @@ <h1 class="title">{{ page_title }}</h1>
</div>
</div>

{{ form_field(form.id, placeholder='intranet', autofocus='autofocus') }}
{{ form_field(form.title, placeholder='Intranet') }}
{{ form_field(form.id, placeholder='intranet', caption=_('Has to be unique.'), autofocus='autofocus') }}
{{ form_field(form.title, placeholder='Intranet', caption=_('Has to be unique.')) }}
{{ form_field(form.server_name, placeholder='www.example.com', caption=_('Has to be unique.')) }}
{{ form_field(form.party_id) }}
{{ form_field(form.board_id) }}
Expand Down
Expand Up @@ -15,7 +15,7 @@ <h1 class="title">{{ page_title }}</h1>

<form action="{{ url_for('.update', site_id=site.id) }}" method="post">
<div class="box">
{{ form_field(form.title, autofocus='autofocus') }}
{{ form_field(form.title, caption=_('Has to be unique.'), autofocus='autofocus') }}
{{ form_field(form.server_name, caption=_('Has to be unique.')) }}
{{ form_field(form.party_id) }}
{{ form_field(form.board_id) }}
Expand Down
8 changes: 6 additions & 2 deletions byceps/blueprints/admin/site/views.py
Expand Up @@ -232,7 +232,11 @@ def update_form(site_id, erroneous_form=None):
"""Show form to update the site."""
site = _get_site_or_404(site_id)

form = erroneous_form if erroneous_form else UpdateForm(obj=site)
form = (
erroneous_form
if erroneous_form
else UpdateForm(site.title, site.server_name, obj=site)
)
_fill_in_common_form_choices(form, site.brand_id)

return {
Expand All @@ -247,7 +251,7 @@ def update(site_id):
"""Update the site."""
site = _get_site_or_404(site_id)

form = UpdateForm(request.form)
form = UpdateForm(site.title, site.server_name, request.form)
_fill_in_common_form_choices(form, site.brand_id)

if not form.validate():
Expand Down
18 changes: 18 additions & 0 deletions byceps/services/site/site_service.py
Expand Up @@ -188,6 +188,24 @@ def get_current_sites(
return {transform(db_site) for db_site in db_sites}


def is_title_available(title: str) -> bool:
"""Check if the title is unused."""
return not db.session.scalar(
select(db.exists().where(db.func.lower(DbSite.title) == title.lower()))
)


def is_server_name_available(server_name: str) -> bool:
"""Check if the server name is unused."""
return not db.session.scalar(
select(
db.exists().where(
db.func.lower(DbSite.server_name) == server_name.lower()
)
)
)


def _db_entity_to_site(db_site: DbSite) -> Site:
news_channel_ids = frozenset(
channel.id for channel in db_site.news_channels
Expand Down

0 comments on commit 72e25d7

Please sign in to comment.