address: hide empty sectors select in manager (#67798)

This commit is contained in:
Thomas NOËL 2022-07-29 01:33:20 +02:00
parent 965d288227
commit 8e1a357c11
2 changed files with 20 additions and 0 deletions

View File

@ -43,6 +43,12 @@ class AddressResource(BaseResource):
class Meta:
abstract = True
@classmethod
def get_manager_form_class(cls, **kwargs):
if not SectorResource.objects.exists():
kwargs['exclude'] = tuple(kwargs.get('exclude') or ()) + ('sectors',)
return super().get_manager_form_class(**kwargs)
def export_json(self):
d = super().export_json()
d['sectors'] = [sector.slug for sector in self.sectors.all()]

View File

@ -24,6 +24,7 @@ from django.core.files import File
import tests.utils
from passerelle.apps.base_adresse.models import BaseAdresse
from passerelle.apps.sector.models import SectorResource
from tests.test_manager import login
BAN = {
"attribution": "BAN",
@ -283,3 +284,16 @@ def test_sectorization(mocked_get, app, base_adresse, sector):
sectors = app.get(url, params=params, status=200).json
assert sectors['err'] == 0
assert sectors['data'] == []
def test_manager_with_or_without_sectors(db, app, admin_user, base_adresse, sector):
app = login(app)
path = '/manage/%s/add' % base_adresse.get_connector_slug()
resp = app.get(path)
assert 'Sectorizations' in str(resp.form.html)
assert 'Secteur scolaire [ecole]' in str(resp.form.html)
sector.delete()
resp = app.get(path)
assert 'Sectorizations' not in str(resp.form.html)
assert 'Secteur scolaire [ecole]' not in str(resp.form.html)