Skip to content

Commit 8cccd79

Browse files
committed
[refactor] Simplify formset handling for outdoor locations in admin #157
Moved logic for rejecting floorplan for outdoor location from django_loci.admin.LocationAdmin to django_loci.base.admin.AbstractLocationAdmin to ensure the extendability. Related to #157 (cherry picked from commit a12916c)
1 parent 4ecfcf7 commit 8cccd79

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

django_loci/admin.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,6 @@ class LocationAdmin(AbstractLocationAdmin):
3535
form = LocationForm
3636
inlines = [FloorPlanInline]
3737

38-
# kept for backward compatibility with Django 3.2.18
39-
def _create_formsets(self, request, obj, change):
40-
# 'data' is not present in POST request for django 3.2.18
41-
if request.method == 'POST' and not request.POST.get('data', None):
42-
data = request.POST.copy()
43-
if data['type'] == 'outdoor':
44-
data['floorplan_set-TOTAL_FORMS'] = '0'
45-
request.POST = data
46-
return super()._create_formsets(request, obj, change)
47-
48-
# for django >= 4.0
49-
def get_formset_kwargs(self, request, obj, inline, prefix):
50-
formset_kwargs = super().get_formset_kwargs(request, obj, inline, prefix)
51-
# manually set TOTAL_FORMS to 0 if the type is outdoor to avoid floorplan form creation
52-
if request.method == 'POST' and formset_kwargs['data']['type'] == 'outdoor':
53-
formset_kwargs['data']['floorplan_set-TOTAL_FORMS'] = '0'
54-
return formset_kwargs
55-
5638

5739
class ObjectLocationForm(AbstractObjectLocationForm):
5840
class Meta(AbstractObjectLocationForm.Meta):

django_loci/base/admin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,23 @@ def floorplans_json_view(self, request, pk):
206206
)
207207
return JsonResponse({'choices': choices})
208208

209+
# kept for backward compatibility with Django 3.2.18
210+
def _create_formsets(self, request, obj, change):
211+
# 'data' is not present in POST request for django 3.2.18
212+
if request.method == 'POST' and not request.POST.get('data', None):
213+
data = request.POST.copy()
214+
if data['type'] == 'outdoor':
215+
data['floorplan_set-TOTAL_FORMS'] = '0'
216+
request.POST = data
217+
return super()._create_formsets(request, obj, change)
218+
219+
def get_formset_kwargs(self, request, obj, inline, prefix):
220+
formset_kwargs = super().get_formset_kwargs(request, obj, inline, prefix)
221+
# manually set TOTAL_FORMS to 0 if the type is outdoor to avoid floorplan form creation
222+
if request.method == 'POST' and formset_kwargs['data']['type'] == 'outdoor':
223+
formset_kwargs['data']['floorplan_set-TOTAL_FORMS'] = '0'
224+
return formset_kwargs
225+
209226

210227
class UnvalidatedChoiceField(forms.ChoiceField):
211228
"""

django_loci/tests/base/test_admin_inline.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -773,20 +773,23 @@ def test_add_indoor_location_without_floor(self):
773773
def test_add_outdoor_with_floorplan(self):
774774
self._login_as_admin()
775775
p = 'floorplan_set'
776-
params = {
777-
'name': 'test-add-outdoor-with-floorplan',
778-
'type': 'outdoor',
779-
'geometry': 'SRID=4326;POINT (12.512324 41.898703)',
780-
'address': 'Piazza Venezia, Roma, Italia',
781-
'{0}-0-floor'.format(p): '1',
782-
'{0}-0-image'.format(p): self._get_simpleuploadedfile(),
783-
'{0}-0-id'.format(p): '',
784-
'{0}-0-location'.format(p): '',
785-
'{0}-TOTAL_FORMS'.format(p): '1',
786-
'{0}-INITIAL_FORMS'.format(p): '0',
787-
'{0}-MIN_NUM_FORMS'.format(p): '0',
788-
'{0}-MAX_NUM_FORMS'.format(p): '1',
789-
}
776+
params = self.params
777+
params.update(
778+
{
779+
'name': 'test-add-outdoor-with-floorplan',
780+
'type': 'outdoor',
781+
'geometry': 'SRID=4326;POINT (12.512324 41.898703)',
782+
'address': 'Piazza Venezia, Roma, Italia',
783+
'{0}-0-floor'.format(p): '1',
784+
'{0}-0-image'.format(p): self._get_simpleuploadedfile(),
785+
'{0}-0-id'.format(p): '',
786+
'{0}-0-location'.format(p): '',
787+
'{0}-TOTAL_FORMS'.format(p): '1',
788+
'{0}-INITIAL_FORMS'.format(p): '0',
789+
'{0}-MIN_NUM_FORMS'.format(p): '0',
790+
'{0}-MAX_NUM_FORMS'.format(p): '1',
791+
}
792+
)
790793
location_url = '{0}_{1}_add'.format(
791794
self.url_prefix, self.location_model.__name__.lower()
792795
)

0 commit comments

Comments
 (0)