# Path: weechat/common/path.py
# def files_path_join(*args):
#     """Join multiple paths after settings.FILES_ROOT."""
#     return __path_join(settings.FILES_ROOT, *args)
# 
# Path: weechat/common/templatetags/localdate.py
# @register.filter()
# def localdate(value, fmt='date'):
#     """
#     Format date with localized date/time format.
#     If fmt == "date", the localized date format is used.
#     If fmt == "datetime", the localized date/fime format is used.
#     Another fmt it is used as-is.
#     """
#     if fmt == 'date':
#         fmt = gettext(settings.DATE_FORMAT)
#     elif fmt == 'datetime':
#         fmt = gettext(settings.DATETIME_FORMAT)
#     date_iso = value.isoformat()
#     date_fmt = dateformat.format(value, fmt)
#     return mark_safe(f'<time datetime="{date_iso}">{date_fmt}</time>')
# 
# Path: weechat/common/utils.py
# def version_to_tuple(version):
#     """Convert version to a tuple of integers."""
#     return tuple(map(int, version.split('.')))


from datetime import datetime
from hashlib import sha1, sha512
from django.conf import settings
from django.db import models
from django.db.models.signals import pre_save
from weechat.common.path import files_path_join
from weechat.common.templatetags.localdate import localdate
from weechat.common.utils import version_to_tuple
import os
import pytz


class Release(models.Model):
    """A WeeChat release."""
    version = models.CharField(max_length=64, primary_key=True)
    description = models.CharField(max_length=64, blank=True)
    date = models.DateField(blank=True, null=True)
    security_issues_fixed = models.IntegerField(default=0)
    securityfix = models.CharField(max_length=256, blank=True)

    def __str__(self):
        security_fix = (f', {self.security_issues_fixed} SECURITY FIX'
                        if self.security_issues_fixed > 0 else '')
        fixed_in = (f', fix in: {", ".join(self.securityfix.split(","))}'
                    if self.securityfix else '')
        return f'{self.version} ({self.date}){security_fix}{fixed_in}'

    def date_l10n(self):
        """Return the release date formatted with localized date format."""
        return localdate(self.date)

    def security_fixed_versions(self):
        """Return the list of versions fixing security issues."""
        return self.securityfix.split(',')

    @property
    def is_released(self):
        """Return True if the version is released."""
        stable_version = Release.objects.get(version='stable').description
        stable_version_tuple = version_to_tuple(stable_version)

# Path: weechat/common/path.py
# def files_path_join(*args):
#     """Join multiple paths after settings.FILES_ROOT."""
#     return __path_join(settings.FILES_ROOT, *args)
# 
# Path: weechat/common/templatetags/localdate.py
# @register.filter()
# def localdate(value, fmt='date'):
#     """
#     Format date with localized date/time format.
#     If fmt == "date", the localized date format is used.
#     If fmt == "datetime", the localized date/fime format is used.
#     Another fmt it is used as-is.
#     """
#     if fmt == 'date':
#         fmt = gettext(settings.DATE_FORMAT)
#     elif fmt == 'datetime':
#         fmt = gettext(settings.DATETIME_FORMAT)
#     date_iso = value.isoformat()
#     date_fmt = dateformat.format(value, fmt)
#     return mark_safe(f'<time datetime="{date_iso}">{date_fmt}</time>')
# 
# Path: weechat/common/utils.py
# def version_to_tuple(version):
#     """Convert version to a tuple of integers."""
#     return tuple(map(int, version.split('.')))


# Path: weechat/download/models.py
from datetime import datetime
from hashlib import sha1, sha512
from django.conf import settings
from django.db import models
from django.db.models.signals import pre_save
from weechat.common.path import files_path_join
from weechat.common.templatetags.localdate import localdate
from weechat.common.utils import version_to_tuple
import os
import pytz



class Release(models.Model):
    """A WeeChat release."""
    version = models.CharField(max_length=64, primary_key=True)
    description = models.CharField(max_length=64, blank=True)
    date = models.DateField(blank=True, null=True)
    security_issues_fixed = models.IntegerField(default=0)
    securityfix = models.CharField(max_length=256, blank=True)

    def __str__(self):
        security_fix = (f', {self.security_issues_fixed} SECURITY FIX'
                        if self.security_issues_fixed > 0 else '')
        fixed_in = (f', fix in: {", ".join(self.securityfix.split(","))}'
                    if self.securityfix else '')
        return f'{self.version} ({self.date}){security_fix}{fixed_in}'

    def date_l10n(self):
        """Return the release date formatted with localized date format."""
        return localdate(self.date)

    def security_fixed_versions(self):
        """Return the list of versions fixing security issues."""
        return self.securityfix.split(',')

    @property
    def is_released(self):
        """Return True if the version is released."""
        stable_version = Release.objects.get(version='stable').description
        stable_version_tuple = version_to_tuple(stable_version)

        return version_to_tuple(self.version) <= stable_version_tuple