def to_json(self, data, options=None): options = options or {} domain = data.pop('domain', None) app_id = data.pop('app_id', None) xmlns = data.pop('xmlns', None) api_path = data.pop('api_path', None) assert all([domain, app_id, xmlns, api_path]), [domain, app_id, xmlns, api_path] data = self.to_simple(data, options) data['@odata.context'] = '{}#{}'.format( absolute_reverse(DeprecatedODataFormMetadataView.urlname, args=[domain, app_id]), xmlns ) next_url = data.pop('meta', {}).get('next') if next_url: data['@odata.nextLink'] = '{}{}{}'.format(get_url_base(), api_path, next_url) data['value'] = data.pop('objects') form_export_schema = get_latest_form_export_schema( domain, app_id, 'http://openrosa.org/formdesigner/' + xmlns ) if form_export_schema: export_items = [ item for item in form_export_schema.group_schemas[0].items if isinstance(item, ExportItem) ] def _get_odata_value_by_export_item(item, xform_json): for path_node in item.path: try: xform_json = xform_json[path_node.name] except KeyError: return None return xform_json for i, xform_json in enumerate(data['value']): data['value'][i] = { get_odata_property_from_export_item(item): _get_odata_value_by_export_item(item, xform_json) for item in export_items } data['value'][i]['xform_id'] = xform_json['id'] return json.dumps(data, cls=DjangoJSONEncoder, sort_keys=True)
def get_properties_by_xmlns(domain, app_id, xmlns): complete_xmlns = 'http://openrosa.org/formdesigner/' + xmlns form_export_schema = get_latest_form_export_schema( domain, app_id, complete_xmlns) or FormExportDataSchema.generate_schema_from_builds( domain, app_id, complete_xmlns) if not form_export_schema.group_schemas: return set() else: export_items = [ item for item in form_export_schema.group_schemas[0].items if isinstance(item, ExportItem) ] return set([ get_odata_property_from_export_item(item) for item in export_items ]) - {''}
def generate_schema_from_builds(domain, app_id, form_xmlns, force_rebuild=False): """Builds a schema from Application builds for a given identifier :param domain: The domain that the export belongs to :param app_id: The app_id that the export belongs to :param unique_form_id: The unique identifier of the item being exported :returns: Returns a FormExportDataSchema instance """ original_id, original_rev = None, None current_xform_schema = get_latest_form_export_schema(domain, app_id, form_xmlns) if current_xform_schema and not force_rebuild: original_id, original_rev = current_xform_schema._id, current_xform_schema._rev else: current_xform_schema = FormExportDataSchema() app_build_ids = get_built_app_ids_for_app_id( domain, app_id, current_xform_schema.last_app_versions.get(app_id) ) for app_doc in iter_docs(Application.get_db(), app_build_ids): app = Application.wrap(app_doc) xform = app.get_form_by_xmlns(form_xmlns, log_missing=False) if not xform: continue xform = xform.wrapped_xform() xform_schema = FormExportDataSchema._generate_schema_from_xform( xform, app.langs, app.copy_of, app.version, ) current_xform_schema = FormExportDataSchema._merge_schemas(current_xform_schema, xform_schema) current_xform_schema.record_update(app.copy_of, app.version) if original_id and original_rev: current_xform_schema._id = original_id current_xform_schema._rev = original_rev current_xform_schema.domain = domain current_xform_schema.app_id = app_id current_xform_schema.xmlns = form_xmlns current_xform_schema.save() return current_xform_schema
def test_get_latest_form_export_schema_empty(self): schema = get_latest_form_export_schema(self.domain, self.app_id, 'not-found') self.assertEqual(schema, None)
def test_get_latest_form_export_schema(self): schema = get_latest_form_export_schema(self.domain, self.app_id, self.xmlns) self.assertEqual(schema._id, self.form_schema._id)