<s> from django . contrib import admin <EOL> from django . core . urlresolvers import reverse <EOL> from django . conf import settings <EOL> from django . db . models import Sum <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from foobar . wallet import api as wallet_api <EOL> from shop import api as shop_api <EOL> from moneyed import Money <EOL> from . exceptions import NotCancelableException <EOL> from . import models , api <EOL> class ReadOnlyMixin ( object ) : <EOL> def has_delete_permission ( self , request , obj = None ) : <EOL> return False <EOL> def has_add_permission ( self , request , obj = None ) : <EOL> return False <EOL> class PurchaseInline ( ReadOnlyMixin , admin . TabularInline ) : <EOL> model = models . Purchase <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> readonly_fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ordering = ( '<STR_LIT>' , ) <EOL> show_change_link = True <EOL> def link ( self , obj ) : <EOL> return '<STR_LIT>' . format ( <EOL> reverse ( '<STR_LIT>' , args = ( obj . id , ) ) , <EOL> obj . id <EOL> ) <EOL> link . allow_tags = True <EOL> class PurchaseItemInline ( ReadOnlyMixin , admin . TabularInline ) : <EOL> model = models . PurchaseItem <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> readonly_fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> def product_name ( self , obj ) : <EOL> obj = shop_api . get_product ( obj . product_id ) <EOL> if obj : <EOL> return obj . name <EOL> def product_ean ( self , obj ) : <EOL> obj = shop_api . get_product ( obj . product_id ) <EOL> if obj : <EOL> return obj . code <EOL> def link ( self , obj ) : <EOL> return '<STR_LIT>' . format ( <EOL> reverse ( '<STR_LIT>' , args = ( obj . product_id , ) ) , <EOL> self . product_name ( obj ) <EOL> ) <EOL> link . allow_tags = True <EOL> @ admin . register ( models . Account ) <EOL> class AccountAdmin ( ReadOnlyMixin , admin . ModelAdmin ) : <EOL> list_display = ( '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> readonly_fields = ( '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> inlines = ( PurchaseInline , ) <EOL> fieldsets = ( <EOL> ( None , { <EOL> '<STR_LIT>' : ( <EOL> '<STR_LIT:id>' , <EOL> '<STR_LIT:user>' , <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> } ) , <EOL> ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> } ) <EOL> ) <EOL> class Media : <EOL> css = { <EOL> '<STR_LIT:all>' : ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> } <EOL> js = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> def balance ( self , obj ) : <EOL> _ , balance = wallet_api . get_balance ( obj . id ) <EOL> return balance <EOL> def wallet_link ( self , obj ) : <EOL> wallet_obj = wallet_api . get_wallet ( obj . id ) <EOL> return '<STR_LIT>' . format ( <EOL> reverse ( '<STR_LIT>' , args = ( wallet_obj . id , ) ) , <EOL> self . balance ( obj ) <EOL> ) <EOL> wallet_link . allow_tags = True <EOL> class PaymentTypeFilter ( admin . SimpleListFilter ) : <EOL> title = _ ( '<STR_LIT>' ) <EOL> parameter_name = '<STR_LIT>' <EOL> def lookups ( self , request , model_admin ) : <EOL> return ( <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> def queryset ( self , request , queryset ) : <EOL> if self . value ( ) == '<STR_LIT>' : <EOL> return queryset . filter ( account = None ) <EOL> elif self . value ( ) == '<STR_LIT>' : <EOL> return queryset . exclude ( account = None ) <EOL> @ admin . register ( models . Purchase ) <EOL> class PurchaseAdmin ( ReadOnlyMixin , admin . ModelAdmin ) : <EOL> list_display = ( '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> readonly_fields = ( '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:status>' , '<STR_LIT>' ) <EOL> inlines = ( PurchaseItemInline , ) <EOL> list_filter = ( '<STR_LIT:status>' , PaymentTypeFilter , ) <EOL> change_list_template = '<STR_LIT>' <EOL> date_hierarchy = '<STR_LIT>' <EOL> actions = [ '<STR_LIT>' ] <EOL> class Media : <EOL> css = { '<STR_LIT:all>' : ( '<STR_LIT>' , ) } <EOL> def get_actions ( self , request ) : <EOL> actions = super ( ) . get_actions ( request ) <EOL> del actions [ '<STR_LIT>' ] <EOL> return actions <EOL> def _amount ( self , obj ) : <EOL> return obj . amount <EOL> def foocard ( self , obj ) : <EOL> return obj . account_id is None <EOL> foocard . boolean = True <EOL> def cancel_purchases ( self , request , queryset ) : <EOL> failed = <NUM_LIT:0> <EOL> for obj in queryset : <EOL> try : <EOL> api . cancel_purchase ( obj . id ) <EOL> except NotCancelableException : <EOL> failed += <NUM_LIT:1> <EOL> count = queryset . count ( ) <EOL> if failed : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> msg = msg % ( count - failed , failed ) <EOL> else : <EOL> msg = _ ( '<STR_LIT>' ) % count <EOL> return self . message_user ( request , msg ) <EOL> cancel_purchases . short_description = _ ( '<STR_LIT>' ) <EOL> def aggregated_sum ( self , qs ) : <EOL> qs = qs . aggregate ( total = Sum ( '<STR_LIT>' ) ) <EOL> return Money ( qs [ '<STR_LIT>' ] or <NUM_LIT:0> , settings . DEFAULT_CURRENCY ) <EOL> def changelist_view ( self , request , extra_context = None ) : <EOL> response = super ( ) . changelist_view ( request , extra_context ) <EOL> if hasattr ( response , '<STR_LIT>' ) : <EOL> qs = response . context_data [ '<STR_LIT>' ] . queryset <EOL> extra_context = { <EOL> '<STR_LIT>' : self . aggregated_sum ( qs ) <EOL> } <EOL> response . context_data . update ( extra_context ) <EOL> return response <EOL> admin . site . index_template = '<STR_LIT>' <EOL> def each_context ( request ) : <EOL> cw_obj , cash_balance = wallet_api . get_balance ( settings . FOOBAR_CASH_WALLET ) <EOL> mw_obj , main_balance = wallet_api . get_balance ( settings . FOOBAR_MAIN_WALLET ) <EOL> total_balance = wallet_api . total_balance ( <EOL> exclude_ids = [ settings . FOOBAR_CASH_WALLET , settings . FOOBAR_MAIN_WALLET ] <EOL> ) <EOL> ctx = _each_context ( request ) <EOL> ctx . update ( { <EOL> '<STR_LIT>' : cw_obj , <EOL> '<STR_LIT>' : cash_balance , <EOL> '<STR_LIT>' : mw_obj , <EOL> '<STR_LIT>' : main_balance , <EOL> '<STR_LIT>' : total_balance , <EOL> '<STR_LIT>' : settings . SWISH_NUMBER <EOL> } ) <EOL> return ctx <EOL> _each_context = admin . site . each_context <EOL> admin . site . each_context = each_context </s>
