From ce1d346bc112c71780129078e5502138a22e8f7a Mon Sep 17 00:00:00 2001 From: Arvind Tiwari Date: Tue, 29 Aug 2017 17:42:35 +0530 Subject: [PATCH] billing form in settings page --- hosting/locale/de/LC_MESSAGES/django.po | 7 ++--- hosting/static/hosting/css/commons.css | 14 +++++++++ hosting/templates/hosting/settings.html | 41 +++++-------------------- hosting/views.py | 40 ++++++++++++++---------- utils/forms.py | 1 + 5 files changed, 48 insertions(+), 55 deletions(-) diff --git a/hosting/locale/de/LC_MESSAGES/django.po b/hosting/locale/de/LC_MESSAGES/django.po index 36a9cc09..e2bba040 100644 --- a/hosting/locale/de/LC_MESSAGES/django.po +++ b/hosting/locale/de/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2017-08-29 14:38+0530\n" +"POT-Creation-Date: 2017-08-29 17:41+0530\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -343,10 +343,7 @@ msgstr "" msgid "Type" msgstr "Kartentyp" -msgid "You are not making any payment here." -msgstr "" - -msgid "Add new Card" +msgid "No Cards found for your account." msgstr "" msgid "Add your public SSH key" diff --git a/hosting/static/hosting/css/commons.css b/hosting/static/hosting/css/commons.css index 4710b294..dd81a423 100644 --- a/hosting/static/hosting/css/commons.css +++ b/hosting/static/hosting/css/commons.css @@ -271,6 +271,20 @@ margin-top: 15px; } +.settings-container .credit-card-details { + padding-bottom: 15px; + border-bottom: 1px solid #eee; +} + +.settings-container .credit-card-details h5 { + font-weight: bold; + font-size: 16px; +} + .credit-card-form { max-width: 360px; +} + +.btn-wide { + min-width: 120px; } \ No newline at end of file diff --git a/hosting/templates/hosting/settings.html b/hosting/templates/hosting/settings.html index f044b4b6..130234db 100644 --- a/hosting/templates/hosting/settings.html +++ b/hosting/templates/hosting/settings.html @@ -17,6 +17,9 @@ {% csrf_token %} {% bootstrap_field field show_label=False type='fields'%} {% endfor %} +
+ +
@@ -25,45 +28,14 @@
{% if credit_card_data.last4 %} -
+
{% trans "Credit Card" %}
{% trans "Last" %} 4: *****{{credit_card_data.last4}}
{% trans "Type" %}: {{credit_card_data.cc_brand}}
- - -
-
- {% if not messages and not form.non_field_errors %} -

- {% blocktrans %}You are not making any payment here.{% endblocktrans %} -

- {% endif %} -
- {% for message in messages %} - {% if 'failed_payment' or 'make_charge_error' in message.tags %} -
  • -

    {{ message|safe }}

    -
- {% endif %} - {% endfor %} - {% for error in form.non_field_errors %} -

- {{ error|escape }} -

- {% endfor %} -
-
-
-
- -
-
{% else %} -

{% trans "Add new Card" %}

+

{% trans "No Cards found for your account." %}

+ {% comment %}
@@ -120,6 +92,7 @@

+ {% endcomment %} {% endif %}
diff --git a/hosting/views.py b/hosting/views.py index a860dfc9..9853078b 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -480,22 +480,14 @@ class SettingsView(LoginRequiredMixin, FormView): login_url = reverse_lazy('hosting:login') form_class = BillingAddressForm - def get_form_kwargs(self): - current_billing_address = self.request.user.billing_addresses.first() - form_kwargs = super(SettingsView, self).get_form_kwargs() - if not current_billing_address: - return form_kwargs - - form_kwargs.update({ - 'initial': { - 'cardholder_name': current_billing_address.cardholder_name, - 'street_address': current_billing_address.street_address, - 'city': current_billing_address.city, - 'postal_code': current_billing_address.postal_code, - 'country': current_billing_address.country, - } - }) - return form_kwargs + def get_form(self, form_class): + """ + Check if the user already saved contact details. If so, then show + the form populated with those details, to let user change them. + """ + return form_class( + instance=self.request.user.billing_addresses.first(), + **self.get_form_kwargs()) def get_context_data(self, **kwargs): context = super(SettingsView, self).get_context_data(**kwargs) @@ -520,6 +512,22 @@ class SettingsView(LoginRequiredMixin, FormView): return context + def post(self, request, *args, **kwargs): + form = self.get_form() + if form.is_valid(): + billing_address_data = form.cleaned_data + billing_address_data.update({ + 'user': self.request.user.id + }) + billing_address_user_form = UserBillingAddressForm( + instance=self.request.user.billing_addresses.first(), + data=billing_address_data) + billing_address_user_form.save() + return self.render_to_response(self.get_context_data()) + else: + billing_address_data = form.cleaned_data + return self.form_invalid(form) + class PaymentVMView(LoginRequiredMixin, FormView): template_name = 'hosting/payment.html' diff --git a/utils/forms.py b/utils/forms.py index c521e3ba..7d9d20c0 100644 --- a/utils/forms.py +++ b/utils/forms.py @@ -117,6 +117,7 @@ class UserBillingAddressForm(forms.ModelForm): class Meta: model = UserBillingAddress + # cardholder name not saved fields = ['street_address', 'city', 'postal_code', 'country', 'user'] labels = { 'street_address': _('Street Building'),