Commit 58180597 authored by Evili del Rio's avatar Evili del Rio

Added search view

parent d48df337
......@@ -22,5 +22,6 @@ urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^health/$', HealthView.as_view(), name='health'),
url(r'^admin/', admin.site.urls),
url(r'^clades/', include('clades.urls')),
url('^', include('django.contrib.auth.urls')),
]
......@@ -84,9 +84,28 @@
]
}
},
{
"model": "clades.species",
"fields": {
"name": "rufonuchalis",
"notes": null,
"genus": [
"Periparus"
]
}
},
{
"model": "clades.species",
"fields": {
"name": "rubidiventris",
"notes": null,
"genus": [
"Periparus"
]
}
},
{
"model": "clades.commonname",
"pk": 1,
"fields": {
"cname": "Mallerenga petita",
"species": [
......@@ -99,7 +118,6 @@
},
{
"model": "clades.commonname",
"pk": 2,
"fields": {
"cname": "Coal Tit",
"species": [
......@@ -112,7 +130,6 @@
},
{
"model": "clades.commonname",
"pk": 3,
"fields": {
"cname": "Carbonero garrapinos",
"species": [
......@@ -122,5 +139,29 @@
"es"
]
}
},
{
"model": "clades.commonname",
"fields": {
"cname": "Rufous-naped tit",
"species": [
"rufonuchalis"
],
"locale": [
"en"
]
}
},
{
"model": "clades.commonname",
"fields": {
"cname": "Rufous-vented tit",
"species": [
"rubidiventris"
],
"locale": [
"en"
]
}
}
]
from django import forms
class CladesSearchForm(forms.Form):
search = forms.CharField(label='Search')
{% extends "clades/base.html" %}{% load i18n %}
{% block content %}
<p>Clades Search Page</p>
<form method="post">{% csrf_token %}
{{ form.as_p }}
</form>
{% for obj in search_results %}
<p><a href="{{obj.get_absolute_url}}">{{ obj }}</a></p>
{% endfor %}
{% endblock content %}
from django.test import TransactionTestCase
from django.utils import translation
from .models import Kingdom, Phylum, Classe, Ordo, Family, Genus, Species, CommonName, Locale
from clades.models import Kingdom, Phylum, Classe, Ordo, Family, Genus, Species, CommonName, Locale
_KINGDOM_NAME = u'Animalia'
_PHYLUM_NAME = u'Chordata'
......@@ -17,7 +17,7 @@ _COMMON_NAME = {
class CladesTest(TransactionTestCase):
fixtures = ['periparus_ater']
fixtures = ['periparus']
def setUp(self):
(self.kingdom, created) = Kingdom.objects.get_or_create(
......
from django.test import TestCase
from django.urls import reverse_lazy
class CladesViewsTest(TestCase):
fixtures = ['periparus']
def visit(self, name, follow=True, **kwargs):
return self.client.get(reverse_lazy(name), follow=True, **kwargs)
def test_home(self):
response = self.visit('clades-home')
self.assertEqual(200, response.status_code, 'No Home view available')
def test_search(self):
response = self.visit('clades-search')
self.assertEqual(200, response.status_code, 'No Search view available')
response = self.client.post(reverse_lazy('clades-search'),
{'search' :'ater'},
follow=True)
self.assertIn(b'Coal', response.content, 'Species name not found')
from django.urls import path
from .views import CladesHomeView, CladesSearchView
urlpatterns = [
path('', CladesHomeView.as_view(), name='clades-home'),
path('search/', CladesSearchView.as_view(), name='clades-search'),
]
from django.shortcuts import render
from django.views.generic import TemplateView
from django.views.generic.edit import FormMixin
from django.urls import reverse_lazy
from .forms import CladesSearchForm
from .models import Species
class CladesHomeView(TemplateView):
template_name = 'clades/home.html'
class CladesSearchView(FormMixin, TemplateView):
template_name = 'clades/search.html'
form_class = CladesSearchForm
success_url = reverse_lazy('clades-search')
def post(self, request, *args, **kwargs):
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
search_items = form.cleaned_data['search'].lower().split(' ')
obj_list = []
for si in search_items:
obj_list.extend(
Species.objects.filter(
name__icontains=si).distinct().order_by('pk'))
obj_list.extend(
Species.objects.filter(
genus__name__icontains=si).distinct().order_by('pk')
)
kwargs= {}
context = self.get_context_data(**kwargs)
context['search_results'] = obj_list
response = self.render_to_response(context, **kwargs)
return response
form_valid.alters_data = True
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment