Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
gjost committed Aug 28, 2015
2 parents 9d154b3 + af74104 commit e139ee2
Show file tree
Hide file tree
Showing 23 changed files with 1,270 additions and 12 deletions.
8 changes: 7 additions & 1 deletion INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ Edit `/etc/ddr/local.cfg` to include the following text.::
docstore_host=127.0.0.1:9200
# Name of the index you wish to use (e.g. "encyc-production", "encyc-stage", "encyc-dev").
docstore_index=encyc-dev


# Absolute path to directory that will hold static and user-uploaded files.
# Note: Should match MEDIA_ROOT and STATIC_ROOT in Makefile.
# Note: Should not have trailing slashes.
static_root=/var/www/ddrpublic/static
media_root=/var/www/ddrpublic/media

# Thumbnail module looks here for full-size images.
# Should be a direct link, with no redirects or CDNs in the way.
media_url_local=http://192.168.56.101/media/public/
Expand Down
2 changes: 1 addition & 1 deletion ddrpublic/ddrpublic/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.9-beta'
VERSION = '1.0'
Empty file added ddrpublic/names/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions ddrpublic/names/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
12 changes: 12 additions & 0 deletions ddrpublic/names/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import logging
logger = logging.getLogger(__name__)

from django.conf import settings


def sitewide(request):
"""Variables that need to be inserted into all templates.
"""
return {
'hide_header_search': True,
}
93 changes: 93 additions & 0 deletions ddrpublic/names/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from collections import OrderedDict
import logging
logger = logging.getLogger(__name__)

from django import forms
from django.conf import settings
from django.utils.datastructures import SortedDict

from namesdb.definitions import FIELD_DEFINITIONS
from names import models


def field_choices(hosts, index, fieldname):
"""
- Get unique field values and counts (aggregations).
- Get approved value labels from definitions.
- Package into django.forms-friendly choices list.
"""
# TODO get these for all fields at once? cache?
aggregations = models.field_values(hosts, index, fieldname)
choices_dict = FIELD_DEFINITIONS.get(fieldname, {}).get('choices', OrderedDict([]))
choices = [
(term, '%s (%s)' % (choices_dict.get(term, term), count))
for term,count in aggregations
]
return sorted(choices, key=lambda x: x[1])

def update_doc_counts(form, response):
"""Add aggregations doc_counts to field choice labels
"""
if hasattr(response, 'get') and response.get('aggregations', None):
aggregations = response.aggregations.to_dict()
else:
aggregations = {}
agg_fieldnames = [key for key in aggregations.iterkeys()]
form_fieldnames = [key for key in form.fields.iterkeys()]
for fieldname in form_fieldnames:
if fieldname in agg_fieldnames:
field_aggs_dict = {
d['key']: d['doc_count']
for d in aggregations[fieldname]['buckets']
}
new_choices = []
for keyword,label in form.fields[fieldname].choices:
count = field_aggs_dict.get(keyword, None)
if count is not None:
label = '%s (%s)' % (label, count)
new_choices.append( (keyword, label) )
form.fields[fieldname].choices = new_choices

class SearchForm(forms.Form):
m_dataset = forms.MultipleChoiceField(required=False, choices=[], widget=forms.CheckboxSelectMultiple)
m_camp = forms.MultipleChoiceField(required=False, choices=[], widget=forms.CheckboxSelectMultiple)
m_originalstate = forms.MultipleChoiceField(required=False, choices=[], widget=forms.CheckboxSelectMultiple)
m_gender = forms.MultipleChoiceField(required=False, choices=[], widget=forms.CheckboxSelectMultiple)
m_birthyear = forms.MultipleChoiceField(required=False, choices=[], widget=forms.CheckboxSelectMultiple)
query = forms.CharField(required=False, max_length=255)

def __init__( self, *args, **kwargs ):
hosts = kwargs.pop('hosts')
index = kwargs.pop('index')
super(SearchForm, self).__init__(*args, **kwargs)
self.fields['m_dataset'].choices = field_choices(hosts, index, 'm_dataset')
self.fields['m_camp'].choices = field_choices(hosts, index, 'm_camp')
self.fields['m_originalstate'].choices = field_choices(hosts, index, 'm_originalstate')
self.fields['m_gender'].choices = field_choices(hosts, index, 'm_gender')
self.fields['m_birthyear'].choices = field_choices(hosts, index, 'm_birthyear')

def update_doc_counts(self, response):
update_doc_counts(self, response)


class FlexiSearchForm(forms.Form):
"""Construct form from any combination of Record filter fields.
"""
def __init__( self, *args, **kwargs ):
hosts = kwargs.pop('hosts')
index = kwargs.pop('index')
filters = kwargs.pop('filters')
super(FlexiSearchForm, self).__init__(*args, **kwargs)
fields = []
fields.append(('query', forms.CharField(required=False, max_length=255)))
for field_name in filters:
fobject = forms.MultipleChoiceField(
required=False,
choices=field_choices(hosts, index, field_name),
widget=forms.CheckboxSelectMultiple
)
fields.append((field_name, fobject))
self.fields = SortedDict(fields)

def update_doc_counts(self, response):
update_doc_counts(self, response)
Empty file.

0 comments on commit e139ee2

Please sign in to comment.