Permalink
Browse files

add subscribe action into admin interface (and other admin/display im…

…provements)
  • Loading branch information...
1 parent 7cd5584 commit 394e4ce483bb9eb7bf31b8910333b43571137184 @gabrielgrant committed Sep 20, 2011
View
@@ -1,6 +1,45 @@
from billing.models import Account, ProductType, Subscription, SubscriptionApprovalStatus
from django.contrib import admin
-admin.site.register(Account)
-admin.site.register(ProductType)
-admin.site.register(Subscription)
+from .loading import get_products
+
+class SubscriptionInline(admin.StackedInline):
+ model = Subscription
+ extra = 0
+ can_delete = False
+ #exclude = ['product_type']
+
+def subscribe_actions_iter():
+ for product in get_products(hidden=True):
+ def create_subscribe_action(product):
+ def subscribe_action(modeladmin, request, accounts):
+ for account in accounts:
+ account.subscribe_to_product(product)
+ if len(accounts) == 1:
+ message_bit = '1 user was'
+ else:
+ message_bit = '%s users were' % len(accounts)
+ message = '%s successfully subscribed to %s' % (message_bit, product.name)
+ modeladmin.message_user(request, message)
+ subscribe_action.__name__ = 'subscribe_to_%s' % product.name
+ return subscribe_action
+ yield create_subscribe_action(product)
+
+class AccountAdmin(admin.ModelAdmin):
+ search_fields = ['owner__id', 'owner__username', 'owner__email']
+ list_display = [
+ '__unicode__',
+ lambda x: x.get_current_product_class().name,
+ lambda x: x.owner.id,
+ lambda x: x.owner.username,
+ lambda x: x.owner.email,
+ ]
+ actions = list(subscribe_actions_iter())
+ inlines = [SubscriptionInline]
+ raw_id_fields = ['owner']
+
+class SubscriptionAdmin(admin.ModelAdmin):
+ list_filter = ['product_type']
+
+admin.site.register(Account, AccountAdmin)
+admin.site.register(Subscription, SubscriptionAdmin)
View
@@ -89,6 +89,10 @@ def get_visible_products(self):
subscribed_products = set(pt.get_product_class() for pt in subscribed_product_types)
visible_products = set(public_products).union(subscribed_products)
return [p for p in all_products if p in visible_products]
+ def __unicode__(self):
+ return "%s's account" % unicode(self.owner)
+ def __repr__(self):
+ return "Account(owner=%s)" % repr(self.owner)
class ProductTypeManager(models.Manager):
def get_for_product(self, product):
@@ -102,9 +106,9 @@ class ProductType(models.Model):
def get_product_class(self):
return billing.loading.get_product(self.name)
def __unicode__(self):
- return 'ProductType(name=%s)' % self.name
+ return self.name
def __repr__(self):
- return self.__unicode__()
+ return 'ProductType(name=%s)' % self.name
class SubscriptionManager(models.Manager):
def filter_by_current_statuses(self, statuses):
@@ -176,9 +180,9 @@ def is_active(self):
def request_approval(self):
ready_for_approval.send(sender=self)
def __unicode__(self):
- return 'Subscription(product=%s, approval_status=%s)' % (self.product_type.name, self.get_current_approval_status())
+ return '%s (%s)' % (self.product_type.name, self.get_current_approval_status())
def __repr__(self):
- return self.__unicode__()
+ return 'Subscription(product=%s, approval_status=%s)' % (self.product_type.name, self.get_current_approval_status())
@receiver(signals.post_save, sender=Subscription)
def auto_add_subscription_approval_status(instance, created, **kwargs):
@@ -49,7 +49,7 @@ def get_plan_str(p):
user = user_by_name or user_by_id
user.billing_account.subscribe_to_product(product_name)
self.stdout.write(
- 'Successfully subscribed User(id=%s, username=%s, email=%s) to %s' %
+ '\nSuccessfully subscribed User(id=%s, username=%s, email=%s) to %s\n\n' %
(user.id, user.username, user.email, product_name)
)
@@ -1,5 +1,7 @@
from django.conf.urls.defaults import patterns, include, url
+import billing
+
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

0 comments on commit 394e4ce

Please sign in to comment.