PR-16 Исправление формы заполнения готовых проектов

fix поправлены баги для PR-41: косметические, создание объектов с пустыми именами.

доработана PR-16
remotes/origin/PR-49
booblegum 9 years ago
parent c55b306e5a
commit 41039288d9
  1. 41
      work_sell/forms.py
  2. 20
      work_sell/migrations/0023_worksellphoto_description.py
  3. 1
      work_sell/models.py
  4. 9
      work_sell/templates/worksell_create.html
  5. 17
      work_sell/views.py

@ -18,28 +18,6 @@ class ContractorWorkSellTrashForm(forms.Form):
class WorkSellForm(forms.ModelForm):
# Define a form field manually for a reverse model vield:
# photos = forms.ModelMultipleChoiceField(
# queryset=WorkSellPhoto.objects.none(),
# widget=forms.CheckboxSelectMultiple,
# required=False,
# )
#
# live_images = forms.ModelMultipleChoiceField(
# queryset=LiveImageUpload.objects.all(),
# widget=forms.CheckboxSelectMultiple,
# required=False,
# )
#
# building_classification = TreeNodeChoiceField(
# BuildingClassfication.objects.exclude(name='_root'),
# widget=forms.Select(attrs={
# 'class': 'selectpicker'
# }),
# required=False,
# level_indicator='',
# )
class Meta:
model = WorkSell
@ -58,34 +36,29 @@ class WorkSellForm(forms.ModelForm):
'name',
'term',
'term_type',
# 'photos'
)
# widgets = {
# 'construction_type': forms.Select(attrs={'class': 'selectpicker'}),
# 'currency': forms.Select(attrs={'class': 'selectpicker'}),
# 'term_type': forms.Select(attrs={'class': 'selectpicker'}),
# 'work_type': forms.Select(attrs={'class': 'selectpicker -project-work-type-select-field'}),
# }
# def clean(self):
# print("CLEAN", self.cleaned_data)
# data = self.cleaned_data.get('specialization')
# data = self.cleaned_data.get('specializations')
# print("before data = ", data)
# # print('spec = ', data.split(','))
# # self.cleaned_data['specialization'] = data.split(',')
# super().clean()
# def clean_specialization(self):
# data = self.cleaned_data['specialization']
# def clean_specializations(self):
# data = self.cleaned_data['specializations']
# print("before data = ", data)
# data = data.split(',')
# # data = data.split(',')
# print("after data = ", data)
# return data
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request')
super().__init__(*args, **kwargs)
self.fields['specializations'].required = True
self.fields['description'].required = True
# self.fields['photos'].queryset = self.instance.photos.all()

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-12-07 16:16
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('work_sell', '0022_auto_20161207_1213'),
]
operations = [
migrations.AddField(
model_name='worksellphoto',
name='description',
field=models.TextField(blank=True),
),
]

@ -60,6 +60,7 @@ class WorkSell(models.Model):
class WorkSellPhoto(models.Model):
img = ImageField(upload_to='worksell/worksell/')
worksell = models.ForeignKey(WorkSell, related_name='photos')
description = models.TextField(blank=True)
class Meta:
verbose_name = 'Изображение Готовая работа'

@ -59,7 +59,9 @@
</div>
<input name="{{ form.budget.html_name }}" class="simple-input icon-rub js-number-format"
placeholder="Введите сумму"
{% if form.budget.value %}
value="{{ form.budget.value }}"
{% endif %}
autocomplete="off">
</div>
</div>
@ -108,7 +110,8 @@
</div>
<i class="fa fa-question-circle-o" aria-hidden="true" title=""></i>
<span class="required">Обязательно</span>
<textarea name="{{ form.description.html_name }}" class="description" rows="8">{{ form.description.value }}</textarea>
<textarea name="{{ form.description.html_name }}" class="description"
rows="8">{{ form.description.value }}></textarea>
</div>
<div class="documentsChat mod" id="fileUploadContainer">
@ -127,9 +130,7 @@
<div class="preview-container">
<div class="file-upload-remove-img-btn"></div>
<img class="preview" src="#" alt="your image"/>
<textarea rows="2" data-limit-rows="true" class="description">
</textarea>
<textarea name="img_description" rows="2" data-limit-rows="true" class="description"></textarea>
</div>
<span style="display:none" class="file-upload-label"></span>

@ -196,8 +196,17 @@ class WorkSellCreateView(BaseMixin, View):
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST, request=request) # Passing `request.FILES` seems unnecessary here. Files are added manually below
# TODO: fix it on front
request.POST.setlist('specializations', request.POST.get('specializations').split(','))
request.POST.setlist('el_format', request.POST.get('el_format').split(','))
# Если фронт не будет возвращать пустую строку при незаполненных данных, то if'ы будут не нужны
if request.POST.get('specializations') == "":
request.POST.pop('specializations')
else:
request.POST.setlist('specializations', request.POST.get('specializations', "").split(','))
if request.POST.get('el_format') == "":
request.POST.pop('el_format')
else:
request.POST.setlist('el_format', request.POST.get('el_format', "").split(','))
print("POST before = ", request.POST)
if form.is_valid():
work_sell = form.save(commit=False)
@ -205,8 +214,8 @@ class WorkSellCreateView(BaseMixin, View):
work_sell.save()
form.save_m2m()
for file in request.FILES.getlist('new_files'):
WorkSellPhoto.objects.create(img=file, worksell=work_sell)
for file, desc in zip(request.FILES.getlist('new_files'), request.POST.getlist('img_description')):
WorkSellPhoto.objects.create(img=file, description=desc, worksell=work_sell)
messages.info(request, 'Работа успешно создана')
redirect_to = reverse('work_sell:detail', kwargs={'pk': work_sell.pk})

Loading…
Cancel
Save