# 456 Rework API key creation
* removes frontend input field holding generated API key * replaces with modal form * reworks tests on API token form
This commit is contained in:
49
user/forms/modals/api_token.py
Normal file
49
user/forms/modals/api_token.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Created on: 08.01.25
|
||||
|
||||
"""
|
||||
from django import forms
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from api.models import APIUserToken
|
||||
from konova.forms.modals import BaseModalForm
|
||||
from konova.utils.mailer import Mailer
|
||||
|
||||
|
||||
class NewAPITokenModalForm(BaseModalForm):
|
||||
confirm = forms.BooleanField(
|
||||
label=_("Confirm"),
|
||||
label_suffix=_(""),
|
||||
widget=forms.CheckboxInput(),
|
||||
required=True,
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.template = "modal/modal_form.html"
|
||||
super().__init__(*args, **kwargs)
|
||||
self.form_title = _("Generate API Token")
|
||||
|
||||
self.form_caption = ""
|
||||
if self.__user_has_api_token():
|
||||
self.form_caption = _("You are about to create a new API token. The existing one will not be usable afterwards.")
|
||||
self.form_caption += "\n"
|
||||
self.form_caption += _("A new token needs to be validated by an administrator!")
|
||||
# Disable automatic w-100 setting for this type of modal form. Looks kinda strange
|
||||
self.fields["confirm"].widget.attrs["class"] = ""
|
||||
|
||||
def __user_has_api_token(self):
|
||||
return self.instance.api_token is not None
|
||||
|
||||
def save(self):
|
||||
user = self.instance
|
||||
if user.api_token is not None:
|
||||
user.api_token.delete()
|
||||
user.api_token = APIUserToken.objects.create()
|
||||
user.save()
|
||||
|
||||
mailer = Mailer()
|
||||
mailer.send_mail_verify_api_token(user)
|
||||
|
||||
return user.api_token
|
||||
|
||||
Reference in New Issue
Block a user