Exemplo n.º 1
0
def create_draw(request):
    """create_draw ws
    """
    # DEPRECATE
    LOG.debug("Received post data: {0}".format(request.POST))

    draw_type = request.POST.get("draw_type")
    if not draw_type:
        return HttpResponseBadRequest("Missing post argument draw_type")

    draw_form = draw_factory.create_form(draw_type, request.POST)
    try:
        bom_draw = draw_form.build_draw()
    except DrawFormError:
        LOG.info("Form not valid: {0}".format(draw_form.errors))
        return HttpResponseBadRequest("Not valid")
    else:
        bom_draw._id = None  # Ensure we have no id
        set_owner(bom_draw, request)
        MONGO.save_draw(bom_draw)
        LOG.info("Generated draw: {0}".format(bom_draw))
        ga_track_draw(bom_draw, "create_draw")
        #  notify users if any
        if bom_draw.users:
            invite_user(bom_draw.users, bom_draw.pk, bom_draw.owner)
        draw_url = reverse('retrieve_draw', args=(bom_draw.pk, ))
        return JsonResponse({'draw_url': draw_url})
Exemplo n.º 2
0
def display_draw(request, draw_id):
    """Returns the data to display a draw
    Given a draw id, retrieves it and returns the data required to display it
    """
    bom_draw = MONGO.retrieve_draw(draw_id)
    now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
    if bom_draw.check_read_access(request.user):
        generated_results = False
        for result in bom_draw.results:
            if ("publication_datetime" in result and "items" not in result
                    and now > result["publication_datetime"]):
                result["items"] = bom_draw.generate_result()
                generated_results = True
        if generated_results:
            LOG.info(
                "Generated results for draw {} as they were scheduled".format(
                    bom_draw))
            MONGO.save_draw(bom_draw)
        draw_data = bom_draw.__dict__.copy()
        draw_form = draw_factory.create_form(bom_draw.draw_type,
                                             initial=draw_data)
        return render(request, "draws/display_draw.html", {
            "draw": draw_form,
            "bom": bom_draw
        })
    else:
        return render(request, "draws/secure_draw.html", {"bom": bom_draw})
Exemplo n.º 3
0
def display_draw(request, draw_id):
    """Returns the data to display a draw
    Given a draw id, retrieves it and returns the data required to display it
    """
    bom_draw = MONGO.retrieve_draw(draw_id)
    draw_type = draw_factory.get_draw_name(bom_draw.draw_type)
    if bom_draw.check_read_access(request.user):
        prev_draw_data = bom_draw.__dict__.copy()
        draw_form = draw_factory.create_form(draw_type, prev_draw_data)
        return render(request, "draws/display_draw.html", {"draw": draw_form, "bom": bom_draw})
    else:
        return render(request, "draws/secure_draw.html", {"bom": bom_draw})
Exemplo n.º 4
0
def update_draw(request, draw_id):
    """Serves the update of a draw
    @draw_id: pk of the draw to update
    Given the draw details through the POST data, updates the draw.
    If success, redirects to display the view, otherwise, returns
        the form with the errors. It always create a new version
        of the draw. Use ws to update parts of the draw without
        creating a new version
    """
    prev_bom_draw = MONGO.retrieve_draw(draw_id)
    draw_type = draw_factory.get_draw_name(prev_bom_draw.draw_type)
    user_can_write_draw(request.user, prev_bom_draw)

    LOG.debug("Received post data: {0}".format(request.POST))
    draw_form = draw_factory.create_form(draw_type, request.POST)
    if not draw_form.is_valid():
        LOG.info("Form not valid: {0}".format(draw_form.errors))
        return render(request, "draws/display_draw.html", {"draw": draw_form, "bom": prev_bom_draw})
    else:
        bom_draw = prev_bom_draw
        raw_draw = draw_form.cleaned_data
        LOG.debug("Form cleaned data: {0}".format(raw_draw))
        # update the draw with the data coming from the POST
        for key, value in raw_draw.items():
            if key not in ("_id", "pk") and value != "":
                setattr(bom_draw, key, value)
        if not bom_draw.is_feasible():
            LOG.info("Draw {0} is not feasible".format(bom_draw))
            draw_form.add_error(None, _("Draw not feasible"))
            draw_form = draw_factory.create_form(draw_type, bom_draw.__dict__.copy())
            return render(request, "draws/display_draw.html", {"draw": draw_form, "bom": bom_draw})
        else:
            bom_draw.add_audit("DRAW_PARAMETERS")
            # generate a result if a private draw
            if not bom_draw.is_shared:
                bom_draw.toss()

            MONGO.save_draw(bom_draw)
            LOG.info("Updated draw: {0}".format(bom_draw))
            return redirect('retrieve_draw', draw_id=bom_draw.pk)
Exemplo n.º 5
0
def create_draw(request, draw_type, is_public):
    """create_draw view
    @param
    Serves the page to create a draw (empty) form
        and handles the creation of a draw.
    When received a GET request returns an empty form to create a draw
        and with a POST and data attempts to create a draw. If success,
        redirects to the draw, otherwise, returns the form with the errors.
    """

    is_public = is_public or is_public == 'True'

    if request.method == 'GET':
        LOG.debug("Serving view to create a draw: {0}".format(draw_type))
        draw_form = draw_factory.create_form(draw_type)
        return render(request, 'draws/new_draw.html',
                      {"draw": draw_form, "is_public": is_public, "draw_type": draw_type, "default_title": draw_form.DEFAULT_TITLE})
    else:
        LOG.debug("Received post data: {0}".format(request.POST))
        draw_form = draw_factory.create_form(draw_type, request.POST)
        try:
            bom_draw = draw_form.build_draw()
        except DrawFormError:
            return render(request, 'draws/new_draw.html',
                          {"draw": draw_form, "is_public": is_public, "draw_type": draw_type})
        else:
            bom_draw._id = None  # Ensure we have no id
            set_owner(bom_draw, request)
            #  generate a result if a private draw
            if not bom_draw.is_shared:
                bom_draw.toss()

            MONGO.save_draw(bom_draw)
            LOG.info("Generated draw: {0}".format(bom_draw))
            ga_track_draw(bom_draw, "create_draw")
            #  notify users if any
            if bom_draw.users:
                invite_user(bom_draw.users, bom_draw.pk, bom_draw.owner)

            return redirect('retrieve_draw', draw_id=bom_draw.pk)
Exemplo n.º 6
0
def try_draw(request, draw_type):
    """validate the draw
    if request.POST contains "try_draw", generates a result
    """
    LOG.debug("Received post data: {0}".format(request.POST))
    draw_form = draw_factory.create_form(draw_type,request.POST)
    try:
        bom_draw = draw_form.build_draw()
    except DrawFormError:
        LOG.info("Form not valid: {0}".format(draw_form.errors))
        return render(request, 'draws/new_draw.html', {"draw": draw_form, "is_public": True, "draw_type": draw_type})
    else:
        bom_draw.toss()
        return render(request, 'draws/new_draw.html',
                      {"draw": draw_form, "is_public": True, "draw_type": draw_type, "bom": bom_draw})
Exemplo n.º 7
0
def create_draw(request, draw_type, is_shared):
    """create_draw view

    Servers the page to create a draw
    """

    is_shared = is_shared or is_shared == 'True'

    LOG.debug("Serving view to create a draw: {0}".format(draw_type))
    try:
        initial = request.GET.copy()
        initial['is_shared'] = is_shared
        draw_form = draw_factory.create_form(draw_type,
                                             initial=initial)
    except draw_factory.DrawNotRegistered as e:
        return HttpResponseNotFound("Draw type not registered: " + str(e))
    return render(request, 'draws/new_draw.html',
                  {"draw": draw_form, "is_shared": is_shared,
                   "draw_type": draw_type,
                   "default_title": draw_form.DEFAULT_TITLE})
Exemplo n.º 8
0
def validate_draw(request):
    """WS to validate a draw"""
    draw_type = request.POST.get("draw_type")
    if not draw_type:
        return HttpResponseBadRequest("Missing post argument draw_type")

    logger.debug("Received post data: {0}".format(request.POST))
    draw_form = draw_factory.create_form(draw_type, request.POST)
    try:
        _ = draw_form.build_draw()
    except DrawFormError:
        logger.info("Form not valid: {0}".format(draw_form.errors))
        return JsonResponse({
            "is_valid": False,
            "errors": draw_form.errors
        })
    else:
        return JsonResponse({
            "is_valid": True,
        })
Exemplo n.º 9
0
def create_draw(request, draw_type, is_shared):
    """create_draw view

    Servers the page to create a draw
    """

    is_shared = is_shared or is_shared == 'True'

    LOG.debug("Serving view to create a draw: {0}".format(draw_type))
    try:
        initial = request.GET.copy()
        initial['is_shared'] = is_shared
        draw_form = draw_factory.create_form(draw_type, initial=initial)
    except draw_factory.DrawNotRegistered as e:
        return HttpResponseNotFound("Draw type not registered: " + str(e))
    return render(
        request, 'draws/new_draw.html', {
            "draw": draw_form,
            "is_shared": is_shared,
            "draw_type": draw_type,
            "default_title": draw_form.DEFAULT_TITLE
        })
Exemplo n.º 10
0
def display_draw(request, draw_id):
    """Returns the data to display a draw
    Given a draw id, retrieves it and returns the data required to display it
    """
    bom_draw = MONGO.retrieve_draw(draw_id)
    now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
    if bom_draw.check_read_access(request.user):
        generated_results = False
        for result in bom_draw.results:
            if ("publication_datetime" in result and "items" not in result
                    and now > result["publication_datetime"]):
                result["items"] = bom_draw.generate_result()
                generated_results = True
        if generated_results:
            LOG.info("Generated results for draw {} as they were scheduled"
                    .format(bom_draw))
            MONGO.save_draw(bom_draw)
        draw_data = bom_draw.__dict__.copy()
        draw_form = draw_factory.create_form(bom_draw.draw_type, initial=draw_data)
        return render(request, "draws/display_draw.html",
                      {"draw": draw_form, "bom": bom_draw})
    else:
        return render(request, "draws/secure_draw.html", {"bom": bom_draw})