Add Resource filtering and table display functionality
This commit is contained in:
@@ -12,6 +12,8 @@ from .models import (
|
|||||||
Structure,
|
Structure,
|
||||||
Subspecialty,
|
Subspecialty,
|
||||||
NormalCase,
|
NormalCase,
|
||||||
|
Resource,
|
||||||
|
Site,
|
||||||
)
|
)
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
@@ -335,6 +337,25 @@ class NormalCaseFilter(django_filters.FilterSet):
|
|||||||
return queryset
|
return queryset
|
||||||
return queryset.filter(age_days__lte=days)
|
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 QuestionSchemaFilter(django_filters.FilterSet):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = QuestionSchema
|
model = QuestionSchema
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ from .models import (
|
|||||||
Structure,
|
Structure,
|
||||||
Finding,
|
Finding,
|
||||||
Subspecialty,
|
Subspecialty,
|
||||||
|
Resource,
|
||||||
)
|
)
|
||||||
|
|
||||||
from django.utils.html import format_html
|
from django.utils.html import format_html
|
||||||
@@ -414,6 +415,18 @@ class SubspecialtyTable(SelectionTable):
|
|||||||
return format_html(record.get_synonym_link())
|
return format_html(record.get_synonym_link())
|
||||||
return f"{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):
|
class CaseCollectionTable(SelectionTable):
|
||||||
edit = tables.LinkColumn(
|
edit = tables.LinkColumn(
|
||||||
"atlas:exam_update", text="Edit", args=[A("pk")], orderable=False
|
"atlas:exam_update", text="Edit", args=[A("pk")], orderable=False
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
{% extends 'atlas/base.html' %}
|
{% extends 'atlas/base.html' %}
|
||||||
|
{% load render_table from django_tables2 %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<a href="{% url 'atlas:resource_create' %}">Create a new resource</a>
|
<a href="{% url 'atlas:resource_create' %}">Create a new resource</a>
|
||||||
|
|
||||||
<h2>Resources</h2>
|
<h2>Resources</h2>
|
||||||
|
|
||||||
<ul id="resource-list">
|
<form>
|
||||||
{% for resource in object_list %}
|
{% render_table table %}
|
||||||
<li class="">
|
</form>
|
||||||
<a href="{% url 'atlas:resource_detail' resource.pk %}">{{resource}}</a>
|
|
||||||
</li>
|
{% include "generic/partials/page_size_form.html" %}
|
||||||
{% endfor %}
|
{% include "generic/partials/filter_bar.html" with filter=filter app_name=app_name collapse_id="bottom-filter-body" %}
|
||||||
</ul>
|
|
||||||
|
|
||||||
{% endblock %}
|
{% 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.edit import CreateView, UpdateView, DeleteView, FormView
|
||||||
from django.views.generic import ListView
|
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.views.decorators.csrf import csrf_exempt
|
||||||
|
|
||||||
from django.urls import reverse_lazy, reverse
|
from django.urls import reverse_lazy, reverse
|
||||||
@@ -1311,9 +1315,11 @@ class ResourceCreate(LoginRequiredMixin, CreateView):
|
|||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
|
||||||
class ResourceView(LoginRequiredMixin, ListView):
|
class ResourceView(LoginRequiredMixin, UserConfigurablePaginationMixin, SingleTableMixin, FilterView):
|
||||||
model = Resource
|
model = Resource
|
||||||
# form_class = ResourceForm
|
table_class = ResourceTable
|
||||||
|
template_name = "atlas/resource_list.html"
|
||||||
|
filterset_class = ResourceFilter
|
||||||
|
|
||||||
|
|
||||||
class CaseCollectionCreate(RevisionMixin, LoginRequiredMixin, CreateView):
|
class CaseCollectionCreate(RevisionMixin, LoginRequiredMixin, CreateView):
|
||||||
|
|||||||
Reference in New Issue
Block a user