Permalink
Browse files

forgotten file.py and made admin file widget open in the same folder …

…as the current image when selecting a new one.

--HG--
rename : filer/media/filer/js/image_widget_thumbnail.js => filer/media/filer/js/admin_file_widget.js
  • Loading branch information...
1 parent 41c40ed commit 32a20a6ec20892b2acb9bc40f702b761dc4f3355 @stefanfoulis committed Oct 28, 2009
@@ -1,7 +1,7 @@
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
from django.contrib.admin.util import unquote, flatten_fieldsets, get_deleted_objects, model_ngettext, model_format_dict
-from django.http import HttpResponseRedirect
+from django.http import HttpResponseRedirect, Http404
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib import admin
@@ -138,7 +138,10 @@ def directory_listing(self, request, folder_id=None, viewtype=None):
elif folder_id == None:
folder = FolderRoot()
else:
- folder = Folder.objects.get(id=folder_id)
+ try:
+ folder = Folder.objects.get(id=folder_id)
+ except Folder.DoesNotExist:
+ raise Http404
# search
def filter_folder(qs, terms=[]):
View
@@ -0,0 +1,112 @@
+from django.utils.translation import ugettext as _
+from django.utils.text import truncate_words
+from django.utils import simplejson
+from django.db import models
+from django import forms
+from django.contrib.admin.widgets import ForeignKeyRawIdWidget
+from django.core.urlresolvers import reverse
+from django.utils.safestring import mark_safe
+from sorl.thumbnail.base import ThumbnailException
+from filer.settings import FILER_MEDIA_PREFIX
+from django.conf import settings as globalsettings
+from filer.models import File
+
+class AdminFileWidget(ForeignKeyRawIdWidget):
+ choices = None
+ input_type = 'hidden'
+ is_hidden = True
+ def render(self, name, value, attrs=None):
+ obj = self.obj_for_value(value)
+ css_id = attrs.get('id', 'id_image_x')
+ css_id_thumbnail_img = "%s_thumbnail_img" % css_id
+ css_id_description_txt = "%s_description_txt" % css_id
+ if attrs is None:
+ attrs = {}
+ related_url = None
+ if value:
+ try:
+ file = File.objects.get(pk=value)
+ related_url = file.logical_folder.get_admin_directory_listing_url_path()
+ except Exception,e:
+ print e
+ if not related_url:
+ related_url = reverse('admin:filer-directory_listing-root')
+ params = self.url_parameters()
+ if params:
+ url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()])
+ else:
+ url = ''
+ if not attrs.has_key('class'):
+ attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook.
+ output = []
+ if obj:
+ try:
+ output.append(u'<img id="%s" src="%s" alt="%s" /> ' % (css_id_thumbnail_img, obj.icons['32'], obj.label) )
+ except ThumbnailException:
+ pass
+ output.append(u'&nbsp;<strong id="%s">%s</strong>' % (css_id_description_txt, obj) )
+ else:
+ output.append(u'<img id="%s" src="" class="quiet" alt="no file selected">' % css_id_thumbnail_img)
+ output.append(u'&nbsp;<strong id="%s">%s</strong>' % (css_id_description_txt, '') )
+ # TODO: "id_" is hard-coded here. This should instead use the correct
+ # API to determine the ID dynamically.
+ output.append('<a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);"> ' % \
+ (related_url, url, name))
+ output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="%s" /></a>' % (globalsettings.ADMIN_MEDIA_PREFIX, _('Lookup')))
+ output.append('</br>')
+ super_attrs = attrs.copy()
+ output.append( super(ForeignKeyRawIdWidget, self).render(name, value, super_attrs) )
+ return mark_safe(u''.join(output))
+ def label_for_value(self, value):
+ obj = self.obj_for_value(value)
+ return '&nbsp;<strong>%s</strong>' % truncate_words(obj, 14)
+ def obj_for_value(self, value):
+ try:
+ key = self.rel.get_related_field().name
+ obj = self.rel.to._default_manager.get(**{key: value})
+ except:
+ obj = None
+ return obj
+
+ class Media:
+ js = (FILER_MEDIA_PREFIX+'js/admin_file_widget.js',
+ FILER_MEDIA_PREFIX+'js/popup_handling.js',)
+
+class AdminFileFormField(forms.ModelChoiceField):
+ widget = AdminFileWidget
+ def __init__(self, rel, queryset, to_field_name, *args, **kwargs):
+ self.rel = rel
+ self.queryset = queryset
+ self.to_field_name = to_field_name
+ self.max_value = None
+ self.min_value = None
+ other_widget = kwargs.pop('widget', None)
+ forms.Field.__init__(self, widget=self.widget(rel), *args, **kwargs)
+
+
+from filer.models import File
+class FilerFileField(models.ForeignKey):
+ default_form_class = AdminFileFormField
+ default_model_class = File
+ def __init__(self, **kwargs):
+ # we call ForeignKey.__init__ with the Image model as parameter...
+ # a FilerImageFiled can only be a ForeignKey to a Image
+ return super(FilerFileField,self).__init__(self.default_model_class, **kwargs)
+ def formfield(self, **kwargs):
+ # This is a fairly standard way to set up some defaults
+ # while letting the caller override them.
+ #defaults = {'form_class': ImageFilerImageWidget}
+ defaults = {
+ 'form_class': self.default_form_class,
+ 'rel': self.rel,
+ }
+ defaults.update(kwargs)
+ return super(FilerFileField, self).formfield(**defaults)
+ def south_field_triple(self):
+ "Returns a suitable description of this field for South."
+ # We'll just introspect ourselves, since we inherit.
+ from south.modelsinspector import introspector
+ field_class = "django.db.models.fields.related.ForeignKey"
+ args, kwargs = introspector(self)
+ # That's our definition!
+ return (field_class, args, kwargs)
View
@@ -7,94 +7,17 @@
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
from sorl.thumbnail.base import ThumbnailException
-from filer import settings
+from filer.settings import FILER_MEDIA_PREFIX
from django.conf import settings as globalsettings
+from filer.fields.file import AdminFileWidget, AdminFileFormField, FilerFileField
+from filer.models import Image
-class AdminImageWidget(ForeignKeyRawIdWidget):
- choices = None
- input_type = 'hidden'
- is_hidden = True
- def render(self, name, value, attrs=None):
- obj = self.obj_for_value(value)
- css_id = attrs.get('id', 'id_image_x')
- css_id_thumbnail_img = "%s_thumbnail_img" % css_id
- css_id_description_txt = "%s_description_txt" % css_id
- if attrs is None:
- attrs = {}
- related_url = reverse('admin:filer-directory_listing-root')
- params = self.url_parameters()
- if params:
- url = '?' + '&amp;'.join(['%s=%s' % (k, v) for k, v in params.items()])
- else:
- url = ''
- if not attrs.has_key('class'):
- attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook.
- output = []
- if obj:
- try:
- output.append(u'<img id="%s" src="%s" alt="%s" /> ' % (css_id_thumbnail_img, obj.thumbnails['admin_tiny_icon'], obj.label) )
- except ThumbnailException:
- pass
- output.append(u'&nbsp;<strong id="%s">%s</strong>' % (css_id_description_txt, obj) )
- else:
- output.append(u'<img id="%s" src="" class="quiet" alt="no image selected">' % css_id_thumbnail_img)
- output.append(u'&nbsp;<strong id="%s">%s</strong>' % (css_id_description_txt, '') )
- # TODO: "id_" is hard-coded here. This should instead use the correct
- # API to determine the ID dynamically.
- output.append('<a href="%s%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);"> ' % \
- (related_url, url, name))
- output.append('<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="%s" /></a>' % (globalsettings.ADMIN_MEDIA_PREFIX, _('Lookup')))
- output.append('</br>')
- super_attrs = attrs.copy()
- output.append( super(ForeignKeyRawIdWidget, self).render(name, value, super_attrs) )
- return mark_safe(u''.join(output))
- def label_for_value(self, value):
- obj = self.obj_for_value(value)
- return '&nbsp;<strong>%s</strong>' % truncate_words(obj, 14)
- def obj_for_value(self, value):
- try:
- key = self.rel.get_related_field().name
- obj = self.rel.to._default_manager.get(**{key: value})
- except:
- obj = None
- return obj
-
- class Media:
- js = (settings.FILER_MEDIA_PREFIX+'js/image_widget_thumbnail.js',
- settings.FILER_MEDIA_PREFIX+'js/popup_handling.js',)
+class AdminImageWidget(AdminFileWidget):
+ pass
-class AdminImageFormField(forms.ModelChoiceField):
+class AdminImageFormField(AdminFileFormField):
widget = AdminImageWidget
- def __init__(self, rel, queryset, to_field_name, *args, **kwargs):
- self.rel = rel
- self.queryset = queryset
- self.to_field_name = to_field_name
- self.max_value = None
- self.min_value = None
- other_widget = kwargs.pop('widget', None)
- forms.Field.__init__(self, widget=self.widget(rel), *args, **kwargs)
-class FilerImageField(models.ForeignKey):
- def __init__(self, **kwargs):
- # we call ForeignKey.__init__ with the Image model as parameter...
- # a FilerImageFiled can only be a ForeignKey to a Image
- from filer.models import Image
- return super(FilerImageField,self).__init__(Image, **kwargs)
- def formfield(self, **kwargs):
- # This is a fairly standard way to set up some defaults
- # while letting the caller override them.
- #defaults = {'form_class': ImageFilerImageWidget}
- defaults = {
- 'form_class': AdminImageFormField,
- 'rel': self.rel,
- }
- defaults.update(kwargs)
- return super(FilerImageField, self).formfield(**defaults)
- def south_field_triple(self):
- "Returns a suitable description of this field for South."
- # We'll just introspect ourselves, since we inherit.
- from south.modelsinspector import introspector
- field_class = "django.db.models.fields.related.ForeignKey"
- args, kwargs = introspector(self)
- # That's our definition!
- return (field_class, args, kwargs)
+class FilerImageField(FilerFileField):
+ default_form_class = AdminImageFormField
+ default_model_class = Image
View
@@ -118,8 +118,26 @@ def url(self):
def file(self):
return self.file_field.file
@property
+ def path(self):
+ try:
+ return self.file_field.path
+ except:
+ return ""
+ @property
def size(self):
return self._file_size or 0
+
+ @property
+ def logical_folder(self):
+ """
+ if this file is not in a specific folder return the Special "unfiled"
+ Folder object
+ """
+ if not self.folder:
+ from filer.models.virtualitems import UnfiledImages
+ return UnfiledImages()
+ else:
+ return self.folder
class Meta:
app_label = 'filer'

0 comments on commit 32a20a6

Please sign in to comment.