Skip to content

Commit

Permalink
Merge pull request #343 from patfreeman/feature_delete_tokens
Browse files Browse the repository at this point in the history
Allow deletion of tokens from web. Issue #337
  • Loading branch information
mik3y authored Sep 12, 2016
2 parents f027e44 + d2954fe commit 4a6529d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
8 changes: 8 additions & 0 deletions pykeg/web/kegadmin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,14 @@ def clean_username(self):
raise forms.ValidationError('Invalid username; use a complete user name or leave blank.')
return username

class DeleteTokenForm(forms.Form):
helper = FormHelper()
helper.form_class = 'form-horizontal user-select'
helper.layout = Layout(
FormActions(
Submit('delete_token', 'Delete Token', css_class='btn-danger'),
)
)

class AddTokenForm(forms.ModelForm):
class Meta:
Expand Down
42 changes: 38 additions & 4 deletions pykeg/web/kegadmin/templates/kegadmin/token_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,44 @@

{% block kegadmin-main %}

<h2>Edit Token</h2>
<div id="deleteModal" class="modal hide fade" tabindex="-1"
role="dialog" aria-labelledby="deleteDialogLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="deleteDialogLabel">Delete Token: {{ token.token_value }}?</h3>
</div>
<div class="modal-body">
<p>
Are you sure you want to delete <b>Token: {{ token.token_value }}</b>?
{% crispy delete_token_form %}
</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>

<h4>{{ token.token_value }} <small>({{ token.auth_device }})</small></h4>

{% crispy form %}
<h3>{{ token.token_value }} <small>({{ token.auth_device }})</small></h3>
<ul class="nav nav-tabs" id="tokenDetails">
<li><a href="#1" data-toggle="tab">Edit</a></li>
<li><a href="#deleteModal" data-toggle="modal">Delete Token</a></li>
</ul>

<div class="row-fluid">
<div class="span12">

<div class="tab-content">
<div class="tab-pane" id="1">
{% crispy form %}
</div>
</div>

{% endblock %}

{% block kb-extrajs %}
<script type="text/javascript">
$(document).ready(function () {
$('#tokenDetails a:first').tab('show');
});
</script>
{% endblock %}
25 changes: 17 additions & 8 deletions pykeg/web/kegadmin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,22 +744,31 @@ def token_list(request):
@staff_member_required
def token_detail(request, token_id):
token = get_object_or_404(models.AuthenticationToken, id=token_id)
delete_token_form = forms.DeleteTokenForm()
context = RequestContext(request)

username = ''
if token.user:
username = token.user.username

form = forms.TokenForm(instance=token, initial={'username': username})
if request.method == 'POST':
form = forms.TokenForm(request.POST, instance=token)
if form.is_valid():
instance = form.save(commit=False)
instance.user = form.cleaned_data['user']
instance.save()
messages.success(request, 'Token updated.')
if 'delete_token' in request.POST:
delete_token_form = forms.DeleteTokenForm(request.POST)
if delete_token_form.is_valid():
token.delete()
messages.success(request, 'The token was deleted.')
return redirect('kegadmin-tokens')
else:
form = forms.TokenForm(request.POST, instance=token)
if form.is_valid():
instance = form.save(commit=False)
instance.user = form.cleaned_data['user']
instance.save()
messages.success(request, 'Token updated.')

context = RequestContext(request)
form = forms.TokenForm(instance=token, initial={'username': username})
context['token'] = token
context['delete_token_form'] = delete_token_form
context['form'] = form
return render_to_response('kegadmin/token_detail.html', context_instance=context)

Expand Down

0 comments on commit 4a6529d

Please sign in to comment.