def getDocument(idDocument, lang): """Gets details of a document. Uses URL arguments: idDocument: mandatory, ID of the requested document lang: mandatory, language for document's metadata TODO: revisar los coalesce. Según lo último que hablamos, se íban a rellenar todos los campos o el documento era impublicable. TODO: cambiar los errores a make_response. """ if lang not in cons.lang: return(jsonify(cons.errors["-1"])) out = dict() m = DocumentModel() authorsData = m.getDocumentAuthors(idDocument) authors = [] for a in authorsData: authors.append(helpers.authorHelper(a, lang)) out["authors"] = authors labels = m.getDocumentLabels(idDocument, lang) out["labels"]=labels pdf = m.getDocumentPdf(idDocument, lang) out["pdf"]=pdf doc = m.getDocumentData(idDocument) if doc==None: return make_response(jsonify({"error": "Document not found"}), 404) out["id"] = doc["id_document"] out["time"] = str(doc["publishing_date"].isoformat()) if lang=="en": out["description"] = helpers.coalesce([doc["description_en"], doc["description_es"]]) out["theme"] = helpers.coalesce([doc["theme_en"], doc["theme_es"]]) out["title"] = helpers.coalesce([doc["title_en"], doc["title_es"]]) if doc["link_en"]!=None: out["link"] = doc["link_en"] out["link_lang"] = "en" elif doc["link_es"]!=None: out["link"] = doc["link_es"] out["link_lang"] = "es" else: out["description"] = helpers.coalesce([doc["description_es"], doc["description_en"]]) out["theme"] = helpers.coalesce([doc["theme_es"], doc["theme_en"]]) out["title"] = helpers.coalesce([doc["title_es"], doc["title_en"]]) if doc["link_es"]!=None: out["link"] = doc["link_es"] out["link_lang"] = "es" elif doc["link_en"]!=None: out["link"] = doc["link_en"] out["link_lang"] = "en" return(jsonify(out))
def togglePublish(id_document): """Toggles the published status of a document.""" m = DocumentModel() status = m.togglePublish(id_document) if status==None: return jsonify(cons.errors["-4"]) else: return jsonify({"result" : status})
def createDocument(): data = json.loads(request.data) c = DocumentModel() result = c.createDocument(data) if data['doc_link'] and data['doc_link'].strip(): c.updateDocumentFile(result['id_doc'], data['doc_link']) return jsonify({'id_doc': result['id_doc']})
def downloadPdf(): """Downloads a PDF. request.args: idPdf: mandatory, PDF ID. """ m = DocumentModel() pdf = m.getPdfData(request.args["idPdf"]) return(send_file(backend["mediaFolder"]+"/"+pdf["hash"]+".pdf", \ mimetype="application/pdf", attachment_filename=pdf["pdf_name"]+".pdf", \ as_attachment=True))
def editDocument(id_document): """Edits a document. Gets the id_document in the URL, and upload this JSON: { "title_es": "Test document ES", "title_en": "Test document EN", "labels_es": [{"id": "1", "label": "Label A"}, {"id": "2", "label": "Label B"}, {"id": "4", "label": "Label c"}], "labels_en": [{"id": "1", "label": "Label A"}, {"id": "3", "label": "Label B"}, {"id": "4", "label": "Label c"}], "theme_es": "Theme ES", "theme_en": "Theme EN", "description_es": "Description ES", "description_en": "Description EN", "authors": [{name": "Charles Powell", "position_en": "Director of the Elcano Royal Institute", "position_es": "Director del Real Instituto Elcano","twitter_user": "******"}, {name": "Charles Powell", "position_en": "Director of the Elcano Royal Institute", "position_es": "Director del Real Instituto Elcano","twitter_user": "******"}, {"name": "Charles Powell", "position_en": "Director of the Elcano Royal Institute", "position_es": "Director del Real Instituto Elcano", "twitter_user": ""}], "link_es": "Link ES", "link_en": "Link EN", "pdfs_es": [{"name": "pdf_es_1", "hash": "8383e83838283e838238"}, {"name": "pdf_es_2", "hash": "8383e83838283e838238"}, {"name": "pdf_es_3", "hash": "8383e83838283e838238"}], "pdfs_en": [{"name": "pdf_en_1", "hash": "8383e83838283e838238"}, {"name": "pdf_en_2", "hash": "8383e83838283e838238"}, {"name": "pdf_en_3", "hash": "8383e83838283e838238"}], "time": "20120211" }""" m = DocumentModel() oldDocPdfEn = map(lambda p: p["hash"], m.getDocumentPdf(id_document, "en")) oldDocPdfEs = map(lambda p: p["hash"], m.getDocumentPdf(id_document, "es")) newDocPdfEn = map(lambda p: p["hash"], request.json["pdfs_en"]) newDocPdfEs = map(lambda p: p["hash"], request.json["pdfs_es"]) newEn = [v for v in newDocPdfEn if v not in oldDocPdfEn] newEs = [v for v in newDocPdfEs if v not in oldDocPdfEs] delEn = [v for v in oldDocPdfEn if v not in newDocPdfEn] delEs = [v for v in oldDocPdfEs if v not in newDocPdfEs] for f in newEn: movePdfFile(f) for f in newEs: movePdfFile(f) for f in delEn: m.deletePdf(id_document, f) deletePdfFile(f) for f in delEs: m.deletePdf(id_document, f) deletePdfFile(f) out = m.editDocument(id_document, request.json, newEn, newEs) return(jsonify({"results": {"id": out}}))
def newDocument(): """Creates a document. Needs a JSON in the form: { "title_es": "Test document ES", "title_en": "Test document EN", "labels_es": [{"id": "1", "label": "Label A"}, {"id": "2", "label": "Label B"}, {"id": "4", "label": "Label c"}], "labels_en": [{"id": "1", "label": "Label A"}, {"id": "3", "label": "Label B"}, {"id": "4", "label": "Label c"}], "theme_es": "Theme ES", "theme_en": "Theme EN", "description_es": "Description ES", "description_en": "Description EN", "authors": [{"twitter_user": "******"}, {"twitter_user": "******"}, {"name": "Charles Powell", "position_en": "Director of the Elcano Royal Institute", "position_es": "Director del Real Instituto Elcano"}], "link_es": "Link ES", "link_en": "Link EN", "pdfs_es": [{"name": "pdf_es_1", "hash": "8383e83838283e838238"}, {"name": "pdf_es_2", "hash": "8383e83838283e838238"}, {"name": "pdf_es_3", "hash": "8383e83838283e838238"}], "pdfs_en": [{"name": "pdf_en_1", "hash": "8383e83838283e838238"}, {"name": "pdf_en_2", "hash": "8383e83838283e838238"}, {"name": "pdf_en_3", "hash": "8383e83838283e838238"}], "time": "20120213" }""" m = DocumentModel() j = request.json if [j["title_es"], j["title_en"], j["theme_es"], j["theme_en"], j["description_es"], j["description_en"]]==\ [None, None, None, None, None, None] or \ [j["title_es"], j["title_en"], j["theme_es"], j["theme_en"], j["description_es"], j["description_en"]]==\ ["","","","","",""]: return(jsonify({"Error": "void document"})) if j["pdfs_en"]: for f in j["pdfs_en"]: movePdfFile(f["hash"]) if j["pdfs_es"]: for f in j["pdfs_es"]: movePdfFile(f["hash"]) out = m.createDocument(j) return(jsonify({"id": out}))
def upateDocument(id): data = json.loads(request.data) c = DocumentModel() result = c.updateDocument(id,data) if data['doc_link'] and data['doc_link'].strip(): c.updateDocumentFile(id, data['doc_link']) for file in os.listdir(app.config['DOCUMENT_FILE_FOLDER']): if str(id) == file.split('.')[0]: try: os.remove(app.config['DOCUMENT_FILE_FOLDER'] + file) except OSError: pass break; return jsonify({'id_doc': id})
def deleteDocument(id): c = DocumentModel() document = DocumentModel().getDocumentToEdit(id) if document['doc_link'] and 'http' not in document['doc_link']: try: os.remove(app.config['DOCUMENT_FILE_FOLDER'] + document['doc_link'].split('/documents/')[1]) except OSError: pass for file in os.listdir(app.config['DOCUMENT_COVER_FOLDER']): if ('doc' + str(document['id_doc'])) == file.split('.')[0]: try: os.remove(app.config['DOCUMENT_COVER_FOLDER'] + file) except OSError: pass document = c.deleteDocument(id) return jsonify({'result': True})
def createFile(): file = request.files['document'] id_doc = request.form['id_doc'] filename = id_doc + '.' + file.filename.rsplit('.', 1)[1] # try: # os.remove(app.config['DOCUMENT_FILE_FOLDER'] + filename) # except OSError: # pass for fileAux in os.listdir(app.config['DOCUMENT_FILE_FOLDER']): if id_doc == fileAux.split('.')[0]: try: os.remove(app.config['DOCUMENT_FILE_FOLDER'] + fileAux) except OSError: pass file.save(os.path.join(app.config['DOCUMENT_FILE_FOLDER'], filename)) c = DocumentModel() c.updateDocumentFile(id_doc, '/documents/' + filename) return jsonify({'result': True})
def getDocumentList(): """ Gets a slice of a document list. Uses URL arguments: page: mandatory, page to present search: optional, search criteria orderbyfield: optional, set by default to title orderbyorder: optional, asc / desc, set by default to asc TODO: modify data validation """ m = DocumentModel() out = [] search = request.args["search"] if "search" in request.args else None orderbyfield = "last_edit_time" orderbyorder = "desc" totalSize = m.getDocumentListSize(search=search) docs = m.getDocumentList(request.args["page"], cons.backend["DocumentListLength"], \ search=search, orderByField=orderbyfield, orderByOrder=orderbyorder) for doc in docs: thisDoc = dict() thisDoc["id"] = doc["id"] thisDoc["english"]=False thisDoc["spanish"]=False if doc["title_en"]!="" and doc["theme_en"]!="" and doc["description_en"]!="": thisDoc["english"]=True if doc["title_es"]!="" and doc["theme_es"]!="" and doc["description_es"]!="": thisDoc["spanish"]=True thisDoc["title"] = doc["title"] thisDoc["time"] = str(doc["publishing_date"].isoformat()) thisDoc["edit_time"] = doc["time"].isoformat() thisDoc["published"] = doc["published"] thisDoc["authors"] = m.getDocumentAuthors(doc["id"]) thisDoc["attachments"] = False if len(m.getDocumentPdf(doc["id"]))>0: thisDoc["attachments"] = True thisDoc["links"] = False if doc["link_es"]!=None or doc["link_en"]!=None: thisDoc["links"] = True out.append(thisDoc) return(jsonify({"results": {"listSize": totalSize, "page": int(request.args["page"]), \ "data": out}}))
def getDocument(id_document): """ Gets a document. Just state the id_document in the URL. """ m = DocumentModel() # Get data from Database d = m.getDocumentBackend(id_document) if d: pdfs_es = m.getDocumentPdf(id_document,"es") pdfs_en = m.getDocumentPdf(id_document,"en") authors = m.getDocumentAuthors(id_document) labels_es = m.getDocumentLabels(id_document,"es") labels_en = m.getDocumentLabels(id_document,"en") json = { "id" : d["id_document"], "title_en" : d["title_en"], "title_es" : d["title_es"], "theme_en" : d["theme_en"], "theme_es": d["theme_es"], "description_en" : d["description_en"], "description_es" : d["description_es"], "link_es" : d["link_es"], "link_en" : d["link_en"], "published" : d["published"], "last_edit_id_user" : d["last_edit_id_user"], "last_edit_time" : d["last_edit_time"], "pdfs_es" : pdfs_es, "pdfs_en" : pdfs_en, "labels_es" : labels_es, "labels_en" : labels_en, "authors" : authors, "time": str(d["publishing_date"].isoformat()) } return jsonify(json) else: return(jsonify({"error": "Document not found."}))
def getDocumentCatalog(): """Gets a slice of a document list. request.args: page: mandatory, page to present lang: mandatory, language (en / es) search: optional, search criteria filterbylabel: optional, comma-separated list of labels ID to filter with """ m = DocumentModel() lang = request.args["lang"] page = int(request.args["page"]) fsearch = request.args["search"] if "search" in request.args else None flabels = request.args["filterbylabel"] if "filterbylabel" in request.args else None try: if fsearch: a = m.searchInLabels(lang, fsearch)["id_document"] searchLabels = set(a) if a else set([]) a = m.searchInAuthors(fsearch)["id_document"] authors = set(a) if a else set([]) else: searchLabels = set([]) authors = set([]) if flabels: a = m.filterByLabels(lang, flabels)["id_document"] filterLabels = set(a) if a else set([]) a = m.searchInDocument(lang, fsearch)["id_document"] docs = set(a) if a else set([]) except ElcanoError as e: return jsonify(e.dict()) if flabels: docs = docs.union(searchLabels).union(authors).intersection(filterLabels) else: docs = docs.union(searchLabels).union(authors) out=[] for i in list(docs): try: docDetail = m.getDocumentDetails(lang, i) docAuthors = m.getDocumentAuthors(i) docLabels = m.getDocumentLabels(i, lang) except ElcanoError as e: return jsonify(e.dict()) doc = dict() doc["id"] = docDetail["id_document"] doc["title"] = docDetail["title"] doc["theme"] = docDetail["theme"] doc["time"] = str(docDetail["publishing_date"].isoformat()) doc["authors"] = [] doc["labels"] = [] for a in docAuthors: au = dict() au["id"] = a["id_author"] au["name"] = a["name"] au["twitter_user"] = a["twitter_user"] doc["authors"].append(au) for l in docLabels: la = dict() la["id"] = l["id_label"] la["label"] = l["label"] doc["labels"].append(la) out.append(doc) sortedOut = sorted(out, key=itemgetter("time"), reverse=True) return(jsonify({"results": sortedOut[page*cons.frontend["documentCatalogListSize"]: \ (page*cons.frontend["documentCatalogListSize"])+ \ cons.frontend["documentCatalogListSize"]], \ "listSize": len(sortedOut), "page": page}))
def deleteDocument(id_document): """Deletes a document. Just state the id_document in the URL.""" m = DocumentModel() return(jsonify({"results": {"id": m.deleteDocument(id_document)}}))
def createTag(): data = json.loads(request.data) # Save topic data c = DocumentModel() result = c.createTag(data) return jsonify({'id_tag': result['id_tag']})
def getDocument(idDocument, lang): """Gets details of a document. Uses URL arguments: idDocument: mandatory, ID of the requested document lang: mandatory, language for document's metadata TODO: revisar los coalesce. Según lo último que hablamos, se íban a rellenar todos los campos o el documento era impublicable. TODO: cambiar los errores a make_response. """ if lang not in cons.lang: return (jsonify(cons.errors["-1"])) out = dict() m = DocumentModel() authorsData = m.getDocumentAuthors(idDocument) authors = [] for a in authorsData: authors.append(helpers.authorHelper(a, lang)) out["authors"] = authors labels = m.getDocumentLabels(idDocument, lang) out["labels"] = labels pdf = m.getDocumentPdf(idDocument, lang) out["pdf"] = pdf doc = m.getDocumentData(idDocument) if doc == None: return make_response(jsonify({"error": "Document not found"}), 404) out["id"] = doc["id_document"] out["time"] = str(doc["publishing_date"].isoformat()) if lang == "en": out["description"] = helpers.coalesce( [doc["description_en"], doc["description_es"]]) out["theme"] = helpers.coalesce([doc["theme_en"], doc["theme_es"]]) out["title"] = helpers.coalesce([doc["title_en"], doc["title_es"]]) if doc["link_en"] != None: out["link"] = doc["link_en"] out["link_lang"] = "en" elif doc["link_es"] != None: out["link"] = doc["link_es"] out["link_lang"] = "es" else: out["description"] = helpers.coalesce( [doc["description_es"], doc["description_en"]]) out["theme"] = helpers.coalesce([doc["theme_es"], doc["theme_en"]]) out["title"] = helpers.coalesce([doc["title_es"], doc["title_en"]]) if doc["link_es"] != None: out["link"] = doc["link_es"] out["link_lang"] = "es" elif doc["link_en"] != None: out["link"] = doc["link_en"] out["link_lang"] = "en" return (jsonify(out))