Cleanup SourceColumn invocations

Update the code of some SourceColumn invocations to be model methods
instead of lambda wapped functions.

Move the translated labels to the models too.

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2018-12-21 23:47:14 -04:00
parent 5623f0b3a4
commit 213f3c1fb4
7 changed files with 109 additions and 104 deletions
+14 -28
View File
@@ -133,58 +133,44 @@ class DocumentStatesApp(MayanAppConfig):
)
SourceColumn(
source=Workflow, label=_('Label'), attribute='label'
attribute='label', is_identifier=True, source=Workflow
)
SourceColumn(
source=Workflow, label=_('Internal name'),
attribute='internal_name'
attribute='internal_name', source=Workflow
)
SourceColumn(
source=Workflow, label=_('Initial state'),
func=lambda context: context['object'].get_initial_state() or _('None')
attribute='get_initial_state', source=Workflow,
)
SourceColumn(
source=WorkflowInstance, label=_('Current state'),
attribute='get_current_state'
attribute='get_current_state', source=WorkflowInstance
)
SourceColumn(
source=WorkflowInstance, label=_('User'),
func=lambda context: getattr(
context['object'].get_last_log_entry(), 'user', _('None')
)
attribute='get_last_transition_user', source=WorkflowInstance
)
SourceColumn(
source=WorkflowInstance, label=_('Last transition'),
attribute='get_last_transition'
attribute='get_last_transition', source=WorkflowInstance
)
SourceColumn(
source=WorkflowInstance, label=_('Date and time'),
func=lambda context: getattr(
context['object'].get_last_log_entry(), 'datetime', _('None')
)
attribute='get_last_transition_datetime', kwargs={
'formatted': True
}, source=WorkflowInstance
)
SourceColumn(
source=WorkflowInstance, label=_('Completion'),
func=lambda context: getattr(
context['object'].get_current_state(), 'completion', _('None')
)
attribute='get_current_completion', source=WorkflowInstance
)
SourceColumn(
source=WorkflowInstanceLogEntry, label=_('Date and time'),
attribute='datetime'
attribute='get_rendered_datetime', source=WorkflowInstanceLogEntry
)
SourceColumn(
source=WorkflowInstanceLogEntry, label=_('User'), attribute='user'
attribute='user', source=WorkflowInstanceLogEntry
)
SourceColumn(
source=WorkflowInstanceLogEntry, label=_('Transition'),
attribute='transition'
attribute='transition', source=WorkflowInstanceLogEntry
)
SourceColumn(
source=WorkflowInstanceLogEntry, label=_('Comment'),
attribute='comment'
attribute='comment', source=WorkflowInstanceLogEntry
)
SourceColumn(
+38
View File
@@ -13,6 +13,7 @@ from django.core.exceptions import PermissionDenied, ValidationError
from django.core.files.base import ContentFile
from django.db import IntegrityError, models
from django.db.models import F, Max, Q
from django.template import Context, Template
from django.urls import reverse
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.module_loading import import_string
@@ -113,6 +114,8 @@ class Workflow(models.Model):
except self.states.model.DoesNotExist:
return None
get_initial_state.short_description = _('Initial state')
def launch_for(self, document):
try:
logger.info(
@@ -408,6 +411,10 @@ class WorkflowInstance(models.Model):
'workflow_instance': self,
}
def get_current_completion(self):
return self.get_current_state().completion
get_current_completion.short_description = _('Completion')
def get_current_state(self):
"""
Actual State - The current state of the workflow. If there are
@@ -420,6 +427,8 @@ class WorkflowInstance(models.Model):
except AttributeError:
return self.workflow.get_initial_state()
get_current_state.short_description = _('Current state')
def get_last_log_entry(self):
try:
return self.log_entries.order_by('datetime').last()
@@ -436,6 +445,28 @@ class WorkflowInstance(models.Model):
except AttributeError:
return None
get_last_transition.short_description = _('Last transition')
def get_last_transition_datetime(self, formatted=False):
entry = self.get_last_log_entry()
if not entry:
return _('None')
else:
if formatted:
return entry.get_rendered_datetime()
else:
return entry.datetime
get_last_transition_datetime.short_description = _('Date and time')
def get_last_transition_user(self):
try:
return self.get_last_log_entry().user
except AttributeError:
return _('None')
get_last_transition_user.short_description = _('User')
def get_transition_choices(self, _user=None):
current_state = self.get_current_state()
@@ -509,6 +540,13 @@ class WorkflowInstanceLogEntry(models.Model):
if self.transition not in self.workflow_instance.get_transition_choices(_user=self.user):
raise ValidationError(_('Not a valid transition choice.'))
def get_rendered_datetime(self):
return Template('{{ instance.datetime }}').render(
context=Context({'instance': self})
)
get_rendered_datetime.short_description = _('Date and time')
def save(self, *args, **kwargs):
result = super(WorkflowInstanceLogEntry, self).save(*args, **kwargs)
context = self.workflow_instance.get_context()