Commit 567abdf7 authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Merge branch 'feature/add-link-to-payment-from-registrations' into 'master'

Add direct link to payment from registrations model admins

Closes #682

See merge request !917
parents 051b59ae 03007170
......@@ -57,4 +57,12 @@ payments\.views module
:undoc-members:
:show-inheritance:
payments\.widgets module
------------------------
.. automodule:: payments.widgets
:members:
:undoc-members:
:show-inheritance:
{% load i18n %}
<div class="readonly">
{% if widget.value %}
<a href="{{ url }}">
{% if processed %}
{% trans "Processed" %}
{% else %}
{% trans "Unprocessed" %}
{% endif %}
</a>
{% else %}
-
{% endif %}
</div>
from django.forms import Widget
from payments.models import Payment
class PaymentWidget(Widget):
template_name = 'payments/widget.html'
def value_from_datadict(self, data, files, name):
return super().value_from_datadict(data, files, name)
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
if value:
payment = Payment.objects.get(pk=value)
context['url'] = payment.get_admin_url()
context['processed'] = payment.processed
return context
"""Registers admin interfaces for the registrations module"""
from django.contrib import admin, messages
from django.contrib.admin.utils import model_ngettext
from django.forms import Field
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _
from payments.widgets import PaymentWidget
from . import services
from .models import Entry, Registration, Renewal
......@@ -64,6 +66,14 @@ class RegistrationAdmin(admin.ModelAdmin):
)
actions = ['accept_selected', 'reject_selected']
def formfield_for_dbfield(self, db_field, request, **kwargs):
field = super().formfield_for_dbfield(db_field, request, **kwargs)
if db_field.name == 'payment':
return Field(widget=PaymentWidget,
initial=field.initial,
required=False)
return field
def changeform_view(self, request, object_id=None, form_url='',
extra_context=None):
"""
......@@ -93,10 +103,10 @@ class RegistrationAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
if obj is None or not (obj.status == Entry.STATUS_REJECTED or
obj.status == Entry.STATUS_ACCEPTED):
return ['status', 'created_at', 'updated_at', 'payment']
return ['status', 'created_at', 'updated_at']
else:
return [field.name for field in self.model._meta.get_fields()
if field.editable]
if field.editable and not field.name == 'payment']
@staticmethod
def name(obj):
......@@ -155,6 +165,7 @@ class RenewalAdmin(RegistrationAdmin):
'length',
'membership_type',
'status',
'payment',
'remarks',
'member',)
}),
......
......@@ -4,6 +4,7 @@ from django.forms import TypedChoiceField
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from payments.widgets import PaymentWidget
from utils.snippets import datetime_to_lectureyear
from .models import Registration, Renewal
......
......@@ -146,13 +146,13 @@ class RegistrationAdminTest(TestCase):
fields = self.admin.get_readonly_fields(request)
self.assertEqual(fields, ['status', 'created_at',
'updated_at', 'payment'])
'updated_at'])
fields = self.admin.get_readonly_fields(request, Registration(
status=Entry.STATUS_CONFIRM
))
self.assertEqual(fields, ['status', 'created_at',
'updated_at', 'payment'])
'updated_at'])
fields = self.admin.get_readonly_fields(request, Registration(
status=Entry.STATUS_REJECTED
......@@ -166,8 +166,8 @@ class RegistrationAdminTest(TestCase):
'programme', 'starting_year',
'address_street', 'address_street2',
'address_postal_code', 'address_city',
'payment', 'membership',
'optin_mailinglist', 'optin_birthday'])
'membership', 'optin_mailinglist',
'optin_birthday'])
fields = self.admin.get_readonly_fields(request, Registration(
status=Entry.STATUS_ACCEPTED
......@@ -181,8 +181,8 @@ class RegistrationAdminTest(TestCase):
'programme', 'starting_year',
'address_street', 'address_street2',
'address_postal_code', 'address_city',
'payment', 'membership',
'optin_mailinglist', 'optin_birthday'])
'membership', 'optin_mailinglist',
'optin_birthday'])
def test_get_actions(self):
actions = self.admin.get_actions(_get_mock_request([]))
......@@ -285,20 +285,20 @@ class RenewalAdminTest(TestCase):
fields = self.admin.get_readonly_fields(request)
self.assertEqual(fields, ['status', 'created_at',
'updated_at', 'payment'])
'updated_at'])
fields = self.admin.get_readonly_fields(request, Renewal(
status=Entry.STATUS_CONFIRM
))
self.assertEqual(fields, ['status', 'created_at',
'updated_at', 'payment', 'member'])
'updated_at', 'member'])
fields = self.admin.get_readonly_fields(request, Renewal(
status=Entry.STATUS_REJECTED
))
self.assertCountEqual(fields, ['created_at', 'updated_at', 'status',
'length', 'membership_type', 'remarks',
'entry_ptr', 'member', 'payment',
'entry_ptr', 'member',
'membership'])
fields = self.admin.get_readonly_fields(request, Renewal(
......@@ -306,7 +306,7 @@ class RenewalAdminTest(TestCase):
))
self.assertCountEqual(fields, ['created_at', 'updated_at', 'status',
'length', 'membership_type', 'remarks',
'entry_ptr', 'member', 'payment',
'entry_ptr', 'member',
'membership'])
def test_get_actions(self):
......
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