def api_key(request): api_key = '' form = APIKeyForm(instance=request.user) if request.method == 'POST': # new key requested form = APIKeyForm(request.POST, instance=request.user) if form.is_valid() and form.cleaned_data['id'] == request.user.id: try: api_key = ApiKey.objects.get(user=request.user) api_key.key = None api_key.save() except ApiKey.DoesNotExist: api_key = ApiKey.objects.create(user=request.user) messages.add_message(request, messages.SUCCESS, 'API Key generated successfully.', extra_tags='alert-success') else: raise PermissionDenied else: try: api_key = ApiKey.objects.get(user=request.user) except ApiKey.DoesNotExist: api_key = ApiKey.objects.create(user=request.user) add_breadcrumb(title="API Key", top_level=True, request=request) return render( request, 'dojo/api_key.html', { 'name': 'API Key', 'metric': False, 'user': request.user, 'key': api_key, 'form': form, })
def api_key(request): api_key = '' form = APIKeyForm(instance=request.user) if request.method == 'POST': # new key requested form = APIKeyForm(request.POST, instance=request.user) if form.is_valid() and form.cleaned_data['id'] == request.user.id: try: api_key = ApiKey.objects.get(user=request.user) api_key.key = None api_key.save() except ApiKey.DoesNotExist: api_key = ApiKey.objects.create(user=request.user) messages.add_message(request, messages.SUCCESS, 'API Key generated successfully.', extra_tags='alert-success') else: raise PermissionDenied else: try: api_key = ApiKey.objects.get(user=request.user) except ApiKey.DoesNotExist: api_key = ApiKey.objects.create(user=request.user) add_breadcrumb(title="API Key", top_level=True, request=request) return render(request, 'dojo/api_key.html', {'name': 'API Key', 'metric': False, 'user': request.user, 'key': api_key, 'form': form, })