0002-manager-filter-journal-entries-by-event-type-50054.patch
src/authentic2/apps/journal/forms.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU Affero General Public License |
15 | 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 |
import unicodedata |
|
17 | 18 |
from datetime import datetime |
18 | 19 | |
19 | 20 |
from django.http import QueryDict |
... | ... | |
161 | 162 | |
162 | 163 |
before_cursor = forms.CharField(widget=forms.HiddenInput(), required=False) |
163 | 164 | |
165 |
event_types = forms.ModelMultipleChoiceField( |
|
166 |
widget=forms.MultipleHiddenInput(), required=False, queryset=models.EventType.objects.all() |
|
167 |
) |
|
168 | ||
164 | 169 |
search = SearchField(required=False, label='') |
165 | 170 | |
166 | 171 |
search_engine_class = search_engine.JournalSearchEngine |
... | ... | |
253 | 258 |
month = self.cleaned_data.get('month') |
254 | 259 |
day = self.cleaned_data.get('day') |
255 | 260 |
search_query = self.cleaned_data.get('_search_query') |
261 |
event_types = self.cleaned_data.get('event_types') |
|
256 | 262 | |
257 | 263 |
if year: |
258 | 264 |
qs = qs.filter(timestamp__year=year) |
... | ... | |
262 | 268 |
qs = qs.filter(timestamp__day=day) |
263 | 269 |
if search_query: |
264 | 270 |
qs = qs.filter(search_query) |
271 |
if event_types: |
|
272 |
qs = qs.filter(type__in=event_types) |
|
265 | 273 |
return qs |
266 | 274 | |
267 | 275 |
def make_querydict(self, name=None, value=None, exclude=()): |
... | ... | |
350 | 358 |
@property |
351 | 359 |
def url(self): |
352 | 360 |
return self.make_url() |
361 | ||
362 | ||
363 |
class ChooseEventTypeForm(forms.Form): |
|
364 |
event_types = forms.ModelMultipleChoiceField( |
|
365 |
widget=forms.CheckboxSelectMultiple(), required=False, queryset=models.EventType.objects.all() |
|
366 |
) |
|
367 | ||
368 |
def __init__(self, *args, **kwargs): |
|
369 |
super().__init__(*args, **kwargs) |
|
370 |
self.fields['event_types'].choices = sorted( |
|
371 |
self.fields['event_types'].choices, key=lambda x: unicodedata.normalize('NFKD', x[1]) |
|
372 |
) |
src/authentic2/manager/journal_views.py | ||
---|---|---|
15 | 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 | 17 |
import uuid |
18 |
from urllib.parse import urlparse |
|
18 | 19 | |
19 | 20 |
from django.contrib.auth import get_user_model |
20 | 21 |
from django.core.exceptions import PermissionDenied, ValidationError |
21 | 22 |
from django.core.validators import EmailValidator |
22 | 23 |
from django.db.models import Q |
24 |
from django.http import QueryDict |
|
23 | 25 |
from django.utils.translation import ugettext_lazy as _ |
26 |
from django.views.generic.edit import FormView |
|
24 | 27 | |
25 |
from authentic2.apps.journal.forms import JournalForm |
|
28 |
from authentic2.apps.journal.forms import JournalForm, ChooseEventTypeForm
|
|
26 | 29 |
from authentic2.apps.journal.search_engine import JournalSearchEngine |
27 | 30 |
from authentic2.apps.journal.views import JournalView |
28 | 31 | |
... | ... | |
96 | 99 | |
97 | 100 | |
98 | 101 |
journal = GlobalJournalView.as_view() |
102 | ||
103 | ||
104 |
class ChooseEventTypesView(views.TitleMixin, FormView): |
|
105 |
form_class = ChooseEventTypeForm |
|
106 |
template_name = 'authentic2/manager/form.html' |
|
107 |
title = _('Choose event types') |
|
108 | ||
109 |
def get_initial(self): |
|
110 |
next_url = urlparse(self.request.GET['next']) |
|
111 |
params = QueryDict(next_url.query) |
|
112 |
return {'event_types': params.getlist('event_types')} |
|
113 | ||
114 |
def form_valid(self, form): |
|
115 |
self.event_type_pks = form.cleaned_data['event_types'].values_list('pk', flat=True) |
|
116 |
return super().form_valid(form) |
|
117 | ||
118 |
def get_success_url(self): |
|
119 |
next_url = urlparse(self.request.GET['next']) |
|
120 |
params = QueryDict(next_url.query, mutable=True) |
|
121 |
params.setlist('event_types', self.event_type_pks) |
|
122 |
next_url = next_url._replace(query=params.urlencode()) |
|
123 |
return next_url.geturl() |
|
124 | ||
125 | ||
126 |
choose_event_types = ChooseEventTypesView.as_view() |
src/authentic2/manager/templates/authentic2/manager/journal.html | ||
---|---|---|
22 | 22 |
<button>{% trans "Search" %}</button> |
23 | 23 |
</form> |
24 | 24 |
</div> |
25 |
<p><a rel="popup" href="{% url 'a2-manager-journal-choose-event-types' %}?next={{ request.get_full_path|urlencode }}">{% trans "Filter by event types" %}</a></p> |
|
25 | 26 |
{% if date_hierarchy.choice_name %} |
26 | 27 |
<h4>{{ date_hierarchy.choice_name }}</h4> |
27 | 28 |
<p> |
src/authentic2/manager/urls.py | ||
---|---|---|
167 | 167 |
# Journal |
168 | 168 |
url(r'^journal/$', journal_views.journal, |
169 | 169 |
name='a2-manager-journal'), |
170 |
url(r'^journal/choose-event-types/$', journal_views.choose_event_types, |
|
171 |
name='a2-manager-journal-choose-event-types'), |
|
170 | 172 | |
171 | 173 |
# backoffice menu as json |
172 | 174 |
url(r'^menu.json$', views.menu_json), |
173 |
- |