Allow to add a prefix when generating voucher codes (#518)

This commit is contained in:
Tobias Kunze
2017-06-16 23:21:23 +02:00
committed by Raphael Michel
parent 8e3cc0df0c
commit 8fa490c938
4 changed files with 11 additions and 5 deletions

View File

@@ -13,8 +13,10 @@ from .event import Event
from .items import Item, ItemVariation, Quota
def _generate_random_code():
def _generate_random_code(prefix=None):
charset = list('ABCDEFGHKLMNPQRSTUVWXYZ23456789')
if prefix:
return prefix + get_random_string(length=settings.ENTROPY['voucher_code'], allowed_chars=charset)
return get_random_string(length=settings.ENTROPY['voucher_code'], allowed_chars=charset)

View File

@@ -10,8 +10,11 @@
{% bootstrap_form_errors form %}
<fieldset>
<legend>{% trans "Voucher codes" %}</legend>
<div class="form-group">
<div class="form-group form-inline">
<div class="col-md-7 col-sm-12 col-md-offset-3">
<input type="text" class="form-control input-xs"
id="voucher-bulk-codes-prefix"
placeholder="{% trans "Prefix (optional)" %}">
<div class="input-group">
<input type="number" class="form-control input-xs"
id="voucher-bulk-codes-num"

View File

@@ -278,10 +278,11 @@ class VoucherRNG(EventPermissionRequiredMixin, View):
except ValueError: # NOQA
return HttpResponseBadRequest()
prefix = request.GET.get('prefix')
while len(codes) < num:
new_codes = set()
for i in range(min(num - len(codes), 500)): # Work around SQLite's SQLITE_MAX_VARIABLE_NUMBER
new_codes.add(_generate_random_code())
new_codes.add(_generate_random_code(prefix=prefix))
new_codes -= set([v['code'] for v in Voucher.objects.filter(code__in=new_codes).values('code')])
codes |= new_codes

View File

@@ -105,11 +105,11 @@ $(function () {
// Vouchers
$("#voucher-bulk-codes-generate").click(function () {
var num = $("#voucher-bulk-codes-num").val();
var prefix = $('#voucher-bulk-codes-prefix').val();
if (num != "") {
$(".form-group:has(#voucher-bulk-codes-num)").addClass("has-error");
var url = $(this).attr("data-rng-url");
$("#id_codes").html("Generating...");
$.getJSON(url + '?num=' + num, function (data) {
$.getJSON(url + '?num=' + num + '&prefix=' + escape(prefix), function (data) {
$("#id_codes").val(data.codes.join("\n"));
});
} else {