diff --git a/limitmonitor/jinja2/limitmonitor/userlimit.html b/limitmonitor/jinja2/limitmonitor/userlimit.html index e390a0ab6638b6b22b64fd06a0be40b1b17157ff..39d5415da2b44973c2d66ef70b4cc2b95f0997e7 100755 --- a/limitmonitor/jinja2/limitmonitor/userlimit.html +++ b/limitmonitor/jinja2/limitmonitor/userlimit.html @@ -155,6 +155,7 @@ SPDX-License-Identifier: AGPL-3.0 diff --git a/middleware/urls.py b/middleware/urls.py index c546b1f34f2a66b95f87d21425ad4419cff2930d..cf82ee743def1b2753bf58635dff2443af8ed5f1 100755 --- a/middleware/urls.py +++ b/middleware/urls.py @@ -24,7 +24,8 @@ from ldapregister.forms import RegistrationForm from ldapregister.views import LdhLoginView from cart.views import CartRegistrationView -from purist.views import Recovery, PasswordChange, PasswordChangeDone +from purist.views import Recovery, PasswordChange, \ + PasswordChangeDone, ProfileConfigureView from invitation.views import InvitationRegistrationView # @@ -48,6 +49,8 @@ urlpatterns = [ limitmonitor.views.toggle_tunnel, name='toggle_tunnel'), url(r'^accounts/profile/new_invitation', limitmonitor.views.new_invitation, name='new_invitation'), + url(r'^accounts/profile/configure/(?P.+)/$', + ProfileConfigureView.as_view(), name='profile_configure'), # url(r'^accounts/register/$', RegistrationView.as_view(form_class=RegistrationForm), name='registration_register'), url(r'^accounts/login/$', LdhLoginView.as_view(), name='auth_login'), url(r'^accounts/recover/$', Recovery.as_view(), name='password_reset_recover'), diff --git a/purist/forms.py b/purist/forms.py index bf2aae028c707672823a988d08537f9112d99e67..61574a78536c0d87d519fba852bd9f100fa2fb5c 100644 --- a/purist/forms.py +++ b/purist/forms.py @@ -4,6 +4,7 @@ from django.contrib.auth.forms import PasswordChangeForm \ as BasePasswordChangeForm from django.contrib.auth import authenticate from password_reset.forms import PasswordRecoveryForm as BasePasswordRecoveryForm +from .models import User class PasswordRecoveryForm(BasePasswordRecoveryForm): @@ -40,3 +41,21 @@ class PasswordChangeForm(BasePasswordChangeForm): self.user.set_woocommerce_password(raw_password=password, woocommerce_id=None) return self.user + + +class ProfileConfigureForm(forms.ModelForm): + class Meta: + model = User + fields = ['email', 'billing_email'] + labels = { + 'email': _('Recovery email'), + 'billing_email': _('Billing email'), + } + widgets = { + 'email': forms.EmailInput(attrs={'size': '100%'}), + 'billing_email': forms.EmailInput(attrs={'size': '100%'}), + } + + def save(self, commit=True): + self.instance.set_woocommerce_billing_email() + return super(ProfileConfigureForm, self).save(commit) diff --git a/purist/migrations/0005_add_billing_email.py b/purist/migrations/0005_add_billing_email.py new file mode 100644 index 0000000000000000000000000000000000000000..129586ac7c49ca802ddb48641615c75439bf5e57 --- /dev/null +++ b/purist/migrations/0005_add_billing_email.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.18 on 2019-04-25 07:23 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('purist', '0004_add_account_type_field'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='billing_email', + field=models.EmailField(default='', max_length=254), + ), + ] diff --git a/purist/models.py b/purist/models.py index 7055d65f6deaad86eadc9ab7e8fa6c5567a3ea5f..17841d659185567ce2d2552694aed6ba86f9142b 100644 --- a/purist/models.py +++ b/purist/models.py @@ -85,6 +85,7 @@ class User(AbstractUser): tunnel_password = models.CharField(max_length=250, default=None, null=True) account_type = EnumIntegerField(enum=AccountType, default=AccountType.UNDEFINED) + billing_email = models.EmailField(default='', null=False) @classmethod def normalize_username(cls, username): @@ -165,7 +166,7 @@ class User(AbstractUser): def get_woocommerce_id(self): - query = "customers?email=" + self.get_identity() + query = "customers?role=all&email=" + self.get_identity() json = self.woo_get_json(query) if json is not None and len(json) == 1: @@ -218,6 +219,32 @@ class User(AbstractUser): def get_identity(self): return self.get_username() + "@" + settings.SITE_DOMAIN.lower() + def get_woocommerce_billing_email(self): + + query = "customers?role=all&email=" + self.get_identity() + json = self.woo_get_json(query) + + if json is not None and len(json) == 1: + billing_email = json[0]["billing"]["email"] + if billing_email != '': + return billing_email + + return None + + def set_woocommerce_billing_email(self, woocommerce_id=None): + + data = { + "billing": { + "email": self.billing_email + }, + } + + if woocommerce_id is None: + woocommerce_id = self.get_woocommerce_id() + query = "customers/" + str(woocommerce_id) + + return self.woo_put_json(query, data) + def save(self, force_insert=False, force_update=False, using=None, update_fields=None): # save django user diff --git a/purist/templates/purist/profile_configure.html b/purist/templates/purist/profile_configure.html new file mode 100644 index 0000000000000000000000000000000000000000..115a0b529ebfbc31c1d615f1d7560494797d0457 --- /dev/null +++ b/purist/templates/purist/profile_configure.html @@ -0,0 +1,81 @@ + +{% load static %} +{% load i18n %} + + + + + + + + {% trans "Profile Settings" %} + + + + + + + +
+ +
+ +
+

{% trans "Profile Settings" %}

+
+
+ +
+ {% if request.user.is_authenticated %} + {% trans "Logged in as" %} {{ username }}
+ {% trans "Profile" %} | + + {% if request.user.is_superuser %} + {% trans "Admin" %} | + {% endif %} + + {% trans "Log out" %} + {% else %} + {% trans "You are not logged in." %}
+ {% trans "Log in" %} + + {% endif %} +
+ +
+ +
+ +
+
+ + + + + diff --git a/purist/views.py b/purist/views.py index 03d69bda9387e3d87310f2c9894508d9ef6d446b..7d7a58d9a96d8ea6905ab7c9036799700bf6e21a 100644 --- a/purist/views.py +++ b/purist/views.py @@ -8,12 +8,14 @@ from django.http import Http404, FileResponse from password_reset.views import Recover from .serializers import UserSerializer -from .forms import PasswordRecoveryForm, PasswordChangeForm +from .forms import PasswordRecoveryForm, PasswordChangeForm, \ + ProfileConfigureForm from django.contrib.auth.views import PasswordChangeView \ as BasePasswordChangeView from django.contrib.auth.views import PasswordChangeDoneView \ as BasePasswordChangeDoneView from django.urls import reverse_lazy +from django.views.generic.edit import UpdateView class UserDetail(APIView): @@ -65,6 +67,28 @@ class PasswordChange(BasePasswordChangeView): return context +class ProfileConfigureView(UpdateView): + template_name = 'purist/profile_configure.html' + form_class = ProfileConfigureForm + success_url = reverse_lazy('profile') + model = User + slug_field = 'username' + slug_url_kwarg = 'username' + + def get_context_data(self, **kwargs): + context = super(ProfileConfigureView, self).get_context_data(**kwargs) + context['username'] = self.request.user.get_username() + context['site_title'] = settings.SITE_TITLE + context['site_provider'] = settings.SITE_PROVIDER + return context + + def get_initial(self): + # Prepopulate form fields with values from User Model and + # WooCommerce + return {"email": self.object.email, + "billing_email": self.object.get_woocommerce_billing_email()} + + def home(request): render_data = { "username": request.user.get_username(),