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

Prevent saving accepted/rejected model

parent a13abe14
...@@ -155,10 +155,11 @@ class RegistrationAdmin(admin.ModelAdmin): ...@@ -155,10 +155,11 @@ class RegistrationAdmin(admin.ModelAdmin):
"""Does the user have the review permission?""" """Does the user have the review permission?"""
return request.user.has_perm('registrations.review_entries') return request.user.has_perm('registrations.review_entries')
def has_change_permission(self, request, obj=None): def save_model(self, request, obj, form, change):
if (obj and obj.status == Registration.STATUS_REVIEW) or not obj: if not (obj.status == Entry.STATUS_REJECTED or
return super().has_change_permission(request, obj) obj.status == Entry.STATUS_ACCEPTED or
return False obj.status == Entry.STATUS_COMPLETED):
super().save_model(request, obj, form, change)
@admin.register(Renewal) @admin.register(Renewal)
...@@ -185,6 +186,7 @@ class RenewalAdmin(RegistrationAdmin): ...@@ -185,6 +186,7 @@ class RenewalAdmin(RegistrationAdmin):
'member',) 'member',)
}), }),
) )
actions = RegistrationAdmin.actions
def get_readonly_fields(self, request, obj=None): def get_readonly_fields(self, request, obj=None):
"""Make all fields read-only and add member if needed""" """Make all fields read-only and add member if needed"""
......
...@@ -11,6 +11,7 @@ from django.utils.translation import ugettext_lazy as _ ...@@ -11,6 +11,7 @@ from django.utils.translation import ugettext_lazy as _
from members.models import Member from members.models import Member
from payments.models import Payment from payments.models import Payment
from payments.widgets import PaymentWidget
from registrations import admin from registrations import admin
from registrations.models import Entry, Registration, Renewal from registrations.models import Entry, Registration, Renewal
...@@ -300,6 +301,50 @@ class RegistrationAdminTest(TestCase): ...@@ -300,6 +301,50 @@ class RegistrationAdminTest(TestCase):
reg.payment = None reg.payment = None
self.assertEqual(self.admin.payment_status(reg), '-') self.assertEqual(self.admin.payment_status(reg), '-')
def test_formfield_for_dbfield(self):
with self.subTest('Payment field'):
field = self.admin.formfield_for_dbfield(
Registration._meta.get_field('payment'), request=None)
self.assertIsInstance(field.widget, PaymentWidget)
with self.subTest('Other field'):
field = self.admin.formfield_for_dbfield(
Registration._meta.get_field('first_name'), request=None)
self.assertNotIsInstance(field.widget, PaymentWidget)
self.assertIsNotNone(field.widget)
def test_save_model(self):
reg = Registration(
status=Registration.STATUS_REVIEW,
birthday=timezone.now()
)
with self.subTest('Status review saves'):
reg.first_name = 'Test1'
self.admin.save_model({}, reg, None, True)
self.assertTrue(
Registration.objects.filter(first_name='Test1').exists())
with self.subTest('Status accepted, no save'):
reg.first_name = 'Test2'
reg.status = Registration.STATUS_ACCEPTED
self.admin.save_model({}, reg, None, True)
self.assertFalse(
Registration.objects.filter(first_name='Test2').exists())
with self.subTest('Status reject, no save'):
reg.first_name = 'Test2'
reg.status = Registration.STATUS_REJECTED
self.admin.save_model({}, reg, None, True)
self.assertFalse(
Registration.objects.filter(first_name='Test2').exists())
with self.subTest('Status completed, no save'):
reg.first_name = 'Test2'
reg.status = Registration.STATUS_COMPLETED
self.admin.save_model({}, reg, None, True)
self.assertFalse(
Registration.objects.filter(first_name='Test2').exists())
class RenewalAdminTest(TestCase): class RenewalAdminTest(TestCase):
......
Supports Markdown
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