From d530365022ab6873ace43a61da427828bfcbd722 Mon Sep 17 00:00:00 2001 From: ArturBaybulatov Date: Mon, 1 Aug 2016 17:24:00 +0300 Subject: [PATCH] #ARC-11 #ARC-4 --- users/forms.py | 18 ++ users/models.py | 4 +- users/templates/user_financial_info_edit.html | 171 +++++++++--------- users/templates/user_profile_edit.html | 2 +- users/views.py | 56 +++--- 5 files changed, 143 insertions(+), 108 deletions(-) diff --git a/users/forms.py b/users/forms.py index a933c42..b68d14d 100644 --- a/users/forms.py +++ b/users/forms.py @@ -41,6 +41,24 @@ class UserProfileEditForm(forms.ModelForm): super().__init__(*args, **kwargs) +class UserProfileBasicInfoEditForm(forms.ModelForm): + class Meta: + model = User + + fields = ( + 'avatar', + 'contractor_specializations', + 'first_name', + 'last_name', + 'location', + 'patronym', + ) + + def __init__(self, *args, **kwargs): + self.request = kwargs.pop('request') + super().__init__(*args, **kwargs) + + class UserFinancialInfoEditForm(forms.ModelForm): class Meta: model = UserFinancialInfo diff --git a/users/models.py b/users/models.py index 3a819db..3295f76 100644 --- a/users/models.py +++ b/users/models.py @@ -68,10 +68,10 @@ class UserFinancialInfo(models.Model): residency = models.CharField(max_length=50, choices=RESIDENCIES) subdivision_code = models.CharField(max_length=10) yandex_money = models.CharField(max_length=50) - + def __str__(self): return self.fio - + class Meta: verbose_name = 'Финансовая информация' verbose_name_plural = 'Финансовая информация' diff --git a/users/templates/user_financial_info_edit.html b/users/templates/user_financial_info_edit.html index ab559b2..640691f 100644 --- a/users/templates/user_financial_info_edit.html +++ b/users/templates/user_financial_info_edit.html @@ -6,73 +6,6 @@
-
-
-
-
-
- profile-image -
-
- - -
-
-
-

ФИО:

- - - -
-
-

Специализация:

-
- - - - Добавить еще - -
-
-
-

Местоположение:

-
- - -
-
-
-
-
- - -
{% csrf_token %} @@ -82,6 +15,76 @@ {% endif %} +
+
+
+
+
+ profile-image +
+
+ +
+
+
+

ФИО

+ + + + +
+ + {% if request.user.is_contractor %} +
+

Специализации

+ +
+ + + {% for spec_id in form.contractor_specializations.value %} +
+ + +
+ {% endfor %} +
+ + + Добавить еще + +
+ {% endif %} + +
+

Местоположение

+ +
+
+
+
+ + +
+
+
+
+
+ + +
@@ -92,13 +95,13 @@
- +
- +
- +
@@ -107,9 +110,9 @@
- {% for residency in form.residency %} + {% for residency in fin_info_form.residency %}
- +

{{ residency.choice_label }}

@@ -122,9 +125,9 @@
- {% for lstatus in form.legal_status %} + {% for lstatus in fin_info_form.legal_status %}
- +

{{ lstatus.choice_label }}

@@ -139,10 +142,10 @@
- +
- +
@@ -156,10 +159,10 @@
- +
- +
@@ -170,7 +173,7 @@
- +
@@ -186,19 +189,19 @@
- +
- +
- +
- {{ form.passport_scan.value }} + {{ fin_info_form.passport_scan.value }}
- +

Прикрепить файл

diff --git a/users/templates/user_profile_edit.html b/users/templates/user_profile_edit.html index 519d379..c3ec3e0 100644 --- a/users/templates/user_profile_edit.html +++ b/users/templates/user_profile_edit.html @@ -149,7 +149,7 @@
diff --git a/users/views.py b/users/views.py index bf4514f..3503822 100644 --- a/users/views.py +++ b/users/views.py @@ -26,6 +26,7 @@ from work_sell.forms import WorkSellForm from .forms import ( ContractorFilterForm, CustomerProfileProjectRealtyForm, + UserProfileBasicInfoEditForm, UserFinancialInfoEditForm, UserProfileEditForm, ) @@ -62,14 +63,11 @@ class UserProfileEditView(BaseMixin, View): def post(self, request, *args, **kwargs): context = self.get_context_data(**kwargs) - customer = request.user - - # Мухтар, смотри, фишечка: specs = request.POST.getlist('contractor_specializations') request.POST.setlist('contractor_specializations', _.compact(specs)) # Ignore empty input values - form = self.form_class(request.POST, request.FILES, request=request, instance=customer) + form = self.form_class(request.POST, request.FILES, request=request, instance=request.user) if form.is_valid(): form.save() @@ -89,7 +87,8 @@ class UserProfileEditView(BaseMixin, View): class UserFinancialInfoEditView(BaseMixin, View): - form_class = UserFinancialInfoEditForm + form_class = UserProfileBasicInfoEditForm + fin_info_form_class = UserFinancialInfoEditForm template_name = 'user_financial_info_edit.html' def dispatch(self, request, *args, **kwargs): @@ -100,25 +99,32 @@ class UserFinancialInfoEditView(BaseMixin, View): def get(self, request, *args, **kwargs): context = self.get_context_data(**_.merge({}, request.GET, kwargs)) - form = self.form_class(request=request, instance=request.user.financial_info) - context.update({'form': form}) + + form = self.form_class(request=request, instance=request.user) + fin_info_form = self.fin_info_form_class(request=request, instance=request.user.financial_info, prefix='fin_info') + + context.update({ + 'form': form, + 'fin_info_form': fin_info_form, + }) + return render(request, self.template_name, context) def post(self, request, *args, **kwargs): context = self.get_context_data(**kwargs) - form = self.form_class(request.POST, request.FILES, request=request, instance=request.user.financial_info) - # if form.is_valid(): - # financical_instance = form.save() - # request.user.contractor_financial_info = financical_instance - # request.user.save() - # messages.info(request, 'Данные успешно отредактированы') - # return redirect(reverse('users:contractor-financical', kwargs={'pk': request.user.pk})) - # - # return render(request, self.template_name, {'form': form}) + specs = request.POST.getlist('contractor_specializations') + request.POST.setlist('contractor_specializations', _.compact(specs)) # Ignore empty input values - if form.is_valid(): - form.save() + form = self.form_class(request.POST, request.FILES, request=request, instance=request.user) + fin_info_form = self.fin_info_form_class(request.POST, request.FILES, request=request, instance=request.user.financial_info, prefix='fin_info') + + if form.is_valid() and fin_info_form.is_valid(): + user = form.save() + fin_info = fin_info_form.save() + + user.financial_info = fin_info + user.save() messages.info(request, 'Финансовая информация успешно отредактирована') redirect_to = request.POST.get('next') @@ -130,7 +136,17 @@ class UserFinancialInfoEditView(BaseMixin, View): '
{form}
' ).format(form=pformat(form.errors))) - context.update({'form': form}) + if fin_info_form.errors: + messages.info(request, ( + '

Произошла ошибка (fin_info_form)

' + '
{form}
' + ).format(form=pformat(fin_info_form.errors))) + + context.update({ + 'form': form, + 'fin_info_form': fin_info_form, + }) + return render(request, self.template_name, context) @@ -458,8 +474,6 @@ class CustomerProfileReviewsView(BaseMixin, View): customer = get_object_or_404(User.customer_objects, pk=self.kwargs['pk']) reviews = Review.objects.filter(target_customer=customer) - # import code; code.interact(local=dict(globals(), **locals())) - context.update({ 'reviews': reviews, 'customer': customer,