Example #1
0
def _delete(request, id):
    """
    Controller for '/news/:id'. DELETE news by id
    """
    response = _RESPONSE_BASE.copy()
    n = news.get(id)
    if n is not None:
        n.delete()
    else:
        _add_error(response, "Not found" + id)
    return JsonResponse(response)
Example #2
0
def _update(request, id):
    """
    Controller for '/news/:id'. PUT (update) news by id
    """
    response = _RESPONSE_BASE.copy()
    n = news.get(id)
    response["news"] = {}
    if n is None:
        _add_error(response, "Not found")
        return JsonResponse(response)

    PUT = QueryDict(request.body)
    missing = _check_missing_params(PUT, "title", "language", "content")
    _add_error(response, missing)
    if _has_errors(response):
        return JsonResponse(response)

    n.title = PUT["title"]
    n.language = PUT["language"]
    n.content = PUT["content"]
    n.publishDate = None if "publishDate" not in PUT else PUT["publishDate"]
    n.eventDate = None if "eventDate" not in PUT else PUT["eventDate"]
    dates = _check_date(response, publishDate=n.publishDate, eventDate=n.eventDate)
    if _has_errors(response):
        return JsonResponse(response)

    n.publishDate = dates["publishDate"]
    n.eventDate = dates["eventDate"]
    n.targets = [] if "targets" not in PUT else PUT["targets"]
    n.targets = _check_targets(response, n.targets)
    if _has_errors(response):
        return JsonResponse(response)

    n.update()

    response["news"] = {
        "id": n.id,
        "title": n.title,
        "content": n.content,
        "language": n.language,
        "publishDate": n.publishDate,
        "eventDate": n.eventDate,
        "targets": n.targets,
    }
    return JsonResponse(response)
Example #3
0
def _read(request, id):
    """
    Controller for '/news/:id'. GET news by id
    """
    response = _RESPONSE_BASE.copy()
    n = news.get(id)
    response["news"] = {}
    if n is not None:
        response["news"] = {
            "id": n.id,
            "title": n.title,
            "content": n.content,
            "language": n.language,
            "publishDate": n.publishDate,
            "eventDate": n.eventDate,
            "targets": list(n.targets),
        }
    else:
        _add_error(response, "Not found")
    return JsonResponse(response)