Skip to content

Commit

Permalink
allow to filter projects
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed Jan 9, 2024
1 parent 3ee0063 commit 333a3fe
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 22 deletions.
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"rules.apps.AutodiscoverRulesConfig",
"organizations",
"taggit",
"django_filters",
]

LOCAL_APPS = [
Expand Down
22 changes: 22 additions & 0 deletions metadata_catalogue/nina/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import django_filters
from taggit.forms import TagField

from .forms import ProjectSearchForm
from .models import Project


class TagFilter(django_filters.CharFilter):
field_class = TagField

def __init__(self, *args, **kwargs):
kwargs.setdefault("lookup_expr", "in")
super().__init__(*args, **kwargs)


class ProjectFilter(django_filters.FilterSet):
tags = TagFilter(field_name="tags__name")

class Meta:
model = Project
form = ProjectSearchForm
fields = ["status", "topics", "category", "departments", "customer"]
18 changes: 18 additions & 0 deletions metadata_catalogue/nina/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from django import forms

from .models import Project


class ProjectSearchForm(forms.ModelForm):
class Meta:
model = Project
fields = ["status", "topics", "category", "departments", "customer"]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = "get"

self.helper.add_input(Submit("submit", "Search"))
9 changes: 6 additions & 3 deletions metadata_catalogue/nina/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from typing import Any

from django.core.exceptions import ObjectDoesNotExist
from django.views.generic import DetailView, ListView
from django_filters.views import FilterView

from metadata_catalogue.projects import views

from .models import Department
from .filters import ProjectFilter
from .models import Department, Project


class ProjectListView(views.ProjectListView):
class ProjectListView(FilterView):
model = Project
paginate_by = 20
filterset_class = ProjectFilter


class ProjectUpdateView(views.ProjectUpdateView):
Expand Down
23 changes: 23 additions & 0 deletions metadata_catalogue/templates/nina/project_filter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends 'base.html' %}

{% load crispy_forms_tags %}

{% block content %}
<h2>Projects</h2>
<div class="pb-5 mt-3 row">
<div class="col-3">{% crispy filter.form %}</div>
<div class="col-9">
<ul class="list-group">
{% for project in object_list %}
<li class="list-group-item">
<h5>
<a href="{% url 'projects-detail' slug=project.slug %}">{{ project.name }}</a>
</h5>
<p>{{ project.description|default_if_none:'No description' }}</p>
</li>
{% endfor %}
</ul>
<div class="mt-3">{% include 'includes/pagination.html' %}</div>
</div>
</div>
{% endblock content %}
18 changes: 0 additions & 18 deletions metadata_catalogue/templates/nina/project_list.html

This file was deleted.

15 changes: 14 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ dependencies = [
"django-organizations>=2.3.1",
"swapper>=1.3.0",
"django-taggit>=5.0.1",
"django-filter>=23.5",
]
requires-python = ">=3.10"
name = ""
Expand Down

0 comments on commit 333a3fe

Please sign in to comment.