From 91813421e47a7b80a523ee1af3c18c0116cdd6c7 Mon Sep 17 00:00:00 2001 From: FinnStutzenstein Date: Wed, 16 Oct 2019 10:54:19 +0200 Subject: [PATCH] Auto-enable tokens during generation --- gulpfile.js | 11 +-- .../static/js/openslides_voting/pdf.js | 2 +- .../static/js/openslides_voting/site.js | 87 +++++++++++++++---- .../token-generate-form.html | 16 ++++ .../templates/openslides_voting/tokens.html | 4 +- openslides_voting/views.py | 13 ++- package.json | 2 +- 7 files changed, 109 insertions(+), 26 deletions(-) create mode 100644 openslides_voting/static/templates/openslides_voting/token-generate-form.html diff --git a/gulpfile.js b/gulpfile.js index a7998e7..349d424 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -36,7 +36,8 @@ gulp.task('templates', function () { moduleSystem: 'IIFE', transformUrl: function (url) { var pathList = url.split(path.sep); - pathList.shift(); + pathList.shift(); // First one is a "" + pathList.shift(); // remove second "openslides_voting" return pathList.join(path.sep); }, })) @@ -64,14 +65,14 @@ gulp.task('translations', function () { }); // Gulp default task. Runs all other tasks before. -gulp.task('default', ['translations', 'templates', 'js-libs'], function () {}); +gulp.task('default', gulp.series('translations', 'templates', 'js-libs', function (done) {done()})); // Watches changes in JavaScript and templates. -gulp.task('watch', ['templates'], function () { +gulp.task('watch', gulp.series('templates', function () { gulp.watch([ path.join('**', 'static', 'templates', '**', '*.html') - ], ['templates']); -}); + ], gulp.series('templates')); +})); /** diff --git a/openslides_voting/static/js/openslides_voting/pdf.js b/openslides_voting/static/js/openslides_voting/pdf.js index 09618d4..e19ffd1 100644 --- a/openslides_voting/static/js/openslides_voting/pdf.js +++ b/openslides_voting/static/js/openslides_voting/pdf.js @@ -292,7 +292,7 @@ angular.module('OpenSlidesApp.openslides_voting.pdf', ['OpenSlidesApp.core.pdf'] var tables = []; var currentTableBody; _.forEach(tokens, function (token, index) { - if ((index % (tokensPerPage+1)) === 0) { + if ((index % (tokensPerPage)) === 0) { if (currentTableBody) { tables.push({ table: { diff --git a/openslides_voting/static/js/openslides_voting/site.js b/openslides_voting/static/js/openslides_voting/site.js index 9a7e487..056c00b 100644 --- a/openslides_voting/static/js/openslides_voting/site.js +++ b/openslides_voting/static/js/openslides_voting/site.js @@ -1016,14 +1016,12 @@ angular.module('OpenSlidesApp.openslides_voting.site', [ .controller('TokensCtrl', [ '$scope', '$http', + 'ngDialog', 'VotingToken', - 'TokenContentProvider', - 'TokenDocumentProvider', - 'PdfCreate', + 'TokenGenerateForm', 'gettextCatalog', 'ErrorMessage', - function ($scope, $http, VotingToken, TokenContentProvider, TokenDocumentProvider, PdfCreate, - gettextCatalog, ErrorMessage) { + function ($scope, $http, ngDialog, VotingToken, TokenGenerateForm, gettextCatalog, ErrorMessage) { VotingToken.bindAll({}, $scope, 'tokens'); $scope.scan = function () { @@ -1045,24 +1043,83 @@ angular.module('OpenSlidesApp.openslides_voting.site', [ $scope.tokenInputDisabled = false; }; - $scope.generate = function (n) { - n = parseInt(n); - if (isNaN(n)) { - return; - } - if (n < 1) { - n = 1; - } else if (n > 4096) { - n = 4096; + $scope.openGenerateDialog = function (keypad) { + ngDialog.open(TokenGenerateForm.getDialog()); + }; + + } +]) + +.factory('TokenGenerateForm', [ + 'gettextCatalog', + function (gettextCatalog) { + return { + getDialog: function () { + return { + template: 'static/templates/openslides_voting/token-generate-form.html', + controller: 'TokenGenerateCtrl', + className: 'ngdialog-theme-default', + closeByEscape: false, + closeByDocument: false, + }; + }, + getFormFields: function () { + return [ + { + key: 'N', + type: 'input', + templateOptions: { + label: gettextCatalog.getString('Amount of Tokens'), + type: 'number', + required: true, + min: 1, + max: 4096, + }, + }, + { + key: 'enable_tokens', + type: 'checkbox', + templateOptions: { + label: gettextCatalog.getString('Enable Tokens') + } + } + ]; } - $http.post('/rest/openslides_voting/voting-token/generate/', {N: n}).then(function (success) { + }; + } +]) + +.controller('TokenGenerateCtrl', [ + '$scope', + '$http', + 'TokenGenerateForm', + 'TokenContentProvider', + 'TokenDocumentProvider', + 'PdfCreate', + 'gettextCatalog', + 'ErrorMessage', + function ($scope, $http, TokenGenerateForm, TokenContentProvider, TokenDocumentProvider, PdfCreate, + gettextCatalog, ErrorMessage) { + $scope.alert = {}; + $scope.model = { + N: 1, + enable_tokens: false + }; + $scope.formFields = TokenGenerateForm.getFormFields(); + + $scope.generate = function (model) { + $http.post('/rest/openslides_voting/voting-token/generate/', model).then(function (success) { var filename = gettextCatalog.getString('Tokens') + '.pdf'; filename = filename.replace(/\s/g,''); var contentProvider = TokenContentProvider.createInstance(success.data); var documentProvider = TokenDocumentProvider.createInstance(contentProvider); PdfCreate.download(documentProvider, filename); + $scope.closeThisDialog(); + }, function (error) { + $scope.alert = ErrorMessage.forAlert(error); }); }; + } ]) diff --git a/openslides_voting/static/templates/openslides_voting/token-generate-form.html b/openslides_voting/static/templates/openslides_voting/token-generate-form.html new file mode 100644 index 0000000..e4d0caf --- /dev/null +++ b/openslides_voting/static/templates/openslides_voting/token-generate-form.html @@ -0,0 +1,16 @@ +

Generate tokens

+ +
+ {{ alert.msg }} +
+ +
+ + + + +
diff --git a/openslides_voting/static/templates/openslides_voting/tokens.html b/openslides_voting/static/templates/openslides_voting/tokens.html index d1692f0..b787a34 100644 --- a/openslides_voting/static/templates/openslides_voting/tokens.html +++ b/openslides_voting/static/templates/openslides_voting/tokens.html @@ -5,9 +5,7 @@ Back to overview - + Generate diff --git a/openslides_voting/views.py b/openslides_voting/views.py index 39e321b..70a8609 100644 --- a/openslides_voting/views.py +++ b/openslides_voting/views.py @@ -883,7 +883,9 @@ def create(self, request, *args, **kwargs): @list_route(methods=['post']) def generate(self, request): """ - Generate n tokens. Provide N (1<=N<=4096) as the only argument: {N: } + Generate n tokens. Provide N (1<=N<=4096) for the amount and enable_tokens + to enable all generated tokens. Request data: + {N: , enable_tokens: Optional} """ if not isinstance(request.data, dict): raise ValidationError({'detail': 'The data has to be a dict.'}) @@ -892,10 +894,19 @@ def generate(self, request): raise ValidationError({'detail': 'N has to be an int.'}) if n < 1 or n > 4096: raise ValidationError({'detail': 'N has to be between 1 and 4096.'}) + enable_tokens = bool(request.data.get("enable_tokens")) # no I,O,i,l,o,0 choices = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrsuvwxyz123456789' tokens = [(''.join(random.choice(choices) for _ in range(12))) for _ in range(n)] + + if enable_tokens: + existing_token_ids = list(VotingToken.objects.values_list('id', flat=True)) # Evaluate queryset now. + VotingToken.objects.bulk_create([ + VotingToken(token=token) for token in tokens + ]) + inform_changed_data(VotingToken.objects.exclude(id__in=existing_token_ids)) + return Response(tokens) @list_route(methods=['post']) diff --git a/package.json b/package.json index 0fe8887..b0d4872 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "devDependencies": { "bower": "~1.8.0", "es6-promise": "~4.1.0", - "gulp": "~3.9.1", + "gulp": "~4.0.0", "gulp-angular-gettext": "~2.2.0", "gulp-angular-templatecache": "~2.0.0", "gulp-if": "^2.0.2",