misc: rename AgendaImportError to be more generic (#78125)

This commit is contained in:
Lauréline Guérin 2023-06-02 10:41:05 +02:00
parent 0eadd96ae8
commit 1813a9c415
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
6 changed files with 24 additions and 24 deletions

View File

@ -21,7 +21,7 @@ from django.db import models
from django.utils.text import slugify from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from lingo.utils.misc import AgendaImportError, clean_import_data, generate_slug from lingo.utils.misc import LingoImportError, clean_import_data, generate_slug
class Agenda(models.Model): class Agenda(models.Model):
@ -71,12 +71,12 @@ class Agenda(models.Model):
try: try:
agenda = Agenda.objects.get(slug=data['slug']) agenda = Agenda.objects.get(slug=data['slug'])
except Agenda.DoesNotExist: except Agenda.DoesNotExist:
raise AgendaImportError(_('Missing "%s" agenda') % data['slug']) raise LingoImportError(_('Missing "%s" agenda') % data['slug'])
if data.get('check_type_group'): if data.get('check_type_group'):
try: try:
data['check_type_group'] = CheckTypeGroup.objects.get(slug=data['check_type_group']) data['check_type_group'] = CheckTypeGroup.objects.get(slug=data['check_type_group'])
except CheckTypeGroup.DoesNotExist: except CheckTypeGroup.DoesNotExist:
raise AgendaImportError(_('Missing "%s" check type group') % data['check_type_group']) raise LingoImportError(_('Missing "%s" check type group') % data['check_type_group'])
agenda.check_type_group = data.get('check_type_group') agenda.check_type_group = data.get('check_type_group')
agenda.save() agenda.save()

View File

@ -20,7 +20,7 @@ import sys
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from lingo.pricing.utils import import_site from lingo.pricing.utils import import_site
from lingo.utils.misc import AgendaImportError from lingo.utils.misc import LingoImportError
class Command(BaseCommand): class Command(BaseCommand):
@ -43,7 +43,7 @@ class Command(BaseCommand):
clean=options['clean'], clean=options['clean'],
overwrite=options['overwrite'], overwrite=options['overwrite'],
) )
except AgendaImportError as exc: except LingoImportError as exc:
raise CommandError('%s' % exc) raise CommandError('%s' % exc)
if filename == '-': if filename == '-':

View File

@ -25,7 +25,7 @@ from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from lingo.agendas.models import Agenda, CheckType from lingo.agendas.models import Agenda, CheckType
from lingo.utils.misc import AgendaImportError, clean_import_data, generate_slug from lingo.utils.misc import LingoImportError, clean_import_data, generate_slug
class PricingError(Exception): class PricingError(Exception):
@ -308,10 +308,10 @@ class Pricing(models.Model):
for category_data in categories: for category_data in categories:
category_slug = category_data['category'] category_slug = category_data['category']
if category_data['category'] not in categories_by_slug: if category_data['category'] not in categories_by_slug:
raise AgendaImportError(_('Missing "%s" pricing category') % category_data['category']) raise LingoImportError(_('Missing "%s" pricing category') % category_data['category'])
for criteria_slug in category_data['criterias']: for criteria_slug in category_data['criterias']:
if (category_slug, criteria_slug) not in criterias_by_categories_and_slug: if (category_slug, criteria_slug) not in criterias_by_categories_and_slug:
raise AgendaImportError( raise LingoImportError(
_('Missing "%s" pricing criteria for "%s" category') % (criteria_slug, category_slug) _('Missing "%s" pricing criteria for "%s" category') % (criteria_slug, category_slug)
) )
data = clean_import_data(cls, data) data = clean_import_data(cls, data)
@ -456,11 +456,11 @@ class AgendaPricing(models.Model):
try: try:
agendas.append(Agenda.objects.get(slug=agenda_slug)) agendas.append(Agenda.objects.get(slug=agenda_slug))
except Agenda.DoesNotExist: except Agenda.DoesNotExist:
raise AgendaImportError(_('Missing "%s" agenda') % agenda_slug) raise LingoImportError(_('Missing "%s" agenda') % agenda_slug)
try: try:
data['pricing'] = Pricing.objects.get(slug=data['pricing']) data['pricing'] = Pricing.objects.get(slug=data['pricing'])
except Pricing.DoesNotExist: except Pricing.DoesNotExist:
raise AgendaImportError(_('Missing "%s" pricing model') % data['pricing']) raise LingoImportError(_('Missing "%s" pricing model') % data['pricing'])
agenda_pricing, created = cls.objects.update_or_create(slug=data['slug'], defaults=data) agenda_pricing, created = cls.objects.update_or_create(slug=data['slug'], defaults=data)
if overwrite and not created: if overwrite and not created:

View File

@ -71,7 +71,7 @@ from lingo.pricing.models import (
PricingCriteriaCategory, PricingCriteriaCategory,
) )
from lingo.pricing.utils import export_site, import_site from lingo.pricing.utils import export_site, import_site
from lingo.utils.misc import AgendaImportError from lingo.utils.misc import LingoImportError
class HomeView(TemplateView): class HomeView(TemplateView):
@ -112,7 +112,7 @@ class ConfigImportView(FormView):
try: try:
results = import_site(config_json, overwrite=False) results = import_site(config_json, overwrite=False)
except AgendaImportError as exc: except LingoImportError as exc:
form.add_error('config_json', '%s' % exc) form.add_error('config_json', '%s' % exc)
return self.form_invalid(form) return self.form_invalid(form)
except KeyError as exc: except KeyError as exc:

View File

@ -22,7 +22,7 @@ from django.core.exceptions import FieldDoesNotExist, ValidationError
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
class AgendaImportError(Exception): class LingoImportError(Exception):
pass pass
@ -69,7 +69,7 @@ def clean_import_data(cls, data):
try: try:
field.run_validators(value) field.run_validators(value)
except ValidationError: except ValidationError:
raise AgendaImportError(_('Bad slug format "%s"') % value) raise LingoImportError(_('Bad slug format "%s"') % value)
return cleaned_data return cleaned_data

View File

@ -21,7 +21,7 @@ from lingo.pricing.models import (
PricingCriteriaCategory, PricingCriteriaCategory,
) )
from lingo.pricing.utils import import_site from lingo.pricing.utils import import_site
from lingo.utils.misc import AgendaImportError from lingo.utils.misc import LingoImportError
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
@ -119,19 +119,19 @@ def test_import_export_agenda_pricing(app):
data = json.loads(output) data = json.loads(output)
Agenda.objects.all().delete() Agenda.objects.all().delete()
with pytest.raises(AgendaImportError) as excinfo: with pytest.raises(LingoImportError) as excinfo:
import_site(data, overwrite=True) import_site(data, overwrite=True)
assert str(excinfo.value) == 'Missing "foo-bar" agenda' assert str(excinfo.value) == 'Missing "foo-bar" agenda'
agenda2 = Agenda.objects.create(label='Baz') agenda2 = Agenda.objects.create(label='Baz')
with pytest.raises(AgendaImportError) as excinfo: with pytest.raises(LingoImportError) as excinfo:
import_site(data, overwrite=True) import_site(data, overwrite=True)
assert str(excinfo.value) == 'Missing "foo-bar" agenda' assert str(excinfo.value) == 'Missing "foo-bar" agenda'
del data['pricing_models'] del data['pricing_models']
Pricing.objects.all().delete() Pricing.objects.all().delete()
agenda = Agenda.objects.create(label='Foo Bar') agenda = Agenda.objects.create(label='Foo Bar')
with pytest.raises(AgendaImportError) as excinfo: with pytest.raises(LingoImportError) as excinfo:
import_site(data, overwrite=True) import_site(data, overwrite=True)
assert str(excinfo.value) == 'Missing "foo" pricing model' assert str(excinfo.value) == 'Missing "foo" pricing model'
@ -220,12 +220,12 @@ def test_import_export_agenda_with_check_types(app):
agenda.check_type_group = None agenda.check_type_group = None
agenda.save() agenda.save()
with pytest.raises(AgendaImportError) as excinfo: with pytest.raises(LingoImportError) as excinfo:
import_site(data, overwrite=True) import_site(data, overwrite=True)
assert str(excinfo.value) == 'Missing "foo" check type group' assert str(excinfo.value) == 'Missing "foo" check type group'
CheckTypeGroup.objects.create(label='foobar') CheckTypeGroup.objects.create(label='foobar')
with pytest.raises(AgendaImportError) as excinfo: with pytest.raises(LingoImportError) as excinfo:
import_site(data, overwrite=True) import_site(data, overwrite=True)
assert str(excinfo.value) == 'Missing "foo" check type group' assert str(excinfo.value) == 'Missing "foo" check type group'
@ -344,12 +344,12 @@ def test_import_export_pricing_with_categories(app):
data = json.loads(output) data = json.loads(output)
del data['pricing_categories'] del data['pricing_categories']
with pytest.raises(AgendaImportError) as excinfo: with pytest.raises(LingoImportError) as excinfo:
import_site(data, overwrite=True) import_site(data, overwrite=True)
assert str(excinfo.value) == 'Missing "foo-bar" pricing category' assert str(excinfo.value) == 'Missing "foo-bar" pricing category'
CriteriaCategory.objects.create(label='Foobar') CriteriaCategory.objects.create(label='Foobar')
with pytest.raises(AgendaImportError) as excinfo: with pytest.raises(LingoImportError) as excinfo:
import_site(data, overwrite=True) import_site(data, overwrite=True)
assert str(excinfo.value) == 'Missing "foo-bar" pricing category' assert str(excinfo.value) == 'Missing "foo-bar" pricing category'
@ -405,7 +405,7 @@ def test_import_export_pricing_with_categories(app):
'criterias': [], 'criterias': [],
}, },
] ]
with pytest.raises(AgendaImportError) as excinfo: with pytest.raises(LingoImportError) as excinfo:
import_site(data, overwrite=True) import_site(data, overwrite=True)
assert str(excinfo.value) == 'Missing "unknown" pricing criteria for "foo-bar-3" category' assert str(excinfo.value) == 'Missing "unknown" pricing criteria for "foo-bar-3" category'
@ -422,7 +422,7 @@ def test_import_export_pricing_with_categories(app):
'criterias': [], 'criterias': [],
}, },
] ]
with pytest.raises(AgendaImportError) as excinfo: with pytest.raises(LingoImportError) as excinfo:
import_site(data, overwrite=True) import_site(data, overwrite=True)
assert str(excinfo.value) == 'Missing "crit-1" pricing criteria for "foo-bar-3" category' assert str(excinfo.value) == 'Missing "crit-1" pricing criteria for "foo-bar-3" category'