def testGetObjectURI(self): o1 = ParaObject() self.assertEqual(o1.getObjectURI(), "/sysprop") o1.type = "dog" self.assertEqual(o1.getObjectURI(), "/dog") o1.id = "123" self.assertEqual(o1.getObjectURI(), "/dog/123") o2 = ParaObject(id_="123 56", type_="dog 2") self.assertEqual(o2.getObjectURI(), "/dog%202/123%2056")
def getChildren(self, obj: ParaObject, type2: str, field: str, term: str, pager: Pager = None): """ Returns all child objects linked to this object. @param obj: the object to execute this method on @param type2: the type of children to look for @param field: the field name to use as filter @param term: the field value to use as filter @param pager: a Pager @return: a list of ParaObject in a one-to-many relationship with this object """ if not obj or not obj.id or not type2: return [] params = self.pagerToParams(pager) params["childrenonly"] = "true" if field: params["field"] = field if term: params["term"] = term url = obj.getObjectURI() + "/links/" + self.urlenc(type2) return self.getItems(self.getEntity(self.invokeGet(url, params)), pager=pager)
def delete(self, obj: ParaObject): """ Deletes an object permanently. @param obj: the object """ if obj: self.invokeDelete(obj.getObjectURI())
def deleteChildren(self, obj: ParaObject, type2: str): """ Deletes all child objects permanently. @param obj: the object to execute this method on @param type2: the children's type. """ params = {"childrenonly": "true"} url = obj.getObjectURI() + "/links/" + self.urlenc(type2) self.invokeDelete(url, params)
def update(self, obj: ParaObject): """ Updates an object permanently. Supports partial updates. @param obj: the object to update @return: updated object """ if not obj: return None return self.getEntity(self.invokePatch(obj.getObjectURI(), obj.jsonSerialize()), False)
def unlinkAll(self, obj: ParaObject): """ Unlinks all objects that are linked to this one. Deletes all Linker objects. Only the links are deleted. Objects are left untouched. @param obj: the object to execute this method on """ if not obj or not obj.id: return url = obj.getObjectURI() + "/links" self.invokeDelete(url)
def voteDown(self, obj: ParaObject, voterid: str): """ Downvote an object and register the vote in DB. @param obj: the object to receive -1 votes @param voterid: the userid of the voter @return: true if vote was successful """ if not obj or not voterid: return False res = self.getEntity(self.invokePatch(obj.getObjectURI(), json.dumps({"_votedown": voterid}))) return True if res else False
def unlink(self, obj: ParaObject, type2: str, id2: str): """ Unlinks an object from this one. Only a link is deleted. Objects are left untouched. @param obj: the object to execute this method on @param type2: the other type @param id2: the other id """ if not obj or not obj.id or not type2 or not id2: return url = obj.getObjectURI() + "/links/" + self.urlenc(type2) + "/" + self.urlenc(id2) self.invokeDelete(url)
def getLinkedObjects(self, obj: ParaObject, type2: str, pager: Pager = None): """ Returns all objects linked to the given one. Only applicable to many-to-many relationships. @param obj: the object to execute this method on @param type2: type of linked objects to search for @param pager: a Pager @return: a list of linked objects """ if not obj or not obj.id or not type2: return [] url = obj.getObjectURI() + "/links/" + self.urlenc(type2) return self.getItems(self.getEntity(self.invokeGet(url, self.pagerToParams(pager))), pager=pager)
def link(self, obj: ParaObject, id2: str): """ Links an object to this one in a many-to-many relationship. Only a link is created. Objects are left untouched. The type of the second object is automatically determined on read. @param obj: the object to execute this method on @param id2: link to the object with this id @return: the id of the Linker object that is created """ if not obj or not obj.id or not id2: return url = obj.getObjectURI() + "/links/" + self.urlenc(id2) return self.getEntity(self.invokePost(url))
def isLinked(self, obj: ParaObject, type2: str, id2: str): """ Checks if this object is linked to another. @param obj: the object to execute this method on @param type2: type of linked objects to search for @param id2: the other id @return: true if the two are linked """ if not obj or not obj.id or not type2 or not id2: return False url = obj.getObjectURI() + "/links/" + self.urlenc(type2) + "/" + self.urlenc(id2) res = self.getEntity(self.invokeGet(url)) return True if res else False
def countLinks(self, obj: ParaObject, type2: str): """ Count the total number of links between this object and another type of object. @param obj: the object to execute this method on @param type2: the other type of object @return: the number of links for the given object """ if not obj or not obj.id or not type2: return 0 pager = Pager() url = obj.getObjectURI() + "/links/" + self.urlenc(type2) self.getItems(self.getEntity(self.invokeGet(url, {"count": "true"})), pager=pager) return pager.count
def create(self, obj: ParaObject): """ Persists an object to the data store. If the object's type and id are given, then the request will be a {@code PUT} request and any existing object will be overwritten. @param obj: the domain object @return: the same object with assigned id or null if not created. """ if not obj: return None if obj.id and obj.type: return self.getEntity(self.invokePut(obj.getObjectURI(), obj.jsonSerialize()), False) else: return self.getEntity(self.invokePost(self.urlenc(obj.type), obj.jsonSerialize()), False)
def countChildren(self, obj: ParaObject, type2: str): """ Count the total number of child objects for this object. @param obj: the object to execute this method on @param type2: the type of the other object @return: the number of links """ if not obj or not obj.id or not type2: return 0 params = {"count": "true", "childrenonly": "true"} pager = Pager() url = obj.getObjectURI() + "/links/" + self.urlenc(type2) self.getItems(self.getEntity(self.invokeGet(url, params)), pager=pager) return pager.count
def findChildren(self, obj: ParaObject, type2: str, query: str, pager: Pager = None): """ Search through all child objects. Only searches child objects directly connected to this parent via the {@code parentid} field. @param obj: the object to execute this method on @param type2: the type of children to look for @param query: a query string @param pager: a Pager @return: a list of ParaObject in a one-to-many relationship with this object """ if not obj or not obj.id or not type2: return [] params = self.pagerToParams(pager) params["childrenonly"] = "true" params["q"] = query url = obj.getObjectURI() + "/links/" + self.urlenc(type2) return self.getItems(self.getEntity(self.invokeGet(url, params)), pager=pager)
def findLinkedObjects(self, obj: ParaObject, type2: str, field: str = "name", query: str = "*", pager: Pager = None): """ Searches through all linked objects in many-to-many relationships. @param obj: the object to execute this method on @param type2: type of linked objects to search for @param field: the name of the field to target (within a nested field "nstd") @param query: a query string @param pager: a Pager @return: a list of linked objects """ if not obj or not obj.id or not type2: return [] params = self.pagerToParams(pager) params["field"] = field params["q"] = query url = obj.getObjectURI() + "/links/" + self.urlenc(type2) return self.getItems(self.getEntity(self.invokeGet(url, params)), pager=pager)