# Path: weechat/common/tracker.py
# def commits_links(commits):
#     """Replace commits or branches by GitHub URLs."""
#     if not commits:
#         return ''
# 
#     # read SVG with git commit/branch
#     filename = project_path_join('templates', 'svg', 'git-commit.html')
#     with open(filename, 'r', encoding='utf-8') as _file:
#         svg_commit = _file.read().strip()
#     filename = project_path_join('templates', 'svg', 'git-branch.html')
#     with open(filename, 'r', encoding='utf-8') as _file:
#         svg_branch = _file.read().strip()
# 
#     images = []
#     for commit in commits.split(','):
#         objtype = 'commit'
#         link = svg_commit
#         if commit.startswith('commit/'):
#             commit = commit[7:]
#         if commit.startswith('tree/'):
#             objtype = 'tree'
#             commit = commit[5:]
#             link = svg_branch
#         repo, commit_id = split_commit(commit)
#         images.append(f'<a href="https://github.com/{repo}/{objtype}/'
#                       f'{commit_id}" target="_blank" rel="noopener">'
#                       f'{link}</a>')
#     return mark_safe(' '.join(images))
# 
# def tracker_links(tracker):
#     """Replace tracker items by URLs.
# 
#     Replace GitHub tracker item(s) (for example: "closes #123")
#     and savannah tracker item(s) (for example: "bug #12345")
#     by URL(s) to this/these item(s).
#     """
#     if not tracker:
#         return ''
#     items = [_replace_link(item) for item in tracker.split(',')]
#     return mark_safe('<br>'.join(items))
# 
# 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/download/models.py
# 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
# 
#     class Meta:
#         ordering = ['-date']


from datetime import date
from django.db import models
from django.utils.safestring import mark_safe
from weechat.common.tracker import commits_links, tracker_links
from weechat.common.templatetags.localdate import localdate
from weechat.download.models import Release
class Task(models.Model):
    """A task (a new feature or bug to fix)."""
    visible = models.BooleanField(default=True)
    version = models.ForeignKey(Release, on_delete=models.CASCADE)
    tracker = models.CharField(max_length=64, blank=True)
    spec = models.CharField(max_length=512, blank=True)
    status = models.IntegerField(default=0)
    commits = models.CharField(max_length=1024, blank=True)
    component = models.CharField(max_length=64, default='core')
    description = models.CharField(max_length=512)
    priority = models.IntegerField(default=0)

    def __str__(self):
        desc = (self.description
                if len(self.description) < 100
                else f'{self.description[0:100]}…')
        version = (f'({self.version.version})' if self.visible
                   else self.version.version)
        tracker = self.tracker if self.tracker else '-'
        return (f'{version}, {tracker}, {self.status}%, {self.component}: '
                f'{desc} ({self.priority})')

    def version_date(self):
        """Return the date of version.

        It is prefixed with "≈ " if the date is in the future.
        """
        try:
            if self.version.date > date.today():
                return mark_safe(f'&asymp; {localdate(self.version.date)}')

# Path: weechat/common/tracker.py
# def commits_links(commits):
#     """Replace commits or branches by GitHub URLs."""
#     if not commits:
#         return ''
# 
#     # read SVG with git commit/branch
#     filename = project_path_join('templates', 'svg', 'git-commit.html')
#     with open(filename, 'r', encoding='utf-8') as _file:
#         svg_commit = _file.read().strip()
#     filename = project_path_join('templates', 'svg', 'git-branch.html')
#     with open(filename, 'r', encoding='utf-8') as _file:
#         svg_branch = _file.read().strip()
# 
#     images = []
#     for commit in commits.split(','):
#         objtype = 'commit'
#         link = svg_commit
#         if commit.startswith('commit/'):
#             commit = commit[7:]
#         if commit.startswith('tree/'):
#             objtype = 'tree'
#             commit = commit[5:]
#             link = svg_branch
#         repo, commit_id = split_commit(commit)
#         images.append(f'<a href="https://github.com/{repo}/{objtype}/'
#                       f'{commit_id}" target="_blank" rel="noopener">'
#                       f'{link}</a>')
#     return mark_safe(' '.join(images))
# 
# def tracker_links(tracker):
#     """Replace tracker items by URLs.
# 
#     Replace GitHub tracker item(s) (for example: "closes #123")
#     and savannah tracker item(s) (for example: "bug #12345")
#     by URL(s) to this/these item(s).
#     """
#     if not tracker:
#         return ''
#     items = [_replace_link(item) for item in tracker.split(',')]
#     return mark_safe('<br>'.join(items))
# 
# 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/download/models.py
# 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
# 
#     class Meta:
#         ordering = ['-date']


# Path: weechat/dev/models.py
from datetime import date
from django.db import models
from django.utils.safestring import mark_safe
from weechat.common.tracker import commits_links, tracker_links
from weechat.common.templatetags.localdate import localdate
from weechat.download.models import Release

class Task(models.Model):
    """A task (a new feature or bug to fix)."""
    visible = models.BooleanField(default=True)
    version = models.ForeignKey(Release, on_delete=models.CASCADE)
    tracker = models.CharField(max_length=64, blank=True)
    spec = models.CharField(max_length=512, blank=True)
    status = models.IntegerField(default=0)
    commits = models.CharField(max_length=1024, blank=True)
    component = models.CharField(max_length=64, default='core')
    description = models.CharField(max_length=512)
    priority = models.IntegerField(default=0)

    def __str__(self):
        desc = (self.description
                if len(self.description) < 100
                else f'{self.description[0:100]}…')
        version = (f'({self.version.version})' if self.visible
                   else self.version.version)
        tracker = self.tracker if self.tracker else '-'
        return (f'{version}, {tracker}, {self.status}%, {self.component}: '
                f'{desc} ({self.priority})')

    def version_date(self):
        """Return the date of version.

        It is prefixed with "≈ " if the date is in the future.
        """
        try:
            if self.version.date > date.today():
                return mark_safe(f'&asymp; {localdate(self.version.date)}')

            return localdate(self.version.date)