Add Resource filtering and table display functionality
This commit is contained in:
@@ -12,6 +12,8 @@ from .models import (
|
||||
Structure,
|
||||
Subspecialty,
|
||||
NormalCase,
|
||||
Resource,
|
||||
Site,
|
||||
)
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models import Q
|
||||
@@ -335,6 +337,25 @@ class NormalCaseFilter(django_filters.FilterSet):
|
||||
return queryset
|
||||
return queryset.filter(age_days__lte=days)
|
||||
|
||||
|
||||
class ResourceFilter(django_filters.FilterSet):
|
||||
name = django_filters.CharFilter(field_name='name', lookup_expr='icontains')
|
||||
site = django_filters.ModelMultipleChoiceFilter(field_name='sites', queryset=lambda request: Site.objects.all() if True else Site.objects.none())
|
||||
subspecialty = django_filters.ModelMultipleChoiceFilter(field_name='subspecialty', queryset=Subspecialty.objects.all())
|
||||
author = django_filters.ModelMultipleChoiceFilter(queryset=get_authors, null_label='No author')
|
||||
has_file = django_filters.BooleanFilter(method='filter_has_file', label='Has file')
|
||||
|
||||
class Meta:
|
||||
model = Resource
|
||||
fields = ('name', 'site', 'subspecialty', 'author')
|
||||
|
||||
def filter_has_file(self, queryset, name, value):
|
||||
if value is True:
|
||||
return queryset.exclude(file__isnull=True).exclude(file__exact='')
|
||||
elif value is False:
|
||||
return queryset.filter(file__isnull=True) | queryset.filter(file__exact='')
|
||||
return queryset
|
||||
|
||||
class QuestionSchemaFilter(django_filters.FilterSet):
|
||||
class Meta:
|
||||
model = QuestionSchema
|
||||
|
||||
@@ -14,6 +14,7 @@ from .models import (
|
||||
Structure,
|
||||
Finding,
|
||||
Subspecialty,
|
||||
Resource,
|
||||
)
|
||||
|
||||
from django.utils.html import format_html
|
||||
@@ -414,6 +415,18 @@ class SubspecialtyTable(SelectionTable):
|
||||
return format_html(record.get_synonym_link())
|
||||
return f"{record.get_synonym_link()}"
|
||||
|
||||
|
||||
class ResourceTable(SelectionTable):
|
||||
name = tables.Column(linkify=("atlas:resource_detail", {"pk": tables.A("pk")}), verbose_name="Resource")
|
||||
|
||||
edit = tables.LinkColumn("atlas:resource_update", text="Edit", args=[A("pk")], orderable=False)
|
||||
delete = tables.LinkColumn("atlas:resource_delete", text="Delete", args=[A("pk")], orderable=False)
|
||||
|
||||
class Meta(SelectionTable.Meta):
|
||||
model = Resource
|
||||
template_name = "django_tables2/bootstrap4.html"
|
||||
fields = ("name", "description")
|
||||
|
||||
class CaseCollectionTable(SelectionTable):
|
||||
edit = tables.LinkColumn(
|
||||
"atlas:exam_update", text="Edit", args=[A("pk")], orderable=False
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
{% extends 'atlas/base.html' %}
|
||||
{% load render_table from django_tables2 %}
|
||||
{% block content %}
|
||||
<a href="{% url 'atlas:resource_create' %}">Create a new resource</a>
|
||||
|
||||
<h2>Resources</h2>
|
||||
|
||||
<ul id="resource-list">
|
||||
{% for resource in object_list %}
|
||||
<li class="">
|
||||
<a href="{% url 'atlas:resource_detail' resource.pk %}">{{resource}}</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<form>
|
||||
{% render_table table %}
|
||||
</form>
|
||||
|
||||
{% include "generic/partials/page_size_form.html" %}
|
||||
{% include "generic/partials/filter_bar.html" with filter=filter app_name=app_name collapse_id="bottom-filter-body" %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
|
||||
+8
-2
@@ -33,6 +33,10 @@ from generic.mixins import SuperuserRequiredMixin, UserConfigurablePaginationMix
|
||||
|
||||
from django.views.generic.edit import CreateView, UpdateView, DeleteView, FormView
|
||||
from django.views.generic import ListView
|
||||
from django_filters.views import FilterView
|
||||
from django_tables2 import SingleTableMixin
|
||||
from .filters import ResourceFilter
|
||||
from .tables import ResourceTable
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
from django.urls import reverse_lazy, reverse
|
||||
@@ -1311,9 +1315,11 @@ class ResourceCreate(LoginRequiredMixin, CreateView):
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
class ResourceView(LoginRequiredMixin, ListView):
|
||||
class ResourceView(LoginRequiredMixin, UserConfigurablePaginationMixin, SingleTableMixin, FilterView):
|
||||
model = Resource
|
||||
# form_class = ResourceForm
|
||||
table_class = ResourceTable
|
||||
template_name = "atlas/resource_list.html"
|
||||
filterset_class = ResourceFilter
|
||||
|
||||
|
||||
class CaseCollectionCreate(RevisionMixin, LoginRequiredMixin, CreateView):
|
||||
|
||||
Reference in New Issue
Block a user