From 20fbc9f732830efa2e781498009770c03034f50c Mon Sep 17 00:00:00 2001 From: HugoNeveux Date: Tue, 5 May 2020 19:42:42 +0200 Subject: [PATCH] Add module search in name and desc --- PDMI/store/forms.py | 8 ++++++++ PDMI/store/templates/store/module_list.html | 4 +++- PDMI/store/views.py | 21 ++++++++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/PDMI/store/forms.py b/PDMI/store/forms.py index d1217a2..454633e 100644 --- a/PDMI/store/forms.py +++ b/PDMI/store/forms.py @@ -34,3 +34,11 @@ class FileFieldForm(forms.ModelForm): } ) } + +class ModuleSearchForm(forms.Form): + name = forms.CharField(required=False, widget=forms.TextInput( + attrs={ + 'class': 'form-control ml-3 w-75', + 'placeholder': 'Search', + 'aria-label': 'Search', + })) diff --git a/PDMI/store/templates/store/module_list.html b/PDMI/store/templates/store/module_list.html index d41693d..5496104 100644 --- a/PDMI/store/templates/store/module_list.html +++ b/PDMI/store/templates/store/module_list.html @@ -8,7 +8,7 @@
- + {{ form.name }}
@@ -27,6 +27,8 @@

View details »

+ {% empty %} + No module matches your request. {% endfor %} diff --git a/PDMI/store/views.py b/PDMI/store/views.py index a45451c..b27b10e 100644 --- a/PDMI/store/views.py +++ b/PDMI/store/views.py @@ -9,8 +9,10 @@ from django.views import View from django.http import HttpResponse, JsonResponse from django.utils import timezone from django.core.files import File +from django.db.models import Q from bootstrap_modal_forms.generic import BSModalCreateView, BSModalLoginView -from .forms import CustomUserCreationForm, CustomAuthenticationForm, FileFieldForm +from .forms import (CustomUserCreationForm, CustomAuthenticationForm, FileFieldForm, + ModuleSearchForm) from .models import Module, Version, Dependency from PDMI import settings from packaging.specifiers import SpecifierSet @@ -44,15 +46,27 @@ class ModuleListView(ListView): """Lists all available modules""" model = Module paginate_by = 100 + form_class = ModuleSearchForm def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['now'] = timezone.now() + context['form'] = ModuleSearchForm(initial={'name': self.request.GET.get('name')}) return context + def get_queryset(self): + try: + name = self.request.GET.get('name') + object_list = self.model.objects.filter(Q(name__icontains=name) | + Q(desc__icontains=name)) + except ValueError: + object_list = self.model.objects.all() + return object_list + class ModuleDetailView(View): """Shows details about module""" + def get(self, request, pk, req_ver="latest"): module = get_object_or_404(Module, id=pk) versions = Version.objects.filter(module=module) @@ -101,14 +115,15 @@ class UploadView(LoginRequiredMixin, FormView): with open(os.path.join(extract_path, 'infos.toml'), 'r') as f: # Reading and parsing toml file module_info = toml.loads(f.read()) - toml_file = File(open(os.path.join(extract_path, 'infos.toml'), 'r')) + toml_file = File( + open(os.path.join(extract_path, 'infos.toml'), 'r')) if os.path.isfile(os.path.join(extract_path, 'README.md')): with open(os.path.join(extract_path, 'README.md'), 'r') as f: # Reading README.md file readme = f.read() else: readme = "No readme provided." - shutil.rmtree(extract_path) # Clean temp files + shutil.rmtree(extract_path) # Clean temp files os.remove(zip_path) for required_field in REQUIRED_FIELDS: if not required_field in module_info.keys():