Skip to content
Snippets Groups Projects
Commit 2c245169 authored by martinburchell's avatar martinburchell
Browse files

Merge pull request #13 from aptivate/item_types

Item types
parents abff5a0f bc616220
No related branches found
No related tags found
No related merge requests found
...@@ -117,9 +117,10 @@ class Importer(object): ...@@ -117,9 +117,10 @@ class Importer(object):
{} {}
) )
def save_rows(self, objects, data_type): def save_rows(self, objects, item_type):
for obj in objects: for obj in objects:
transport.items.create(obj) item = transport.items.create(obj)
transport.items.add_term(item['id'], 'item-types', item_type)
return len(objects) return len(objects)
...@@ -128,11 +129,12 @@ class Importer(object): ...@@ -128,11 +129,12 @@ class Importer(object):
file_format = profile.get('format') file_format = profile.get('format')
skip_header = profile.get('skip_header', False) skip_header = profile.get('skip_header', False)
item_type = profile.get('type')
rows = self.get_rows_iterator(fobject, file_format) rows = self.get_rows_iterator(fobject, file_format)
items = self.process_rows(rows, profile['columns'], skip_header) items = self.process_rows(rows, profile['columns'], skip_header)
return self.save_rows(items, 'message') return self.save_rows(items, item_type)
class CellConverter(object): class CellConverter(object):
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
GEOPOLL_CONFIG = {
"label": "geopoll",
"name": "Geopoll",
"format": "excel",
"type": "question",
"columns": [
{
"name": "Province",
"type": "ignore",
"field": "ignore"
},
{
"name": "CreatedDate",
"type": "date",
"field": "timestamp",
"date_format": "%m/%d/%y"
},
{
"name": "AgeGroup",
"type": "ignore",
"field": "ignore"
},
{
"name": "QuestIO",
"type": "text",
"field": "body"
}
],
"skip_header": 1
}
def update_geopoll_config(apps, schema_editor):
Profile = apps.get_model('chn_spreadsheet', 'SheetProfile')
Profile.objects.filter(label='geopoll').update(
profile=GEOPOLL_CONFIG)
class Migration(migrations.Migration):
dependencies = [
('chn_spreadsheet', '0004_add_rapidpro_config'),
]
operations = [
migrations.RunPython(update_geopoll_config)
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
RAPIDPRO_CONFIG = {
"label": "rapidpro",
"name": "RapidPro",
"format": "excel",
"type": "rumor",
"columns": [
{
"name": "Phone",
"type": "ignore",
"field": "ignore"
},
{
"name": "Name",
"type": "ignore",
"field": "ignore"
},
{
"name": "Groups",
"type": "ignore",
"field": "ignore"
},
{
"name": "Last Seen",
"type": "date",
"field": "timestamp",
"date_format": "%m/%d/%y %H:%M:%S"
},
{
"name": "Rumors (Text) - DEY Say sample flow",
"type": "text",
"field": "body"
},
{
"name": "Channel",
"type": "ignore",
"field": "ignore"
}
],
"skip_header": 1
}
def update_rapidpro_config(apps, schema_editor):
Profile = apps.get_model('chn_spreadsheet', 'SheetProfile')
Profile.objects.filter(label='rapidpro').update(profile=RAPIDPRO_CONFIG)
class Migration(migrations.Migration):
dependencies = [
('chn_spreadsheet', '0005_update_geopoll_config'),
]
operations = [
migrations.RunPython(update_rapidpro_config)
]
...@@ -27,3 +27,10 @@ def test_items_imported(importer): ...@@ -27,3 +27,10 @@ def test_items_imported(importer):
assert items[0]['body'] == "What is the cuse of ebola?" assert items[0]['body'] == "What is the cuse of ebola?"
assert items[0]['timestamp'] == pytz.utc.localize( assert items[0]['timestamp'] == pytz.utc.localize(
datetime.datetime(2015, 5, 1)) datetime.datetime(2015, 5, 1))
item_types = transport.taxonomies.term_itemcount(
slug='item-types')
counts_per_item = {t['name']: t['count'] for t in item_types}
assert counts_per_item['question'] == num_saved
...@@ -6,6 +6,8 @@ import pytz ...@@ -6,6 +6,8 @@ import pytz
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
import transport
from ..importer import ( from ..importer import (
Importer, Importer,
SheetProfile, SheetImportException SheetProfile, SheetImportException
...@@ -300,3 +302,17 @@ def test_process_rows_ignores_empty_lines(importer): ...@@ -300,3 +302,17 @@ def test_process_rows_ignores_empty_lines(importer):
] ]
assert objects == expected_objects assert objects == expected_objects
@pytest.mark.django_db
def test_save_rows_creates_item_with_term(importer):
objects = [{'body': "Text", 'timestamp': datetime.datetime(2014, 7, 21)}]
assert importer.save_rows(objects, 'question') == 1
item_types = transport.taxonomies.term_itemcount(
slug='item-types')
counts_per_item = {t['name']: t['count'] for t in item_types}
assert counts_per_item['question'] == 1
assert counts_per_item['rumor'] == 0
...@@ -27,3 +27,10 @@ def test_items_imported(importer): ...@@ -27,3 +27,10 @@ def test_items_imported(importer):
assert items[0]['body'] == "That there is a special budget to give money to the family of each dead in Liberia since the Ebola outbreak." assert items[0]['body'] == "That there is a special budget to give money to the family of each dead in Liberia since the Ebola outbreak."
assert items[0]['timestamp'] == pytz.utc.localize( assert items[0]['timestamp'] == pytz.utc.localize(
datetime.datetime(2015, 4, 19, 21, 35, 20)) datetime.datetime(2015, 4, 19, 21, 35, 20))
item_types = transport.taxonomies.term_itemcount(
slug='item-types')
counts_per_item = {t['name']: t['count'] for t in item_types}
assert counts_per_item['rumor'] == num_saved
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
def create_item_types(apps, schema_editor):
Taxonomy = apps.get_model('taxonomies', 'Taxonomy')
taxonomy = Taxonomy(name="Item Types", slug="item-types")
taxonomy.save()
item_types = (
("question", "Question"),
("rumor", "Rumor"),
)
Term = apps.get_model('taxonomies', 'Term')
new_terms = [Term(
name=name,
long_name=long_name,
taxonomy=taxonomy) for name, long_name in item_types]
db_alias = schema_editor.connection.alias
Term.objects.using(db_alias).bulk_create(new_terms)
class Migration(migrations.Migration):
dependencies = [
('hid', '0003_rename_question_type_terms'),
('taxonomies', '0002_auto_20150716_2018')
]
operations = [
migrations.RunPython(create_item_types)
]
...@@ -31,7 +31,7 @@ class ItemTable(tables.Table): ...@@ -31,7 +31,7 @@ class ItemTable(tables.Table):
body = tables.Column(verbose_name=_('Message')) body = tables.Column(verbose_name=_('Message'))
category = tables.TemplateColumn( category = tables.TemplateColumn(
template_name='hid/categories_column.html', template_name='hid/categories_column.html',
accessor='terms.0.name', accessor='terms'
) )
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
...@@ -40,9 +40,13 @@ class ItemTable(tables.Table): ...@@ -40,9 +40,13 @@ class ItemTable(tables.Table):
def render_category(self, record, value): def render_category(self, record, value):
Template = loader.get_template('hid/categories_column.html') Template = loader.get_template('hid/categories_column.html')
selected = []
for term in value:
if term['taxonomy'] == 'ebola-questions':
selected.append(term['name'])
ctx = { ctx = {
'categories': self.categories, 'categories': self.categories,
'category': value, 'selected': selected,
'record': record 'record': record
} }
......
{% if categories %} {% if categories %}
<select name="category-{{ record.id }}" class="form-control"> <select name="category-{{ record.id }}" class="form-control">
{% if not category %} {% if not selected %}
<option value="" selected="selected">---------</option> <option value="" selected="selected">---------</option>
{% endif %} {% endif %}
{% for cat in categories %} {% for cat in categories %}
<option value="{{ cat.0 }}"{% if category == cat.0 %} selected="selected"{% endif %}>{{ cat.1 }}</option> <option value="{{ cat.0 }}"{% if cat.0 in selected %} selected="selected"{% endif %}>{{ cat.1 }}</option>
{% endfor %} {% endfor %}
</select> </select>
{% endif %} {% endif %}
...@@ -11,6 +11,6 @@ def test_list_taxonomies_returns_taxonomies(): ...@@ -11,6 +11,6 @@ def test_list_taxonomies_returns_taxonomies():
taxonomies = transport.taxonomies.list() taxonomies = transport.taxonomies.list()
assert len(taxonomies) == 1 names = [t['name'] for t in taxonomies]
[taxonomy] = taxonomies
assert taxonomy['name'] == 'Ebola Questions' assert taxonomy.name in names
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