Skip to content

Commit

Permalink
Merge pull request #1020 from watchdogpolska/descriptive-jst-widget
Browse files Browse the repository at this point in the history
Added possibility to assign non community JST to the institution.
  • Loading branch information
dzejkobi authored Jun 11, 2021
2 parents 05aa361 + 5a3eb71 commit 27be7c9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion feder/institutions/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class Meta:
model = Institution
fields = ["name", "tags", "jst", "regon", "email"]
widgets = {
"jst": autocomplete.ModelSelect2(url="teryt:community-autocomplete"),
"jst": autocomplete.ModelSelect2(url="teryt:jst-autocomplete"),
"tags": forms.CheckboxSelectMultiple,
}
5 changes: 5 additions & 0 deletions feder/teryt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
views.CustomCommunityAutocomplete.as_view(),
name="community-autocomplete",
),
url(
_(r"^jst-autocomplete/$"),
views.JSTAutocomplete.as_view(),
name="jst-autocomplete",
),
]

app_name = "feder.teryt"
29 changes: 21 additions & 8 deletions feder/teryt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,38 @@ def get_queryset(self):
return qs.voivodeship()


class CustomCommunityAutocomplete(CommunityAutocomplete):
def get_queryset(self):
class JSTAutocompleteMixin:
def get_base_queryset(self):
"""
Overridden to use JST model instead fo JednostkaAdministracyjna.
additionally select_related "parent" and "parent__parent" has been added.
Refactored from CommunityAutocomplete view to use JST model
instead of JednostkaAdministracyjna.
additionally select_related "parent" and "parent__parent" has been added
and filtered only the active records.
"""
qs = (
JST.objects.community()
.select_related("category", "parent", "parent__parent")
.all()
return JST.objects.filter(active=True).select_related(
"category", "parent", "parent__parent"
)

def get_queryset(self):
qs = self.get_base_queryset()

if self.q:
qs = qs.filter(name__istartswith=self.q)

county = self.forwarded.get("county", None)
if county:
return qs.filter(parent=county)

return qs

def get_result_label(self, result):
return result.get_full_name()


class CustomCommunityAutocomplete(JSTAutocompleteMixin, CommunityAutocomplete):
def get_base_queryset(self):
return super().get_base_queryset().community()


class JSTAutocomplete(JSTAutocompleteMixin, CommunityAutocomplete):
pass

0 comments on commit 27be7c9

Please sign in to comment.