-
Notifications
You must be signed in to change notification settings - Fork 130
/
admin.py
142 lines (99 loc) · 4.64 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
from copy import deepcopy
from django.contrib import admin
from django.core import urlresolvers
from ordered_model.admin import OrderedModelAdmin
from django.utils.translation import ugettext_lazy as _
from .models import UserPlan, Plan, PlanQuota, Quota, PlanPricing, Pricing, Order, BillingInfo
from plans.models import Invoice
class UserLinkMixin(object):
def user_link(self, obj):
change_url = urlresolvers.reverse('admin:auth_user_change', args=(obj.user.id,))
return '<a href="%s">%s</a>' % (change_url, obj.user.username)
user_link.short_description = 'User'
user_link.allow_tags = True
class PlanQuotaInline(admin.TabularInline):
model = PlanQuota
class PlanPricingInline(admin.TabularInline):
model = PlanPricing
class QuotaAdmin(OrderedModelAdmin):
list_display = ('codename', 'name', 'description', 'unit', 'is_boolean', 'move_up_down_links', )
def copy_plan(modeladmin, request, queryset):
"""
Admin command for duplicating plans preserving quotas and pricings.
"""
for plan in queryset:
plan_copy = deepcopy(plan)
plan_copy.id = None
plan_copy.available = False
plan_copy.default = False
plan_copy.created = None
plan_copy.save(force_insert=True)
for pricing in plan.planpricing_set.all():
pricing.id = None
pricing.plan = plan_copy
pricing.save(force_insert=True)
for quota in plan.planquota_set.all():
quota.id = None
quota.plan = plan_copy
quota.save(force_insert=True)
copy_plan.short_description = _('Make a plan copy')
class PlanAdmin(OrderedModelAdmin):
search_fields = ('name', 'customized__username', 'customized__email', )
list_filter = ('available', 'visible')
list_display = ('name', 'description', 'customized', 'default', 'available', 'created', 'move_up_down_links')
inlines = (PlanPricingInline, PlanQuotaInline)
list_select_related = True
raw_id_fields = ('customized',)
actions = [copy_plan, ]
def queryset(self, request):
return super(PlanAdmin, self).queryset(request).select_related('customized')
class BillingInfoAdmin(UserLinkMixin, admin.ModelAdmin):
search_fields = ('user__username', 'user__email', 'tax_number', 'name')
list_display = ('user', 'tax_number', 'name', 'street', 'zipcode', 'city', 'country')
list_select_related = True
readonly_fields = ('user_link',)
exclude = ('user',)
def make_order_completed(modeladmin, request, queryset):
for order in queryset:
order.complete_order()
make_order_completed.short_description = _('Make selected orders completed')
def make_order_invoice(modeladmin, request, queryset):
for order in queryset:
if Invoice.objects.filter(type=Invoice.INVOICE_TYPES['INVOICE'], order=order).count() == 0:
Invoice.create(order, Invoice.INVOICE_TYPES['INVOICE'])
make_order_invoice.short_description = _('Make invoices for orders')
class InvoiceInline(admin.TabularInline):
model = Invoice
extra = 0
class OrderAdmin(admin.ModelAdmin):
list_filter = ('status', 'created', 'completed', 'plan__name', 'pricing')
raw_id_fields = ('user',)
search_fields = ('id', 'user__username', 'user__email')
list_display = (
'id', 'name', 'created', 'user', 'status', 'completed', 'tax', 'amount', 'currency', 'plan', 'pricing')
actions = [make_order_completed, make_order_invoice]
inlines = (InvoiceInline, )
def queryset(self, request):
return super(OrderAdmin, self).queryset(request).select_related('plan', 'pricing', 'user')
class InvoiceAdmin(admin.ModelAdmin):
search_fields = ('full_number', 'buyer_tax_number', 'user__username', 'user__email')
list_filter = ('type', 'issued')
list_display = (
'full_number', 'issued', 'total_net', 'currency', 'user', 'tax', 'buyer_name', 'buyer_city', 'buyer_tax_number')
list_select_related = True
raw_id_fields = ('user', 'order')
class UserPlanAdmin(UserLinkMixin, admin.ModelAdmin):
list_filter = ('active', 'expire', 'plan__name', 'plan__available', 'plan__visible',)
search_fields = ('user__username', 'user__email', 'plan__name',)
list_display = ('user', 'plan', 'expire', 'active')
list_select_related = True
readonly_fields = ['user_link', ]
fields = ('user_link', 'plan', 'expire', 'active' )
raw_id_fields = ['plan', ]
admin.site.register(Quota, QuotaAdmin)
admin.site.register(Plan, PlanAdmin)
admin.site.register(UserPlan, UserPlanAdmin)
admin.site.register(Pricing)
admin.site.register(Order, OrderAdmin)
admin.site.register(BillingInfo, BillingInfoAdmin)
admin.site.register(Invoice, InvoiceAdmin)