Unify and remove events view and widgets
Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django.template import Context, Template
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.utils.html import format_html
|
from django.utils.html import format_html
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
@@ -64,6 +65,32 @@ class EmailInput(forms.widgets.Input):
|
|||||||
return super(EmailInput, self).render(name, value, attrs=attrs)
|
return super(EmailInput, self).render(name, value, attrs=attrs)
|
||||||
|
|
||||||
|
|
||||||
|
class ObjectLinkWidget(object):
|
||||||
|
template_string = '<a href="{{ url }}">{{ object_type }}{{ label }}</a>'
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.template = Template(template_string=self.template_string)
|
||||||
|
|
||||||
|
def render(self, name=None, value=None):
|
||||||
|
label = ''
|
||||||
|
object_type = ''
|
||||||
|
url = None
|
||||||
|
|
||||||
|
if value:
|
||||||
|
label = force_text(value)
|
||||||
|
object_type = '{}: '.format(value._meta.verbose_name)
|
||||||
|
try:
|
||||||
|
url = value.get_absolute_url()
|
||||||
|
except AttributeError:
|
||||||
|
url = None
|
||||||
|
|
||||||
|
return self.template.render(
|
||||||
|
context=Context(
|
||||||
|
{'label': label, 'object_type': object_type, 'url': url or '#'}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class PlainWidget(forms.widgets.Widget):
|
class PlainWidget(forms.widgets.Widget):
|
||||||
"""
|
"""
|
||||||
Class to define a form widget that effectively nulls the htmls of a
|
Class to define a form widget that effectively nulls the htmls of a
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from mayan.apps.common import (
|
|||||||
MayanAppConfig, menu_object, menu_secondary, menu_tools, menu_topbar,
|
MayanAppConfig, menu_object, menu_secondary, menu_tools, menu_topbar,
|
||||||
menu_user
|
menu_user
|
||||||
)
|
)
|
||||||
from mayan.apps.common.widgets import TwoStateWidget
|
from mayan.apps.common.widgets import ObjectLinkWidget, TwoStateWidget
|
||||||
from mayan.apps.navigation import SourceColumn
|
from mayan.apps.navigation import SourceColumn
|
||||||
|
|
||||||
from .licenses import * # NOQA
|
from .licenses import * # NOQA
|
||||||
@@ -17,7 +17,7 @@ from .links import (
|
|||||||
link_notification_mark_read_all, link_user_notifications_list
|
link_notification_mark_read_all, link_user_notifications_list
|
||||||
)
|
)
|
||||||
from .widgets import (
|
from .widgets import (
|
||||||
widget_event_object_link, widget_event_type_link, widget_event_user_link
|
widget_event_actor_link, widget_event_type_link
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -40,18 +40,19 @@ class EventsApp(MayanAppConfig):
|
|||||||
is_sortable=True, label=_('Date and time'), source=Action
|
is_sortable=True, label=_('Date and time'), source=Action
|
||||||
)
|
)
|
||||||
SourceColumn(
|
SourceColumn(
|
||||||
func=widget_event_user_link, label=_('Actor'), source=Action
|
func=widget_event_actor_link, label=_('Actor'), source=Action
|
||||||
)
|
)
|
||||||
SourceColumn(
|
SourceColumn(
|
||||||
func=widget_event_type_link, label=_('Event'), source=Action
|
func=widget_event_type_link, label=_('Event'), source=Action
|
||||||
)
|
)
|
||||||
SourceColumn(
|
SourceColumn(
|
||||||
func=widget_event_object_link, kwargs={
|
attribute='action_object', label=_('Action object'), source=Action,
|
||||||
'attribute': 'action_object'
|
widget=ObjectLinkWidget
|
||||||
}, label=_('Action object'), source=Action
|
|
||||||
)
|
)
|
||||||
|
|
||||||
SourceColumn(
|
SourceColumn(
|
||||||
func=widget_event_object_link, label=_('Target'), source=Action
|
attribute='target', label=_('Target'), source=Action,
|
||||||
|
widget=ObjectLinkWidget
|
||||||
)
|
)
|
||||||
|
|
||||||
SourceColumn(
|
SourceColumn(
|
||||||
@@ -66,7 +67,7 @@ class EventsApp(MayanAppConfig):
|
|||||||
is_sortable=True, label=_('Date and time'), source=Notification
|
is_sortable=True, label=_('Date and time'), source=Notification
|
||||||
)
|
)
|
||||||
SourceColumn(
|
SourceColumn(
|
||||||
func=widget_event_user_link, kwargs={'attribute': 'action'},
|
func=widget_event_actor_link, kwargs={'attribute': 'action'},
|
||||||
label=_('Actor'), source=Notification
|
label=_('Actor'), source=Notification
|
||||||
)
|
)
|
||||||
SourceColumn(
|
SourceColumn(
|
||||||
@@ -74,10 +75,10 @@ class EventsApp(MayanAppConfig):
|
|||||||
label=_('Event'), source=Notification
|
label=_('Event'), source=Notification
|
||||||
)
|
)
|
||||||
SourceColumn(
|
SourceColumn(
|
||||||
func=widget_event_object_link, kwargs={
|
attribute='action.target', label=_('Target'), source=Notification,
|
||||||
'attribute': 'action.target'
|
widget=ObjectLinkWidget
|
||||||
}, label=_('Target'), source=Notification
|
|
||||||
)
|
)
|
||||||
|
|
||||||
SourceColumn(
|
SourceColumn(
|
||||||
attribute='read', is_sortable=True, label=_('Seen'),
|
attribute='read', is_sortable=True, label=_('Seen'),
|
||||||
source=Notification, widget=TwoStateWidget
|
source=Notification, widget=TwoStateWidget
|
||||||
@@ -97,5 +98,5 @@ class EventsApp(MayanAppConfig):
|
|||||||
menu_user.bind_links(
|
menu_user.bind_links(
|
||||||
links=(
|
links=(
|
||||||
link_event_types_subscriptions_list, link_current_user_events
|
link_event_types_subscriptions_list, link_current_user_events
|
||||||
)
|
), position=50
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from .views import (
|
|||||||
CurrentUserEventListView, EventListView, EventTypeSubscriptionListView,
|
CurrentUserEventListView, EventListView, EventTypeSubscriptionListView,
|
||||||
NotificationListView, NotificationMarkRead, NotificationMarkReadAll,
|
NotificationListView, NotificationMarkRead, NotificationMarkReadAll,
|
||||||
ObjectEventListView, ObjectEventTypeSubscriptionListView,
|
ObjectEventListView, ObjectEventTypeSubscriptionListView,
|
||||||
UserEventListView, VerbEventListView
|
VerbEventListView
|
||||||
)
|
)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
@@ -41,10 +41,6 @@ urlpatterns = [
|
|||||||
r'^user/$', CurrentUserEventListView.as_view(),
|
r'^user/$', CurrentUserEventListView.as_view(),
|
||||||
name='current_user_events'
|
name='current_user_events'
|
||||||
),
|
),
|
||||||
url(
|
|
||||||
r'^user/(?P<pk>\d+)/$', UserEventListView.as_view(),
|
|
||||||
name='user_events'
|
|
||||||
),
|
|
||||||
url(
|
url(
|
||||||
r'^user/event_types/subscriptions/$',
|
r'^user/event_types/subscriptions/$',
|
||||||
EventTypeSubscriptionListView.as_view(),
|
EventTypeSubscriptionListView.as_view(),
|
||||||
|
|||||||
@@ -256,26 +256,7 @@ class ObjectEventTypeSubscriptionListView(FormView):
|
|||||||
return ModelEventType.get_for_instance(instance=self.get_object())
|
return ModelEventType.get_for_instance(instance=self.get_object())
|
||||||
|
|
||||||
|
|
||||||
class UserEventListView(ObjectEventListView):
|
class CurrentUserEventListView(ObjectEventListView):
|
||||||
def get_extra_context(self):
|
|
||||||
context = super(UserEventListView, self).get_extra_context()
|
|
||||||
context.update(
|
|
||||||
{
|
|
||||||
'no_results_text': _(
|
|
||||||
'Events are actions that have been performed to this '
|
|
||||||
'user account or by this user account.'
|
|
||||||
),
|
|
||||||
'no_results_title': _('There are no events for this user'),
|
|
||||||
'object': self.get_object(),
|
|
||||||
'title': _(
|
|
||||||
'Events for user: %s'
|
|
||||||
) % self.get_object(),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return context
|
|
||||||
|
|
||||||
|
|
||||||
class CurrentUserEventListView(UserEventListView):
|
|
||||||
def get_object(self):
|
def get_object(self):
|
||||||
return self.request.user
|
return self.request.user
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.apps import apps
|
||||||
|
from django.template import Context, Template
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
@@ -10,26 +12,37 @@ from mayan.apps.common.utils import resolve_attribute
|
|||||||
from .classes import EventType
|
from .classes import EventType
|
||||||
|
|
||||||
|
|
||||||
def widget_event_object_link(context, attribute='target'):
|
def widget_event_actor_link(context, attribute=None):
|
||||||
entry = context['object']
|
entry = context['object']
|
||||||
label = ''
|
|
||||||
url = '#'
|
|
||||||
obj_type = ''
|
|
||||||
|
|
||||||
obj = resolve_attribute(obj=entry, attribute=attribute)
|
ContentType = apps.get_model(
|
||||||
|
app_label='contenttypes', model_name='ContentType'
|
||||||
|
)
|
||||||
|
|
||||||
if obj:
|
if attribute:
|
||||||
obj_type = '{}: '.format(obj._meta.verbose_name)
|
entry = getattr(entry, attribute)
|
||||||
if hasattr(obj, 'get_absolute_url'):
|
|
||||||
url = obj.get_absolute_url()
|
|
||||||
label = force_text(obj)
|
|
||||||
|
|
||||||
return mark_safe(
|
if entry.actor == entry.target:
|
||||||
'<a href="%(url)s">%(obj_type)s%(label)s</a>' % {
|
label = _('System')
|
||||||
'url': url, 'label': label, 'obj_type': obj_type
|
url = None
|
||||||
|
else:
|
||||||
|
label = entry.actor
|
||||||
|
content_type = ContentType.objects.get_for_model(model=entry.actor)
|
||||||
|
|
||||||
|
url = reverse(
|
||||||
|
viewname='events:events_for_object', kwargs={
|
||||||
|
'app_label': content_type.app_label, 'model': content_type.model,
|
||||||
|
'object_id': entry.actor.pk
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if url:
|
||||||
|
return Template(
|
||||||
|
template_string='<a href="{{ url }}">{{ label }}</a>'
|
||||||
|
).render(context=Context({'label': entry.actor, 'url': url}))
|
||||||
|
else:
|
||||||
|
return label
|
||||||
|
|
||||||
|
|
||||||
def widget_event_type_link(context, attribute=None):
|
def widget_event_type_link(context, attribute=None):
|
||||||
entry = context['object']
|
entry = context['object']
|
||||||
@@ -43,20 +56,3 @@ def widget_event_type_link(context, attribute=None):
|
|||||||
'label': EventType.get(name=entry.verb)
|
'label': EventType.get(name=entry.verb)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def widget_event_user_link(context, attribute=None):
|
|
||||||
entry = context['object']
|
|
||||||
|
|
||||||
if attribute:
|
|
||||||
entry = getattr(entry, attribute)
|
|
||||||
|
|
||||||
if entry.actor == entry.target:
|
|
||||||
return _('System')
|
|
||||||
else:
|
|
||||||
return mark_safe(
|
|
||||||
'<a href="%(url)s">%(label)s</a>' % {
|
|
||||||
'url': reverse('events:user_events', kwargs={'pk': entry.actor.pk}),
|
|
||||||
'label': entry.actor
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user