Implemented client profile creation

This commit is contained in:
Kumi 2020-06-03 19:24:26 +02:00
parent 07f5fe74c6
commit 4bf2553888
2 changed files with 75 additions and 15 deletions

View file

@ -31,22 +31,43 @@ class AdminCreateForm(ModelForm):
model = get_user_model()
fields = ('first_name', 'last_name', "display_name", "email", 'mobile', "role", "image")
class ClientForm(ModelForm):
class ClientEditForm(ModelForm):
company = CharField(required=False, label=_('Company Name'))
mobile = PhoneNumberField(required=False, label=_('Mobile Number'))
address1 = CharField(label=_('Address'))
address2 = CharField(label=_('Address'))
address2 = CharField(label=_('Address'), required=False)
zip = CharField(label=_('ZIP'))
city = CharField(label=_('City'))
state = CharField(label=_('State'))
country = CountryField()
vat_id = VATNumberFormField(label=_('VAT Number'))
company_id = CharField(label=_('Company Registration Number'))
state = CharField(label=_('State'), required=False)
country = CountryField().formfield()
vat_id = VATNumberFormField(label=_('VAT Number'), required=False)
company_id = CharField(label=_('Company Registration Number'), required=False)
default_currency = ModelChoiceField(Currency.objects.all(), label=_("Default Currency"))
brands = ModelMultipleChoiceField(Brand.objects.all(), label=_("Associated Brands"))
marketing_opt_in = BooleanField(label=_("Opted in to marketing messages"))
pgp_key = CharField(label=_("GPG encryption key"))
marketing_opt_in = BooleanField(label=_("Opted in to marketing messages"), required=False)
pgp_key = CharField(label=_("GPG encryption key"), required=False)
class Meta:
model = get_user_model()
fields = ('first_name', 'last_name', "company", "email", 'mobile')
fields = ('first_name', 'last_name', "company", "email", 'mobile', 'address1', 'address2', 'zip', 'city', 'state', 'country', 'vat_id', 'company_id', 'default_currency', 'brands', 'marketing_opt_in', 'pgp_key')
class ClientCreateForm(ModelForm):
company = CharField(required=False, label=_('Company Name'))
mobile = PhoneNumberField(required=False, label=_('Mobile Number'))
address1 = CharField(label=_('Address'))
address2 = CharField(label=_('Address'), required=False)
zip = CharField(label=_('ZIP'))
city = CharField(label=_('City'))
state = CharField(label=_('State'), required=False)
country = CountryField().formfield()
vat_id = VATNumberFormField(label=_('VAT Number'), required=False)
company_id = CharField(label=_('Company Registration Number'), required=False)
default_currency = ModelChoiceField(Currency.objects.all(), label=_("Default Currency"))
brands = ModelMultipleChoiceField(Brand.objects.all(), label=_("Associated Brands"))
marketing_opt_in = BooleanField(label=_("Opted in to marketing messages"), required=False)
pgp_key = CharField(label=_("GPG encryption key"), required=False)
send_password = BooleanField(label=_("Send password reset email now?"), required=False)
class Meta:
model = get_user_model()
fields = ('first_name', 'last_name', "company", "email", 'mobile', 'address1', 'address2', 'zip', 'city', 'state', 'country', 'vat_id', 'company_id', 'default_currency', 'brands', 'marketing_opt_in', 'pgp_key', 'send_password')

View file

@ -2,9 +2,10 @@ from django.conf import settings
from django.urls import reverse_lazy
from django.contrib.auth import get_user_model
from django.contrib import messages
from django.utils import timezone
from core.models import AdminProfile, ClientProfile, ClientGroup
from core.forms.profiles import AdminEditForm, AdminCreateForm, ClientForm
from core.forms.profiles import AdminEditForm, AdminCreateForm, ClientEditForm, ClientCreateForm
from core.views.backend.generic import BackendFormView, BackendListView, BackendDeleteView, BackendUpdateView, BackendCreateView
from core.helpers.auth import request_password
@ -119,7 +120,7 @@ class ClientListView(BackendListView):
class ClientEditView(BackendFormView):
template_name = f"{settings.EXPEPHALON_BACKEND}/clients/update.html"
form_class = ClientForm
form_class = ClientEditForm
success_url = reverse_lazy("clients")
def get_context_data(self, **kwargs):
@ -129,12 +130,23 @@ class ClientEditView(BackendFormView):
def get_initial(self):
initial = super().get_initial()
client = get_user_model().objects.get(id=self.kwargs["pk"])
assert type(client.profile) == ClientProfile
client = ClientProfile.objects.get(id=self.kwargs["pk"]).user
initial["first_name"] = client.first_name
initial["last_name"] = client.last_name
initial["email"] = client.username
initial["mobile"] = client.profile.mobile
initial["address1"] = client.profile.address1
initial["address2"] = client.profile.address2
initial["zip"] = client.profile.zip
initial["city"] = client.profile.city
initial["state"] = client.profile.state
initial["country"] = client.profile.country
initial["vat_id"] = client.profile.vat_id
initial["company_id"] = client.profile.company_id
initial["default_currency"] = client.profile.default_currency
initial["brands"] = client.profile.brands.all()
initial["marketing_opt_in"] = bool(client.profile.marketing_opt_in)
initial["pgp_key"] = client.profile.pgp_key
return initial
def form_valid(self, form):
@ -144,6 +156,18 @@ class ClientEditView(BackendFormView):
client.username = form.cleaned_data["email"]
client.email = form.cleaned_data["email"]
client.profile.mobile = form.cleaned_data["mobile"]
client.profile.address1 = form.cleaned_data["address1"]
client.profile.address2 = form.cleaned_data["address2"]
client.profile.zip = form.cleaned_data["zip"]
client.profile.city = form.cleaned_data["city"]
client.profile.state = form.cleaned_data["state"]
client.profile.country = form.cleaned_data["country"]
client.profile.vat_id = form.cleaned_data["vat_id"]
client.profile.company_id = form.cleaned_data["company_id"]
client.profile.default_currency = form.cleaned_data["default_currency"]
client.profile.brands.set(form.cleaned_data["brands"])
client.profile.marketing_opt_in = timezone.now() if form.cleaned_data["marketing_opt_in"] and not client.profile.marketing_opt_in else None
client.profile.pgp_key = form.cleaned_data["pgp_key"]
client.profile.save()
client.save()
@ -166,7 +190,7 @@ class ClientDeleteView(BackendDeleteView):
class ClientCreateView(BackendFormView):
template_name = f"{settings.EXPEPHALON_BACKEND}/clients/create.html"
form_class = ClientForm
form_class = ClientCreateForm
success_url = reverse_lazy("admins")
def get_context_data(self, **kwargs):
@ -184,11 +208,26 @@ class ClientCreateView(BackendFormView):
profile = ClientProfile()
profile.user = client
profile.mobile = form.cleaned_data["mobile"]
profile.address1 = form.cleaned_data["address1"]
profile.address2 = form.cleaned_data["address2"]
profile.zip = form.cleaned_data["zip"]
profile.city = form.cleaned_data["city"]
profile.state = form.cleaned_data["state"]
profile.country = form.cleaned_data["country"]
profile.vat_id = form.cleaned_data["vat_id"]
profile.company_id = form.cleaned_data["company_id"]
profile.default_currency = form.cleaned_data["default_currency"]
profile.marketing_opt_in = timezone.now() if form.cleaned_data["marketing_opt_in"] else None
profile.pgp_key = form.cleaned_data["pgp_key"]
client.save()
profile.save()
request_password(client)
profile.brands.set(form.cleaned_data["brands"])
profile.save()
if form.cleaned_data["send_password"]:
request_password(client)
messages.success(self.request, f"Client {client.get_full_name} was successfully created. They should receive an email to set their password shortly.")