Add module search in name and desc

This commit is contained in:
HugoNeveux 2020-05-05 19:42:42 +02:00
parent 7e7d95ffb0
commit 20fbc9f732
3 changed files with 29 additions and 4 deletions

View File

@ -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',
}))

View File

@ -8,7 +8,7 @@
<!-- Search form --> <!-- Search form -->
<form class="form-inline d-flex justify-content-center md-form form-sm mt-0"> <form class="form-inline d-flex justify-content-center md-form form-sm mt-0">
<i class="fa fa-search" aria-hidden="true"></i> <i class="fa fa-search" aria-hidden="true"></i>
<input class="form-control ml-3 w-75" type="text" placeholder="Search" aria-label="Search"> {{ form.name }}
</form> </form>
</div> </div>
</div> </div>
@ -27,6 +27,8 @@
<p><a class="btn btn-secondary" href="{% url 'mod_detail' pk=module.id req_ver='latest' %}" role="button">View details &raquo;</a></p> <p><a class="btn btn-secondary" href="{% url 'mod_detail' pk=module.id req_ver='latest' %}" role="button">View details &raquo;</a></p>
</div> </div>
</div> </div>
{% empty %}
No module matches your request.
{% endfor %} {% endfor %}
</div> </div>
</div> </div>

View File

@ -9,8 +9,10 @@ from django.views import View
from django.http import HttpResponse, JsonResponse from django.http import HttpResponse, JsonResponse
from django.utils import timezone from django.utils import timezone
from django.core.files import File from django.core.files import File
from django.db.models import Q
from bootstrap_modal_forms.generic import BSModalCreateView, BSModalLoginView 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 .models import Module, Version, Dependency
from PDMI import settings from PDMI import settings
from packaging.specifiers import SpecifierSet from packaging.specifiers import SpecifierSet
@ -44,15 +46,27 @@ class ModuleListView(ListView):
"""Lists all available modules""" """Lists all available modules"""
model = Module model = Module
paginate_by = 100 paginate_by = 100
form_class = ModuleSearchForm
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['now'] = timezone.now() context['now'] = timezone.now()
context['form'] = ModuleSearchForm(initial={'name': self.request.GET.get('name')})
return context 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): class ModuleDetailView(View):
"""Shows details about module""" """Shows details about module"""
def get(self, request, pk, req_ver="latest"): def get(self, request, pk, req_ver="latest"):
module = get_object_or_404(Module, id=pk) module = get_object_or_404(Module, id=pk)
versions = Version.objects.filter(module=module) 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: with open(os.path.join(extract_path, 'infos.toml'), 'r') as f:
# Reading and parsing toml file # Reading and parsing toml file
module_info = toml.loads(f.read()) 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')): if os.path.isfile(os.path.join(extract_path, 'README.md')):
with open(os.path.join(extract_path, 'README.md'), 'r') as f: with open(os.path.join(extract_path, 'README.md'), 'r') as f:
# Reading README.md file # Reading README.md file
readme = f.read() readme = f.read()
else: else:
readme = "No readme provided." readme = "No readme provided."
shutil.rmtree(extract_path) # Clean temp files shutil.rmtree(extract_path) # Clean temp files
os.remove(zip_path) os.remove(zip_path)
for required_field in REQUIRED_FIELDS: for required_field in REQUIRED_FIELDS:
if not required_field in module_info.keys(): if not required_field in module_info.keys():