Add extensible data filtering feature allowing to remove hard coded listing of documents with missing required metadata. Closes gl-issue #178.
This commit is contained in:
@@ -19,9 +19,10 @@ from .handlers import (
|
||||
from .links import (
|
||||
link_about, link_current_user_details, link_current_user_edit,
|
||||
link_current_user_locale_profile_details,
|
||||
link_current_user_locale_profile_edit, link_license, link_setup, link_tools
|
||||
link_current_user_locale_profile_edit, link_filters, link_license,
|
||||
link_setup, link_tools
|
||||
)
|
||||
from .menus import menu_facet, menu_main, menu_secondary
|
||||
from .menus import menu_facet, menu_main, menu_secondary, menu_tools
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -97,6 +98,10 @@ class CommonApp(MayanAppConfig):
|
||||
)
|
||||
)
|
||||
|
||||
menu_tools.bind_links(
|
||||
links=(link_filters,)
|
||||
)
|
||||
|
||||
post_save.connect(
|
||||
user_locale_profile_create,
|
||||
dispatch_uid='user_locale_profile_create',
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext
|
||||
|
||||
from acls.models import AccessControlList
|
||||
from permissions import Permission
|
||||
|
||||
|
||||
class ModelAttribute(object):
|
||||
__registry = {}
|
||||
@@ -95,3 +99,53 @@ class MissingItem(object):
|
||||
self.description = description
|
||||
self.view = view
|
||||
self.__class__._registry.append(self)
|
||||
|
||||
|
||||
class Filter(object):
|
||||
_registry = {}
|
||||
|
||||
@classmethod
|
||||
def get(cls, slug):
|
||||
return cls._registry[slug]
|
||||
|
||||
@classmethod
|
||||
def all(cls):
|
||||
return cls._registry
|
||||
|
||||
def __init__(self, label, slug, filter_kwargs, model, object_permission=None, hide_links=False):
|
||||
self.label = label
|
||||
self.slug = slug
|
||||
self.filter_kwargs = filter_kwargs
|
||||
self.model = model
|
||||
self.object_permission = object_permission
|
||||
self.hide_links = hide_links
|
||||
|
||||
self.__class__._registry[self.slug] = self
|
||||
|
||||
def __unicode__(self):
|
||||
return unicode(self.label)
|
||||
|
||||
def get_queryset(self, user):
|
||||
queryset = self.model.objects.all()
|
||||
for kwargs in self.filter_kwargs:
|
||||
queryset = queryset.filter(**kwargs)
|
||||
|
||||
queryset = queryset.distinct()
|
||||
|
||||
if self.object_permission:
|
||||
try:
|
||||
# Check to see if the user has the permissions globally
|
||||
Permission.check_permissions(
|
||||
user, (self.object_permission,)
|
||||
)
|
||||
except PermissionDenied:
|
||||
# No global permission, filter ther queryset per object +
|
||||
# permission
|
||||
return AccessControlList.objects.filter_by_access(
|
||||
self.object_permission, user, queryset
|
||||
)
|
||||
else:
|
||||
# Has the permission globally, return all results
|
||||
return queryset
|
||||
else:
|
||||
return queryset
|
||||
|
||||
@@ -9,6 +9,7 @@ from django.db import models
|
||||
from django.utils.html import escape
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .classes import Filter
|
||||
from .models import UserLocaleProfile
|
||||
from .utils import return_attrib
|
||||
from .widgets import DetailSelectMultiple, DisableableSelectWidget, PlainWidget
|
||||
@@ -134,6 +135,14 @@ class FileDisplayForm(forms.Form):
|
||||
fd.close()
|
||||
|
||||
|
||||
class FilterForm(forms.Form):
|
||||
filter_slug = forms.ChoiceField(label=_('Field'))
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(FilterForm, self).__init__(*args, **kwargs)
|
||||
self.fields['filter_slug'].choices = Filter.all().items()
|
||||
|
||||
|
||||
class LicenseForm(FileDisplayForm):
|
||||
FILENAME = 'LICENSE'
|
||||
DIRECTORY = []
|
||||
|
||||
@@ -23,6 +23,10 @@ link_current_user_locale_profile_edit = Link(
|
||||
icon='fa fa-globe', text=_('Edit locale profile'),
|
||||
view='common:current_user_locale_profile_edit'
|
||||
)
|
||||
link_filters = Link(
|
||||
icon='fa fa-filter', text=_('Data filters'),
|
||||
view='common:filter_selection'
|
||||
)
|
||||
link_license = Link(
|
||||
icon='fa fa-book', text=_('License'), view='common:license_view'
|
||||
)
|
||||
|
||||
@@ -7,7 +7,8 @@ from django.views.generic import RedirectView
|
||||
from .views import (
|
||||
AboutView, CurrentUserDetailsView, CurrentUserEditView,
|
||||
CurrentUserLocaleProfileDetailsView, CurrentUserLocaleProfileEditView,
|
||||
HomeView, LicenseView, SetupListView, ToolsListView
|
||||
FilterResultListView, FilterSelectView, HomeView, LicenseView,
|
||||
SetupListView, ToolsListView
|
||||
)
|
||||
|
||||
urlpatterns = patterns(
|
||||
@@ -37,6 +38,14 @@ urlpatterns = patterns(
|
||||
r'^user/locale/edit/$', CurrentUserLocaleProfileEditView.as_view(),
|
||||
name='current_user_locale_profile_edit'
|
||||
),
|
||||
url(
|
||||
r'^filter/select/$', FilterSelectView.as_view(),
|
||||
name='filter_selection'
|
||||
),
|
||||
url(
|
||||
r'^filter/(?P<slug>[\w-]+)/results/$', FilterResultListView.as_view(),
|
||||
name='filter_results'
|
||||
),
|
||||
)
|
||||
|
||||
urlpatterns += patterns(
|
||||
|
||||
@@ -5,15 +5,16 @@ from json import dumps
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.core.urlresolvers import reverse, reverse_lazy
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.http import Http404, HttpResponseRedirect
|
||||
from django.template import RequestContext
|
||||
from django.utils import timezone, translation
|
||||
from django.utils.http import urlencode
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.translation import ugettext_lazy as _, ugettext
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from .classes import Filter
|
||||
from .forms import (
|
||||
LicenseForm, LocaleProfileForm, LocaleProfileForm_view,
|
||||
FilterForm, LicenseForm, LocaleProfileForm, LocaleProfileForm_view,
|
||||
UserForm, UserForm_view
|
||||
)
|
||||
from .generics import * # NOQA
|
||||
@@ -99,6 +100,45 @@ class CurrentUserLocaleProfileEditView(SingleObjectEditView):
|
||||
return self.request.user.locale_profile
|
||||
|
||||
|
||||
class FilterSelectView(SimpleView):
|
||||
form_class = FilterForm
|
||||
template_name = 'appearance/generic_form.html'
|
||||
|
||||
def get_form(self):
|
||||
return FilterForm()
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
'form': self.get_form(),
|
||||
'title': _('Filter selection')
|
||||
}
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
return HttpResponseRedirect(
|
||||
reverse(
|
||||
'common:filter_results',
|
||||
args=(request.POST.get('filter_slug'),)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class FilterResultListView(SingleObjectListView):
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
'hide_links': self.get_filter().hide_links,
|
||||
'title': _('Results for filter: %s') % self.get_filter()
|
||||
}
|
||||
|
||||
def get_filter(self):
|
||||
try:
|
||||
return Filter.get(self.kwargs['slug'])
|
||||
except KeyError:
|
||||
raise Http404(ugettext('Filter not found'))
|
||||
|
||||
def get_queryset(self):
|
||||
return self.get_filter().get_queryset(user=self.request.user)
|
||||
|
||||
|
||||
class HomeView(TemplateView):
|
||||
template_name = 'appearance/home.html'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user