Skip to content

Commit

Permalink
Changed create customer to use username #28
Browse files Browse the repository at this point in the history
Instead of possibly slow retrieval of all users from the API to have
them selectable in a dropdown
  • Loading branch information
sebtesobe committed Oct 25, 2017
1 parent 567ea65 commit 5ed7a2e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
28 changes: 21 additions & 7 deletions apimanager/customers/forms.py
Expand Up @@ -6,6 +6,8 @@
from django import forms
from django.conf import settings

from obp.api import APIError


class CreateCustomerForm(forms.Form):
bank_id = forms.ChoiceField(
Expand All @@ -17,20 +19,20 @@ class CreateCustomerForm(forms.Form):
),
choices=[],
)
user_id = forms.ChoiceField(
label='User',
widget=forms.Select(
username = forms.CharField(
label='Username',
widget=forms.TextInput(
attrs={
'placeholder': 'The name of the user',
'class': 'form-control',
}
),
choices=[],
)
customer_number = forms.CharField(
label='Customer Number',
widget=forms.TextInput(
attrs={
'placeholder': 'new customer number, e.g. 687687678',
'placeholder': 'E.g. `007`',
'class': 'form-control',
}
),
Expand All @@ -49,7 +51,7 @@ class CreateCustomerForm(forms.Form):
label='Mobile Phone Number',
widget=forms.TextInput(
attrs={
'placeholder': '+49 123 456 78 90 12',
'placeholder': 'E.g. +49 123 456 78 90 12',
'class': 'form-control',
}
),
Expand All @@ -59,7 +61,7 @@ class CreateCustomerForm(forms.Form):
label='Email',
widget=forms.TextInput(
attrs={
'placeholder': 'person@example.com',
'placeholder': 'E.g. person@example.com',
'class': 'form-control',
}
),
Expand Down Expand Up @@ -234,3 +236,15 @@ def clean_dob_of_dependants(self):
return data.split(',')
else:
return []

def clean_username(self):
username = self.cleaned_data['username']
if not hasattr(self, 'api'):
raise forms.ValidationError('No API object available')
try:
user = self.api.get('/users/username/{}'.format(username))
except APIError as err:
raise forms.ValidationError(err)
else:
self.cleaned_data['user_id'] = user['user_id']
return username
6 changes: 3 additions & 3 deletions apimanager/customers/templates/customers/create.html
Expand Up @@ -25,10 +25,10 @@ <h1>Create Customer</h1>
</div>
</div>
<div class="col-xs-12 col-sm-4">
{% if form.user_id.errors %}<div class="alert alert-danger">{{ form.user_id.errors }}</div>{% endif %}
{% if form.username.errors %}<div class="alert alert-danger">{{ form.username.errors }}</div>{% endif %}
<div class="form-group">
{{ form.user_id.label_tag }}
{{ form.user_id }}
{{ form.username.label_tag }}
{{ form.username }}
</div>
</div>
<div class="col-xs-12 col-sm-4">
Expand Down
9 changes: 5 additions & 4 deletions apimanager/customers/views.py
Expand Up @@ -25,14 +25,15 @@ class CreateView(LoginRequiredMixin, FormView):

def dispatch(self, request, *args, **kwargs):
self.api = API(request.session.get('obp'))
return super(CreateView, self).dispatch(request, *args,**kwargs)
return super(CreateView, self).dispatch(request, *args, **kwargs)

def get_form(self, *args, **kwargs):
form = super(CreateView, self).get_form(*args, **kwargs)
# Cannot add api in constructor: super complains about unknown kwarg
form.api = self.api
fields = form.fields
try:
fields['bank_id'].choices = self.api.get_bank_id_choices()
fields['user_id'].choices = self.api.get_user_id_choices()
except APIError as err:
messages.error(self.request, err)
fields['last_ok_date'].initial =\
Expand Down Expand Up @@ -76,7 +77,7 @@ def form_valid(self, form):
except APIError as err:
messages.error(self.request, err)
return super(CreateView, self).form_invalid(form)
msg = 'Customer number {} has been created successfully!'.format(
result['customer_number'])
msg = 'Customer number {} for user {} has been created successfully!'.format( # noqa
result['customer_number'], data['username'])
messages.success(self.request, msg)
return super(CreateView, self).form_valid(form)

0 comments on commit 5ed7a2e

Please sign in to comment.