Skip to content

Commit

Permalink
Merge 7f51250 into c414a68
Browse files Browse the repository at this point in the history
  • Loading branch information
romcheg authored Dec 17, 2019
2 parents c414a68 + 7f51250 commit 3887b81
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/ralph/accounts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
from ralph.admin.sites import ralph_site
from ralph.back_office.models import BackOfficeAsset
from ralph.lib.transitions.models import Transition
from ralph.sim_cards.models import SIMCard

ACCEPTANCE_TRANSITION_ID = settings.ACCEPT_ASSETS_FOR_CURRENT_USER_CONFIG['TRANSITION_ID'] # noqa: E509
ACCEPTANCE_SIM_TRANSITION_ID = settings.ACCEPT_ASSETS_FOR_CURRENT_USER_CONFIG['TRANSITION_SIM_ID'] # noqa: E509
ACCEPTANCE_BACK_OFFICE_ACCEPT_STATUS = settings.ACCEPT_ASSETS_FOR_CURRENT_USER_CONFIG['BACK_OFFICE_ACCEPT_STATUS'] # noqa: E509
ACCEPTANCE_SIMCARD_ACCEPT_STATUS = settings.ACCEPT_ASSETS_FOR_CURRENT_USER_CONFIG['SIMCARD_ACCEPT_STATUS'] # noqa: E509
ACCEPTANCE_LOAN_TRANSITION_ID = settings.ACCEPT_ASSETS_FOR_CURRENT_USER_CONFIG['LOAN_TRANSITION_ID'] # noqa: E509
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
Expand All @@ -25,6 +28,9 @@ def transition_exists(transition_id):
acceptance_transition_exists = partial(
transition_exists, ACCEPTANCE_TRANSITION_ID
)
acceptance_sim_transition_exists = partial(
transition_exists, ACCEPTANCE_SIM_TRANSITION_ID
)
loan_transition_exists = partial(
transition_exists, ACCEPTANCE_LOAN_TRANSITION_ID
)
Expand All @@ -36,9 +42,18 @@ def get_assets(user, status):
).filter(user=user)


def get_simcards(user, status):
return SIMCard.objects.filter(
status=status
).filter(user=user)

get_assets_to_accept = partial(
get_assets, status=ACCEPTANCE_BACK_OFFICE_ACCEPT_STATUS
)

get_simcards_to_accept = partial(
get_simcards, status=ACCEPTANCE_SIMCARD_ACCEPT_STATUS
)
get_assets_to_accept_loan = partial(
get_assets, status=ACCEPTANCE_BACK_OFFICE_ACCEPT_LOAN_STATUS
)
Expand All @@ -60,6 +75,19 @@ def get_acceptance_url(user):
return None


def get_simcard_acceptance_url(user):
assets_to_accept = get_simcards_to_accept(user)
admin_instance = ralph_site.get_admin_instance_for_model(
SIMCard
)
url_name = admin_instance.get_transition_bulk_url_name()
if assets_to_accept:
url = reverse(url_name, args=(ACCEPTANCE_SIM_TRANSITION_ID,))
query = urlencode([('select', a.id) for a in assets_to_accept])
return '?'.join((url, query))
return None


def get_loan_acceptance_url(user):
assets_to_accept = get_assets_to_accept_loan(user)
admin_instance = ralph_site.get_admin_instance_for_model(
Expand Down
19 changes: 18 additions & 1 deletion src/ralph/admin/templatetags/dashboard_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
get_assets_to_accept_loan,
get_assets_to_accept_return,
get_loan_acceptance_url,
get_return_acceptance_url
get_return_acceptance_url,
get_simcard_acceptance_url,
get_simcards_to_accept
)
from ralph.assets.models import BaseObject, Service, ServiceEnvironment
from ralph.back_office.models import BackOfficeAsset
Expand Down Expand Up @@ -54,6 +56,18 @@ def get_user_equipment_to_accept_tile_data(user):
}


def get_user_simcard_to_accept_tile_data(user):
simcard_to_accept_count = get_simcards_to_accept(user).count()
if not simcard_to_accept_count:
return None
return {
'class': 'equipment-to-accept',
'label': _('SIM Card to pick up'),
'count': simcard_to_accept_count,
'url': get_simcard_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 @@ -179,6 +193,9 @@ def ralph_summary(context):
accept_tile_data = get_user_equipment_to_accept_tile_data(user=user)
if accept_tile_data:
results.append(accept_tile_data)
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_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
11 changes: 10 additions & 1 deletion src/ralph/admin/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from ..templatetags.dashboard_tags import (
get_user_equipment_to_accept_loan_tile_data,
get_user_equipment_to_accept_return_tile_data,
get_user_equipment_to_accept_tile_data)
get_user_equipment_to_accept_tile_data,
get_user_simcard_to_accept_tile_data)


class TestTemplatetags(TestCase):
Expand Down Expand Up @@ -34,3 +35,11 @@ def test_hardware_return_label(self, mocked_gatar, mocked_grau):
mocked_grau.return_value = "boo"
ret = get_user_equipment_to_accept_return_tile_data(None)
assert ret['label'] == _('Hardware return')

@patch('ralph.admin.templatetags.dashboard_tags.get_simcard_acceptance_url')
@patch('ralph.admin.templatetags.dashboard_tags.get_simcards_to_accept')
def test_simcard_release_label(self, mocked_gata, mocked_gau):
mocked_gata().count.return_value = 1
mocked_gau.return_value = "boo"
ret = get_user_simcard_to_accept_tile_data(None)
self.assertEqual(ret['label'], _('SIM Card to pick up'))
6 changes: 6 additions & 0 deletions src/ralph/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,16 @@ def get_sentinels(sentinels_string):
'TRANSITION_ID': os.environ.get(
'ACCEPT_ASSETS_FOR_CURRENT_USER_TRANSITION_ID', None
),
'TRANSITION_SIM_ID': os.environ.get(
'ACCEPT_SIMCARD_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
),
'LOAN_TRANSITION_ID': os.environ.get(
'LOAN_ASSETS_FOR_CURRENT_USER_TRANSITION_ID', None
),
Expand Down
56 changes: 55 additions & 1 deletion src/ralph/sim_cards/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _

from ralph.back_office.models import autocomplete_user, Warehouse
from ralph.back_office.models import (
autocomplete_user,
BackOfficeAsset,
Warehouse
)
from ralph.lib.mixins.models import (
AdminAbsoluteUrlMixin,
NamedMixin,
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 TransitionWorkflowBase
from ralph.reports.models import ReportLanguage

PUK_CODE_VALIDATORS = [
MinLengthValidator(5),
Expand Down Expand Up @@ -157,6 +163,54 @@ def assign_user(cls, instances, **kwargs):
for instance in instances:
instance.user = user

@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 BackOfficeAsset._generate_report(
instances=instances, name=report_name, requester=requester,
language=kwargs['report_language']
)

@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={
Expand Down

0 comments on commit 3887b81

Please sign in to comment.