Пример #1
0
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))
Пример #2
0
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."}))
Пример #3
0
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}))
Пример #4
0
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))