Skip to content
Snippets Groups Projects
Commit 3c2e7dc4 authored by decentral1se's avatar decentral1se
Browse files

Merge branch 'filter-by-tags' into 'staging'

Filter by tags.

See merge request !93
parents e62a1a2e b57f6e32
No related branches found
No related tags found
1 merge request!93Filter by tags.
Pipeline #2669 passed
Showing with 70 additions and 24 deletions
......@@ -11,6 +11,7 @@ class HidAppConfig(AppConfig):
CategoryFilter,
LocationFilter,
TimeRangeFilter,
TagsFilter,
)
from hid.tabs.view_and_edit_table import ViewAndEditTableTab
from hid.widgets.term_count_chart import TermCountChartWidget
......@@ -21,6 +22,7 @@ class HidAppConfig(AppConfig):
register_filter('category', CategoryFilter())
register_filter('time_range', TimeRangeFilter())
register_filter('location', LocationFilter())
register_filter('tags', TagsFilter())
register_tab('view-and-edit-table', ViewAndEditTableTab())
......
......@@ -9,6 +9,13 @@ class CategoryFilter(object):
)
class TagsFilter(object):
def apply(self, filters, query_dict, **kwargs):
tags = kwargs.get('tags', None)
if tags is not None:
filters.update(tags=tags)
class TimeRangeFilter(object):
def apply(self, filters, query_dict, **kwargs):
start_time = query_dict.get('start_time', None)
......
......@@ -293,7 +293,7 @@
"name": "feedback",
"page": 1,
"position": 2,
"settings": "{\"label\":\"Feedback\",\"columns\":[\"select_item\",\"created\",\"timestamp\",\"body\",\"translation\",\"category\",\"location\"],\"filters\":{\"terms\":[]},\"dynamic_filters\":[\"time_range\",\"category\", \"location\"],\"categories\":[\"bangladesh-refugee-crisis-sectors\"]}",
"settings": "{\"label\":\"Feedback\",\"columns\":[\"select_item\",\"created\",\"timestamp\",\"body\",\"translation\",\"category\",\"location\"],\"filters\":{\"terms\":[]},\"dynamic_filters\":[\"time_range\",\"category\", \"location\", \"tags\"],\"categories\":[\"bangladesh-refugee-crisis-sectors\"]}",
"tab_type": "view-and-edit-table"
},
"model": "tabbed_page.tabinstance",
......
......@@ -35,6 +35,12 @@ class ItemTable(tables.Table):
accessor='terms',
attrs={'td': {'class': 'col-md-2'}}
)
tags = tables.TemplateColumn(
verbose_name=_('Tags'),
template_name='hid/tags_column.html',
accessor='terms',
attrs={'td': {'class': 'col-md-2'}}
)
body = tables.TemplateColumn(
template_name='hid/body_column.html',
verbose_name=_('Feedback'),
......@@ -74,6 +80,17 @@ class ItemTable(tables.Table):
return Template.render(ctx)
def render_tags(self, record, value):
Template = loader.get_template('hid/tags_column.html')
try:
tags = filter(None, [term['name'] for term in record['terms']])
ctx = {'tags': ', '.join(tags)}
except KeyError:
ctx = {'tags': []}
return Template.render(ctx)
@staticmethod
def get_selected(params):
""" Given a request parameter list, return the items that were
......
......@@ -146,8 +146,11 @@ class ViewAndEditTableTab(object):
return tuple((t['name'], t['name']) for t in all_terms)
def _get_location_options(self, **kwargs):
items = transport_items.list()
return {'items': list(set(item['location'] for item in items))}
locations = filter(None, [
item['location'] for item
in transport_items.list()
])
return {'items': list(set(locations))}
def _build_actions_dropdown(self, question_types):
items = [
......@@ -193,6 +196,8 @@ class ViewAndEditTableTab(object):
request.GET.pop('start_time')
if request.GET.get('end_time'):
request.GET.pop('end_time')
if request.GET.get('tags'):
request.GET.pop('tags')
else:
filters = kwargs.get('filters', {})
......@@ -220,6 +225,9 @@ class ViewAndEditTableTab(object):
if previously_selected_location is not False:
location_options.update({'selected': previously_selected_location})
if request.GET.get('tags', False):
filters.update(tags=request.GET['tags'])
items = self._get_items(request, **kwargs)
# Build the table
......
......@@ -13,9 +13,9 @@
<div class="panel-body">
Editing ID {{ item.id }}
<div class="row">
{% if form.body %}
{% if form.body %}
<div class="col-xs-12 col-md-4 input-group form-group item-original-text {% if form.body.errors %}has-error{% endif %}">
<label>
{% trans "Feedback" %}
......@@ -28,7 +28,7 @@
{{ form.body|add_class:'form-control'|attr:"placeholder:Type your original text here" }}
</div>
{% endif %}
{% if form.translation %}
<div class="col-xs-12 col-md-4 input-group form-group item-translation-text {% if form.translation.errors %}has-error{% endif %}">
<label>
......@@ -40,9 +40,9 @@
{{ form.translation|add_class:'form-control'|attr:"placeholder:Type your translation text here" }}
</div>
{% endif %}
<div class="col-xs-12 col-md-3 input-group form-group item-attributes-text">
{% if form.category %}
<div class="form-group pull-left input-group item-category {% if form.category.errors %}has-error{% endif %}">
<label>
......@@ -90,7 +90,7 @@
</div>
</div>
{% endif %}
{% if form.location %}
<div class="form-group input-group item-location {% if form.location.errors %}has-error{% endif %}">
<label>
......@@ -104,12 +104,12 @@
{% endif %}
</div>
</div>
<div class="row">
<div class="col-xs-12 col-md-4 form-group pull-right">
{% if update %}
{% bootstrap_button "Update" button_type="submit" name="action" value="update" button_class="btn btn-lg btn-success" %}
......@@ -119,13 +119,13 @@
{% bootstrap_button "Cancel" button_type="submit" name="action" value="cancel" button_class="btn btn-lg btn-border" %}
{% endif %}
</div>
<div class="col-xs-12 col-md-8 form-group pull-left">
{% bootstrap_button "Delete" button_type="submit" name="action" value="delete" button_class="btn btn-danger" %}
</div>
</div>
</div>
</div>
......
{% if request %}
{% if record.body %}
<a href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ record.body }}</a>
<a target="_blank" href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ record.body }}</a>
{% else %}
<a href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'><p class="missing-body"> Missing text </p></a>
<a target="_blank" href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'><p class="missing-body"> Missing text </p></a>
{% endif %}
{% else %}
{{ record.body }}
......
{% load i18n %}
<div class="filter-tags form-group input-group">
<label class="col-sm-3 control-label" for="tags-selector">{% trans "Tags" %}</label>
<div class="text-container">
<input style="width:140px;" type="text" placeholder="All Tags" name="tags">
</div>
</div>
{% load i18n %}
<div class="filter-dates form-group input-group input-daterange">
<label>Dates</label>
<input type="date" value="{{ filters.start_time }}" class="form-control" name="start_time">
<input type="date" value="{{ filters.end_time }}" class="form-control" name="end_time">
<input style="width:140px;" type="date" value="{{ filters.start_time }}" class="form-control" name="start_time">
<input style="width:140px;" type="date" value="{{ filters.end_time }}" class="form-control" name="end_time">
</div>
{{ tags }}
{% if request %}
<a href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ record.translation }}</a>
<a target="_blank" href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ record.translation }}</a>
{% else %}
{{ record.translation }}
{% endif %}
......@@ -67,6 +67,10 @@ class ItemViewSet(viewsets.ModelViewSet, BulkDestroyModelMixin):
items = items.filter(terms__id=matches[0].id)
tags = self.request.query_params.get('tags', None)
if tags is not None:
items = items.filter(terms__name__icontains=tags)
location = self.request.query_params.get('location', None)
if location is not None:
items = items.filter(location__icontains=location)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment