<s> from __future__ import absolute_import , unicode_literals <EOL> import os <EOL> import django <EOL> from django import forms <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from feincms import settings <EOL> from . import logger <EOL> from . models import Category , MediaFile <EOL> from . fields import AdminFileWithPreviewWidget <EOL> class MediaCategoryAdminForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Category <EOL> if django . VERSION > ( <NUM_LIT:1> , <NUM_LIT:7> ) : <EOL> fields = '<STR_LIT>' <EOL> def clean_parent ( self ) : <EOL> data = self . cleaned_data [ '<STR_LIT>' ] <EOL> if data is not None and self . instance in data . path_list ( ) : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return data <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( MediaCategoryAdminForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . queryset = self . fields [ '<STR_LIT>' ] . queryset . exclude ( pk = self . instance . pk ) <EOL> class MediaFileAdminForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = MediaFile <EOL> widgets = { '<STR_LIT:file>' : AdminFileWithPreviewWidget } <EOL> if django . VERSION > ( <NUM_LIT:1> , <NUM_LIT:7> ) : <EOL> fields = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( MediaFileAdminForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if settings . FEINCMS_MEDIAFILE_OVERWRITE and self . instance . id : <EOL> field = self . instance . file . field <EOL> if not hasattr ( field , '<STR_LIT>' ) : <EOL> original_generate = field . generate_filename <EOL> def _gen_fname ( instance , filename ) : <EOL> if instance . id and hasattr ( instance , '<STR_LIT>' ) : <EOL> logger . info ( "<STR_LIT>" % ( <EOL> instance . original_name ) ) <EOL> instance . file . storage . delete ( instance . original_name ) <EOL> return instance . original_name <EOL> return original_generate ( instance , filename ) <EOL> field . generate_filename = _gen_fname <EOL> field . _feincms_generate_filename_patched = True <EOL> def clean_file ( self ) : <EOL> if settings . FEINCMS_MEDIAFILE_OVERWRITE and self . instance . id : <EOL> new_base , new_ext = os . path . splitext ( <EOL> self . cleaned_data [ '<STR_LIT:file>' ] . name ) <EOL> old_base , old_ext = os . path . splitext ( self . instance . file . name ) <EOL> if new_ext . lower ( ) != old_ext . lower ( ) : <EOL> raise forms . ValidationError ( _ ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) % { '<STR_LIT>' : old_ext , '<STR_LIT>' : new_ext } ) <EOL> self . instance . original_name = self . instance . file . name <EOL> return self . cleaned_data [ '<STR_LIT:file>' ] </s>
