2023-09-25 08:25:44 +00:00
|
|
|
from django.template.loader import render_to_string
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2023-12-25 07:24:35 +00:00
|
|
|
from accounts.models import Account
|
2023-10-07 09:41:20 +00:00
|
|
|
from assets.models import Asset
|
2023-09-25 08:25:44 +00:00
|
|
|
from audits.models import UserLoginLog
|
|
|
|
from notifications.notifications import UserMessage
|
|
|
|
from users.models import User
|
|
|
|
|
|
|
|
|
|
|
|
class UserLoginReminderMsg(UserMessage):
|
|
|
|
subject = _('User login reminder')
|
|
|
|
|
|
|
|
def __init__(self, user, user_log: UserLoginLog):
|
|
|
|
self.user_log = user_log
|
|
|
|
super().__init__(user)
|
|
|
|
|
|
|
|
def get_html_msg(self) -> dict:
|
|
|
|
user_log = self.user_log
|
|
|
|
context = {
|
|
|
|
'ip': user_log.ip,
|
|
|
|
'city': user_log.city,
|
|
|
|
'username': user_log.username,
|
2023-12-25 07:24:35 +00:00
|
|
|
'recipient': self.user,
|
2023-09-25 08:25:44 +00:00
|
|
|
'user_agent': user_log.user_agent,
|
|
|
|
}
|
|
|
|
message = render_to_string('acls/user_login_reminder.html', context)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'subject': str(self.subject),
|
|
|
|
'message': message
|
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
user = User.objects.first()
|
|
|
|
user_log = UserLoginLog.objects.first()
|
|
|
|
return cls(user, user_log)
|
2023-10-07 09:41:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetLoginReminderMsg(UserMessage):
|
|
|
|
subject = _('Asset login reminder')
|
|
|
|
|
2023-10-12 12:04:28 +00:00
|
|
|
def __init__(self, user, asset: Asset, login_user: User, account_username):
|
2023-10-07 09:41:20 +00:00
|
|
|
self.asset = asset
|
|
|
|
self.login_user = login_user
|
2023-10-12 12:04:28 +00:00
|
|
|
self.account_username = account_username
|
2023-10-07 09:41:20 +00:00
|
|
|
super().__init__(user)
|
|
|
|
|
|
|
|
def get_html_msg(self) -> dict:
|
2023-12-25 07:24:35 +00:00
|
|
|
account = Account.objects.get(asset=self.asset, username=self.account_username)
|
2023-10-07 09:41:20 +00:00
|
|
|
context = {
|
2023-12-25 07:24:35 +00:00
|
|
|
'recipient': self.user,
|
2023-10-07 09:41:20 +00:00
|
|
|
'username': self.login_user.username,
|
2023-12-25 07:24:35 +00:00
|
|
|
'name': self.login_user.name,
|
2023-10-07 09:41:20 +00:00
|
|
|
'asset': str(self.asset),
|
2023-10-12 12:04:28 +00:00
|
|
|
'account': self.account_username,
|
2023-12-25 07:24:35 +00:00
|
|
|
'account_name': account.name,
|
2023-10-07 09:41:20 +00:00
|
|
|
}
|
|
|
|
message = render_to_string('acls/asset_login_reminder.html', context)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'subject': str(self.subject),
|
|
|
|
'message': message
|
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
user = User.objects.first()
|
|
|
|
asset = Asset.objects.first()
|
|
|
|
return cls(user, asset, user)
|