class XFormPhoneMetadata(jsonobject.JsonObject): """ Metadata of an xform, from a meta block structured like: <Meta> <timeStart /> <timeEnd /> <instanceID /> <userID /> <deviceID /> <username /> <!-- CommCare extension --> <appVersion /> <location /> </Meta> See spec: https://bitbucket.org/javarosa/javarosa/wiki/OpenRosaMetaDataSchema username is not part of the spec but included for convenience """ timeStart = jsonobject.DateTimeProperty() timeEnd = jsonobject.DateTimeProperty() instanceID = jsonobject.StringProperty() userID = jsonobject.StringProperty() deviceID = jsonobject.StringProperty() username = jsonobject.StringProperty() appVersion = jsonobject.StringProperty() location = GeoPointProperty()
def format_data(self, data): column_name = self.column_id for row in data: try: row[column_name] = '{g.latitude} {g.longitude} {g.altitude} {g.accuracy}'.format( g=GeoPointProperty().wrap(row[column_name])) except BadValueError: row[column_name] = u'{} ({})'.format(row[column_name], _('Invalid Location'))
class Metadata(DocumentSchema): """ Metadata of an xform, from a meta block structured like: <Meta> <timeStart /> <timeEnd /> <instanceID /> <userID /> <deviceID /> <deprecatedID /> <username /> <!-- CommCare extension --> <appVersion /> <location /> </Meta> See spec: https://bitbucket.org/javarosa/javarosa/wiki/OpenRosaMetaDataSchema username is not part of the spec but included for convenience """ timeStart = DateTimeProperty() timeEnd = DateTimeProperty() instanceID = StringProperty() userID = StringProperty() deviceID = StringProperty() deprecatedID = StringProperty() username = StringProperty() appVersion = StringProperty() location = GeoPointProperty() @property def commcare_version(self): from corehq.apps.receiverwrapper.util import get_commcare_version_from_appversion_text from distutils.version import LooseVersion version_text = get_commcare_version_from_appversion_text( self.appVersion) if version_text: return LooseVersion(version_text)
def transform_xform_for_elasticsearch(doc_dict): """ Given an XFormInstance, return a copy that is ready to be sent to elasticsearch, or None, if the form should not be saved to elasticsearch """ doc_ret = copy.deepcopy(doc_dict) if 'meta' in doc_ret['form']: if not is_valid_date(doc_ret['form']['meta'].get('timeEnd', None)): doc_ret['form']['meta']['timeEnd'] = None if not is_valid_date(doc_ret['form']['meta'].get('timeStart', None)): doc_ret['form']['meta']['timeStart'] = None # Some docs have their @xmlns and #text here if isinstance(doc_ret['form']['meta'].get('appVersion'), dict): doc_ret['form']['meta'] = format_form_meta_for_es( doc_ret['form']['meta']) app_version_info = get_app_version_info( doc_ret['domain'], doc_ret.get('build_id'), doc_ret.get('version'), doc_ret['form']['meta'], ) doc_ret['form']['meta'][ 'commcare_version'] = app_version_info.commcare_version doc_ret['form']['meta'][ 'app_build_version'] = app_version_info.build_version try: geo_point = GeoPointProperty().wrap( doc_ret['form']['meta']['location']) doc_ret['form']['meta']['geo_point'] = geo_point.lat_lon except (KeyError, BadValueError): doc_ret['form']['meta']['geo_point'] = None pass try: user_id = doc_ret['form']['meta']['userID'] except KeyError: user_id = None doc_ret['user_type'] = get_user_type(user_id) doc_ret['inserted_at'] = datetime.datetime.utcnow().isoformat() try: case_blocks = extract_case_blocks(doc_ret) except PhoneDateValueError: pass else: for case_dict in case_blocks: for date_modified_key in ['date_modified', '@date_modified']: if not is_valid_date(case_dict.get(date_modified_key, None)): if case_dict.get(date_modified_key) == '': case_dict[date_modified_key] = None else: case_dict.pop(date_modified_key, None) # convert all mapped dict properties to nulls if they are empty strings for object_key in ['index', 'attachment', 'create', 'update']: if object_key in case_dict and not isinstance( case_dict[object_key], dict): case_dict[object_key] = None try: doc_ret["__retrieved_case_ids"] = list( set(case_update_from_block(cb).id for cb in case_blocks)) except CaseGenerationException: doc_ret["__retrieved_case_ids"] = [] if 'backend_id' not in doc_ret: doc_ret['backend_id'] = 'couch' return doc_ret