Verified Commit dc7be8a1 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Fix save order for payments in registrations

parent 4ae90c69
......@@ -10,9 +10,9 @@ from django.utils import timezone
import members
from members.models import Membership, Profile
from payments.models import Payment
from utils.snippets import datetime_to_lectureyear
from registrations import emails
from registrations.models import Entry, Registration, Renewal
from utils.snippets import datetime_to_lectureyear
def _generate_username(registration):
......@@ -120,9 +120,9 @@ def accept_entries(queryset):
# User is not unique, do not proceed
continue
payment = _create_payment_for_entry(entry)
entry.refresh_from_db()
entry.status = Entry.STATUS_ACCEPTED
entry.updated_at = timezone.now()
entry.payment = _create_payment_for_entry(entry)
try:
if entry.registration.username is None:
......@@ -130,18 +130,18 @@ def accept_entries(queryset):
entry.registration)
entry.registration.save()
emails.send_registration_accepted_message(entry.registration,
payment)
entry.payment)
except Registration.DoesNotExist:
try:
emails.send_renewal_accepted_message(entry.renewal, payment)
emails.send_renewal_accepted_message(entry.renewal,
entry.payment)
except Renewal.DoesNotExist:
pass
entry.save()
updated_entries.append(entry.pk)
return Entry.objects.filter(
pk__in=updated_entries).update(status=Entry.STATUS_ACCEPTED,
updated_at=timezone.now())
return len(updated_entries)
def _create_payment_for_entry(entry):
......@@ -177,13 +177,9 @@ def _create_payment_for_entry(entry):
except Renewal.DoesNotExist:
pass
payment = Payment.objects.create(
return Payment.objects.create(
amount=amount,
)
entry.payment = payment
entry.save()
return payment
def _create_member_from_registration(registration):
......
......@@ -265,21 +265,16 @@ class ServicesTest(TestCase):
p5 = services._create_payment_for_entry(self.e5)
self.assertEqual(p1.amount, settings.MEMBERSHIP_PRICES['year'])
self.assertEqual(p1.registrations_entry, self.e1)
self.assertEqual(p1.processed, False)
self.assertEqual(p2.amount, settings.MEMBERSHIP_PRICES['study'])
self.assertEqual(p2.registrations_entry, self.e2)
self.assertEqual(p2.processed, False)
self.assertEqual(p3.amount, settings.MEMBERSHIP_PRICES['study'] -
settings.MEMBERSHIP_PRICES['year'])
self.assertEqual(p3.registrations_entry, self.e3)
self.assertEqual(p3.processed, False)
self.assertEqual(p4.amount, settings.MEMBERSHIP_PRICES['year'])
self.assertEqual(p4.registrations_entry, self.e4)
self.assertEqual(p4.processed, False)
self.assertEqual(p5.amount, settings.MEMBERSHIP_PRICES['study'] -
settings.MEMBERSHIP_PRICES['year'])
self.assertEqual(p5.registrations_entry, self.e5)
self.assertEqual(p5.processed, False)
@mock.patch('registrations.services.check_unique_user')
......@@ -461,9 +456,17 @@ class ServicesTest(TestCase):
services.process_payment(p)
p0 = services._create_payment_for_entry(self.e0)
self.e0.payment = p0
self.e0.save()
p1 = services._create_payment_for_entry(self.e1)
self.e1.payment = p1
self.e1.save()
p2 = services._create_payment_for_entry(self.e2)
self.e2.payment = p2
self.e2.save()
p3 = services._create_payment_for_entry(self.e3)
self.e3.payment = p3
self.e3.save()
Entry.objects.filter(
pk__in=[self.e1.pk, self.e2.pk, self.e3.pk]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment