Пример #1
0
def _create(request):
    """
    Controller for '/news/'. POST create new piece of news
    """
    response = _RESPONSE_BASE.copy()
    missing = _check_missing_params(request.POST, "title", "language", "content")
    _add_error(response, missing)
    if _has_errors(response):
        return JsonResponse(response)

    publishDate = None if "publishDate" not in request.POST else request.POST["publishDate"]
    eventDate = None if "eventDate" not in request.POST else request.POST["eventDate"]

    dates = _check_date(response, publishDate=publishDate, eventDate=eventDate)
    if _has_errors(response):
        return JsonResponse(response)

    publishDate = dates["publishDate"]
    eventDate = dates["eventDate"]

    targets = None if "targets" not in request.POST else request.POST["targets"]
    targets = _check_targets(response, targets)
    if _has_errors(response):
        return JsonResponse(response)

    try:
        n = news.create(
            request.POST["title"], request.POST["content"], request.POST["language"], publishDate, eventDate, targets
        )
    except Exception as e:
        _add_error(response, str(e))
        logger.error(e)
        return JsonResponse(response)

    response["news"] = {}
    response["news"][n.id] = {
        "id": n.id,
        "title": n.title,
        "content": n.content,
        "language": n.language,
        "publishDate": n.publishDate,
        "eventDate": n.eventDate,
        "targets": list(n.targets),
    }

    return JsonResponse(response)
Пример #2
0
 def setUp(self):
     self.c = Client()
     self.A = news.create('title', 'test', 'fi', eventDate="2016-03-01T00:00:00+00:00")
     self.B = news.create('title2', 'test2', 'en', None, None, [1])