admin.py 5.27 KB
Newer Older
1
"""Django admin interface for the shopping cart models. """
2
from django.contrib import admin
3

Will Daly committed
4
from shoppingcart.models import (
5
    Coupon,
6
    CourseRegistrationCodeInvoiceItem,
7 8
    DonationConfiguration,
    Invoice,
9 10
    InvoiceTransaction,
    PaidCourseRegistrationAnnotation
Will Daly committed
11
)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27


class SoftDeleteCouponAdmin(admin.ModelAdmin):
    """
    Admin for the Coupon table.
    soft-delete on the coupons
    """
    fields = ('code', 'description', 'course_id', 'percentage_discount', 'created_by', 'created_at', 'is_active')
    raw_id_fields = ("created_by",)
    readonly_fields = ('created_at',)
    actions = ['really_delete_selected']

    def queryset(self, request):
        """ Returns a QuerySet of all model instances that can be edited by the
        admin site. This is used by changelist_view. """
        # Default: qs = self.model._default_manager.get_active_coupons_query_set()
28 29
        # Queryset with all the coupons including the soft-deletes: qs = self.model._default_manager.get_queryset()
        query_string = self.model._default_manager.get_active_coupons_queryset()  # pylint: disable=protected-access
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        return query_string

    def get_actions(self, request):
        actions = super(SoftDeleteCouponAdmin, self).get_actions(request)
        del actions['delete_selected']
        return actions

    def really_delete_selected(self, request, queryset):
        """override the default behavior of selected delete method"""
        for obj in queryset:
            obj.is_active = False
            obj.save()

        if queryset.count() == 1:
            message_bit = "1 coupon entry was"
        else:
            message_bit = "%s coupon entries were" % queryset.count()
        self.message_user(request, "%s successfully deleted." % message_bit)

    def delete_model(self, request, obj):
        """override the default behavior of single instance of model delete method"""
        obj.is_active = False
        obj.save()

    really_delete_selected.short_description = "Delete s selected entries"
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

class CourseRegistrationCodeInvoiceItemInline(admin.StackedInline):
    """Admin for course registration code invoice items.

    Displayed inline within the invoice admin UI.
    """
    model = CourseRegistrationCodeInvoiceItem
    extra = 0
    can_delete = False
    readonly_fields = (
        'qty',
        'unit_price',
        'currency',
        'course_id',
    )

    def has_add_permission(self, request):
        return False


class InvoiceTransactionInline(admin.StackedInline):
    """Admin for invoice transactions.

    Displayed inline within the invoice admin UI.
    """
    model = InvoiceTransaction
    extra = 0
    readonly_fields = (
        'created',
        'modified',
        'created_by',
        'last_modified_by'
    )


class InvoiceAdmin(admin.ModelAdmin):
    """Admin for invoices.

    This is intended for the internal finance team
    to be able to view and update invoice information,
    including payments and refunds.

    """
    date_hierarchy = 'created'
    can_delete = False
    readonly_fields = ('created', 'modified')
    search_fields = (
        'internal_reference',
        'customer_reference_number',
        'company_name',
    )
    fieldsets = (
        (
            None, {
                'fields': (
                    'internal_reference',
                    'customer_reference_number',
                    'created',
                    'modified',
                )
            }
        ),
        (
            'Billing Information', {
                'fields': (
                    'company_name',
                    'company_contact_name',
                    'company_contact_email',
                    'recipient_name',
                    'recipient_email',
                    'address_line_1',
                    'address_line_2',
                    'address_line_3',
                    'city',
                    'state',
                    'zip',
                    'country'
                )
            }
        )
    )
    readonly_fields = (
        'internal_reference',
        'customer_reference_number',
        'created',
        'modified',
        'company_name',
        'company_contact_name',
        'company_contact_email',
        'recipient_name',
        'recipient_email',
        'address_line_1',
        'address_line_2',
        'address_line_3',
        'city',
        'state',
        'zip',
        'country'
    )
    inlines = [
        CourseRegistrationCodeInvoiceItemInline,
        InvoiceTransactionInline
    ]

    def save_formset(self, request, form, formset, change):
        """Save the user who created and modified invoice transactions. """
        instances = formset.save(commit=False)
        for instance in instances:
            if isinstance(instance, InvoiceTransaction):
                if not hasattr(instance, 'created_by'):
                    instance.created_by = request.user
                instance.last_modified_by = request.user
                instance.save()

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False


177
admin.site.register(PaidCourseRegistrationAnnotation)
178
admin.site.register(Coupon, SoftDeleteCouponAdmin)
Will Daly committed
179
admin.site.register(DonationConfiguration)
180
admin.site.register(Invoice, InvoiceAdmin)