示例#1
0
def getNewCatalogFrontend():
    """Gets the news catalog. request.args:
    page: mandatory, page to show
    lang: mandatory, language
    search: optional, search criteria (searches in titles and texts)
    filterbylabel: optional, comma-separated list of labels ID to filter with

    TODO: habría que traerse el criterio de ordenación de la base de
    datos junto a los ID para ordenar antes de pedir todos los datos finalmente
    """
    m = NewModel()
    search = request.args["search"] if "search" in request.args else None
    lang = request.args["lang"]
    filterByLabel = request.args[
        "filterbylabel"] if "filterbylabel" in request.args else None
    page = int(request.args["page"])
    notLang = "en" if lang == "es" else "es"

    try:
        if search:
            ids = m.searchNewsByFeatures(search, True).union(
                m.searchNewsByLabel(search, True))
        else:
            ids = m.searchNewsByFeatures("", True)
        if filterByLabel:
            ids = ids.intersection(m.filterByLabels(lang, filterByLabel))

        out = []
        for i in list(ids):
            newsDetails = m.getNewDetails(i)
            labels = m.getLabelsForNew(i, lang)
            newsDetails["labels"] = labels
            newsDetails.pop("section_" + notLang)
            newsDetails.pop("text_" + notLang)
            newsDetails.pop("title_" + notLang)
            newsDetails.pop("url_" + notLang)
            newsDetails["title"] = newsDetails.pop("title_" + lang)
            newsDetails["text"] = newsDetails.pop("text_" + lang)
            newsDetails["url"] = newsDetails.pop("url_" + lang)
            newsDetails["section"] = newsDetails.pop("section_" + lang)
            newsDetails["time"] = str(
                newsDetails["publishing_date"].isoformat())
            newsDetails.pop("publishing_date")
            out.append(newsDetails)

        sortedOut = sorted(out, key=itemgetter("new_time"), reverse=True)

        return(jsonify({"results": {"page": page, "listSize": len(out), \
                                    "data": sortedOut[cons.frontend["newsCatalogPageSize"]*page:\
                                                      cons.frontend["newsCatalogPageSize"]*(page+1)]}}))
    except ElcanoError as e:
        return (jsonify(e.dict()))
示例#2
0
def getNewCatalogFrontend():
    """Gets the news catalog. request.args:
    page: mandatory, page to show
    lang: mandatory, language
    search: optional, search criteria (searches in titles and texts)
    filterbylabel: optional, comma-separated list of labels ID to filter with

    TODO: habría que traerse el criterio de ordenación de la base de
    datos junto a los ID para ordenar antes de pedir todos los datos finalmente
    """
    m = NewModel()
    search = request.args["search"] if "search" in request.args else None
    lang = request.args["lang"]
    filterByLabel = request.args["filterbylabel"] if "filterbylabel" in request.args else None
    page = int(request.args["page"])
    notLang = "en" if lang=="es" else "es"

    try:
        if search:
            ids = m.searchNewsByFeatures(search, True).union(m.searchNewsByLabel(search, True))
        else:
            ids = m.searchNewsByFeatures("", True)
        if filterByLabel:
            ids = ids.intersection(m.filterByLabels(lang, filterByLabel))

        out=[]
        for i in list(ids):
            newsDetails = m.getNewDetails(i)
            labels = m.getLabelsForNew(i, lang)
            newsDetails["labels"] = labels
            newsDetails.pop("section_"+notLang)
            newsDetails.pop("text_"+notLang)
            newsDetails.pop("title_"+notLang)
            newsDetails.pop("url_"+notLang)
            newsDetails["title"]=newsDetails.pop("title_"+lang)
            newsDetails["text"]=newsDetails.pop("text_"+lang)
            newsDetails["url"]=newsDetails.pop("url_"+lang)
            newsDetails["section"] = newsDetails.pop("section_"+lang)
            newsDetails["time"] = str(newsDetails["publishing_date"].isoformat())
            newsDetails.pop("publishing_date")
            out.append(newsDetails)

        sortedOut = sorted(out, key=itemgetter("new_time"), reverse=True)

        return(jsonify({"results": {"page": page, "listSize": len(out), \
                                    "data": sortedOut[cons.frontend["newsCatalogPageSize"]*page:\
                                                      cons.frontend["newsCatalogPageSize"]*(page+1)]}}))
    except ElcanoError as e:
        return(jsonify(e.dict()))
示例#3
0
def getNewCatalog():
    """Gets the news catalog. request.args:
    page: mandatory, page to show
    search: optional, search criteria (searches in titles, texts, and labels)
    """
    m = NewModel()
    search = request.args["search"] if "search" in request.args else None
    page = int(request.args["page"])

    if search:
        ids = m.searchNewsByFeatures(search, False).union(
            m.searchNewsByLabel(search, False))
    else:
        ids = m.searchNewsByFeatures("", False)

    news = []
    for new in ids:
        out = dict()
        d = m.getNewDetails(new)
        if d["title_en"] and d["text_en"] and d["url_en"]:
            out["english"] = True
        else:
            out["english"] = False
        if d["title_es"] and d["text_es"] and d["url_es"]:
            out["spanish"] = True
        else:
            out["spanish"] = False
        out["title"] = d["title_es"] if d["title_es"]!=None else \
                       d["title_en"] if d["title_en"]!=None else "Sin título"
        out["time"] = str(d["publishing_date"].isoformat())
        out["published"] = d["published"]
        out["id"] = d["id"]
        news.append(out)

    return(jsonify({"results": {"page": page, "listSize": len(news),
                                "data": sorted(news, key=itemgetter("time"), reverse=True)\
                                [cons.backend["newsCatalogPageSize"]*page:\
                                 cons.backend["newsCatalogPageSize"]*(page+1)]}}))
示例#4
0
def getNewCatalog():
    """Gets the news catalog. request.args:
    page: mandatory, page to show
    search: optional, search criteria (searches in titles, texts, and labels)
    """
    m = NewModel()
    search = request.args["search"] if "search" in request.args else None
    page = int(request.args["page"])

    if search:
        ids = m.searchNewsByFeatures(search, False).union(m.searchNewsByLabel(search, False))
    else:
        ids = m.searchNewsByFeatures("", False)

    news = []
    for new in ids:
        out = dict()
        d = m.getNewDetails(new)
        if d["title_en"] and d["text_en"] and d["url_en"]:
            out["english"]=True
        else:
            out["english"]=False
        if d["title_es"] and d["text_es"] and d["url_es"]:
            out["spanish"]=True
        else:
            out["spanish"]=False
        out["title"] = d["title_es"] if d["title_es"]!=None else \
                       d["title_en"] if d["title_en"]!=None else "Sin título"
        out["time"] = str(d["publishing_date"].isoformat())
        out["published"] = d["published"]
        out["id"] = d["id"]
        news.append(out)

    return(jsonify({"results": {"page": page, "listSize": len(news),
                                "data": sorted(news, key=itemgetter("time"), reverse=True)\
                                [cons.backend["newsCatalogPageSize"]*page:\
                                 cons.backend["newsCatalogPageSize"]*(page+1)]}}))