Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Auto-enable tokens during generation #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
}))
Expand Down Expand Up @@ -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'));
}));


/**
Expand Down
2 changes: 1 addition & 1 deletion openslides_voting/static/js/openslides_voting/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
87 changes: 72 additions & 15 deletions openslides_voting/static/js/openslides_voting/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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);
});
};

}
])

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1 translate>Generate tokens</h1>

<div uib-alert ng-show="alert.show" ng-class="'alert-' + alert.type" close="alert={}">
{{ alert.msg }}
</div>

<form name="keypadForm" ng-submit="generate(model)">
<formly-form model="model" fields="formFields">
<button type="submit" ng-disabled="keypadForm.$invalid" class="btn btn-primary" translate>
Generate
</button>
<button type="button" ng-click="closeThisDialog()" class="btn btn-default" translate>
Cancel
</button>
</formly-form>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
<i class="fa fa-angle-double-left fa-lg"></i>
<translate>Back to overview</translate>
</a>
<a class="btn btn-default btn-sm"
ng-bootbox-prompt="{{ 'How much voting tokens should be generated?' | translate }}"
ng-bootbox-prompt-action="generate(result)">
<a class="btn btn-default btn-sm" ng-click="openGenerateDialog()">
<i class="fa fa-magic fa-lg"></i>
<translate>Generate</translate>
</a>
Expand Down
13 changes: 12 additions & 1 deletion openslides_voting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: <n>}
Generate n tokens. Provide N (1<=N<=4096) for the amount and enable_tokens
to enable all generated tokens. Request data:
{N: <n>, enable_tokens: Optional<boolean>}
"""
if not isinstance(request.data, dict):
raise ValidationError({'detail': 'The data has to be a dict.'})
Expand All @@ -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'])
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down