Make simple wizard - add feature to upload few files

remotes/origin/1203
Ivan Kovalkovskyi 11 years ago
parent 5d4d737f14
commit ed1353299a
  1. 34
      templates/client/wizard/second_step.html
  2. 59
      templates/client/wizard/third_step.html
  3. 9
      wizard/forms.py
  4. 7
      wizard/models.py
  5. 7
      wizard/urls.py
  6. 82
      wizard/views.py

@ -6,20 +6,22 @@
<h1>Добавить событие</h1> <h1>Добавить событие</h1>
</div> </div>
<div class="page-body clearfix"> <div class="page-body clearfix">
<form action="" method="post">{% csrf_token %}
{{ wizard.form.media }} {{ wizard.form.media }}
<div class="set-sect p-form adm-form passed"> <div class="set-sect p-form adm-form passed">
<div class="set-sect-title clearfix"> <div class="set-sect-title clearfix">
<h3>Шаг 1. Основная информация</h3> <h3>Шаг 1. Основная информация</h3>
<div class="afh-right"> <div class="afh-right">
<button name="wizard_goto_step" type="submit" class="button icon-edit"
<a class="button icon-edit" href="#">изменить</a> value="{{ wizard.steps.prev }}">изменить
</button>
</div> </div>
</div> </div>
</div> </div>
<div class="set-sect p-form adm-form"> <div class="set-sect p-form adm-form">
<div class="set-sect-title clearfix"> <div class="set-sect-title clearfix">
<h3>Шаг 2. Статистика и условия участия</h3> <h3>Шаг 2. Статистика и условия участия</h3>
@ -29,7 +31,6 @@
</div> </div>
</div> </div>
<form action="" method="post">{% csrf_token %}
{{ wizard.management_form }} {{ wizard.management_form }}
{% if wizard.form.forms %} {% if wizard.form.forms %}
{{ wizard.form.management_form }} {{ wizard.form.management_form }}
@ -268,42 +269,33 @@
<div class="abb-left"> <div class="abb-left">
<div class="mfpl-button-line"> <div class="mfpl-button-line">
<div class="mfpl-button"> {# <div class="mfpl-button">#}
<a class="button big grey a-more" href="#">пропустить этот шаг</a> {# <a class="button big grey a-more" href="#">пропустить этот шаг</a>#}
</div> {# </div>#}
<div class="mfpl-text"> <div class="mfpl-text">
<p>более полная информация повышает рейтинг вашего мероприятия и позволяет ранжировать <p>более полная информация повышает рейтинг вашего мероприятия и позволяет
ранжировать
его выше других</p> его выше других</p>
</div> </div>
</div> </div>
</div> </div>
{% if wizard.steps.prev %}
<button class="button big orange a-more" name="wizard_goto_step" type="submit"
value="{{ wizard.steps.first }}">"first step"
</button>
<button class="button big orange a-more" name="wizard_goto_step" type="submit"
value="{{ wizard.steps.prev }}"> "prev step"
</button>
{% endif %}
<button class="button big orange a-more" type="submit">Дале</button> <button class="button big orange a-more" type="submit">Дале</button>
</div> </div>
</form>
</div> </div>
<div class="set-sect p-form adm-form passed"> <div class="set-sect p-form adm-form passed">
<div class="set-sect-title clearfix"> <div class="set-sect-title clearfix">
<h3>Шаг 3. Добавление фото</h3> <h3>Шаг 3. Добавление фото</h3>
<div class="afh-right">
<a class="button icon-edit" href="#">изменить</a>
</div>
</div> </div>
</div> </div>
</form>
</div> </div>
{% endwith %} {% endwith %}
{% endif %} {% endif %}

@ -13,49 +13,66 @@
{% endblock %} {% endblock %}
{% block content_list %} {% block content_list %}
{{ wizard.form.media }}
<div class="page-title"> <div class="page-title">
<h1>Добавить событие</h1> <h1>Добавить событие</h1>
</div> </div>
<div class="page-body clearfix"> <div class="page-body clearfix">
<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
<div class="set-sect p-form adm-form passed"> <div class="set-sect p-form adm-form passed">
<div class="set-sect-title clearfix"> <div class="set-sect-title clearfix">
<h3>Шаг 1. Основная информация</h3> <h3>Шаг 1. Основная информация</h3>
<div class="afh-right"> <div class="afh-right">
<a class="button icon-edit" href="#">изменить</a> <button name="wizard_goto_step" type="submit" class="button icon-edit"
value="{{ wizard.steps.first }}">изменить
</button>
</div> </div>
</div> </div>
</div> </div>
<div class="set-sect p-form adm-form passed"> <div class="set-sect p-form adm-form passed">
<div class="set-sect-title clearfix"> <div class="set-sect-title clearfix">
<h3>Шаг 2. Статистика и условия участия</h3> <h3>Шаг 2. Статистика и условия участия</h3>
<div class="afh-right"> <div class="afh-right">
<a class="button icon-edit" href="#">изменить</a> <button name="wizard_goto_step" type="submit" class="button icon-edit"
value="{{ wizard.steps.prev }}">изменить
</button>
</div> </div>
</div> </div>
</div> </div>
<div class="set-sect p-form adm-form"> <div class="set-sect p-form adm-form">
<div class="set-sect-title clearfix"> <div class="set-sect-title clearfix">
<h3>Шаг 3. Добавление фото</h3> <h3>Шаг 3. Добавление фото</h3>
<div class="afh-right"> <div class="afh-right">
<div class="sst-info"><b>+ 1,2 балла</b> к рейтингу</div> <div class="sst-info"><b>+ 1,2 балла</b> к рейтингу</div>
</div> </div>
</div> </div>
{{ form.errors }}
<form action="" method="post" enctype="multipart/form-data">{% csrf_token %} {{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{% with wizard.form as form %}
<hr/> <hr/>
<div class="mf-photos-list"> <div class="mf-photos-list">
<div class="mfpl-button-line"> <div class="mfpl-button-line">
<div class="mfpl-button"> <div class="mfpl-button">
<input class="button big icon-camera" value="выберите фотографии" type="file" id="files" name="files[]" multiple /> {{ form.attachments }}
</div> </div>
<div class="mfpl-text"> <div class="mfpl-text">
<p>более полная информация повышает рейтинг вашего мероприятия и позволяет ранжировать его выше других</p> <p>более полная информация повышает рейтинг вашего мероприятия и позволяет
ранжировать его выше других</p>
</div> </div>
</div> </div>
@ -65,19 +82,33 @@
<div class="a-bot-buttons-line clearfix"> <div class="a-bot-buttons-line clearfix">
<div class="abb-left"> <div class="abb-left">
<a class="button big grey a-more" href="#">пропустить этот шаг</a> <button class="button big grey a-more" type="submit">пропустить этот шаг</button>
</div> </div>
<div class="abb-right"> <div class="abb-right">
<button class="button big orange a-more" type="submit">далее</button> <button id="next" class="button big orange a-more" type="submit">далее</button>
</div> </div>
<div id="res"> </div>
</div> </div>
</form> {% endwith %}
{% endif %}
</div> </div>
</form>
</div> </div>
<!-- for response debugging -->
{% comment %} <script>
$("#next").click(function(e){
e.preventDefault();
$.ajax({url: '/wizard/ajax/', success: function(result){
$("#res").html(result);
}});
});
</script>{% endcomment %}
<script type="text/javascript"> <script type="text/javascript">
var cnt = 0;
function handleFileSelect(evt) { function handleFileSelect(evt) {
var files = evt.target.files; // FileList object var files = evt.target.files; // FileList object
@ -96,11 +127,13 @@
return function (e) { return function (e) {
// Render thumbnail. // Render thumbnail.
var div = document.createElement('div'); var div = document.createElement('div');
div.innerHTML = ['<li><div class="mfpli-pict"> <img class="thumb" src="', e.target.result, div.innerHTML = ['<li><div class="mfpli-pict"> <img class="thumb" src="', e.target.result,
'" title="', theFile.name, '" title="', theFile.name,
'"/></div>','<div class="mfpli-body"> <div class="mf-line full-width"> <div class="mf-field"> <textarea name="pdescr', '"/></div>', '<div class="mfpli-body"> <div class="mf-line full-width"> <div class="mf-field"> <textarea name="pdescr_',
i.toString(),'" cols="30" rows="10" placeholder="Описание"></textarea></div></div>', cnt.toString(), '" cols="30" rows="10" placeholder="Описание"></textarea></div></div>',
'</li>'].join(''); '</li>'].join('');
cnt +=1 ;
document.getElementById('list').insertBefore(div, null); document.getElementById('list').insertBefore(div, null);
}; };
})(f); })(f);

@ -4,6 +4,8 @@ from theme.models import Theme, Tag
from place_exposition.models import PlaceExposition from place_exposition.models import PlaceExposition
from city.models import City from city.models import City
from country.models import Country from country.models import Country
from multiupload.fields import MultiFileField, MultiFileInput
choices = ((0, ''), (1.0, u'Ежегодно'), (2.0, u'2 раза в год'), (3.0, u'3 раза в год'), (4.0, u'4 раза в год'), choices = ((0, ''), (1.0, u'Ежегодно'), (2.0, u'2 раза в год'), (3.0, u'3 раза в год'), (4.0, u'4 раза в год'),
(5.0, u'5 раз в год'), (0.5, u'Раз в 2 года'), ( (5.0, u'5 раз в год'), (0.5, u'Раз в 2 года'), (
@ -64,3 +66,10 @@ class ExpoForm2(forms.Form):
deadline_date = forms.DateField(widget=forms.TextInput(attrs={'placeholder':'дд.мм.гг'})) deadline_date = forms.DateField(widget=forms.TextInput(attrs={'placeholder':'дд.мм.гг'}))
class ExpoForm3(forms.Form):
attachments = MultiFileField(min_num=0, max_num=2, max_file_size=1024*1024*5, widget=MultiFileInput(
attrs={'class':'button big icon-camera', 'value':u'выберите фотографии', 'id':'files'}
))
formlist = [ExpoForm1, ExpoForm2, ExpoForm3]

@ -1,3 +1,8 @@
from django.db import models from django.db import models
from django.conf import settings
import os
# Create your models here. # Create your models here.
class Attachment(models.Model):
file = models.FileField(upload_to=os.path.join(settings.MEDIA_ROOT,'attachments_files'))

@ -1,9 +1,8 @@
from django.conf.urls import patterns, url, include from django.conf.urls import patterns, url, include
from wizard.forms import ExpoForm1, ExpoForm2 from wizard.views import wizard_view
from wizard.views import ExpoWizard, add_photo_view
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^add-photo/', add_photo_view, name="add-photo"), url(r'^ajax/$', 'wizard.views.ajax_view'),
url(r'^$', ExpoWizard.as_view([ExpoForm1, ExpoForm2])) url(r'^$', wizard_view)
) )

@ -1,27 +1,36 @@
from django.shortcuts import render_to_response, render
from django.contrib.formtools.wizard.views import SessionWizardView from django.contrib.formtools.wizard.views import SessionWizardView
from django.core.files.storage import FileSystemStorage from django.core.files.storage import FileSystemStorage
from django.http import HttpResponseRedirect
from django.conf import settings
import os import os
from proj import settings from photologue.models import Photo, Gallery
from wizard import forms
from exposition.models import Exposition, Statistic from exposition.models import Exposition, Statistic
from wizard.models import Attachment
from functions.form_check import translit_with_separator from functions.form_check import translit_with_separator
from django.http import HttpResponseRedirect
# defining different template for every form # defining different template for every form
TEMPLATES = {'0':'client/wizard/first_step.html', '1':'client/wizard/second_step.html'} TEMPLATES = {
'0': 'client/wizard/first_step.html',
'1': 'client/wizard/second_step.html',
'2': 'client/wizard/third_step.html'
}
post = None
files = None
# main view that handle all data from 3 forms(steps) and finally create an Exposition
class ExpoWizard(SessionWizardView): class ExpoWizard(SessionWizardView):
location=os.path.join(settings.MEDIA_ROOT, 'temp', 'files')
location = os.path.join(settings.MEDIA_ROOT, 'temp')
file_storage = FileSystemStorage(location, settings.MEDIA_URL) file_storage = FileSystemStorage(location, settings.MEDIA_URL)
def done(self, form_list, **kwargs): def done(self, form_list, **kwargs):
upload_file = form_list[0].cleaned_data['logo'] upload_logo = form_list[0].cleaned_data.get('logo')
upload_images = self.request.FILES.getlist(u'2-attachments')
data = self.get_all_cleaned_data() data = self.get_all_cleaned_data()
expo = Exposition.objects.language('ru').create(
expo = Exposition.objects.language(self.request.LANGUAGE_CODE).create(
name=data.get('name'), name=data.get('name'),
data_begin=data.get('date_start'), data_begin=data.get('date_start'),
data_end=data.get('date_end'), data_end=data.get('date_end'),
@ -37,10 +46,10 @@ class ExpoWizard(SessionWizardView):
foundation_year=data.get('found_year'), foundation_year=data.get('found_year'),
area=data.get('square'), area=data.get('square'),
price_day = '%i %s'%(data.get('one_day'), self.request.POST['oneDayCurrency1']), price_day=data.get('one_day'),
price_all = '%i %s'%(data.get('all_days'), self.request.POST['allDaysCurrency1']), price_all=data.get('all_days'),
price_day_bar = '%i %s'%(data.get('pre_one_day'),self.request.POST['oneDayCurrency1']), price_day_bar=data.get('pre_one_day'),
price_all_bar = '%i %s'%(data.get('pre_all_days'),self.request.POST['allDaysCurrency1']), price_all_bar=data.get('pre_all_days'),
min_area=data.get('min_square'), min_area=data.get('min_square'),
registration_payment=data.get('registration_depos'), registration_payment=data.get('registration_depos'),
@ -52,6 +61,16 @@ class ExpoWizard(SessionWizardView):
quality_label=0, quality_label=0,
audience=0 audience=0
) )
photos = []
for i, photo in enumerate(upload_images):
photos.append(Photo.objects.language(self.request.LANGUAGE_CODE).create(
image=photo,
title=data.get(u'pdescr_%i' % i, photo.name))
)
for photo in photos:
expo.upload_photo(photo)
expo.tag = [data.get('tag')] expo.tag = [data.get('tag')]
expo.theme = [data.get('theme')] expo.theme = [data.get('theme')]
if data['membership1']: if data['membership1']:
@ -79,31 +98,26 @@ class ExpoWizard(SessionWizardView):
area=data.get('square') area=data.get('square')
) )
if upload_file: if upload_logo:
self.file_storage.delete(upload_file.name) self.file_storage.delete(upload_logo.name)
if upload_images:
return HttpResponseRedirect("add-photo") for f in upload_images:
# return render_to_response('done.html', { self.file_storage.delete(f.name)
# 'form_data': [form.cleaned_data for form in form_list], return HttpResponseRedirect('/')
# })
def get_template_names(self): def get_template_names(self):
return [TEMPLATES[self.steps.current]] return [TEMPLATES[self.steps.current]]
from django.core.context_processors import csrf
import logging
loger = logging.getLogger(__name__)
PATH_TO_FILE = os.path.join(settings.MEDIA_ROOT, 'exposition_foto/') from wizard.forms import formlist
wizard_view = ExpoWizard.as_view(formlist)
from django.contrib.formtools.wizard.storage.session import SessionStorage
from django.http import HttpResponse
import json
def add_photo_view(request): def ajax_view(request):
if request.POST: form = formlist[2](post, files, prefix='2')
for key, file in request.FILES.items(): return HttpResponse(json.dumps({'posts':post,'files':files}), content_type='application/json')
loger.debug(key)
loger.debug(len(request.FILES))
context = {}
context.update(csrf(request))
return render(request, "client/wizard/third_step.html", context)

Loading…
Cancel
Save