# Path: apps/core/models.py
# class UserLog(models.Model):
#     """
#     denotes single log for an user or global event
#     - user:  user object
#     - level: level of log; python log level
#     - time:  event time
#     - ip:    event ip
#     - hide:  hide log in user log page
#     - text:  detail log message
#     """
#     user = models.ForeignKey(User, on_delete=models.CASCADE,
#                              related_name='user_logs', blank=True, null=True)
#     level = models.IntegerField()
#     time = models.DateTimeField(auto_now=True)
#     ip = models.GenericIPAddressField()
#     hide = models.BooleanField(default=False)
#     text = models.CharField(max_length=500)
# 
#     def pretty(self):
#         time = localtime(self.time).isoformat()
#         username = self.user.username if self.user else 'unknown'
#         level, ip, text = self.level, self.ip, self.text
#         return f'{time}/{level} ({ip}, {username}) {text}'
# 
#     def __str__(self):
#         time_str = localtime(self.time).isoformat()
#         return f'{time_str}/{self.level} ({self.user}) {self.text}'


from datetime import datetime
from os import path
from django.conf import settings
from django.core.mail import send_mail
from django.core.management.base import BaseCommand
from django.utils import timezone
from apps.core.models import UserLog
    timestamp_file = path.join(settings.LOG_DIR, 'warning.stamp')

    def get_timestamp(self):
        try:
            with open(self.timestamp_file, 'r') as f:
                return float(f.read().strip())
        except Exception:
            return 0.0

    def set_timestamp(self, timestamp):
        with open(self.timestamp_file, 'w') as f:
            f.write(str(timestamp))

    def handle(self, *args, **options):
        start_time = timezone.make_aware(
            datetime.fromtimestamp(self.get_timestamp()),
        )
        logs = UserLog.objects.filter(
            time__gt=start_time, level__gte=30,
        ).all()
        if not logs:
            return

        end_time = max(list(map(lambda x: x.time, logs)))
        self.set_timestamp(end_time.timestamp())

        if settings.DEBUG:
            return

        content = ''.join(list(map(lambda x: x.pretty() + '<br/>\n', logs)))

# Path: apps/core/models.py
# class UserLog(models.Model):
#     """
#     denotes single log for an user or global event
#     - user:  user object
#     - level: level of log; python log level
#     - time:  event time
#     - ip:    event ip
#     - hide:  hide log in user log page
#     - text:  detail log message
#     """
#     user = models.ForeignKey(User, on_delete=models.CASCADE,
#                              related_name='user_logs', blank=True, null=True)
#     level = models.IntegerField()
#     time = models.DateTimeField(auto_now=True)
#     ip = models.GenericIPAddressField()
#     hide = models.BooleanField(default=False)
#     text = models.CharField(max_length=500)
# 
#     def pretty(self):
#         time = localtime(self.time).isoformat()
#         username = self.user.username if self.user else 'unknown'
#         level, ip, text = self.level, self.ip, self.text
#         return f'{time}/{level} ({ip}, {username}) {text}'
# 
#     def __str__(self):
#         time_str = localtime(self.time).isoformat()
#         return f'{time_str}/{self.level} ({self.user}) {self.text}'


# Path: apps/core/management/commands/send-mail.py
from datetime import datetime
from os import path
from django.conf import settings
from django.core.mail import send_mail
from django.core.management.base import BaseCommand
from django.utils import timezone
from apps.core.models import UserLog

    timestamp_file = path.join(settings.LOG_DIR, 'warning.stamp')

    def get_timestamp(self):
        try:
            with open(self.timestamp_file, 'r') as f:
                return float(f.read().strip())
        except Exception:
            return 0.0

    def set_timestamp(self, timestamp):
        with open(self.timestamp_file, 'w') as f:
            f.write(str(timestamp))

    def handle(self, *args, **options):
        start_time = timezone.make_aware(
            datetime.fromtimestamp(self.get_timestamp()),
        )
        logs = UserLog.objects.filter(
            time__gt=start_time, level__gte=30,
        ).all()
        if not logs:
            return

        end_time = max(list(map(lambda x: x.time, logs)))
        self.set_timestamp(end_time.timestamp())

        if settings.DEBUG:
            return

        content = ''.join(list(map(lambda x: x.pretty() + '<br/>\n', logs)))

        emails = list(map(lambda x: x[1], settings.ADMINS))