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