Fakultas Ilmu Komputer UI

Commit 01068096 authored by Muhammad Fairuzi Teguh's avatar Muhammad Fairuzi Teguh
Browse files

[REFACTOR] add filter jadwal_donor in daftar_donor admin

parent c94e0b17
Pipeline #49766 passed with stages
in 2 minutes and 36 seconds
from django.contrib import admin
from django.db.models import Q
from django.http import HttpResponse
from django.utils.translation import gettext_lazy as _
from donor.models import JadwalDonor, DaftarDonor
from donor.models import DaftarDonor, JadwalDonor
from main.admin import InputFilter
def download_jadwaldonor(modeladmin, request, queryset):
......@@ -68,9 +71,21 @@ class JadwalDonorAdmin(admin.ModelAdmin):
actions = [download_jadwaldonor]
class JadwalDonorFilter(InputFilter):
parameter_name = 'lokasi_donor'
title = _('lokasi donor')
def queryset(self, request, queryset):
if self.value() is not None:
lokasi_donor = self.value()
return queryset.filter(
Q(jadwal_donor__location__contains=lokasi_donor)
)
@admin.register(DaftarDonor)
class DaftarDonorAdmin(admin.ModelAdmin):
list_filter = ('has_attended', )
list_filter = ('has_attended', JadwalDonorFilter, 'jadwal_donor__time_start')
def get_readonly_fields(self, request, obj=None):
readonly_fields = list(set(
......
......@@ -30,3 +30,23 @@ class UserAdmin(DjangoUserAdmin):
@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
pass
# common/admin.py
class InputFilter(admin.SimpleListFilter):
template = 'admin/input_filter.html'
def lookups(self, request, model_admin):
# Dummy, required to show the filter.
return ((),)
def choices(self, changelist):
# Grab only the "all" option.
all_choice = next(super().choices(changelist))
all_choice['query_parts'] = (
(k, v)
for k, v in changelist.get_filters_params().items()
if k != self.parameter_name
)
yield all_choice
{% load i18n %}
<h3>{% blocktrans with filter_title=title %} By {{ filter_title }} {% endblocktrans %}</h3>
<ul>
<li>
{% with choices.0 as all_choice %}
<form method="GET" action="">
{% for k, v in all_choice.query_parts %}
<input type="hidden" name="{{ k }}" value="{{ v }}" />
{% endfor %}
<input type="text"
value="{{ spec.value|default_if_none:'' }}"
name="{{ spec.parameter_name }}"/>
{% if not all_choice.selected %}
<strong><a href="{{ all_choice.query_string }}">x {% trans 'Remove' %}</a></strong>
{% endif %}
</form>
{% endwith %}
</li>
</ul>
\ No newline at end of file
......@@ -2,4 +2,4 @@ sonar.sources=.
sonar.exclusions=dblood/**/*,manage.py
sonar.test.inclusions=**/test*.py
sonar.python.coverage.reportPaths=coverage.xml
sonar.coverage.exclusions=**/apps.py,**/admin.py
\ No newline at end of file
sonar.coverage.exclusions=**/apps.py,**/admin.py,templates/
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment