Пример #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}}))