Skip to content
Snippets Groups Projects
Commit 5bf9a689 authored by Alice Heaton's avatar Alice Heaton :speech_balloon:
Browse files

Merge branch 'develop' into bar_chart_unknown

parents c92db698 b09fafd8
No related branches found
No related tags found
No related merge requests found
Showing
with 280 additions and 10 deletions
......@@ -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:
transport.items.create(obj)
item = transport.items.create(obj)
transport.items.add_term(item['id'], 'item-types', item_type)
return len(objects)
......@@ -128,11 +129,12 @@ class Importer(object):
file_format = profile.get('format')
skip_header = profile.get('skip_header', False)
item_type = profile.get('type')
rows = self.get_rows_iterator(fobject, file_format)
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):
......
# -*- 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)
]
# -*- 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": "text",
"field": "network_provider"
}
],
"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', '0006_update_rapidpro_config'),
]
operations = [
migrations.RunPython(update_rapidpro_config)
]
......@@ -27,3 +27,10 @@ def test_items_imported(importer):
assert items[0]['body'] == "What is the cuse of ebola?"
assert items[0]['timestamp'] == pytz.utc.localize(
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
from django.utils.translation import ugettext as _
import transport
from ..importer import (
Importer,
SheetProfile, SheetImportException
......@@ -300,3 +302,17 @@ def test_process_rows_ignores_empty_lines(importer):
]
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,11 @@ 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]['timestamp'] == pytz.utc.localize(
datetime.datetime(2015, 4, 19, 21, 35, 20))
assert items[0]['network_provider'] == '8737 (Cellcom)'
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 models, migrations
class Migration(migrations.Migration):
dependencies = [
('data_layer', '0005_message_last_modified'),
]
operations = [
migrations.AddField(
model_name='message',
name='network_provider',
field=models.CharField(max_length=200, blank=True),
),
]
......@@ -21,6 +21,7 @@ class Message(DataLayerModel):
body = models.TextField()
timestamp = models.DateTimeField(null=True)
terms = models.ManyToManyField(Term, related_name="items")
network_provider = models.CharField(max_length=200, blank=True)
def apply_term(self, term):
# TODO: test this
......
# -*- 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):
body = tables.Column(verbose_name=_('Message'))
category = tables.TemplateColumn(
template_name='hid/categories_column.html',
accessor='terms.0.name',
accessor='terms'
)
def __init__(self, *args, **kwargs):
......@@ -40,9 +40,13 @@ class ItemTable(tables.Table):
def render_category(self, record, value):
Template = loader.get_template('hid/categories_column.html')
selected = []
for term in value:
if term['taxonomy'] == 'ebola-questions':
selected.append(term['name'])
ctx = {
'categories': self.categories,
'category': value,
'selected': selected,
'record': record
}
......
{% if categories %}
<select name="category-{{ record.id }}" class="form-control">
{% if not category %}
{% if not selected %}
<option value="" selected="selected">---------</option>
{% endif %}
{% 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 %}
</select>
{% endif %}
......@@ -11,6 +11,6 @@ def test_list_taxonomies_returns_taxonomies():
taxonomies = transport.taxonomies.list()
assert len(taxonomies) == 1
[taxonomy] = taxonomies
assert taxonomy['name'] == 'Ebola Questions'
names = [t['name'] for t in taxonomies]
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