Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions project/creditor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,22 @@ def in_timescope(self, timescope=None):
scope_start_ts, scope_end_ts = self.resolve_timescope(timescope)
scope_start = scope_start_ts.date()
scope_end = scope_end_ts.date()

return (self.start <= scope_end
and (not self.end
or self.end >= scope_end))
or self.end >= scope_start))

@transaction.atomic()
def transaction_exists(self, timescope=None):
start, end = self.resolve_timescope(timescope)
uid_source = self.make_uid_source(timescope)
uid = hashlib.sha1(uid_source.encode('UTF-8')).hexdigest()
# Check for uid match
if Transaction.objects.filter(unique_id=uid).count():
return True
# check for time based match
qs = Transaction.objects.filter(
owner=self.owner, tag=self.tag, unique_id=uid, stamp__gte=start, stamp__lte=end
owner=self.owner, tag=self.tag, stamp__gte=start, stamp__lte=end
)
if qs.count():
return True
Expand Down
63 changes: 45 additions & 18 deletions project/creditor/tests/test_recurring.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import calendar
import datetime
from django.utils import timezone

import pytest
import pytz
Expand All @@ -10,10 +11,11 @@
def year_start_end(timescope=None):
if timescope is None:
timescope = datetime.datetime.now()
start = start = datetime.datetime(timescope.year, 1, 1).date()
start = datetime.datetime(timescope.year, 1, 1).date()
end = datetime.datetime(start.year, 12, calendar.monthrange(start.year, 12)[1]).date()
return (start, end)


def quarter_start_end(timescope=None):
if timescope is None:
timescope = datetime.datetime.now()
Expand All @@ -29,79 +31,101 @@ def quarter_start_end(timescope=None):
end = datetime.datetime(start.year, end_month, calendar.monthrange(start.year, end_month)[1]).date()
return (start, end)


def month_start_end(timescope=None):
if timescope is None:
timescope = datetime.datetime.now()
timescope = timezone.now()
start = datetime.datetime(timescope.year, timescope.month, 1).date()
end = datetime.datetime(start.year, start.month, calendar.monthrange(start.year, start.month)[1]).date()
return (start, end)


@pytest.mark.django_db
def test_yearly_in_scope_with_end():
now = datetime.datetime.now()
now = timezone.now()
start, end = year_start_end(now)
t = MembershipfeeFactory(start=start, end=end)
assert t.in_timescope(now)
for x in range(5):
t = MembershipfeeFactory(start=start, end=end)
assert t.in_timescope(now)
ret1 = t.conditional_add_transaction(now)
assert ret1
ret2 = t.conditional_add_transaction(now)
assert not ret2


@pytest.mark.django_db
def test_yearly_in_scope_without_end():
now = datetime.datetime.now()
now = timezone.now()
start, end = year_start_end(now - datetime.timedelta(weeks=60))
end = None
t = MembershipfeeFactory(start=start, end=end)
assert t.in_timescope(now)
for x in range(5):
t = MembershipfeeFactory(start=start, end=end)
assert t.in_timescope(now)
ret1 = t.conditional_add_transaction(now)
assert ret1
ret2 = t.conditional_add_transaction(now)
assert not ret2


@pytest.mark.django_db
def test_yearly_not_in_scope_ended():
now = datetime.datetime.now()
now = timezone.now()
start, end = year_start_end(now - datetime.timedelta(weeks=60))
t = MembershipfeeFactory(start=start, end=end)
assert not t.in_timescope(now)
ret2 = t.conditional_add_transaction(now)
assert not ret2


@pytest.mark.django_db
def test_yearly_not_in_scope_notstarted():
now = datetime.datetime.now()
now = timezone.now()
start, end = year_start_end(now + datetime.timedelta(weeks=60))
t = MembershipfeeFactory(start=start, end=end)
assert not t.in_timescope(now)
ret2 = t.conditional_add_transaction(now)
assert not ret2


@pytest.mark.django_db
def test_quarterly_in_scope_with_end():
now = datetime.datetime.now()
now = timezone.now()

start, end = quarter_start_end(now)
end += datetime.timedelta(weeks=15)
t = QuarterlyFactory(start=start, end=end)
assert t.in_timescope(now)


@pytest.mark.django_db
def test_quarterly_in_scope_without_end():
now = datetime.datetime.now()
now = timezone.now()

start, end = month_start_end(now - datetime.timedelta(weeks=15))
end = None
t = QuarterlyFactory(start=start, end=end)
assert t.in_timescope(now)

@pytest.mark.django_db
def test_quarterly_not_in_scope_ended():
now = datetime.datetime.now()
now = timezone.now()

start, end = month_start_end(now - datetime.timedelta(weeks=25))
t = QuarterlyFactory(start=start, end=end)
assert not t.in_timescope(now)

@pytest.mark.django_db
def test_quarterly_not_in_scope_notstarted():
now = datetime.datetime.now()
now = timezone.now()

start, end = month_start_end(now + datetime.timedelta(weeks=25))
t = QuarterlyFactory(start=start, end=end)
assert not t.in_timescope(now)

@pytest.mark.django_db
def test_monthly_in_scope_with_end():
now = datetime.datetime.now()
now = timezone.now()

start, end = month_start_end(now)
end += datetime.timedelta(weeks=6)
t = KeyholderfeeFactory(start=start, end=end)
Expand All @@ -110,7 +134,8 @@ def test_monthly_in_scope_with_end():

@pytest.mark.django_db
def test_monthly_in_scope_without_end():
now = datetime.datetime.now()
now = timezone.now()

start, end = month_start_end(now - datetime.timedelta(weeks=6))
end = None
t = KeyholderfeeFactory(start=start, end=end)
Expand All @@ -119,15 +144,17 @@ def test_monthly_in_scope_without_end():

@pytest.mark.django_db
def test_monthly_not_in_scope_ended():
now = datetime.datetime.now()
now = timezone.now()

start, end = month_start_end(now - datetime.timedelta(weeks=10))
t = KeyholderfeeFactory(start=start, end=end)
assert not t.in_timescope(now)


@pytest.mark.django_db
def test_monthly_not_in_scope_notstarted():
now = datetime.datetime.now()
now = timezone.now()

start, end = month_start_end(now + datetime.timedelta(weeks=10))
t = KeyholderfeeFactory(start=start, end=end)
assert not t.in_timescope(now)