Skip to content

Commit

Permalink
filter datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed Oct 23, 2024
1 parent 6152ad0 commit e7c5e2e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 14 deletions.
26 changes: 26 additions & 0 deletions metadata_catalogue/datasets/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django_filters import FilterSet, BooleanFilter, CharFilter
from django.forms.widgets import CheckboxInput, NullBooleanSelect
from . import models, forms


class DatasetFilter(FilterSet):
search = CharFilter(method="filter_search", label="Full text search")
my_datasets = BooleanFilter(
label="My datasets", method="filter_my_datasets", widget=CheckboxInput()
)
public = BooleanFilter(widget=NullBooleanSelect())

def filter_my_datasets(self, queryset, name, value):
if value:
return queryset.filter(owner=self.request.user)
return queryset

def filter_search(self, queryset, name, value):
if value:
return queryset.search(value)
return queryset

class Meta:
model = models.Dataset
fields = {"public": ["exact"]}
form = forms.DatasetFilterForm
23 changes: 16 additions & 7 deletions metadata_catalogue/datasets/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
from django.forms import (
ModelForm,
widgets,
CharField,
IntegerField,
)
from django.forms import ModelForm, widgets, CharField, IntegerField, Form
import logging
from datetime import date
from crispy_forms.helper import FormHelper
Expand All @@ -14,10 +9,13 @@


class DatasetCreateForm(ModelForm):
abstract = CharField(widget=widgets.Textarea(attrs=dict(rows=2)))
abstract = CharField(
label="Description", widget=widgets.Textarea(attrs=dict(rows=2))
)
formation_period_start = IntegerField(label="Temporal start (year)")
formation_period_end = IntegerField(label="Temporal end (year)")
geographic_description = CharField(label="Geographic description")
source = CharField(required=True, widget=widgets.Textarea(attrs=dict(rows=2)))

def __init__(self, *args, user, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -65,3 +63,14 @@ class Meta:
),
"notes": widgets.Textarea({"rows": 2, "class": "textarea-bordered"}),
}


class DatasetFilterForm(Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.disable_csrf = True
self.helper.add_input(Submit("submit", "Search", css_class="mt-2"))

class Meta:
widgets = {"my_datasets": widgets.CheckboxInput()}
32 changes: 29 additions & 3 deletions metadata_catalogue/datasets/managers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.gis.db import models
from django.db.models import Q
from django.contrib.postgres.search import SearchVector

from . import logger
from .libs.csw.mapping import CSWMapping
Expand All @@ -11,7 +12,9 @@

class DatasetQuerySet(models.QuerySet):
def as_csw(self, *args, base_url="", warn=True, **kwargs):
logger.warning("DANGER: This method consumes the queryset and returns and array of items")
logger.warning(
"DANGER: This method consumes the queryset and returns and array of items"
)
return [CSWMapping(instance, base_url) for instance in self]

def csw_filter(self, filter):
Expand All @@ -27,13 +30,17 @@ def csw_filter(self, filter):

def csw_sort(self, sort):
try:
return self.order_by(f'{"-" if sort["order"] == "DESC" else ""}{SORT_CONFIG[sort["propertyname"]]}')
return self.order_by(
f'{"-" if sort["order"] == "DESC" else ""}{SORT_CONFIG[sort["propertyname"]]}'
)
except (AttributeError, KeyError):
logger.warn(f"Not implemented! {sort}")
return self

def as_geoapi_resource(self, base_url, *args, warn=True, **kwargs):
logger.warn("DANGER: This method consumes the queryset and returns and array of items")
logger.warn(
"DANGER: This method consumes the queryset and returns and array of items"
)
return [
ResourceMapping(instance, base_url).as_resource()
for instance in self.select_related("metadata", "content").exclude(
Expand All @@ -45,6 +52,25 @@ def as_geoapi_resource(self, base_url, *args, warn=True, **kwargs):
)
]

def search(self, text):
return (
self.select_related("metadata")
.annotate(
search=SearchVector(
"name",
"metadata__title",
"metadata__abstract",
"metadata__geographic_description",
"metadata__fts",
"metadata__project_title",
"metadata__project_abstract",
"metadata__project_study_area_description",
"metadata__project_design_description",
)
)
.filter(search=text)
)


class DatasetManager(models.Manager):
def get_queryset(self):
Expand Down
9 changes: 6 additions & 3 deletions metadata_catalogue/datasets/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.http import HttpResponse, HttpResponseNotFound
from .models import Dataset
from . import tables, forms
from . import tables, forms, filters
from django.views.generic import CreateView, DetailView
from typing import Any
from django_tables2 import SingleTableView
from django_tables2.views import SingleTableMixin
from django_filters.views import FilterView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.postgres.aggregates import ArrayAgg

Expand All @@ -16,9 +17,11 @@ def get_dataset_vrt_view(request, dataset_uuid):
return HttpResponseNotFound()


class DatasetsListPage(SingleTableView):
class DatasetsListPage(SingleTableMixin, FilterView):
model = Dataset
table_class = tables.DatasetTable
filterset_class = filters.DatasetFilter
template_name = "datasets/dataset_list.html"

def get_queryset(self):
qs = super().get_queryset().order_by("created_at")
Expand Down
1 change: 0 additions & 1 deletion metadata_catalogue/templates/datasets/dataset_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ <h1 class="font-bold text-4xl">Create Dataset</h1>
</div>
<form action="{% url 'dataset-create' %}" method="post">
{% crispy form %}
{% csrf_token %}
</form>
{% endblock article %}
6 changes: 6 additions & 0 deletions metadata_catalogue/templates/datasets/dataset_list.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends 'app.html' %}

{% load render_table from django_tables2 %}
{% load crispy_forms_tags %}

{% block article %}
<div>
Expand All @@ -10,6 +11,11 @@ <h1 class="font-bold text-4xl">Datasets</h1>
<a href="{% url 'dataset-create' %}" class="btn btn-primary"><i class="fas fa-plus"></i> Dataset</a>
</div>
</div>
{% if filter %}
<form action="" method="get" class="flex gap-8">
{% crispy filter.form %}
</form>
{% endif %}
{% render_table table %}
</div>
{% endblock article %}
4 changes: 4 additions & 0 deletions metadata_catalogue/theme/static_src/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@
.dataset-metadata-section-values *:nth-child(2) {
@apply col-span-3;
}

select {
@apply pr-8 !important;
}

0 comments on commit e7c5e2e

Please sign in to comment.