Beispiel #1
0
    def get(self, request, *args, **kwargs):  # pylint: disable=unused-argument
        """Create a CSV with enrollment codes"""
        order_hash = kwargs["hash"]
        order = get_object_or_404(
            B2BOrder.objects.select_related("program_run"),
            unique_id=order_hash)

        rows = ({
            "url":
            make_checkout_url(
                code=code,
                product_id=order.product_version.text_id,
                run_tag=order.program_run.run_tag
                if order.program_run else None,
            )
        } for code in Coupon.objects.filter(
            versions__payment_version__b2border=order).values_list(
                "coupon_code", flat=True))

        instructions = [
            "Distribute the links below to each of your learners. Additional instructions are available at:",
            '=HYPERLINK("https://xpro.zendesk.com/hc/en-us/articles/360048166292-How-to-I-distribute-my-enrollment-codes-that-I-purchased-in-a-bulk-order-")',
        ]

        return make_csv_http_response(
            csv_rows=rows,
            filename=f"enrollmentcodes-{order_hash}.csv",
            instructions=instructions,
        )
Beispiel #2
0
def test_make_csv_http_response_empty():
    """
    make_csv_http_response should handle empty data sets by returning an empty response
    """
    response = make_csv_http_response(csv_rows=[], filename="empty_filename")
    out_rows = [
        line.split(",") for line in response.content.decode().splitlines()
    ]
    assert out_rows == []
    assert response[
        "Content-Disposition"] == 'attachment; filename="empty_filename"'
    assert response["Content-Type"] == "text/csv"
Beispiel #3
0
def test_make_csv_http_response():
    """
    make_csv_http_response should make a HttpResponse object suitable for serving a CSV file.
    """
    rows = [{"a": "B", "c": "d"}, {"a": "e", "c": "f"}]
    response = make_csv_http_response(csv_rows=rows, filename="test_filename")
    out_rows = [
        line.split(",") for line in response.content.decode().splitlines()
    ]
    assert out_rows == [["a", "c"], ["B", "d"], ["e", "f"]]
    assert response[
        "Content-Disposition"] == 'attachment; filename="test_filename"'
    assert response["Content-Type"] == "text/csv"
Beispiel #4
0
def coupon_code_csv_view(request, version_id):
    """View for returning a csv file of coupon codes"""
    if not (request.user and request.user.is_staff):
        raise PermissionDenied
    coupon_payment_version = get_object_or_404(CouponPaymentVersion, id=version_id)

    return make_csv_http_response(
        csv_rows=(
            {"code": code}
            for code in coupon_payment_version.couponversion_set.values_list(
                "coupon__coupon_code", flat=True
            )
        ),
        filename=f"coupon_codes_{coupon_payment_version.payment.name}.csv",
    )
Beispiel #5
0
def bulk_assignment_csv_view(request, bulk_assignment_id):
    """View for returning a csv file of bulk assigned coupons"""
    if not (request.user and request.user.is_staff):
        raise PermissionDenied
    bulk_assignment = (
        BulkCouponAssignment.objects.filter(id=bulk_assignment_id)
        .prefetch_related(
            "assignments__product_coupon__coupon",
            "assignments__product_coupon__product",
        )
        .first()
    )
    if not bulk_assignment:
        raise Http404

    # It's assumed that the bulk assignment will have the same coupon payment for all of the individual assignments, so
    # use the name value for the first coupon payment for the filename.
    first_assignment = bulk_assignment.assignments.first()
    first_coupon_name = first_assignment.product_coupon.coupon.payment.name
    return make_csv_http_response(
        csv_rows=(
            {
                "email": product_coupon_assignment.email,
                "enrollment_url": make_checkout_url(
                    product_id=product_coupon_assignment.product_coupon.product.id,
                    code=product_coupon_assignment.product_coupon.coupon.coupon_code,
                ),
                "coupon_code": product_coupon_assignment.product_coupon.coupon.coupon_code,
            }
            for product_coupon_assignment in bulk_assignment.assignments.all()
        ),
        filename="Bulk Assign {coupon_name} {formatted_dt}.csv".format(
            coupon_name=first_coupon_name[0:COUPON_NAME_FILENAME_LIMIT],
            formatted_dt=format_datetime_for_filename(bulk_assignment.created_on),
        ),
    )