Skip to content

Commit

Permalink
use haystack for cazador
Browse files Browse the repository at this point in the history
  • Loading branch information
aniversarioperu committed Jan 25, 2016
1 parent bb1215b commit acefa54
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 11 deletions.
19 changes: 19 additions & 0 deletions manolo/apps/cazador/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from haystack.forms import HighlightedSearchForm


class CazadorForm(HighlightedSearchForm):
def search(self):
sqs = super(CazadorForm, self).search()

if not self.is_valid():
return self.no_query_found()

if not self.cleaned_data.get('q'):
return self.no_query_found()

sqs = self.searchqueryset.auto_query(self.cleaned_data['q'])

if self.load_all:
sqs = sqs.load_all()

return sqs
Empty file.
Empty file.
73 changes: 73 additions & 0 deletions manolo/apps/cazador/management/commands/import_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Add deudores al Estado to index"""
from tqdm import tqdm

from django.core.management.base import BaseCommand, CommandError

from cazador.models import Cazador


DBS = ['deudores', 'candidato_2014', 'narcoindultos', 'redam']


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('-i',
action='store',
dest='input_file',
help='Enter filename with data to import.',
)
parser.add_argument('-db',
action='store',
dest='database',
help='Enter database, options: {}'.format(
', '.join(DBS)))

def handle(self, *args, **options):
if not options['input_file']:
raise CommandError("Input Filename does not exist.")
if options['database'] not in DBS:
raise CommandError("Name of database is incorrect. Use one of the "
"following options: {}".format(
', '.join(DBS)))

input_file = options['input_file']
database = options['database']
self.process_file(input_file, database)

def process_file(self, input_file, database):
print(input_file, database)
with open(input_file, "r") as handle:
data = handle.readlines()

entries = []
for raw_line in tqdm(data):
line = raw_line.strip()
fields = line.split("\t")
if database == 'candidato_2014':
c = Cazador(
raw_data=" ".join([
fields[2],
fields[3],
fields[4],
fields[1],
fields[7],
]),
source=database,
)
elif database == 'redam':
c = Cazador(
raw_data=", ".join(fields),
source=database,
)
elif database == 'narcoindultos':
c = Cazador(
raw_data=" ".join(fields),
source=database,
)
else:
c = Cazador(
raw_data=" ".join([fields[2], fields[10], fields[1]]),
source=database,
)
entries.append(c)
Cazador.objects.bulk_create(entries)
21 changes: 21 additions & 0 deletions manolo/apps/cazador/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
]

operations = [
migrations.CreateModel(
name='Cazador',
fields=[
('id', models.AutoField(serialize=False, primary_key=True)),
('raw_data', models.TextField()),
('source', models.TextField()),
],
),
]
6 changes: 5 additions & 1 deletion manolo/apps/cazador/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

from django.db import models

# Create your models here.

class Cazador(models.Model):
id = models.AutoField(primary_key=True)
raw_data = models.TextField()
source = models.TextField()
9 changes: 9 additions & 0 deletions manolo/apps/cazador/search_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from haystack import indexes
from .models import Cazador


class CazadorIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)

def get_model(self):
return Cazador
4 changes: 2 additions & 2 deletions manolo/apps/cazador/templates/cazador/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<tbody>
{% for result in results %}
<tr>
<td>{{ result.1 }}</td>
<td>{{ result.3 }}</td>
<td>{{ result.source }}</td>
<td>{{ result.raw_data }}</td>
</tr>
{% endfor %}
</tbody>
Expand Down
20 changes: 13 additions & 7 deletions manolo/apps/cazador/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from django.shortcuts import render_to_response, redirect
from django.shortcuts import render_to_response

from .utils import search
from cazador.forms import CazadorForm
from django.views.decorators.csrf import csrf_exempt


@csrf_exempt
def index(request):
if 'q' in request.GET:
name = request.GET['q'].strip()
results = search(name)
form = CazadorForm(request.GET)
query = request.GET['q']

return render_to_response('cazador/results.html', {'results': results})
all_items = form.search()
all_items = [i.object for i in all_items]

return render_to_response('cazador/index.html')
return render_to_response("cazador/results.html",
{
"results": all_items,
}
)
1 change: 0 additions & 1 deletion manolo/apps/visitors/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from django.db import models
import hashlib


class Visitor(models.Model):
Expand Down
1 change: 1 addition & 0 deletions manolo/templates/search/indexes/cazador/cazador_text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ object.raw_data }}
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ python-slugify==0.1.0
six==1.9
elasticsearch==1.4.0
wheel==0.24.0
tqdm

0 comments on commit acefa54

Please sign in to comment.