Skip to content

Commit

Permalink
Merge pull request #53 from PiotrIw/dependencies-update
Browse files Browse the repository at this point in the history
v1.1.18 improvements and fixes
  • Loading branch information
PiotrIw authored Dec 11, 2023
2 parents 56ec613 + 6b72e90 commit 24eb19a
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 17 deletions.
2 changes: 1 addition & 1 deletion poradnia/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.1.17"
__version__ = "1.1.18"


# Compatibility to eg. django-rest-framework
Expand Down
6 changes: 3 additions & 3 deletions poradnia/advicer/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from crispy_forms.layout import Div, Fieldset, Layout, Submit
from dal import autocomplete
from django.utils.translation import gettext_lazy as _
from teryt_tree.dal_ext.filters import AreaMultipleFilter

from poradnia.teryt.filters import AreaMultipleFilter
from poradnia.users.filters import UserChoiceFilter

from .models import Advice, Area, Issue
Expand Down Expand Up @@ -90,8 +90,8 @@ def form(self):
),
Div(
Div("institution_kind", css_class="col-sm-12 col-md-3"),
Div("helped", css_class="col-sm-12 col-md-3"),
Div("community", css_class="col-sm-12 col-md-3"),
Div("helped", css_class="col-sm-12 col-md-2"),
Div("community", css_class="col-sm-12 col-md-7"),
css_class="row",
),
),
Expand Down
10 changes: 4 additions & 6 deletions poradnia/cases/templates/cases/case_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
{% block content %}
{% get_obj_perms request.user for object as "case_perms" %}
<style>
.permissions_help {
/* position: fixed; */
/* display: inline-block; */
.permissions_help_icon {
cursor: pointer;
}

Expand All @@ -56,13 +54,13 @@
z-index: 1;
bottom: -350px;
left: 100px;
margin-left: -60px;
margin-left: -90px;
opacity: 0;
transition: opacity 0.3s;
white-space: pre-line; /* This will preserve line breaks */
}

.permissions_help:hover .permissions_helptext {
.fa-shield:active + .permissions_helptext {
visibility: visible;
opacity: 1;
}
Expand Down Expand Up @@ -172,7 +170,7 @@ <h1 class="case-header-title{% if user.is_staff and object.handled %} success{%
<div class="panel-body">
{% crispy casegroup_form %}
<div class="permissions_help">
<i class="fa fa-shield"> Uprawnienia w grupach</i>
<i class="fa fa-shield permissions_help_icon"> Uprawnienia w grupach</i>
<span class="permissions_helptext">{{ permissions_help_text}}</span>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion poradnia/keys/templates/keys/key_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

{% block content %}
<div class="text-right">
<p><a href="https://github.com/watchdogpolska/poradnia/wiki/Klucze-dost%C4%99pu" class="btn btn-primary"><i
<p><a href="https://github.com/watchdogpolska/poradnia/blob/master/docs/modules/keys.rst" class="btn btn-primary"><i
class="fa fa-question"></i> {% trans 'Help' %}</a>
<a href="{% url 'keys:create' %}" class="btn btn-primary"><i
class="fa fa-cog"></i> {% trans 'Generate new key' %}</a></p>
Expand Down
64 changes: 64 additions & 0 deletions poradnia/teryt/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from functools import reduce

from django.db.models import Q
from django.utils.translation import gettext as _
from django_filters.filters import ModelMultipleChoiceFilter

from .models import JST


class AreaMultipleFilter(ModelMultipleChoiceFilter):
"""
Multiple choice filter for JSTs.
The queryset should implement a `area_in` method.
"""

def __init__(self, *args, **kwargs):
label = kwargs.pop("label", _("Area"))
required = kwargs.pop("required", False)
queryset = kwargs.pop("queryset", JST.objects.all())
method = kwargs.pop("method", lambda q, _, v: q.area_in(v))
super(ModelMultipleChoiceFilter, self).__init__(
*args,
label=label,
required=required,
queryset=queryset,
method=method,
**kwargs,
)

@staticmethod
def filter_area_in(queryset, value, area_field=None):
"""
Multiselect filter by area.
If `value` is empty, returns an empty queryset.
`area_field` is the field which points to a field of type `JST`.
This method is not automatically injected to the queryset.
It merely serves as a sample implementation of an `area_in` filter.
"""

def with_area_field(path):
if area_field is None:
return path
else:
return "{}__{}".format(area_field, path)

if not value:
return queryset.none()

return queryset.filter(
reduce(
lambda x, y: x | y,
[
Q(
**{
with_area_field("tree_id"): jst.tree_id,
with_area_field("lft__range"): (jst.lft, jst.rght),
}
)
for jst in value
],
)
)
15 changes: 15 additions & 0 deletions poradnia/teryt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,20 @@ class JST(JednostkaAdministracyjna):
def get_absolute_url(self):
return reverse("teryt:details", kwargs={"slug": self.slug})

def get_full_name(self):
name = f"{self.name} ({self.id}, {self.category})"
if self.parent:
name = f"{self.parent} / {name}"
if self.parent.parent:
name = f"{self.parent.parent} / {name}"
return name

@property
def tree_name(self):
return self.get_full_name()

class Meta:
proxy = True

def __str__(self):
return self.tree_name
8 changes: 2 additions & 6 deletions poradnia/teryt/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from django.urls import re_path
from django.utils.translation import gettext_lazy as _
from teryt_tree.dal_ext.views import (
CommunityAutocomplete,
CountyAutocomplete,
VoivodeshipAutocomplete,
)
from teryt_tree.dal_ext.views import CountyAutocomplete, VoivodeshipAutocomplete

from . import views

Expand All @@ -24,7 +20,7 @@
),
re_path(
_(r"^community-autocomplete/$"),
CommunityAutocomplete.as_view(),
views.AdviceCommunityAutocomplete.as_view(),
name="community-autocomplete",
),
]
Expand Down
17 changes: 17 additions & 0 deletions poradnia/teryt/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dal import autocomplete
from django.views.generic import DetailView, ListView

from poradnia.advicer.models import Advice
Expand Down Expand Up @@ -26,3 +27,19 @@ class JSTListView(ListView):
def get_queryset(self):
qs = super().get_queryset()
return qs.voivodeship()


class AdviceCommunityAutocomplete(autocomplete.Select2QuerySetView):
def get_result_label(self, result):
return result.tree_name

def get_queryset(self):
qs = JST.objects.community().select_related("category").all()

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

0 comments on commit 24eb19a

Please sign in to comment.