Skip to content

Commit

Permalink
removed EditImageForm, it can be created via upload_image_form_factory
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolerd committed Aug 13, 2015
1 parent c82a2e5 commit b3aef09
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 40 deletions.
42 changes: 9 additions & 33 deletions project/admin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class MailTemplateForm(Form):
html = TextAreaField('Текст письма', validators=[DataRequired()])


def image_upload_form_factory(config):
def image_upload_form_factory(config, is_update=False):
assert config is not None # cause config=None can be passed

class ImageUploadForm(Form):
Expand All @@ -110,36 +110,12 @@ class ImageUploadForm(Form):
)
],
)
image = FileField(
label='Картинка',
validators=[
AllowedMime(config['IMG_MIMES']),
DataRequired(),
]
)
return ImageUploadForm


class EditImageForm(Form):
title = StringField(
label='Название',
validators=[
Length(
max=32,
message='Must not exceed 32 symbols',
if not is_update:
image = FileField(
label='Картинка',
validators=[
AllowedMime(config['IMG_MIMES']),
DataRequired(),
]
)
],
)
description = TextAreaField(
label='Описание (замещающий текст)',
validators=[
Length(
max=128,
message='Must not exceed 128 symbols',
)
],
)
delete = BooleanField(
label='Удалить',
description='Эта картинка будет удалена навсегда (очень надолго!)',
)
return ImageUploadForm
20 changes: 14 additions & 6 deletions project/admin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,18 @@ def get(self, entry_id):
entry = None
if entry_id is None:
# Add a new entry
clazz = self.create_form(config=current_app.config)
entry_form = clazz()
form_class = self.create_form(config=current_app.config)
entry_form = form_class()
else:
# Update an old entry
entry = self.model.bl.get(entry_id)
if entry is None:
abort(404)
entry_form = self.update_form(obj=entry)
form_class = self.update_form(
config=current_app.config,
is_update=True,
)
entry_form = form_class(obj=entry)

return self.render_response(
entry_form=entry_form,
Expand All @@ -112,8 +116,8 @@ def get(self, entry_id):
def post(self, entry_id):
if entry_id is None:
# Add a new entry
clazz = self.create_form(config=current_app.config)
form = clazz()
form_class = self.create_form(config=current_app.config)
form = form_class()
if form.validate_on_submit():
image = request.files['image']
self.model.bl.save_image(
Expand All @@ -127,7 +131,11 @@ def post(self, entry_id):
else:
# Update an old entry
instance = self.model.bl.get(entry_id)
form = self.update_form(obj=instance)
form_class = self.update_form(
config=current_app.config,
is_update=True,
)
form = form_class(obj=instance)
if form.validate_on_submit():
if 'delete' in form.data:
instance.bl.delete()
Expand Down
1 change: 0 additions & 1 deletion project/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ def register_section(*, section_name, list_endpoint,
gallery_image_detail = GalleryImageDetail.as_view(
name='gallery_image_detail',
create_form=forms.image_upload_form_factory,
update_form=forms.EditImageForm,
model=models.UploadedImage,
success_url='gallery_images_list',
template='admin/image_upload.html',
Expand Down

0 comments on commit b3aef09

Please sign in to comment.