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

@ -13,49 +13,66 @@
{% endblock %}
{% block content_list %}
{{ wizard.form.media }}
<div class="page-title">
<h1>Добавить событие</h1>
</div>
<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-title clearfix">
<h3>Шаг 1. Основная информация</h3>
<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 class="set-sect p-form adm-form passed">
<div class="set-sect-title clearfix">
<h3>Шаг 2. Статистика и условия участия</h3>
<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 class="set-sect p-form adm-form">
<div class="set-sect-title clearfix">
<h3>Шаг 3. Добавление фото</h3>
<div class="afh-right">
<div class="sst-info"><b>+ 1,2 балла</b> к рейтингу</div>
</div>
</div>
<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
{{ form.errors }}
{{ 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/>
<div class="mf-photos-list">
<div class="mfpl-button-line">
<div class="mfpl-button">
<input class="button big icon-camera" value="выберите фотографии" type="file" id="files" name="files[]" multiple />
{{ form.attachments }}
</div>
<div class="mfpl-text">
<p>более полная информация повышает рейтинг вашего мероприятия и позволяет ранжировать его выше других</p>
<p>более полная информация повышает рейтинг вашего мероприятия и позволяет
ранжировать его выше других</p>
</div>
</div>
@ -65,19 +82,33 @@
<div class="a-bot-buttons-line clearfix">
<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 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 id="res"> </div>
</div>
</form>
{% endwith %}
{% endif %}
</div>
</form>
</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">
var cnt = 0;
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
@ -96,11 +127,13 @@
return function (e) {
// Render thumbnail.
var div = document.createElement('div');
div.innerHTML = ['<li><div class="mfpli-pict"> <img class="thumb" src="', e.target.result,
'" title="', theFile.name,
'"/></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>',
'"/></div>', '<div class="mfpli-body"> <div class="mf-line full-width"> <div class="mf-field"> <textarea name="pdescr_',
cnt.toString(), '" cols="30" rows="10" placeholder="Описание"></textarea></div></div>',
'</li>'].join('');
cnt +=1 ;
document.getElementById('list').insertBefore(div, null);
};
})(f);

@ -4,6 +4,8 @@ from theme.models import Theme, Tag
from place_exposition.models import PlaceExposition
from city.models import City
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 раза в год'),
(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':'дд.мм.гг'}))
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.conf import settings
import os
# 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 wizard.forms import ExpoForm1, ExpoForm2
from wizard.views import ExpoWizard, add_photo_view
from wizard.views import wizard_view
urlpatterns = patterns('',
url(r'^add-photo/', add_photo_view, name="add-photo"),
url(r'^$', ExpoWizard.as_view([ExpoForm1, ExpoForm2]))
url(r'^ajax/$', 'wizard.views.ajax_view'),
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.core.files.storage import FileSystemStorage
from django.http import HttpResponseRedirect
from django.conf import settings
import os
from proj import settings
from wizard import forms
from photologue.models import Photo, Gallery
from exposition.models import Exposition, Statistic
from wizard.models import Attachment
from functions.form_check import translit_with_separator
from django.http import HttpResponseRedirect
# 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):
location=os.path.join(settings.MEDIA_ROOT, 'temp', 'files')
location = os.path.join(settings.MEDIA_ROOT, 'temp')
file_storage = FileSystemStorage(location, settings.MEDIA_URL)
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()
expo = Exposition.objects.language('ru').create(
expo = Exposition.objects.language(self.request.LANGUAGE_CODE).create(
name=data.get('name'),
data_begin=data.get('date_start'),
data_end=data.get('date_end'),
@ -37,10 +46,10 @@ class ExpoWizard(SessionWizardView):
foundation_year=data.get('found_year'),
area=data.get('square'),
price_day = '%i %s'%(data.get('one_day'), self.request.POST['oneDayCurrency1']),
price_all = '%i %s'%(data.get('all_days'), self.request.POST['allDaysCurrency1']),
price_day_bar = '%i %s'%(data.get('pre_one_day'),self.request.POST['oneDayCurrency1']),
price_all_bar = '%i %s'%(data.get('pre_all_days'),self.request.POST['allDaysCurrency1']),
price_day=data.get('one_day'),
price_all=data.get('all_days'),
price_day_bar=data.get('pre_one_day'),
price_all_bar=data.get('pre_all_days'),
min_area=data.get('min_square'),
registration_payment=data.get('registration_depos'),
@ -52,6 +61,16 @@ class ExpoWizard(SessionWizardView):
quality_label=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.theme = [data.get('theme')]
if data['membership1']:
@ -79,31 +98,26 @@ class ExpoWizard(SessionWizardView):
area=data.get('square')
)
if upload_file:
self.file_storage.delete(upload_file.name)
return HttpResponseRedirect("add-photo")
# return render_to_response('done.html', {
# 'form_data': [form.cleaned_data for form in form_list],
# })
if upload_logo:
self.file_storage.delete(upload_logo.name)
if upload_images:
for f in upload_images:
self.file_storage.delete(f.name)
return HttpResponseRedirect('/')
def get_template_names(self):
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):
if request.POST:
for key, file in request.FILES.items():
loger.debug(key)
loger.debug(len(request.FILES))
context = {}
context.update(csrf(request))
return render(request, "client/wizard/third_step.html", context)
def ajax_view(request):
form = formlist[2](post, files, prefix='2')
return HttpResponse(json.dumps({'posts':post,'files':files}), content_type='application/json')

Loading…
Cancel
Save