Skip to content

Commit

Permalink
Split up filtering for Files and Images.
Browse files Browse the repository at this point in the history
Add a `can_edit` field to the returned json from the api for Images and Files.
Style uneditable rows.
Toggle disabled state of DELETE button depending on selected row.
  • Loading branch information
Marko Tibold committed Aug 24, 2012
1 parent c135e0b commit aac7162
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
5 changes: 5 additions & 0 deletions fiber/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ def object_created(self, user, obj):
"""
pass

def filter_images(self, user, qs):
return qs

def filter_files(self, user, qs):
return qs
15 changes: 13 additions & 2 deletions fiber/rest_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@

from fiber.models import Page, PageContentItem, ContentItem, Image, File
from fiber.utils.date import friendly_datetime
from fiber.app_settings import API_PERMISSION_CLASS
from fiber.utils import class_loader


from .views import ApiRoot, MovePageView, MovePageContentItemView, ListView, TreeListView, FileListView, ImageListView, InstanceView

PERMISSIONS = class_loader.load_class(API_PERMISSION_CLASS)


class PageResource(ModelResource):
model = Page
Expand Down Expand Up @@ -66,7 +71,10 @@ def filename(self, instance):
def updated(self, instance):
return friendly_datetime(instance.updated)

include = ('url', 'file_url', 'filename', 'updated')
def can_edit(self, instance):
return PERMISSIONS.can_edit(self.view.user, instance)

include = ('url', 'file_url', 'filename', 'updated', 'can_edit')


class ImageResource(FileResource):
Expand All @@ -81,7 +89,10 @@ def filename(self, instance):
def size(self, instance):
return '%s x %d' % (instance.width, instance.height)

include = ('url', 'image_url', 'filename', 'size', 'updated')
def can_edit(self, instance):
return PERMISSIONS.can_edit(self.view.user, instance)

include = ('url', 'image_url', 'filename', 'size', 'updated', 'can_edit')


urlpatterns = patterns('',
Expand Down
8 changes: 2 additions & 6 deletions fiber/rest_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ def check_fields(self, order_by):
if order_by not in self.orderable_fields:
raise ErrorResponse(status=HTTP_400_BAD_REQUEST, content="Can not order by the passed value.")

def get_queryset(self, *args, **kwargs):
qs = super(PaginatedListView, self).get_queryset(*args, **kwargs)
return PERMISSIONS.filter_objects(self.request.user, qs)

def serialize_page_info(self, page):
"""
Override Django REST Framework's method
Expand All @@ -98,7 +94,7 @@ class FileListView(PaginatedListView):

def get_queryset(self, *args, **kwargs):
qs = super(FileListView, self).get_queryset(*args, **kwargs)

qs = PERMISSIONS.filter_files(self.request.user, qs)
search = self.request.GET.get('search', None)
if search:
qs = qs.filter(file__icontains=search)
Expand All @@ -122,7 +118,7 @@ class ImageListView(PaginatedListView):

def get_queryset(self, *args, **kwargs):
qs = super(ImageListView, self).get_queryset(*args, **kwargs)

qs = PERMISSIONS.filter_images(self.request.user, qs)
search = self.request.GET.get('search', None)
if search:
# TODO: image_icontains searches in the entire path, it should only search in the filename (use iregex for this?)
Expand Down
1 change: 1 addition & 0 deletions fiber/static/fiber/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ ul.tree .moving > div .title {
}

/* permissions-related styling */
.ui-dialog .not-editable,
#df-wpr-sidebar .sidebar-tree .not-editable .title {
color: #f00;
}
Expand Down
24 changes: 18 additions & 6 deletions fiber/static/fiber/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,13 @@ var BaseFileSelectDialog = AdminRESTDialog.extend({
return 'file';
},

uneditables_formatter: function(value, row_data) {
if (!row_data.can_edit) {
return '<span class="not-editable">' + value + '</span>';
}
return value;
},

create_upload_button: function() {
var button_pane = this.uiDialog.parent().find('.ui-dialog-buttonpane');

Expand Down Expand Up @@ -710,9 +717,14 @@ var BaseFileSelectDialog = AdminRESTDialog.extend({

delete_button.addClass('ui-button-disabled ui-state-disabled');

this.select_grid.bind('datagrid.select', function() {
delete_button.attr('disabled', '');
delete_button.removeClass('ui-button-disabled ui-state-disabled');
this.select_grid.bind('datagrid.select', function(e) {
if (e.row.can_edit){
delete_button.attr('disabled', '');
delete_button.removeClass('ui-button-disabled ui-state-disabled');
}
else if (!delete_button.hasClass('ui-button-disabled ui-state-disabled')){
delete_button.addClass('ui-button-disabled ui-state-disabled');
}
});

var self = this;
Expand Down Expand Up @@ -783,11 +795,11 @@ Fiber.ImageSelectDialog = BaseFileSelectDialog.extend({
// TODO: use image checksum for cache busting?
return '<span style="display: none;">' + row_data.image_url + '</span>' + '<img src="' + row_data.image_url + '?_c=' + encodeURIComponent(row_data.url) + '" title="' + row_data.title + '"/>';
}

this.select_grid.simple_datagrid({
columns: [
{title: gettext('Image'), key: 'image', on_generate: thumbnail_formatter},
{title: gettext('Filename'), key: 'filename'},
{title: gettext('Filename'), key: 'filename', on_generate: this.uneditables_formatter},
{title: gettext('Size'), key: 'size'},
{title: gettext('Updated'), key: 'updated'}
],
Expand Down Expand Up @@ -868,7 +880,7 @@ Fiber.FileSelectDialog = BaseFileSelectDialog.extend({

this.select_grid.simple_datagrid({
columns: [
{title: gettext('Filename'), key: 'filename'},
{title: gettext('Filename'), key: 'filename', on_generate: this.uneditables_formatter},
{title: gettext('Updated'), key: 'updated'}
],
url: this.options.url,
Expand Down

0 comments on commit aac7162

Please sign in to comment.