Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional flow_title field to the TextItService #2086

Merged
merged 5 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/restservices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ Adding TextIt
Where:

- ``auth_token`` - The authentication token for the rest service.
- ``flow_title`` - The flow title in textit.
- ``flow_uuid`` - The flow uuid in textit.
- ``contacts`` - The contact used in the flow.
- ``service_url`` - The external url.
Expand All @@ -183,6 +184,7 @@ Where:
{
xform: 9929,
auth_token: "abffbbb8f16f7a1bc75f141b5asdsadafc6d2d7d2b",
flow_title: "test_flow",
flow_uuid: "cf7d7891-a01b-4ca9-9adssd-7baf5f77c741",
contacts: "52d4ff71-4d4e-464c-asda-f0c04cc9e66d"
id: 236,
Expand Down
7 changes: 6 additions & 1 deletion onadata/apps/main/models/meta_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from onadata.libs.utils.cache_tools import XFORM_METADATA_CACHE, safe_delete
from onadata.libs.utils.common_tags import (GOOGLE_SHEET_DATA_TYPE, TEXTIT,
XFORM_META_PERMS)
XFORM_META_PERMS, TEXTIT_DETAILS)

CHUNK_SIZE = 1024
INSTANCE_MODEL_NAME = "instance"
Expand Down Expand Up @@ -414,6 +414,11 @@ def textit(content_object, data_value=None):

return obj and obj.data_value

@staticmethod
def textit_flow_details(content_object, data_value: str = ""):
data_type = TEXTIT_DETAILS
return unique_type_for_form(content_object, data_type, data_value)

@property
def is_linked_dataset(self):
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def _create_textit_service(self):
'id': rs.pk,
'xform': self.xform.pk,
'active': True,
'inactive_reason': ''
'inactive_reason': '',
'flow_title': ''
}
response.data.pop('date_modified')
response.data.pop('date_created')
Expand Down Expand Up @@ -106,7 +107,8 @@ def test_retrieve_textit_services(self):
'id': _id,
'xform': self.xform.pk,
'active': True,
'inactive_reason': ''
'inactive_reason': '',
'flow_title': ''
}
response.data.pop('date_modified')
response.data.pop('date_created')
Expand Down Expand Up @@ -140,14 +142,34 @@ def test_update(self):
"xform": self.xform.pk,
"auth_token": "sadsdfhsdf",
"flow_uuid": "sdfskhfskdjhfs",
"contacts": "ksadaskjdajsda"
"contacts": "ksadaskjdajsda",
"flow_title": "test-flow"
}

request = self.factory.put('/', data=post_data, **self.extra)
response = self.view(request, pk=rest.pk)

self.assertEquals(response.status_code, 200)
self.assertEquals(response.data['name'], "textit")
self.assertEqual(response.data['flow_title'], 'test-flow')
metadata_count = MetaData.objects.count()

# Flow title can be updated
put_data = {
'flow_title': 'new-name',
'xform': self.xform.pk,
'name': 'textit',
'service_url': 'https://textit.io',
'auth_token': 'sadsdfhsdf',
'flow_uuid': 'sdfskhfskdjhfs',
'contacts': 'ksadaskjdajsda',
}
request = self.factory.put('/', data=put_data, **self.extra)
response = self.view(request, pk=rest.pk)

self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.data['flow_title'], 'new-name')
self.assertEqual(MetaData.objects.count(), metadata_count)

def test_update_with_errors(self):
rest = self._create_textit_service()
Expand Down
5 changes: 4 additions & 1 deletion onadata/libs/models/textit_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TextItService(object):
# pylint: disable=R0913
def __init__(self, xform, service_url=None, name=None, auth_token=None,
flow_uuid=None, contacts=None,
pk=None):
pk=None, flow_title: str = ""):
self.pk = pk # pylint: disable=C0103
self.xform = xform
self.auth_token = auth_token
Expand All @@ -35,6 +35,7 @@ def __init__(self, xform, service_url=None, name=None, auth_token=None,
self.date_modified = None
self.active = True
self.inactive_reason = ""
self.flow_title = flow_title

def save(self):
"""
Expand Down Expand Up @@ -68,6 +69,8 @@ def save(self):
self.contacts)

MetaData.textit(self.xform, data_value=data_value)
MetaData.textit_flow_details(
self.xform, data_value=self.flow_title)

if self.xform.is_merged_dataset:
for xform in self.xform.mergedxform.xforms.all():
Expand Down
13 changes: 11 additions & 2 deletions onadata/libs/serializers/textit_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
from onadata.apps.main.models.meta_data import MetaData
from onadata.libs.models.textit_service import TextItService
from onadata.libs.serializers.fields.xform_field import XFormField
from onadata.libs.utils.common_tags import TEXTIT_DETAILS


class TextItSerializer(serializers.Serializer):
id = serializers.IntegerField(source='pk', read_only=True)
xform = XFormField()
auth_token = serializers.CharField(max_length=255, required=True)
flow_title = serializers.CharField(max_length=255, default="")
flow_uuid = serializers.CharField(max_length=255, required=True)
contacts = serializers.CharField(max_length=255, required=True)
name = serializers.CharField(max_length=50, required=True)
Expand All @@ -20,9 +22,15 @@ class TextItSerializer(serializers.Serializer):
inactive_reason = serializers.CharField(read_only=True)

def to_representation(self, instance):
meta_data = MetaData.objects.filter(
data_type=TEXTIT_DETAILS, object_id=instance.xform.pk).first()
flow_title = ""
if meta_data:
flow_title = meta_data.data_value
text_it = TextItService(pk=instance.pk, xform=instance.xform,
service_url=instance.service_url,
name=instance.name)
name=instance.name,
flow_title=flow_title)
text_it.date_modified = instance.date_modified
text_it.date_created = instance.date_created
text_it.active = instance.active
Expand All @@ -43,7 +51,8 @@ def update(self, instance, validated_data):
service_url = validated_data.get('service_url', instance.service_url)

instance = TextItService(xform, service_url, name, auth_token,
flow_uuid, contacts, instance.pk)
flow_uuid, contacts, instance.pk,
flow_title=validated_data.get('flow_title'))
instance.save()

return instance
Expand Down
1 change: 1 addition & 0 deletions onadata/libs/utils/common_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
MODE = "mode"

TEXTIT = 'textit'
TEXTIT_DETAILS = 'textit_details'
OSM = 'osm'
SELECT_BIND_TYPE = 'string'
MULTIPLE_SELECT_TYPE = 'select all that apply'
Expand Down