Skip to content

Commit

Permalink
Add support for multiple-language labels
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverroick committed Mar 15, 2017
1 parent 8cb48d7 commit dcf7e07
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 5 deletions.
45 changes: 44 additions & 1 deletion jsonattrs/mixins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
from jsonattrs.models import Schema
from collections import defaultdict
import itertools


def template_xlang_labels(attr):
try:
labels = ['data-label-{}="{}"'.format(k, v)
for k, v in sorted(attr.items())]
return ' '.join(labels)
except AttributeError:
return ''


def xlang_choices(attr, value):
if attr.choices:
try:
xlang_c = dict(zip(attr.choices, attr.choice_labels_xlat))

if isinstance(value, (tuple, list)):
try:
labels = [xlang_c[k].items()
for k in xlang_c if k in value]

xlang_labels = defaultdict(list)
for k, v in list(itertools.chain(*labels)):
xlang_labels[k].append(v)

xlang_labels = {k: ', '.join(sorted(v))
for k, v in xlang_labels.items()}

return template_xlang_labels(xlang_labels)
except AttributeError:
pass

return template_xlang_labels(xlang_c.get(value))

except TypeError:
pass
return ''


class JsonAttrsMixin:
Expand All @@ -11,6 +50,10 @@ def get_context_data(self, *args, **kwargs):

schemas = Schema.objects.from_instance(obj)
attrs = [a for s in schemas for a in s.attributes.all()]
context[field] = [(a.long_name, a.render(obj_attrs.get(a.name, '—')))

context[field] = [(a.long_name,
a.render(obj_attrs.get(a.name, '—')),
template_xlang_labels(a.long_name_xlat),
xlang_choices(a, obj_attrs.get(a.name, '—')))
for a in attrs if not a.omit]
return context
110 changes: 106 additions & 4 deletions tests/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
from . import factories


class XLangLabelsTest(TestCase):
def test_dict(self):
res = mixins.template_xlang_labels({'en': 'Field 1', 'de': 'Feld 1'})
assert 'data-label-en="Field 1"' in res
assert 'data-label-de="Feld 1"' in res

def test_string(self):
assert mixins.template_xlang_labels('Field 1') == ''

def test_none(self):
assert mixins.template_xlang_labels(None) == ''


class JsonAttrsView(mixins.JsonAttrsMixin, TemplateView):
attributes_field = 'attrs'

Expand Down Expand Up @@ -68,7 +81,96 @@ def test_get_context(self):
context = view.get_context_data()
assert len(context['attrs']) == 4

assert context['attrs'][0] == ('Field 1', 'Some value')
assert context['attrs'][1] == ('Field 2', '—')
assert context['attrs'][2] == ('Field 3', 'Choice 1, Choice 3')
assert context['attrs'][3] == ('Field 4', 'Choice 2')
assert context['attrs'][0] == ('Field 1', 'Some value', '', '')
assert context['attrs'][1] == ('Field 2', '—', '', '')
assert context['attrs'][2] == ('Field 3', 'Choice 1, Choice 3', '', '')
assert context['attrs'][3] == ('Field 4', 'Choice 2', '', '')

def test_get_context_xlang(self):
models.create_attribute_types()
org = factories.OrganizationFactory.create()
project = factories.ProjectFactory.create(organization=org)
content_type = ContentType.objects.get(
app_label='tests', model='party')

schema1 = models.Schema.objects.create(
content_type=content_type,
selectors=(org.id, project.id),
default_language='en')

models.Attribute.objects.create(
schema=schema1,
name='field_1',
long_name={'en': 'Field 1', 'de': 'Feld 1'},
attr_type=models.AttributeType.objects.get(name='text'),
index=0
)
models.Attribute.objects.create(
schema=schema1,
name='field_2',
long_name={'en': 'Field 2', 'de': 'Feld 2'},
attr_type=models.AttributeType.objects.get(name='text'),
index=1
)
models.Attribute.objects.create(
schema=schema1,
name='field_3',
long_name={'en': 'Field 3', 'de': 'Feld 3'},
attr_type=models.AttributeType.objects.get(name='select_multiple'),
choices=['one', 'two', 'three'],
choice_labels=[{'en': 'Choice 1', 'de': 'Wahl 1'},
{'en': 'Choice 2', 'de': 'Wahl 2'},
{'en': 'Choice 3', 'de': 'Wahl 3'}],
index=2,
)
models.Attribute.objects.create(
schema=schema1,
name='field_4',
long_name={'en': 'Field 4', 'de': 'Feld 4'},
attr_type=models.AttributeType.objects.get(name='select_one'),
choices=['one', 'two', 'three'],
choice_labels=[{'en': 'Choice 1', 'de': 'Wahl 1'},
{'en': 'Choice 2', 'de': 'Wahl 2'},
{'en': 'Choice 3', 'de': 'Wahl 3'}],
index=3,
)

party = factories.PartyFactory.create(
project=project,
attrs={'field_1': 'Some value',
'field_3': ['one', 'three'],
'field_4': 'two'}
)

view = JsonAttrsView()
view.object = party
context = view.get_context_data()
assert len(context['attrs']) == 4

field_1 = context['attrs'][0]
assert field_1[0] == 'Field 1'
assert field_1[1] == 'Some value'
assert 'data-label-en="Field 1"' in field_1[2]
assert 'data-label-de="Feld 1"' in field_1[2]

field_2 = context['attrs'][1]
assert field_2[0] == 'Field 2'
assert field_2[1] == '—'
assert 'data-label-en="Field 2"' in field_2[2]
assert 'data-label-de="Feld 2"' in field_2[2]

field_3 = context['attrs'][2]
assert field_3[0] == 'Field 3'
assert field_3[1] == 'Choice 1, Choice 3'
assert 'data-label-en="Field 3"' in field_3[2]
assert 'data-label-de="Feld 3"' in field_3[2]
assert 'data-label-en="Choice 1, Choice 3"' in field_3[3]
assert 'data-label-de="Wahl 1, Wahl 3"' in field_3[3]

field_4 = context['attrs'][3]
assert field_4[0] == 'Field 4'
assert field_4[1] == 'Choice 2'
assert 'data-label-en="Field 4"' in field_4[2]
assert 'data-label-de="Feld 4"' in field_4[2]
assert 'data-label-en="Choice 2"' in field_4[3]
assert 'data-label-de="Wahl 2"' in field_4[3]

0 comments on commit dcf7e07

Please sign in to comment.