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):
|
2024-05-24 07:42:12 +00:00
|
|
|
subject = _('User login alert for asset')
|
2023-10-07 09:41:20 +00:00
|
|
|
|
2024-02-06 03:33:04 +00:00
|
|
|
def __init__(self, user, asset: Asset, login_user: User, account: Account, input_username):
|
2023-10-07 09:41:20 +00:00
|
|
|
self.asset = asset
|
|
|
|
self.login_user = login_user
|
2024-02-06 03:33:04 +00:00
|
|
|
self.account = account
|
|
|
|
self.input_username = input_username
|
2023-10-07 09:41:20 +00:00
|
|
|
super().__init__(user)
|
|
|
|
|
|
|
|
def get_html_msg(self) -> dict:
|
|
|
|
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),
|
2024-02-06 03:33:04 +00:00
|
|
|
'account': self.input_username,
|
|
|
|
'account_name': self.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)
|