def add_action(request): form = ActionForm() if request.method == 'POST' and form.validate(request.form): form.save() return redirect('/news/admin/actions/all/') images_form = ActionImageForm(action='/news/admin/actions/add_image_action/') return render_to_response( 'news/admin/actions_add.html', {'form':form.as_widget(), 'images_form':images_form.as_widget()})
def edit_action(request, key): action = Action.get(key) if not action: return redirect('/news/admin/actions/all/') form = ActionForm(instance=action) if request.method == 'POST' and form.validate(request.form): form.save() return redirect('/news/admin/actions/all/') images_form = ActionImageForm(action='/news/admin/actions/add_image/%s/' % key) return render_to_response( 'news/admin/actions_add.html', {'form':form.as_widget(), 'images_form':images_form.as_widget(), 'action':action})
def add_image_to_new_action(request): action = Action(title=u'Новая акция', discount=0.0) action.put() action_key = action.key() form = ActionForm(instance=action) images_form = ActionImageForm(action='/news/admin/actions/add_image_action/') if request.method == 'POST': if images_form.validate(request.form, request.files): img = images_form['image_file'] content_type = 'image/jpeg' if not images_form['title']: title = '' else: title = images_form['title'] title = title.replace('"', '"') thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=img, thumb_size=(100, 100), content_type=content_type, title=title) if not images_form['width'] or not images_form['height']: width = height = 300 else: width = images_form['width'] height = images_form['height'] thumb_img.add_new_thumb(blob_img=img, thumb_size=(width, height), content_type=content_type, title=title) thumb_img.put() news_image = ActionImage() news_image.title = title news_image.action = action news_image.image = thumb_img news_image.size = '%sx%s' % (width, height) news_image.put() images_form = ActionImageForm(action='/news/admin/actions/add_image/%s/' % action_key) return redirect('/news/admin/actions/edit/%s/' % action_key) images_form = ActionImageForm(action='/news/admin/actions/add_image/%s/' % action_key) return render_to_response('news/admin/actions_add.html', {'form':form.as_widget(), 'images_form':images_form.as_widget()})