# Path: issues/helpers.py
# def day_range(start_date, end_date):
#     """ Given two dates, generate (date, day#) pairs for all the days in the
#     range, inclusive
# 
#     >>> day1 = datetime.datetime(year=2011, month=9, day=29)
#     >>> day2 = datetime.datetime(year=2011, month=10, day=3)
#     >>> list(day_range(day1, day2))
#     [(datetime.date(2011, 9, 29), 0), (datetime.date(2011, 9, 30), 1), (datetime.dat
#     e(2011, 10, 1), 2), (datetime.date(2011, 10, 2), 3)]
#     """
#     for day in range(days_apart(start_date, end_date) + 1):
#         date = start_date.date() + datetime.timedelta(days=day)
#         yield date, day
# 
# def days_of_work(issues):
#     """Given a queryset of issues, return the number of days it is estimated
#     to complete all of them."""
#     return float(sum(i or 1 for i in issues.values_list('days_estimate', flat=True)))
# 
# def days_apart(start_date, end_date):
#     """ Return the number of days apart between two datetimes
# 
#     >>> day1 = datetime.datetime(year=2011, month=9, day=29)
#     >>> day2 = datetime.datetime(year=2011, month=10, day=3)
#     >>> days_apart(day1, day2)
#     4
#     """
#     return (end_date.date() - start_date.date()).days
# 
# def work_left(issues, date):
#     """Given a queryset of issues, return how many days of work is
#     estimated to complete all of them excluding completed issues on or before
#     a given date"""
#     one_day = datetime.timedelta(days=1)
#     return days_of_work(issues.exclude(Q(close_date__isnull=True) | Q(close_date__gt=date + one_day)))
# 
# def business_day_range(start_date, end_date):
#     """ Given two dates, generate (date, day#) pairs for all the business days in
#     the range, inclusive
# 
#     >>> day1 = datetime.datetime(year=2011, month=5, day=31)
#     >>> day2 = datetime.datetime(year=2011, month=6, day=6)
#     >>> list(business_day_range(day1, day2))
#     [(datetime.date(2011, 5, 31), 0), (datetime.date(2011, 6, 1), 1),
#     (datetime.date(2011, 6, 2), 2), (datetime.date(2011, 6, 3), 3)]
#     """
#     for date, day in day_range(start_date, end_date):
#         if is_business_day(date):
#             yield date, day
# 
# def is_business_day(date):
#     return date.weekday() <= 4


import datetime
from django.db import models
from django.db.models import Q, Count
from django.utils.timezone import now, utc
from django.contrib.auth.models import User
from django.utils.safestring import SafeString
from issues.helpers import day_range, days_of_work, days_apart, work_left, business_day_range, is_business_day
        from issues.forms import IssueForm
        form_kwargs.update(kwargs)
        return self.form_class()(**form_kwargs)

    def has_new_notes(self, viewing_user):
        if viewing_user.is_authenticated():
            notes = self.note_set.exclude(creator=viewing_user).order_by('-created')[:1]
            if notes:
                return IssueViewed.objects.filter(user=viewing_user, issue=self).exclude(last_viewed__lte=notes[0].created).count() == 0
        return False

    def get_notes(self):
        return self.note_set.all().order_by('created')

    @models.permalink
    def get_absolute_url(self):
        return ('issue_detail', (), {'pk': self.id})

    def __unicode__(self):
        return self.title


class Commit(models.Model):
    revision = models.CharField(max_length=40)
    issue = models.ForeignKey(Issue)
    created = models.DateTimeField(default=now)

    def get_url(self):
        if self.issue.project.scm_owner and self.issue.project.scm_repo:
            if self.issue.project.scm_type == 'GH':
                return "https://github.com/" + self.issue.project.scm_owner + "/" + self.issue.project.scm_repo + "/commit/" + self.revision

# Path: issues/helpers.py
# def day_range(start_date, end_date):
#     """ Given two dates, generate (date, day#) pairs for all the days in the
#     range, inclusive
# 
#     >>> day1 = datetime.datetime(year=2011, month=9, day=29)
#     >>> day2 = datetime.datetime(year=2011, month=10, day=3)
#     >>> list(day_range(day1, day2))
#     [(datetime.date(2011, 9, 29), 0), (datetime.date(2011, 9, 30), 1), (datetime.dat
#     e(2011, 10, 1), 2), (datetime.date(2011, 10, 2), 3)]
#     """
#     for day in range(days_apart(start_date, end_date) + 1):
#         date = start_date.date() + datetime.timedelta(days=day)
#         yield date, day
# 
# def days_of_work(issues):
#     """Given a queryset of issues, return the number of days it is estimated
#     to complete all of them."""
#     return float(sum(i or 1 for i in issues.values_list('days_estimate', flat=True)))
# 
# def days_apart(start_date, end_date):
#     """ Return the number of days apart between two datetimes
# 
#     >>> day1 = datetime.datetime(year=2011, month=9, day=29)
#     >>> day2 = datetime.datetime(year=2011, month=10, day=3)
#     >>> days_apart(day1, day2)
#     4
#     """
#     return (end_date.date() - start_date.date()).days
# 
# def work_left(issues, date):
#     """Given a queryset of issues, return how many days of work is
#     estimated to complete all of them excluding completed issues on or before
#     a given date"""
#     one_day = datetime.timedelta(days=1)
#     return days_of_work(issues.exclude(Q(close_date__isnull=True) | Q(close_date__gt=date + one_day)))
# 
# def business_day_range(start_date, end_date):
#     """ Given two dates, generate (date, day#) pairs for all the business days in
#     the range, inclusive
# 
#     >>> day1 = datetime.datetime(year=2011, month=5, day=31)
#     >>> day2 = datetime.datetime(year=2011, month=6, day=6)
#     >>> list(business_day_range(day1, day2))
#     [(datetime.date(2011, 5, 31), 0), (datetime.date(2011, 6, 1), 1),
#     (datetime.date(2011, 6, 2), 2), (datetime.date(2011, 6, 3), 3)]
#     """
#     for date, day in day_range(start_date, end_date):
#         if is_business_day(date):
#             yield date, day
# 
# def is_business_day(date):
#     return date.weekday() <= 4


# Path: issues/models.py
import datetime
from django.db import models
from django.db.models import Q, Count
from django.utils.timezone import now, utc
from django.contrib.auth.models import User
from django.utils.safestring import SafeString
from issues.helpers import day_range, days_of_work, days_apart, work_left, business_day_range, is_business_day
        from issues.forms import IssueForm

        form_kwargs.update(kwargs)
        return self.form_class()(**form_kwargs)

    def has_new_notes(self, viewing_user):
        if viewing_user.is_authenticated():
            notes = self.note_set.exclude(creator=viewing_user).order_by('-created')[:1]
            if notes:
                return IssueViewed.objects.filter(user=viewing_user, issue=self).exclude(last_viewed__lte=notes[0].created).count() == 0
        return False

    def get_notes(self):
        return self.note_set.all().order_by('created')

    @models.permalink
    def get_absolute_url(self):
        return ('issue_detail', (), {'pk': self.id})

    def __unicode__(self):
        return self.title


class Commit(models.Model):
    revision = models.CharField(max_length=40)
    issue = models.ForeignKey(Issue)
    created = models.DateTimeField(default=now)

    def get_url(self):
        if self.issue.project.scm_owner and self.issue.project.scm_repo:
            if self.issue.project.scm_type == 'GH':
                return "https://github.com/" + self.issue.project.scm_owner + "/" + self.issue.project.scm_repo + "/commit/" + self.revision

            elif self.issue.project.scm_type == 'BB':