<s> from django . conf . urls import patterns , url <EOL> from django . contrib import admin <EOL> from django . contrib import messages <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . views . generic . base import TemplateView <EOL> from . forms import CouponGenerationForm <EOL> from . models import Coupon , Campaign <EOL> class CouponAdmin ( admin . ModelAdmin ) : <EOL> list_display = [ '<STR_LIT>' , '<STR_LIT:code>' , '<STR_LIT:type>' , '<STR_LIT:value>' , '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> list_filter = [ '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> raw_id_fields = ( '<STR_LIT:user>' , ) <EOL> search_fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:code>' , '<STR_LIT:value>' ) <EOL> def get_urls ( self ) : <EOL> urls = super ( CouponAdmin , self ) . get_urls ( ) <EOL> my_urls = patterns ( <EOL> '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , self . admin_site . admin_view ( GenerateCouponsAdminView . as_view ( ) ) , name = '<STR_LIT>' ) , <EOL> ) <EOL> return my_urls + urls <EOL> class GenerateCouponsAdminView ( TemplateView ) : <EOL> template_name = '<STR_LIT>' <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( GenerateCouponsAdminView , self ) . get_context_data ( ** kwargs ) <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> form = CouponGenerationForm ( self . request . POST ) <EOL> if form . is_valid ( ) : <EOL> context [ '<STR_LIT>' ] = Coupon . objects . create_coupons ( <EOL> form . cleaned_data [ '<STR_LIT>' ] , <EOL> form . cleaned_data [ '<STR_LIT:type>' ] , <EOL> form . cleaned_data [ '<STR_LIT:value>' ] , <EOL> form . cleaned_data [ '<STR_LIT>' ] , <EOL> form . cleaned_data [ '<STR_LIT>' ] , <EOL> form . cleaned_data [ '<STR_LIT>' ] , <EOL> ) <EOL> messages . success ( self . request , _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> form = CouponGenerationForm ( ) <EOL> context [ '<STR_LIT>' ] = form <EOL> return context <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> context = self . get_context_data ( ** kwargs ) <EOL> return self . render_to_response ( context ) <EOL> class CampaignAdmin ( admin . ModelAdmin ) : <EOL> list_display = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def num_coupons ( self , obj ) : <EOL> return obj . coupons . count ( ) <EOL> num_coupons . short_description = _ ( "<STR_LIT>" ) <EOL> def num_coupons_used ( self , obj ) : <EOL> return obj . coupons . used ( ) . count ( ) <EOL> num_coupons_used . short_description = _ ( "<STR_LIT>" ) <EOL> def num_coupons_unused ( self , obj ) : <EOL> return obj . coupons . used ( ) . count ( ) <EOL> num_coupons_unused . short_description = _ ( "<STR_LIT>" ) <EOL> def num_coupons_expired ( self , obj ) : <EOL> return obj . coupons . expired ( ) . count ( ) <EOL> num_coupons_expired . short_description = _ ( "<STR_LIT>" ) <EOL> admin . site . register ( Coupon , CouponAdmin ) <EOL> admin . site . register ( Campaign , CampaignAdmin ) </s>
