11from django import forms
2- from django .conf import settings
32from django .contrib .admin import widgets
3+ from django .contrib .admin .helpers import AdminForm
44from django .core .exceptions import ValidationError
55from django .db import models
66from django .utils .translation import gettext as _
99from ..utils .files import get_valid_filename
1010
1111
12- class AsPWithHelpMixin :
13- def as_p_with_help (self ):
14- "Returns this form rendered as HTML <p>s with help text formated for admin."
15- return self ._html_output (
16- normal_row = '<p%(html_class_attr)s>%(label)s %(field)s</p>%(help_text)s' ,
17- error_row = '%s' ,
18- row_ender = '</p>' ,
19- help_text_html = '<p class="help">%s</p>' ,
20- errors_on_separate_row = True )
12+ class WithFieldsetMixin :
13+ def get_fieldsets (self ):
14+ return getattr (self , "fieldsets" , [
15+ (None , {"fields" : [field for field in self .fields ]})
16+ ])
2117
18+ def admin_form (self ):
19+ "Returns a class contains the Admin fieldset to show form as admin form"
20+ return AdminForm (self , self .get_fieldsets (), {})
2221
23- class CopyFilesAndFoldersForm (forms .Form , AsPWithHelpMixin ):
22+
23+ class CopyFilesAndFoldersForm (forms .Form ):
2424 suffix = forms .CharField (required = False , help_text = _ ("Suffix which will be appended to filenames of copied files." ))
2525 # TODO: We have to find a way to overwrite files with different storage backends first.
2626 # overwrite_files = forms.BooleanField(required=False, help_text=_("Overwrite a file if there already exists a file with the same filename?"))
@@ -32,7 +32,7 @@ def clean_suffix(self):
3232 return self .cleaned_data ['suffix' ]
3333
3434
35- class RenameFilesForm (forms .Form , AsPWithHelpMixin ):
35+ class RenameFilesForm (WithFieldsetMixin , forms .Form ):
3636 rename_format = forms .CharField (required = True )
3737
3838 def clean_rename_format (self ):
@@ -55,24 +55,26 @@ def clean_rename_format(self):
5555 return self .cleaned_data ['rename_format' ]
5656
5757
58- class ResizeImagesForm (forms .Form , AsPWithHelpMixin ):
59- if 'cmsplugin_filer_image' in settings .INSTALLED_APPS :
60- thumbnail_option = models .ForeignKey (
61- ThumbnailOption ,
62- null = True ,
63- blank = True ,
64- verbose_name = _ ("thumbnail option" ),
65- on_delete = models .CASCADE ,
66- ).formfield ()
58+ class ResizeImagesForm (WithFieldsetMixin , forms .Form ):
59+ fieldsets = ((None , {"fields" : (
60+ "thumbnail_option" ,
61+ ("width" , "height" ),
62+ ("crop" , "upscale" ))}),)
63+
64+ thumbnail_option = models .ForeignKey (
65+ ThumbnailOption ,
66+ null = True ,
67+ blank = True ,
68+ verbose_name = _ ("thumbnail option" ),
69+ on_delete = models .CASCADE ,
70+ ).formfield ()
71+
6772 width = models .PositiveIntegerField (_ ("width" ), null = True , blank = True ).formfield (widget = widgets .AdminIntegerFieldWidget )
6873 height = models .PositiveIntegerField (_ ("height" ), null = True , blank = True ).formfield (widget = widgets .AdminIntegerFieldWidget )
6974 crop = models .BooleanField (_ ("crop" ), default = True ).formfield ()
7075 upscale = models .BooleanField (_ ("upscale" ), default = True ).formfield ()
7176
7277 def clean (self ):
7378 if not (self .cleaned_data .get ('thumbnail_option' ) or ((self .cleaned_data .get ('width' ) or 0 ) + (self .cleaned_data .get ('height' ) or 0 ))):
74- if 'cmsplugin_filer_image' in settings .INSTALLED_APPS :
75- raise ValidationError (_ ('Thumbnail option or resize parameters must be choosen.' ))
76- else :
77- raise ValidationError (_ ('Resize parameters must be choosen.' ))
79+ raise ValidationError (_ ('Thumbnail option or resize parameters must be choosen.' ))
7880 return self .cleaned_data
0 commit comments