def editLinkByID (id: str, newLink): #TODO normalize if (isinstance(newLink, Link)): dbconnection.updateLinkById(id, newLink) else: link = Link() link.fromJSON(newLink) dbconnection.updateLinkById(id, link)
def getLinksContainingWord(field, word): current_app.logger.info("call to getLinksContainingWord") regularExpression = "^.*" + word + ".*$" regularExpression = re.compile(regularExpression, re.IGNORECASE) query = {field: {"$regex": regularExpression}} entities = docsCollection.find(query) entitiesFormatted = [] for entity in entities: link = Link() link.fromJSON(entity) entitiesFormatted.append(link) return entitiesFormatted
def getLinkByField(field, value): if field == "_id": query = {field: ObjectId(value)} else: query = {field: value} entity = docsCollection.find_one(query) if entity: link = Link() link.fromJSON(entity) return link else: return False
def getLinksByField(field, value): if field == "_id": query = {field: ObjectId(value)} else: query = {field: value} entities = docsCollection.find(query) entitiesFormatted = [] for entity in entities: #current_app.logger.info("prueba") link = Link() link.fromJSON(entity) entitiesFormatted.append(link) return entitiesFormatted
def editLinkPartially (title, newLink): #TODO rewrite for query by id, check if needed editionLink = getLink(title) formattedNewLink = Link() formattedNewLink.fromJSON(newLink) if formattedNewLink.alias: editionLink.alias = formattedNewLink.alias if formattedNewLink.operation: editionLink.operation = formattedNewLink.operation if formattedNewLink.links: editionLink.links = formattedNewLink.links editLinkByID(editionLink.id, editionLink)
def drawLinkBetween2 (title: str, operation: str, source: str, destination: str): #TODO normalize and rewrite to query by id if TextOperations.hasEnoughSlots(operation, 2): links = [source, destination] link = Link(title, operation, links) saveLink(link)
def updateLinkById(id, modifiedLink): query = {"_id": ObjectId(id)} link = modifiedLink.toJSON() entity = docsCollection.update_one(query, {"$set": link}) for innerLink in link["links"]: if not existsLink(innerLink): newLink = Link(innerLink) addLink(newLink)
def updateLink(title, modifiedLink): query = {"alias": title} link = modifiedLink.toJSON() entity = docsCollection.update_one(query, {"$set": link}) for innerLink in link["links"]: if not existsLink(innerLink): newLink = Link(innerLink) addLink(newLink)
def registerSimpleLink(jsonInput: object) -> list: """ Creates and saves a new link to the database, using a json as input. Validates that the json has the required format. The created link will either have just an alias (an empty link), or be complete (alias, plus operation and links). The function returns the created link. Parameters ---------- name : jsonInput A json provided as input, which will be validated against the TextOperations.validateJSON function (an "alias" property is the only requirement). Raises ------ TypeError If jsonInput parameter doesn't pass the validation, an error is raised. """ if (TextOperations.validateJSON(jsonInput)): linkAlias = jsonInput["alias"] link = Link(linkAlias) if propertyExists(jsonInput, "text"): link.fromUnformattedText(jsonInput["text"]) createdLinks = saveLink(link) return createdLinks else: raise TypeError("Inadequate json input format. JSON input must have a name attribute, at least.")
def addLink(link): jsonLink = link.toJSON() createdLinks = [] query = {"alias": link.alias} if not docsCollection.find_one(query): x = docsCollection.insert_one(jsonLink) createdLinks.append(jsonify(jsonLink)) for linkInside in link.links: if not existsLink(linkInside): newLink = Link(linkInside) createdLinks.append(addLink(newLink)) return createdLinks
def registerSimpleEntry (title: str, text: str): #TODO rewrite and normalize newEntry = Link(title, text, [], "") saveLink(newEntry)
def groupLinks (title: str, operation: str, titles: list): #TODO normalize and raise exception if conditions aren't fulfilled and rewrite to query by id if TextOperations.hasEnoughSlots(operation, len(titles)): link = Link(title, operation, titles) saveLink(link)
def enumerateLinks (title: str, operationBeginning: str, titles: list): #TODO normalize and rewrite to query by id operation = TextOperations.generateOperation(operationBeginning, len(titles)) link = Link (title, operation, titles) saveLink(link)
def getLink(title): query = {"alias": title} entity = docsCollection.find_one(query) link = Link() link.fromJSON(entity) return link
def registerLink (title: str, operation: str, entitiesLinked:list = []): #TODO normalize and check if needed newLink = Link(title, operation, entitiesLinked) saveLink (newLink)
def registerLinkFromUnformattedText(title: str, text: str): #TODO normalize and check if needed newEntry = Link(title, text, [], "") newEntry.fromUnformattedText(text) saveLink(newEntry)
def registerEmptyEntry(title: str): #TODO normalize and check if needed newEntry = Link(title) saveLink(newEntry)