diff --git a/services/admin.py b/services/admin.py index 8f233996..46b72227 100644 --- a/services/admin.py +++ b/services/admin.py @@ -1,7 +1,12 @@ +from django import forms +from django.conf import settings from django.contrib import admin +from django.core.exceptions import ValidationError +from django.utils.translation import gettext_lazy as _ from modeltranslation.admin import TranslationAdmin from services.models.notification import Announcement, ErrorMessage +from services.models.search_rule import ExclusionWord class NotificationAdmin(TranslationAdmin): @@ -10,5 +15,26 @@ class NotificationAdmin(TranslationAdmin): list_filter = ("active",) +class ExlusionWordForm(forms.ModelForm): + + def clean(self): + cleaned_data = super().clean() + accepted_language_shorts = [lang[0] for lang in settings.LANGUAGES] + if cleaned_data.get("language_short") not in accepted_language_shorts: + raise ValidationError( + _("Language short must be one of") + + f":{' ,'.join(accepted_language_shorts)}" + ) + return cleaned_data + + +class ExclusionWordAdmin(admin.ModelAdmin): + list_display = ("word", "language_short") + + model = ExclusionWord + form = ExlusionWordForm + + +admin.site.register(ExclusionWord, ExclusionWordAdmin) admin.site.register(Announcement, NotificationAdmin) admin.site.register(ErrorMessage, NotificationAdmin)