mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-27 17:39:51 -04:00
Move data transformation into the form
Follows what we’re doing with the folders stuff. Avoids having too many very straightforward methods on the model. Especially when the data they need is only used by the form. So it’s better to encapsulate the logic in the form.
This commit is contained in:
@@ -525,9 +525,11 @@ class ChooseTimeForm(StripWhitespaceForm):
|
||||
|
||||
|
||||
class CreateKeyForm(StripWhitespaceForm):
|
||||
def __init__(self, existing_key_names=[], *args, **kwargs):
|
||||
self.existing_key_names = [x.lower() for x in existing_key_names]
|
||||
super(CreateKeyForm, self).__init__(*args, **kwargs)
|
||||
def __init__(self, existing_keys=[], *args, **kwargs):
|
||||
self.existing_key_names = [
|
||||
key['name'].lower() for key in existing_keys
|
||||
]
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
key_type = RadioField(
|
||||
'Type of key',
|
||||
|
||||
@@ -87,7 +87,7 @@ def api_keys(service_id):
|
||||
@login_required
|
||||
@user_has_permissions('manage_api_keys', restrict_admin_usage=True)
|
||||
def create_api_key(service_id):
|
||||
form = CreateKeyForm(current_service.api_key_names)
|
||||
form = CreateKeyForm(current_service.api_keys)
|
||||
form.key_type.choices = [
|
||||
(KEY_TYPE_NORMAL, 'Live – sends to anyone'),
|
||||
(KEY_TYPE_TEAM, 'Team and whitelist – limits who you can send to'),
|
||||
|
||||
@@ -345,10 +345,6 @@ class Service():
|
||||
key=lambda key: key['name'].lower(),
|
||||
)
|
||||
|
||||
@property
|
||||
def api_key_names(self):
|
||||
return [key['name'] for key in self.api_keys]
|
||||
|
||||
def get_api_key(self, id):
|
||||
try:
|
||||
return next(key for key in self.api_keys if key['id'] == id)
|
||||
|
||||
@@ -5,10 +5,12 @@ from app.main.forms import CreateKeyForm
|
||||
|
||||
|
||||
def test_return_validation_error_when_key_name_exists(client):
|
||||
def _get_names():
|
||||
return ['some key', 'another key']
|
||||
_existing_keys = [
|
||||
{'name': 'some key'},
|
||||
{'name': 'another key'},
|
||||
]
|
||||
|
||||
form = CreateKeyForm(_get_names(),
|
||||
form = CreateKeyForm(_existing_keys,
|
||||
formdata=MultiDict([('key_name', 'Some key')]))
|
||||
form.key_type.choices = [('a', 'a'), ('b', 'b')]
|
||||
form.validate()
|
||||
|
||||
Reference in New Issue
Block a user