def test_shorten_url(self): config = CMISConfig.get_solo() UrlMapping.objects.create( long_pattern="https://openzaak.utrechtproeftuin.nl/zaken/", short_pattern="https://oz.nl/", config=config, ) long_url = "https://openzaak.utrechtproeftuin.nl/zaken/api/v1/zaakinformatieobjecten/fc345347-3115-4f0a-8808-e392d66e1886" short_url = shrink_url(long_url) self.assertEqual( short_url, "https://oz.nl/api/v1/zaakinformatieobjecten/fc345347-3115-4f0a-8808-e392d66e1886", )
def build_properties(cls, data: dict) -> dict: """Construct property dictionary. The structure of the dictionary is (where ``property_name``, ``property_value`` and ``property_type`` are the name, value and type of the property): .. code-block:: python properties = { "property_name": { "value": property_value, "type": property_type, } } """ config = CMISConfig.objects.get() props = {} for key, value in data.items(): prop_name = mapper(key, type=cls.type_name) if not prop_name: logger.debug("CMIS_ADAPTER: No property name found for key '%s'", key) continue if value is not None: prop_type = get_cmis_type(cls.type_class, key) if ( get_type(cls.type_class, key) == QueriableUrl and settings.CMIS_URL_MAPPING_ENABLED ): value = shrink_url(value) elif isinstance(value, datetime.datetime): value = value.astimezone(pytz.timezone(config.time_zone)).strftime( "%Y-%m-%dT%H:%M:%S.000Z" ) elif isinstance(value, datetime.date): # In CMIS, there is no propertyDate, only propertyDateTime. # So dates need to be in the datetime format value = value.strftime("%Y-%m-%dT00:00:00.000Z") props[prop_name] = {"value": str(value), "type": prop_type} return props
def test_shorten_url_no_mapping(self): long_url = "https://openzaak.utrechtproeftuin.nl/zaken/api/v1/zaakinformatieobjecten/fc345347-3115-4f0a-8808-e392d66e1886" with self.assertRaises(NoURLMappingException): shrink_url(long_url)
def query(self, return_type_name: str, lhs: List[str] = None, rhs: List[str] = None) -> List[CMISBaseObject]: """Perform an SQL query in the DMS :param return_type_name: string, either Folder, Document, Oio or Gebruiksrechten :param lhs: list of strings, with the LHS of the SQL query :param rhs: list of strings, with the RHS of the SQL query :return: type, either Folder, Document, Oio or Gebruiksrechten """ return_type = self.get_return_type(return_type_name) processed_rhs = rhs # Any query that filters based on URL fields needs to be converted to use the short URL version if settings.CMIS_URL_MAPPING_ENABLED and lhs is not None and rhs is not None: processed_rhs = [] # Join all the queries and find which fields are used to filter joined_lhs = " ".join(lhs) column_names = re.findall(r"([a-z]+?:.+?__[a-z]+)", joined_lhs) for index, item_rhs in enumerate(rhs): column_name = column_names[index] property_name = reverse_mapper(column_name, type=return_type_name.lower()) if (property_name is not None and get_type( return_type.type_class, property_name) == QueriableUrl and item_rhs != ""): processed_rhs.append(shrink_url(item_rhs)) else: processed_rhs.append(item_rhs) table = return_type.table where = (" WHERE " + " AND ".join(lhs)) if lhs else "" query = CMISQuery("SELECT * FROM %s%s" % (table, where)) statement = query(*processed_rhs) if processed_rhs else query() soap_envelope = make_soap_envelope( auth=(self.user, self.password), repository_id=self.main_repo_id, statement=statement, cmis_action="query", ) logger.debug(soap_envelope.toprettyxml()) try: soap_response = self.request("DiscoveryService", soap_envelope=soap_envelope.toxml()) # Corsa raises an error if the query retrieves 0 results except CmisRuntimeException as exc: if "objectNotFound" in exc.message: return [] else: raise exc xml_response = extract_xml_from_soap(soap_response) logger.debug(pretty_xml(xml_response)) extracted_data = extract_object_properties_from_xml( xml_response, "query") return [return_type(cmis_object) for cmis_object in extracted_data]
def build_properties(cls, data: dict, new: bool = True) -> dict: """Construct property dictionary. The structure of the dictionary is (where ``property_name``, ``property_value`` and ``property_type`` are the name, value and type of the property): .. code-block:: python properties = { "property_name": { "value": property_value, "type": property_type, } } """ config = CMISConfig.objects.get() props = {} for key, value in data.items(): prop_name = mapper(key, type="document") if not prop_name: logger.debug("CMIS_ADAPTER: No property name found for key '%s'", key) continue if value is not None: prop_type = get_cmis_type(EnkelvoudigInformatieObject, key) if ( settings.CMIS_URL_MAPPING_ENABLED and get_type(EnkelvoudigInformatieObject, key) == QueriableUrl and value != "" ): value = shrink_url(value) elif isinstance(value, datetime.datetime): value = value.astimezone(pytz.timezone(config.time_zone)).strftime( "%Y-%m-%dT%H:%M:%S.000Z" ) elif isinstance(value, datetime.date): # In CMIS, there is no propertyDate, only propertyDateTime. # So dates need to be in the datetime format value = value.strftime("%Y-%m-%dT00:00:00.000Z") elif isinstance(value, bool): value = str(value).lower() props[prop_name] = {"value": str(value), "type": prop_type} # When a Gebruiksrechten object is deleted, the field in the Document needs to be None. elif key == "indicatie_gebruiksrecht": prop_type = get_cmis_type(EnkelvoudigInformatieObject, key) props[prop_name] = {"value": "", "type": prop_type} # For documents that are not new, the uuid shouldn't be written props.pop(mapper("uuid"), None) if new: # increase likelihood of uniqueness of title by appending a random string title, suffix = data.get("titel"), get_random_string() if title is not None: props["cmis:name"] = { "value": f"{title}-{suffix}", "type": get_cmis_type(EnkelvoudigInformatieObject, "name"), } # For new documents, the uuid needs to be set prop_name = mapper("uuid") prop_type = get_cmis_type(EnkelvoudigInformatieObject, "uuid") new_uuid = str(uuid.uuid4()) props[prop_name] = {"value": new_uuid, "type": prop_type} # The identification needs to be set ONLY for newly created documents. # identificatie is immutable once the document is created prop_name = mapper("identificatie") if not props.get(prop_name, {}).get("value"): prop_type = get_cmis_type(EnkelvoudigInformatieObject, "identificatie") props[prop_name] = {"value": new_uuid, "type": prop_type} return props