Repository: City-of-Helsinki/linkedevents

Issue:
Add hel.fi event languages (as in_language?)



Discussion:

Resolving patch:
diff --git a/events/api.py b/events/api.py
--- a/events/api.py
+++ b/events/api.py
@@ -526,10 +526,14 @@ class Meta:
 
 class LanguageSerializer(LinkedEventsSerializer):
     view_name = 'language-detail'
+    translation_available = serializers.SerializerMethodField()
 
     class Meta:
         model = Language
 
+    def get_translation_available(self, obj):
+        return obj.id in [language[0] for language in settings.LANGUAGES]
+
 
 class LanguageViewSet(viewsets.ReadOnlyModelViewSet):
     queryset = Language.objects.all()
diff --git a/events/migrations/0010_add_in_languages.py b/events/migrations/0010_add_in_languages.py
new file mode 100644
--- /dev/null
+++ b/events/migrations/0010_add_in_languages.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9 on 2015-12-21 12:32
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.manager
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('events', '0009_auto_20151129_0934'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='event',
+            name='in_language',
+            field=models.ManyToManyField(blank=True, related_name='events', to='events.Language', verbose_name='In language'),
+        ),
+    ]
diff --git a/events/models.py b/events/models.py
--- a/events/models.py
+++ b/events/models.py
@@ -258,6 +258,8 @@ class Event(MPTTModel, BaseModel, SchemalessFieldMixin):
 
     is_recurring_super = models.BooleanField(default=False)
 
+    in_language = models.ManyToManyField(Language, verbose_name=_('In language'), related_name='events', blank=True)
+
     # Custom fields not from schema.org
     keywords = models.ManyToManyField(Keyword)
     audience = models.CharField(verbose_name=_('Audience'), max_length=255, null=True, blank=True)


Test patch:
diff --git a/events/tests/test_event_get.py b/events/tests/test_event_get.py
new file mode 100644
--- /dev/null
+++ b/events/tests/test_event_get.py
@@ -0,0 +1,74 @@
+# -*- coding: utf-8 -*-
+from django.core.urlresolvers import reverse
+import pytest
+from .utils import get, assert_fields_exist
+
+
+# === util methods ===
+
+def get_list(api_client):
+    list_url = reverse('event-list')
+    return get(api_client, list_url)
+
+
+def get_detail(api_client, detail_pk):
+    detail_url = reverse('event-detail', kwargs={'pk': detail_pk})
+    return get(api_client, detail_url)
+
+
+def assert_event_fields_exist(data):
+    fields = (
+        '@context',
+        '@id',
+        '@type',
+        'audience',
+        'created_time',
+        'custom_data',
+        'data_source',
+        'date_published',
+        'description',
+        'end_time',
+        'event_status',
+        'external_links',
+        'headline',
+        'id',
+        'image',
+        'in_language',
+        'info_url',
+        'keywords',
+        'last_modified_by',
+        'last_modified_time',
+        'location',
+        'location_extra_info',
+        'name',
+        'offers',
+        'origin_id',
+        'provider',
+        'publisher',
+        'secondary_headline',
+        'short_description',
+        'start_time',
+        'sub_events',
+        'super_event'
+    )
+    assert_fields_exist(data, fields)
+
+
+# === tests ===
+
+@pytest.mark.django_db
+def test_get_event_list_check_fields_exist(api_client, event):
+    """
+    Tests that event list endpoint returns the correct fields.
+    """
+    response = get_list(api_client)
+    assert_event_fields_exist(response.data['data'][0])
+
+
+@pytest.mark.django_db
+def test_get_event_detail_check_fields_exist(api_client, event):
+    """
+    Tests that event detail endpoint returns the correct fields.
+    """
+    response = get_detail(api_client, event.pk)
+    assert_event_fields_exist(response.data)
diff --git a/events/tests/test_language_get.py b/events/tests/test_language_get.py
new file mode 100644
--- /dev/null
+++ b/events/tests/test_language_get.py
@@ -0,0 +1,73 @@
+# -*- coding: utf-8 -*-
+from django.core.urlresolvers import reverse
+from django.conf import settings
+import pytest
+from events.models import Language
+from .utils import get, assert_fields_exist
+
+
+@pytest.fixture
+def default_languages():
+    languages = []
+    for language in settings.LANGUAGES:
+        languages.append(Language.objects.get_or_create(id=language[0], name=language[1])[0])
+    return languages
+
+
+# === util methods ===
+
+def get_list(api_client):
+    list_url = reverse('language-list')
+    return get(api_client, list_url)
+
+
+def get_detail(api_client, detail_pk):
+    detail_url = reverse('language-detail', kwargs={'pk': detail_pk})
+    return get(api_client, detail_url)
+
+
+def assert_language_fields_exist(data):
+    fields = (
+        'id',
+        'translation_available',
+        'name',
+        '@id',
+        '@context',
+        '@type'
+    )
+    assert_fields_exist(data, fields)
+
+
+# === tests ===
+
+@pytest.mark.django_db
+def test_get_language_list_check_fields_exist(api_client, default_languages):
+    """
+    Tests that language list endpoint returns the correct fields.
+    """
+    response = get_list(api_client)
+    assert_language_fields_exist(response.data['data'][0])
+
+
+@pytest.mark.django_db
+def test_get_languaget_detail_check_fields_exist(api_client, default_languages):
+    """
+    Tests that language detail endpoint returns the correct fields.
+    """
+    response = get_detail(api_client, default_languages[0].pk)
+    assert_language_fields_exist(response.data)
+
+
+@pytest.mark.django_db
+def test_get_language_check_translation_available(api_client, default_languages):
+    """
+    Tests that a default language's translation available is True while an additional language's is False.
+    """
+    Language.objects.get_or_create(id='tlh', name='Klingon')
+
+    response = get_detail(api_client, 'fi')
+    assert response.data['translation_available'] == True
+
+    response = get_detail(api_client, 'tlh')
+    assert response.data['translation_available'] == False
+
diff --git a/events/tests/utils.py b/events/tests/utils.py
--- a/events/tests/utils.py
+++ b/events/tests/utils.py
@@ -36,3 +36,18 @@ def assert_event_data_is_equal(d1, d2):
     links1 = set([comp(link) for link in d1.get('external_links', [])])
     links2 = set([comp(link) for link in d2.get('external_links', [])])
     assert links1 == links2
+
+
+def get(api_client, url):
+    response = api_client.get(url, format='json')
+    assert response.status_code == 200, str(response.content)
+    return response
+
+
+def assert_fields_exist(data, fields):
+    for field in fields:
+        assert field in data
+    assert len(data) == len(fields)
+
+
+