Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

access card acceptance transition #3672

Merged
merged 5 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ tags
*.lock
.*.swp
.*.swo
.envrc
2 changes: 1 addition & 1 deletion docs/development/transitions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Transitions
The transition change state (e.g. status) of object from one to another - that's helps in product lifecycle management. For each object (asset, support, licence) you can define some workflow (set of transitions) and provide special actions for each transition.
The transition change state (e.g. status) of object from one to another - that helps in product lifecycle management. For each object (asset, support, licence) you can define some workflow (set of transitions) and provide special actions for each transition.

## Custom template for transition
For each transition you can separately specify Django's template in the transition's form, to do this add new item to ``TRANSITION_TEMPLATES`` in your settings file. For instance:
Expand Down
17 changes: 17 additions & 0 deletions src/ralph/access_cards/migrations/0005_auto_20211022_1038.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations

from ralph.lib.transitions.migration import TransitionActionMigration


class Migration(migrations.Migration):

dependencies = [
('access_cards', '0004_auto_20200513_1014'),
]

operations = [
TransitionActionMigration()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

]
79 changes: 79 additions & 0 deletions src/ralph/access_cards/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
from mptt.models import MPTTModel

from ralph.accounts.models import RalphUser, Regionalizable
from ralph.attachments.utils import send_transition_attachments_to_user
from ralph.back_office.models import autocomplete_user
from ralph.lib.hooks import get_hook
from ralph.lib.mixins.models import AdminAbsoluteUrlMixin, TimeStampMixin
from ralph.lib.transitions.conf import get_report_name_for_transition_id
from ralph.lib.transitions.decorators import transition_action
from ralph.lib.transitions.fields import TransitionField
from ralph.lib.transitions.models import TransitionWorkflowBaseWithPermissions
from ralph.reports.helpers import generate_report
from ralph.reports.models import ReportLanguage


class AccessCardStatus(Choices):
Expand Down Expand Up @@ -191,3 +196,77 @@ def add_notes(cls, instances, **kwargs):
instance.notes = '{}\n{}'.format(
instance.notes, kwargs['notes']
)

@classmethod
@transition_action(
run_after=['release_report']
)
def assign_requester_as_an_owner(cls, instances, requester, **kwargs):
"""Assign current user as an owner"""
for instance in instances:
instance.owner = requester
instance.save()

@classmethod
@transition_action(
form_fields={
'accept': {
'field': forms.BooleanField(
label=_(
'I have read and fully understand and '
'accept the agreement.'
)
)
},
}
)
def accept_asset_release_agreement(cls, instances, requester, **kwargs):
pass

@classmethod
@transition_action(
form_fields={
'report_language': {
'field': forms.ModelChoiceField(
label=_('Release report language'),
queryset=ReportLanguage.objects.all().order_by('-default'),
empty_label=None
),
'exclude_from_history': True
}
},
return_attachment=True,
run_after=['assign_owner', 'assign_user']
)
def release_report(cls, instances, requester, transition_id, **kwargs):
report_name = get_report_name_for_transition_id(transition_id)
return generate_report(
instances=instances, name=report_name, requester=requester,
language=kwargs['report_language'],
context=cls._get_report_context(instances)
)

@classmethod
@transition_action(run_after=['release_report'])
def send_attachments_to_user(
cls, requester, transition_id, **kwargs
):
context_func = get_hook(
'back_office.transition_action.email_context'
)
send_transition_attachments_to_user(
requester=requester,
transition_id=transition_id,
context_func=context_func,
**kwargs
)

@classmethod
def _get_report_context(cls, instances):
context = [
{
'visual_number': obj.visual_number,
}
for obj in instances
]
return context
30 changes: 30 additions & 0 deletions src/ralph/accounts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings
from django.core.urlresolvers import reverse

from ralph.access_cards.models import AccessCard
from ralph.admin.sites import ralph_site
from ralph.back_office.models import BackOfficeAsset
from ralph.lib.transitions.models import Transition
Expand All @@ -17,6 +18,8 @@
ACCEPTANCE_BACK_OFFICE_ACCEPT_LOAN_STATUS = settings.ACCEPT_ASSETS_FOR_CURRENT_USER_CONFIG['BACK_OFFICE_ACCEPT_LOAN_STATUS'] # noqa: E509
ACCEPTANCE_RETURN_TRANSITION_ID = settings.ACCEPT_ASSETS_FOR_CURRENT_USER_CONFIG['RETURN_TRANSITION_ID'] # noqa: E509
ACCEPTANCE_BACK_OFFICE_RETURN_STATUS = settings.ACCEPT_ASSETS_FOR_CURRENT_USER_CONFIG['BACK_OFFICE_ACCEPT_RETURN_STATUS'] # noqa: E509
ACCEPTANCE_ACCESS_CARD_TRANSITION_ID = settings.ACCEPT_ASSETS_FOR_CURRENT_USER_CONFIG['TRANSITION_ACCESS_CARD_ID'] # noqa: E509
ACCEPTANCE_ACCESS_CARD_ACCEPT_STATUS = settings.ACCEPT_ASSETS_FOR_CURRENT_USER_CONFIG['ACCESS_CARD_ACCEPT_ACCEPT_STATUS'] # noqa: E509


def transition_exists(transition_id):
Expand All @@ -34,6 +37,9 @@ def transition_exists(transition_id):
loan_transition_exists = partial(
transition_exists, ACCEPTANCE_LOAN_TRANSITION_ID
)
acceptance_access_card_transition_exists = partial(
transition_exists, ACCEPTANCE_ACCESS_CARD_TRANSITION_ID
)


def get_assets(user, status):
Expand All @@ -47,6 +53,14 @@ def get_simcards(user, status):
status=status
).filter(user=user)


def get_access_cards(user, status):
return AccessCard.objects.filter(
status=status,
user=user
)


get_assets_to_accept = partial(
get_assets, status=ACCEPTANCE_BACK_OFFICE_ACCEPT_STATUS
)
Expand All @@ -60,6 +74,9 @@ def get_simcards(user, status):
get_assets_to_accept_return = partial(
get_assets, status=ACCEPTANCE_BACK_OFFICE_RETURN_STATUS
)
get_access_cards_to_accept = partial(
get_access_cards, status=ACCEPTANCE_ACCESS_CARD_ACCEPT_STATUS
)


def get_acceptance_url(user):
Expand Down Expand Up @@ -112,3 +129,16 @@ def get_return_acceptance_url(user):
query = urlencode([('select', a.id) for a in assets_to_accept])
return '?'.join((url, query))
return None


def get_access_card_acceptance_url(user):
assets_to_accept = get_access_cards_to_accept(user)
admin_instance = ralph_site.get_admin_instance_for_model(
AccessCard
)
url_name = admin_instance.get_transition_bulk_url_name()
if assets_to_accept:
url = reverse(url_name, args=(ACCEPTANCE_ACCESS_CARD_TRANSITION_ID,))
query = urlencode([('select', a.id) for a in assets_to_accept])
return '?'.join((url, query))
return None
17 changes: 17 additions & 0 deletions src/ralph/admin/templatetags/dashboard_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from ralph.accounts.helpers import (
get_acceptance_url,
get_access_card_acceptance_url,
get_access_cards_to_accept,
get_assets_to_accept,
get_assets_to_accept_loan,
get_assets_to_accept_return,
Expand Down Expand Up @@ -68,6 +70,18 @@ def get_user_simcard_to_accept_tile_data(user):
}


def get_user_access_card_to_accept_tile_data(user):
access_card_to_accept_count = get_access_cards_to_accept(user).count()
if not access_card_to_accept_count:
return None
return {
'class': 'equipment-to-accept',
'label': _('Access Card pick up'),
'count': access_card_to_accept_count,
'url': get_access_card_acceptance_url(user),
}


def get_user_equipment_to_accept_loan_tile_data(user):
assets_to_accept_count = get_assets_to_accept_loan(user).count()
if not assets_to_accept_count:
Expand Down Expand Up @@ -196,6 +210,9 @@ def ralph_summary(context):
accept_for_simcard_tile_data = get_user_simcard_to_accept_tile_data(user=user) # noqa
if accept_for_simcard_tile_data:
results.append(accept_for_simcard_tile_data)
accept_for_access_card_tile_data = get_user_access_card_to_accept_tile_data(user=user) # noqa
if accept_for_access_card_tile_data:
results.append(accept_for_access_card_tile_data)
accept_for_loan_tile_data = get_user_equipment_to_accept_loan_tile_data(user=user) # noqa
if accept_for_loan_tile_data:
results.append(accept_for_loan_tile_data)
Expand Down
20 changes: 20 additions & 0 deletions src/ralph/lib/transitions/migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db.migrations.operations.base import Operation


class TransitionActionMigration(Operation):
"""
Force transition related `Action` objects creation or update.
This is an empty migration used only to trigger `post_migrate` signal.
"""
def state_forwards(self, app_label, state):
pass

def database_forwards(
self, app_label, schema_editor, from_state, to_state
):
pass

def database_backwards(
self, app_label, schema_editor, from_state, to_state
):
pass
2 changes: 2 additions & 0 deletions src/ralph/lib/transitions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,8 @@ def update_transitions_after_migrate(**kwargs):
"""
Create or update transition for models which detetected
TRANSITION_ATTR_TAG in any field in model.
To force transition update if no database related model fields change,
use `ralph.lib.transitions.migration.TransitionActionMigration`
"""
sender_models = list(kwargs['sender'].get_models())
action_ids = set()
Expand Down
6 changes: 6 additions & 0 deletions src/ralph/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,19 @@ def get_sentinels(sentinels_string):
'TRANSITION_SIM_ID': os.environ.get(
'ACCEPT_SIMCARD_FOR_CURRENT_USER_CONFIG', None
),
'TRANSITION_ACCESS_CARD_ID': os.environ.get(
'ACCEPT_ACCESS_CARD_FOR_CURRENT_USER_CONFIG', None
),
# in_progress by default
'BACK_OFFICE_ACCEPT_STATUS': os.environ.get(
'ACCEPT_ASSETS_FOR_CURRENT_USER_BACK_OFFICE_ACCEPT_STATUS', 2
),
'SIMCARD_ACCEPT_STATUS': os.environ.get(
'SIMCARD_FOR_CURRENT_USER_BACK_OFFICE_ACCEPT_STATUS', 2
),
'ACCESS_CARD_ACCEPT_ACCEPT_STATUS': os.environ.get(
'ACCESS_CARD_FOR_CURRENT_USER_BACK_OFFICE_ACCEPT_STATUS', 2
),
'LOAN_TRANSITION_ID': os.environ.get(
'LOAN_ASSETS_FOR_CURRENT_USER_TRANSITION_ID', None
),
Expand Down