def dehydrate(self, bundle): # Setting slug-field bundle.data['slug'] = slugify(unidecode(bundle.data['title'])) # If image is set if bundle.data['image']: # Parse to FileObject used by Filebrowser temp_image = FileObject(bundle.data['image']) # Itterate the different versions (by key) for ver in VERSIONS.keys(): # Check if the key start with article_ (if it does, we want to crop to that size) if ver.startswith('events_'): # Adding the new image to the object bundle.data['image_' + ver] = temp_image.version_generate(ver).url # Unset the image-field del (bundle.data['image']) # Do the same thing for the company image if 'image' in bundle.data and bundle.data['image']: for company in bundle.data['company_event']: temp_image = FileObject( company.data['companies'].data['old_image']) for ver in VERSIONS.keys(): if ver.startswith('companies_thumb'): company.data['companies'].data[ 'old_image_' + ver] = temp_image.version_generate(ver).url del (company.data['companies'].data['old_image']) # Returning washed object return bundle
def handle_noargs(self, **options): import os, re from filebrowser.settings import EXTENSION_LIST, EXCLUDE, MEDIA_ROOT, DIRECTORY, VERSIONS, EXTENSIONS # Precompile regular expressions filter_re = [] for exp in EXCLUDE: filter_re.append(re.compile(exp)) for k, v in VERSIONS.iteritems(): exp = (r'_%s.(%s)') % (k, '|'.join(EXTENSION_LIST)) filter_re.append(re.compile(exp)) path = os.path.join(MEDIA_ROOT, DIRECTORY) # walkt throu the filebrowser directory # for all/new files (except file versions itself and excludes) for dirpath, dirnames, filenames in os.walk(path): for filename in filenames: filtered = False # no "hidden" files (stating with ".") if filename.startswith('.'): continue # check the exclude list for re_prefix in filter_re: if re_prefix.search(filename): filtered = True if filtered: continue (tmp, extension) = os.path.splitext(filename) if extension in EXTENSIONS["Image"]: self.createVersions(os.path.join(dirpath, filename))
def handle_noargs(self, **options): import os, re from filebrowser.settings import EXTENSION_LIST, EXCLUDE, VERSIONS, EXTENSIONS from filebrowser.conf import fb_settings # Precompile regular expressions filter_re = [] for exp in EXCLUDE: filter_re.append(re.compile(exp)) for k, v in VERSIONS.items(): exp = (r"_{0}.({1})").format(k, "|".join(EXTENSION_LIST)) filter_re.append(re.compile(exp)) path = os.path.join(fb_settings.MEDIA_ROOT, fb_settings.DIRECTORY) # walkt throu the filebrowser directory # for all/new files (except file versions itself and excludes) for dirpath, dirnames, filenames in os.walk(path): for filename in filenames: filtered = False # no "hidden" files (stating with ".") if filename.startswith("."): continue # check the exclude list for re_prefix in filter_re: if re_prefix.search(filename): filtered = True if filtered: continue (tmp, extension) = os.path.splitext(filename) if extension in EXTENSIONS["Image"]: self.createVersions(os.path.join(dirpath, filename))
def version(self): "Returns version name" ext = (r'_(%s)$') % ('|'.join(VERSIONS.keys())) match = re.search(ext, self.filename_root, re.IGNORECASE) if match: return match.groups()[0] return None
def __init__(self, value, version_suffix=ADMIN_THUMBNAIL): """ Initialize Media for filebrowser. :param value: string with format 'app_label.model_name:pk' :param version_suffix: string with information about version to display """ assert isinstance(value, str) assert version_suffix in VERSIONS.keys() self.value = value try: model_representation, self.object_id = value.split(':') except ValueError: raise FBExtensionObjectException(CORECT_FORMAT_EXCEPTION) if not all((model_representation, self.object_id)): raise FBExtensionObjectException(CORECT_FORMAT_EXCEPTION) try: self.app_label, self.model_name = model_representation.split('.') except ValueError: raise FBExtensionObjectException(CORECT_FORMAT_EXCEPTION) self.version_suffix = version_suffix
def handle(self, *args, **options): media_path = "" if len(args): media_path = args[0] path = os.path.join(MEDIA_ROOT, media_path) else: path = os.path.join(MEDIA_ROOT, DIRECTORY) if not os.path.isdir(path): raise CommandError('<media_path> must be a directory in MEDIA_ROOT (If you don\'t add a media_path the default path is FILEBROWSER_DIRECTORY).\n"%s" is no directory.' % path); # get version name while 1: self.stdout.write('\nSelect a version you whant to generate:\n') for version in VERSIONS: self.stdout.write(' * %s\n' % version) version_name = raw_input('(leave blank to generate all versions): ') if version_name == "": selected_version = None break else: try: tmp = VERSIONS[version_name] selected_version = version_name break except: self.stderr.write('Error: Version "%s" doesn\'t exist.\n' % version_name) version_name = None continue # Precompile regular expressions filter_re = [] for exp in EXCLUDE: filter_re.append(re.compile(exp)) for k,v in VERSIONS.iteritems(): exp = (r'_%s.(%s)') % (k, '|'.join(EXTENSION_LIST)) filter_re.append(re.compile(exp)) # walkt throu the filebrowser directory # for all/new files (except file versions itself and excludes) for dirpath,dirnames,filenames in os.walk(path): if not SAVE_FULL_URL: dirpath = os.path.relpath(dirpath, MEDIA_ROOT) for filename in filenames: filtered = False # no "hidden" files (stating with ".") if filename.startswith('.'): continue # check the exclude list for re_prefix in filter_re: if re_prefix.search(filename): filtered = True if filtered: continue (tmp, extension) = os.path.splitext(filename) if extension in EXTENSIONS["Image"]: self.createVersions(os.path.join(dirpath, filename), selected_version)
def handle(self, *args, **options): media_path = "" if len(args): media_path = args[0] path = os.path.join(MEDIA_ROOT, media_path) else: path = os.path.join(MEDIA_ROOT, DIRECTORY) if not os.path.isdir(path): raise CommandError('<media_path> must be a directory in MEDIA_ROOT (If you don\'t add a media_path the default path is FILEBROWSER_DIRECTORY).\n"%s" is no directory.' % path); # get version name while 1: self.stdout.write('\nSelect a version you want to generate:\n') for version in VERSIONS: self.stdout.write(' * %s\n' % version) version_name = raw_input('(leave blank to generate all versions): ') if version_name == "": selected_version = None break else: try: tmp = VERSIONS[version_name] selected_version = version_name break except: self.stderr.write('Error: Version "%s" doesn\'t exist.\n' % version_name) version_name = None continue # Precompile regular expressions filter_re = [] for exp in EXCLUDE: filter_re.append(re.compile(exp)) for k,v in VERSIONS.iteritems(): exp = (r'_%s(%s)') % (k, '|'.join(EXTENSION_LIST)) filter_re.append(re.compile(exp)) # walkt throu the filebrowser directory # for all/new files (except file versions itself and excludes) for dirpath,dirnames,filenames in os.walk(path, followlinks=True): for filename in filenames: filtered = False # no "hidden" files (stating with ".") if filename.startswith('.'): continue # check the exclude list for re_prefix in filter_re: if re_prefix.search(filename): filtered = True if filtered: continue (tmp, extension) = os.path.splitext(filename) if extension in EXTENSIONS["Image"]: self.createVersions(os.path.join(dirpath, filename), selected_version)
def test_should_return_correct_original_name_using_predefined_versions(self): expected = 'testimage.jpg' for version_suffix in VERSIONS.keys(): file_object = self.F_IMAGE.version_generate(version_suffix) namer = self._get_namer(version_suffix, file_object) self.assertNotEqual(file_object.filename, expected) self.assertEqual(namer.get_original_name(), expected) self.assertEqual(file_object.original_filename, expected)
def find_image_versions(image): img = image img_strings = [] for ver in VERSIONS.keys(): if ver.startswith('events_'): img_strings.append(img.version_generate(ver).url) return img_strings
def _get_options(self, version_suffix, extra_options=None): options = dict(VERSIONS.get(version_suffix, {})) if extra_options: options.update(extra_options) if 'size' in options and 'width' not in options: width, height = options['size'] options['width'] = width options['height'] = height return options
def find_image_versions(company): img = company.old_image img_strings = [] for ver in VERSIONS.keys(): if ver.startswith('companies_'): img_strings.append(img.version_generate(ver).url) return img_strings
def find_image_versions(article): img = article.image img_strings = [] for ver in VERSIONS.keys(): if ver.startswith("article_"): img_strings.append(img.version_generate(ver).url) return img_strings
def _get_namer(self, version_suffix, file_object=None, **extra_options): if not file_object: file_object = self.F_IMAGE extra_options.update(VERSIONS.get(version_suffix, {})) return self.NAMER_CLASS( file_object=file_object, version_suffix=version_suffix, filename_root=file_object.filename_root, extension=file_object.extension, options=extra_options, )
def _generate_version(self, version_path, version_suffix, options): """ Generate Version for an Image. value has to be a path relative to the storage location. """ tmpfile = File(tempfile.NamedTemporaryFile()) try: f = self.site.storage.open(self.path) except IOError: return "" im = Image.open(f) version_dir, version_basename = os.path.split(version_path) root, ext = os.path.splitext(version_basename) version = process_image(im, options) if not version: version = im if 'methods' in options: for m in options['methods']: if callable(m): version = m(version) # IF need Convert RGB if ext in [".jpg", ".jpeg"] and version.mode not in ("L", "RGB"): version = version.convert("RGB") # save version quality = VERSIONS.get(version_suffix, {}).get("quality", VERSION_QUALITY) try: version.save( tmpfile, format=Image.EXTENSION[ext.lower()], quality=quality, optimize=(os.path.splitext(version_path)[1] != '.gif')) except IOError: version.save(tmpfile, format=Image.EXTENSION[ext.lower()], quality=quality) # remove old version, if any if version_path != self.site.storage.get_available_name(version_path): self.site.storage.delete(version_path) self.site.storage.save(version_path, tmpfile) # set permissions if DEFAULT_PERMISSIONS is not None: os.chmod(self.site.storage.path(version_path), DEFAULT_PERMISSIONS) return version_path
def test_version_attributes_3(self): """ FileObject version attributes/methods with alternative versions_basedir # is_version # original # original_filename # versions_basedir # versions # admin_versions # version_name(suffix) # version_generate(suffix) """ # new settings version_list = sorted([ '_test/_versions/folder/testimage_{}.jpg'.format(name) for name in VERSIONS.keys() ]) admin_version_list = ['_test/_versions/folder/testimage_large.jpg'] self.assertEqual(self.F_IMAGE.is_version, False) self.assertEqual(self.F_IMAGE.original.path, self.F_IMAGE.path) self.assertEqual(self.F_IMAGE.versions_basedir, "_test/_versions/") self.assertEqual(self.F_IMAGE.versions(), version_list) self.assertEqual(self.F_IMAGE.admin_versions(), admin_version_list) self.assertEqual(self.F_IMAGE.version_name("large"), "testimage_large.jpg") self.assertEqual(self.F_IMAGE.version_path("large"), "_test/_versions/folder/testimage_large.jpg") # version does not exist yet f_version = FileObject(os.path.join(site.directory, 'folder', "testimage_large.jpg"), site=site) self.assertEqual(f_version.exists, False) # generate version f_version = self.F_IMAGE.version_generate("large") self.assertEqual(f_version.path, "_test/_versions/folder/testimage_large.jpg") self.assertEqual(f_version.exists, True) self.assertEqual(f_version.is_version, True) self.assertEqual(f_version.original_filename, "testimage.jpg") self.assertEqual(f_version.original.path, self.F_IMAGE.path) self.assertEqual(f_version.versions(), []) self.assertEqual(f_version.admin_versions(), [])
def render(self, name, value, attrs=None, renderer=None): if value is None: value = "" if value != "" and not isinstance(value, FileObject): value = FileObject(value, site=self.site) final_attrs = self.build_attrs(attrs, extra_attrs={"type": self.input_type, "name": name}) final_attrs['data-extensions'] = json.dumps(self.extensions) final_attrs['data-thumbnail_size'] = "{}" if self.thumbnail: final_attrs['data-thumbnail'] = self.thumbnail fb_version = VERSIONS.get(self.thumbnail, None) if fb_version: final_attrs['data-thumbnail_size'] = json.dumps(dict( width=fb_version['width'], height=fb_version['height'] )) filebrowser_site = self.site allowed_title = _('Allowed') return render_to_string("uploadfield/uploadfield_widget.html", locals())
def browse(self, request): "Browse Files/Directories." filter_re = [] for exp in EXCLUDE: filter_re.append(re.compile(exp)) # do not filter if VERSIONS_BASEDIR is being used if not VERSIONS_BASEDIR: for k, v in VERSIONS.items(): exp = (r'_%s(%s)$') % (k, '|'.join(EXTENSION_LIST)) filter_re.append(re.compile(exp, re.IGNORECASE)) def filter_browse(item): "Defining a browse filter" filtered = item.filename.startswith('.') for re_prefix in filter_re: if re_prefix.search(item.filename): filtered = True if filtered: return False return True query = request.GET.copy() path = u'%s' % os.path.join(self.directory, query.get('dir', '')) filelisting = FileListing( path, filter_func=filter_browse, sorting_by=query.get('o', DEFAULT_SORTING_BY), sorting_order=query.get('ot', DEFAULT_SORTING_ORDER), site=self) files = [] if SEARCH_TRAVERSE and query.get("q"): listing = filelisting.files_walk_filtered() else: listing = filelisting.files_listing_filtered() # If we do a search, precompile the search pattern now do_search = query.get("q") if do_search: re_q = re.compile(query.get("q").lower(), re.M) filter_type = query.get('filter_type') filter_date = query.get('filter_date') for fileobject in listing: # date/type filter append = False if (not filter_type or fileobject.filetype == filter_type) and (not filter_date or get_filterdate(filter_date, fileobject.date or 0)): append = True # search if do_search and not re_q.search(fileobject.filename.lower()): append = False # append if append: files.append(fileobject) filelisting.results_total = len(listing) filelisting.results_current = len(files) p = Paginator(files, LIST_PER_PAGE) page_nr = request.GET.get('p', '1') try: page = p.page(page_nr) except (EmptyPage, InvalidPage): page = p.page(p.num_pages) return render(request, 'filebrowser/index.html', admin_context({ 'p': p, 'page': page, 'filelisting': filelisting, 'query': query, 'title': _(u'FileBrowser'), 'is_popup': "pop" in request.GET, # ChangeList uses "pop" 'settings_var': get_settings_var(directory=self.directory), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': "", 'filebrowser_site': self }, request))
def test_version_attributes_3(self): """ FileObject version attributes/methods with alternative versions_basedir # is_version # original # original_filename # versions_basedir # versions # admin_versions # version_name(suffix) # version_generate(suffix) """ # new settings version_list = sorted(['_test/_versions/folder/testimage_{}.jpg'.format(name) for name in VERSIONS.keys()]) admin_version_list = ['_test/_versions/folder/testimage_large.jpg'] self.assertEqual(self.F_IMAGE.is_version, False) self.assertEqual(self.F_IMAGE.original.path, self.F_IMAGE.path) self.assertEqual(self.F_IMAGE.versions_basedir, "_test/_versions/") self.assertEqual(self.F_IMAGE.versions(), version_list) self.assertEqual(self.F_IMAGE.admin_versions(), admin_version_list) self.assertEqual(self.F_IMAGE.version_name("large"), "testimage_large.jpg") self.assertEqual(self.F_IMAGE.version_path("large"), "_test/_versions/folder/testimage_large.jpg") # version does not exist yet f_version = FileObject(os.path.join(site.directory, 'folder', "testimage_large.jpg"), site=site) self.assertEqual(f_version.exists, False) # generate version f_version = self.F_IMAGE.version_generate("large") self.assertEqual(f_version.path, "_test/_versions/folder/testimage_large.jpg") self.assertEqual(f_version.exists, True) self.assertEqual(f_version.is_version, True) self.assertEqual(f_version.original_filename, "testimage.jpg") self.assertEqual(f_version.original.path, self.F_IMAGE.path) self.assertEqual(f_version.versions(), []) self.assertEqual(f_version.admin_versions(), [])
import os import re # DJANGO IMPORTS from django.core.management.base import BaseCommand, CommandError from django.conf import settings from django.utils.six.moves import input # FILEBROWSER IMPORTS from filebrowser.settings import EXTENSION_LIST, EXCLUDE, DIRECTORY, VERSIONS, EXTENSIONS from filebrowser.base import FileListing, FileObject filter_re = [] for exp in EXCLUDE: filter_re.append(re.compile(exp)) for k, v in VERSIONS.items(): exp = (r'_%s(%s)') % (k, '|'.join(EXTENSION_LIST)) filter_re.append(re.compile(exp)) class Command(BaseCommand): args = '<media_path>' help = "(Re)Generate image versions." def add_arguments(self, parser): parser.add_argument('media_path', nargs='?') def handle(self, *args, **options): media_path = "" if len(args):
def browse(self, request): "Browse Files/Directories." filter_re = [] for exp in EXCLUDE: filter_re.append(re.compile(exp)) for k, v in VERSIONS.items(): exp = (r'_%s(%s)$') % (k, '|'.join(EXTENSION_LIST)) filter_re.append(re.compile(exp, re.IGNORECASE)) def filter_browse(item): "Defining a browse filter" filtered = item.filename.startswith('.') for re_prefix in filter_re: if re_prefix.search(item.filename): filtered = True if filtered: return False return True query = request.GET.copy() path = u'%s' % os.path.join(self.directory, query.get('dir', '')) filelisting = FileListing( path, filter_func=filter_browse, sorting_by=query.get('o', DEFAULT_SORTING_BY), sorting_order=query.get('ot', DEFAULT_SORTING_ORDER), site=self) files = [] if SEARCH_TRAVERSE and query.get("q"): listing = filelisting.files_walk_filtered() else: listing = filelisting.files_listing_filtered() # If we do a search, precompile the search pattern now do_search = query.get("q") if do_search: re_q = re.compile(query.get("q").lower(), re.M) filter_type = query.get('filter_type') filter_date = query.get('filter_date') for fileobject in listing: # date/type filter append = False if (not filter_type or fileobject.filetype == filter_type) and (not filter_date or get_filterdate(filter_date, fileobject.date or 0)): append = True # search if do_search and not re_q.search(fileobject.filename.lower()): append = False # append if append: files.append(fileobject) filelisting.results_total = len(listing) filelisting.results_current = len(files) p = Paginator(files, LIST_PER_PAGE) page_nr = request.GET.get('p', '1') try: page = p.page(page_nr) except (EmptyPage, InvalidPage): page = p.page(p.num_pages) context = { 'p': p, 'page': page, 'filelisting': filelisting, 'query': query, 'title': _(u'FileBrowser'), 'is_popup': "pop" in request.REQUEST, # ChangeList uses "pop" 'settings_var': get_settings_var(directory=self.directory), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': "", 'filebrowser_site': self } if hasattr(admin_site,'has_permission'): context['has_permission'] = admin_site.has_permission(request) if hasattr(admin_site, 'site_url'): context['site_url'] = admin_site.site_url return render_to_response( 'filebrowser/index.html', context, context_instance=Context(request, current_app=self.name) )
def browse(self, request): "Browse Files/Directories." filter_re = [] for exp in EXCLUDE: filter_re.append(re.compile(exp)) for k, v in VERSIONS.items(): exp = (r"_%s(%s)$") % (k, "|".join(EXTENSION_LIST)) filter_re.append(re.compile(exp, re.IGNORECASE)) def filter_browse(item): "Defining a browse filter" filtered = item.filename.startswith(".") for re_prefix in filter_re: if re_prefix.search(item.filename): filtered = True if filtered: return False return True query = request.GET.copy() path = u"%s" % os.path.join(self.directory, query.get("dir", "")) filelisting = FileListing( path, filter_func=filter_browse, sorting_by=query.get("o", DEFAULT_SORTING_BY), sorting_order=query.get("ot", DEFAULT_SORTING_ORDER), site=self, ) files = [] if SEARCH_TRAVERSE and query.get("q"): listing = filelisting.files_walk_filtered() else: listing = filelisting.files_listing_filtered() # If we do a search, precompile the search pattern now do_search = query.get("q") re_q = None if do_search: re_q = re.compile(query.get("q").lower(), re.M) filter_type = query.get("filter_type") filter_date = query.get("filter_date") for fileobject in listing: # date/type filter append = False if (not filter_type or fileobject.filetype == filter_type) and ( not filter_date or get_filterdate(filter_date, fileobject.date or 0) ): append = True # search if do_search and not re_q.search(fileobject.filename.lower()): append = False # append if append: files.append(fileobject) filelisting.results_total = len(listing) filelisting.results_current = len(files) p = Paginator(files, LIST_PER_PAGE) page_nr = request.GET.get("p", "1") try: page = p.page(page_nr) except (EmptyPage, InvalidPage): page = p.page(p.num_pages) return render_to_response( "filebrowser/index.html", { "p": p, "page": page, "filelisting": filelisting, "query": query, "title": _(u"FileBrowser"), "settings_var": get_settings_var(directory=self.directory), "breadcrumbs": get_breadcrumbs(query, query.get("dir", "")), "breadcrumbs_title": "", "filebrowser_site": self, }, context_instance=Context(request, current_app=self.name), )
import re # DJANGO IMPORTS from django.core.management.base import BaseCommand, CommandError from django.conf import settings from django.utils.six.moves import input # FILEBROWSER IMPORTS from filebrowser.settings import EXTENSION_LIST, EXCLUDE, DIRECTORY, VERSIONS from filebrowser.base import FileListing filter_re = [] for exp in EXCLUDE: filter_re.append(re.compile(exp)) for k, v in VERSIONS.items(): exp = (r'_%s(%s)') % (k, '|'.join(EXTENSION_LIST)) filter_re.append(re.compile(exp)) class Command(BaseCommand): args = '<media_path>' help = "(Re)Generate image versions." def handle(self, *args, **options): media_path = "" if len(args): media_path = args[0] path = media_path else: