2021-10-20 11:45:37 +00:00
|
|
|
from django.template.loader import render_to_string
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2021-09-24 02:21:56 +00:00
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
from common.utils import reverse as js_reverse
|
|
|
|
from notifications.notifications import UserMessage
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
|
|
|
2021-10-22 12:06:16 +00:00
|
|
|
class PermedAssetsWillExpireUserMsg(UserMessage):
|
2022-06-14 08:03:42 +00:00
|
|
|
def __init__(self, user, assets, day_count=0):
|
2021-09-24 02:21:56 +00:00
|
|
|
super().__init__(user)
|
|
|
|
self.assets = assets
|
2023-12-11 03:14:36 +00:00
|
|
|
self.day_count = _('today') if day_count == 0 else day_count + _('day')
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
|
|
def get_html_msg(self) -> dict:
|
2021-10-20 11:45:37 +00:00
|
|
|
subject = _("You permed assets is about to expire")
|
|
|
|
context = {
|
|
|
|
'name': self.user.name,
|
2022-06-15 07:01:56 +00:00
|
|
|
'count': self.day_count,
|
2021-10-20 11:45:37 +00:00
|
|
|
'items': [str(asset) for asset in self.assets],
|
|
|
|
'item_type': _("permed assets"),
|
|
|
|
'show_help': True
|
|
|
|
}
|
|
|
|
message = render_to_string('perms/_msg_permed_items_expire.html', context)
|
2021-09-24 02:21:56 +00:00
|
|
|
return {
|
|
|
|
'subject': subject,
|
|
|
|
'message': message
|
|
|
|
}
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
@classmethod
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
from users.models import User
|
|
|
|
from assets.models import Asset
|
|
|
|
user = User.objects.first()
|
|
|
|
assets = Asset.objects.all()[:10]
|
|
|
|
return cls(user, assets)
|
2021-09-24 02:21:56 +00:00
|
|
|
|
|
|
|
|
2021-10-21 12:03:05 +00:00
|
|
|
class AssetPermsWillExpireForOrgAdminMsg(UserMessage):
|
2021-09-24 02:21:56 +00:00
|
|
|
|
2022-06-14 08:03:42 +00:00
|
|
|
def __init__(self, user, perms, org, day_count=0):
|
2021-09-24 02:21:56 +00:00
|
|
|
super().__init__(user)
|
|
|
|
self.perms = perms
|
|
|
|
self.org = org
|
2023-12-05 11:09:17 +00:00
|
|
|
self.day_count = _('today') if day_count == 0 else day_count + _('day')
|
2021-09-24 02:21:56 +00:00
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
def get_items_with_url(self):
|
|
|
|
items_with_url = []
|
|
|
|
for perm in self.perms:
|
|
|
|
url = js_reverse(
|
|
|
|
'perms:asset-permission-detail',
|
|
|
|
kwargs={'pk': perm.id}, external=True,
|
2022-07-20 07:44:00 +00:00
|
|
|
api_to_ui=True, is_console=True
|
2021-10-20 11:45:37 +00:00
|
|
|
) + f'?oid={perm.org_id}'
|
|
|
|
items_with_url.append([perm.name, url])
|
|
|
|
return items_with_url
|
|
|
|
|
|
|
|
def get_html_msg(self):
|
|
|
|
items_with_url = self.get_items_with_url()
|
|
|
|
subject = _("Asset permissions is about to expire")
|
|
|
|
context = {
|
|
|
|
'name': self.user.name,
|
2022-07-25 06:02:07 +00:00
|
|
|
'count': str(self.day_count),
|
2021-10-20 11:45:37 +00:00
|
|
|
'items_with_url': items_with_url,
|
|
|
|
'item_type': _('asset permissions of organization {}').format(self.org)
|
|
|
|
}
|
|
|
|
message = render_to_string('perms/_msg_item_permissions_expire.html', context)
|
2021-09-24 02:21:56 +00:00
|
|
|
return {
|
|
|
|
'subject': subject,
|
|
|
|
'message': message
|
|
|
|
}
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
@classmethod
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
from users.models import User
|
|
|
|
from perms.models import AssetPermission
|
|
|
|
from orgs.models import Organization
|
2021-09-24 02:21:56 +00:00
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
user = User.objects.first()
|
|
|
|
perms = AssetPermission.objects.all()[:10]
|
|
|
|
org = Organization.objects.first()
|
|
|
|
return cls(user, perms, org)
|