diff --git a/project/creditor/models.py b/project/creditor/models.py index 7315ed5c..e23cf429 100644 --- a/project/creditor/models.py +++ b/project/creditor/models.py @@ -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 diff --git a/project/creditor/tests/test_recurring.py b/project/creditor/tests/test_recurring.py index c0fabe9b..93ed0ba8 100644 --- a/project/creditor/tests/test_recurring.py +++ b/project/creditor/tests/test_recurring.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import calendar import datetime +from django.utils import timezone import pytest import pytz @@ -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() @@ -29,9 +31,10 @@ 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) @@ -39,47 +42,65 @@ def month_start_end(timescope=None): @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) @@ -87,21 +108,24 @@ def test_quarterly_in_scope_without_end(): @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) @@ -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) @@ -119,7 +144,8 @@ 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) @@ -127,7 +153,8 @@ def test_monthly_not_in_scope_ended(): @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)