Skip to content

Commit

Permalink
Add customer profile and payment profile to admin
Browse files Browse the repository at this point in the history
  • Loading branch information
treyhunner committed Jun 25, 2013
1 parent 0695517 commit 1d09754
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
33 changes: 32 additions & 1 deletion authorizenet/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.contrib import admin
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
from authorizenet.models import Response, CIMResponse
from authorizenet.models import (Response, CIMResponse, CustomerProfile,
CustomerPaymentProfile)
from authorizenet.forms import CustomerPaymentForm, CustomerPaymentAdminForm


class ResponseAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -78,3 +80,32 @@ def response_link(self, obj):
response_link.short_description = 'transaction response'

admin.site.register(CIMResponse, CIMResponseAdmin)


class CustomerPaymentProfileInline(admin.StackedInline):
model = CustomerPaymentProfile
extra = 0
max_num = 0
form = CustomerPaymentForm


class CustomerProfileAdmin(admin.ModelAdmin):
list_display = ['profile_id', 'customer']
readonly_fields = ['profile_id', 'customer']
inlines = [CustomerPaymentProfileInline]

def get_readonly_fields(self, request, obj=None):
return self.readonly_fields if obj is not None else []

admin.site.register(CustomerProfile, CustomerProfileAdmin)


class CustomerPaymentProfileAdmin(admin.ModelAdmin):
list_display = ['payment_profile_id', 'customer_profile', 'customer']
readonly_fields = ['payment_profile_id', 'customer', 'customer_profile']
form = CustomerPaymentAdminForm

def get_readonly_fields(self, request, obj=None):
return self.readonly_fields if obj is not None else []

admin.site.register(CustomerPaymentProfile, CustomerPaymentProfileAdmin)
13 changes: 10 additions & 3 deletions authorizenet/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ def __init__(self, *args, **kwargs):
self.customer = kwargs.pop('customer', None)
return super(CustomerPaymentForm, self).__init__(*args, **kwargs)

def save(self):
def save(self, commit=True):
instance = super(CustomerPaymentForm, self).save(commit=False)
instance.customer = self.customer
if self.customer:
instance.customer = self.customer
instance.expiration_date = self.cleaned_data['expiration_date']
instance.card_code = self.cleaned_data.get('card_code')
instance.save()
if commit:
instance.save()
return instance

class Meta:
Expand All @@ -120,6 +122,11 @@ class Meta:
'expiration_date', 'card_code')


class CustomerPaymentAdminForm(CustomerPaymentForm):
class Meta(CustomerPaymentForm.Meta):
fields = ('customer',) + CustomerPaymentForm.Meta.fields


class HostedCIMProfileForm(forms.Form):
token = forms.CharField(widget=forms.HiddenInput)
def __init__(self, token, *args, **kwargs):
Expand Down
5 changes: 4 additions & 1 deletion authorizenet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ def sync(self):

objects = CustomerProfileManager()

def __unicode__(self):
return self.profile_id


class CustomerPaymentProfile(models.Model):

Expand Down Expand Up @@ -354,6 +357,6 @@ def update(self, **data):
return self

def __unicode__(self):
return self.card_number
return self.payment_profile_id

objects = CustomerPaymentProfileManager()

0 comments on commit 1d09754

Please sign in to comment.