# 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 Package(models.Model):
    """A WeeChat package."""
    version = models.ForeignKey(Release, on_delete=models.CASCADE)
    type = models.ForeignKey(Type, on_delete=models.CASCADE)
    filename = models.CharField(max_length=512, blank=True)
    sha1sum = models.CharField(max_length=128, blank=True)
    sha512sum = models.CharField(max_length=128, blank=True)
    display_time = models.BooleanField(default=False)
    directory = models.CharField(max_length=256, blank=True)
    url = models.CharField(max_length=512, blank=True)
    text = models.CharField(max_length=512, blank=True)

    def __str__(self):
        if self.filename != '':
            string = self.filename
        elif self.directory != '':
            string = self.directory
        elif self.url != '':
            string = self.url
        else:
            string = self.text
        return f'{self.version.version}-{self.type.type}, {string}'

    def fullname(self):
        """Return the path for package."""
        if self.filename:
            return files_path_join(self.type.directory, self.filename)
        if self.directory:

# 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 Package(models.Model):
    """A WeeChat package."""
    version = models.ForeignKey(Release, on_delete=models.CASCADE)
    type = models.ForeignKey(Type, on_delete=models.CASCADE)
    filename = models.CharField(max_length=512, blank=True)
    sha1sum = models.CharField(max_length=128, blank=True)
    sha512sum = models.CharField(max_length=128, blank=True)
    display_time = models.BooleanField(default=False)
    directory = models.CharField(max_length=256, blank=True)
    url = models.CharField(max_length=512, blank=True)
    text = models.CharField(max_length=512, blank=True)

    def __str__(self):
        if self.filename != '':
            string = self.filename
        elif self.directory != '':
            string = self.directory
        elif self.url != '':
            string = self.url
        else:
            string = self.text
        return f'{self.version.version}-{self.type.type}, {string}'

    def fullname(self):
        """Return the path for package."""
        if self.filename:
            return files_path_join(self.type.directory, self.filename)
        if self.directory:

            return files_path_join(self.directory)