Skip to content

Commit

Permalink
Merge 7ad71ce into b9f2ffe
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszkarykowski committed Jan 14, 2020
2 parents b9f2ffe + 7ad71ce commit e0923c0
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 2 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
104 changes: 104 additions & 0 deletions src/ralph/sim_cards/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import datetime
import os
import tempfile
from functools import partial

from dj.choices import Choices
Expand All @@ -10,17 +13,23 @@
RegexValidator
)
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

from ralph.attachments.helpers import add_attachment_from_disk
from ralph.back_office.models import autocomplete_user, Warehouse
from ralph.lib.external_services import ExternalService, obj_to_dict

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 Report, ReportLanguage

PUK_CODE_VALIDATORS = [
MinLengthValidator(5),
Expand Down Expand Up @@ -157,6 +166,101 @@ 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
def _generate_report(cls, name, requester, instances, language):
report = Report.objects.get(name=name)
template = report.templates.filter(language=language).first()
if not template:
template = report.templates.filter(default=True).first()

template_content = ''
with open(template.template.path, 'rb') as f:
template_content = f.read()

data_instances = [
{
'card_number': obj.card_number,
'carrier': obj.carrier.name,
'pin1': obj.pin1,
'puk1': obj.puk1,
'phone_number': obj.phone_number,
}
for obj in instances
]
service_pdf = ExternalService('PDF')
result = service_pdf.run(
template=template_content,
data={
'id': ', '.join([str(obj.id) for obj in instances]),
'now': datetime.datetime.now(),
'logged_user': obj_to_dict(requester),
'affected_user': obj_to_dict(instances[0].user),
'owner': obj_to_dict(instances[0].owner),
'assets': data_instances,
}
)
filename = "_".join([
timezone.now().isoformat()[:10],
instances[0].user.get_full_name().lower().replace(' ', '-'),
name,
]) + '.pdf'
with tempfile.TemporaryDirectory() as tmp_dirpath:
output_path = os.path.join(tmp_dirpath, filename)
with open(output_path, 'wb') as f:
f.write(result)
return add_attachment_from_disk(
instances, output_path, requester,
_('Document autogenerated by {} transition.').format(name)
)

@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 SIMCard._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 e0923c0

Please sign in to comment.