<s> from __future__ import unicode_literals <EOL> from collections import OrderedDict <EOL> from django . contrib import admin <EOL> from django . contrib . auth import get_user_model <EOL> from django . forms import widgets <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from shop . models . notification import Notification , NotificationAttachment <EOL> from shop . models . order import OrderModel <EOL> class NotificationAttachmentAdmin ( admin . TabularInline ) : <EOL> model = NotificationAttachment <EOL> extra = <NUM_LIT:0> <EOL> @ admin . register ( Notification ) <EOL> class NotificationAdmin ( admin . ModelAdmin ) : <EOL> USER_CHOICES = ( ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) , ( <NUM_LIT:0> , _ ( "<STR_LIT>" ) ) , ) <EOL> list_display = ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> inlines = ( NotificationAttachmentAdmin , ) <EOL> save_as = True <EOL> def get_form ( self , request , obj = None , ** kwargs ) : <EOL> kwargs . update ( widgets = { <EOL> '<STR_LIT>' : widgets . Select ( choices = self . get_transition_choices ( ) ) , <EOL> '<STR_LIT>' : widgets . Select ( choices = self . get_mailto_choices ( ) ) , <EOL> } ) <EOL> return super ( NotificationAdmin , self ) . get_form ( request , obj , ** kwargs ) <EOL> def get_transition_choices ( self ) : <EOL> choices = OrderedDict ( ) <EOL> status_field = [ f for f in OrderModel . _meta . fields if f . name == '<STR_LIT:status>' ] . pop ( ) <EOL> for transition in status_field . get_all_transitions ( OrderModel ) : <EOL> if transition . target : <EOL> transition_name = OrderModel . get_transition_name ( transition . target ) <EOL> choices [ transition . target ] = transition_name <EOL> return choices . items ( ) <EOL> def get_mailto_choices ( self ) : <EOL> choices = list ( self . USER_CHOICES ) <EOL> for user in get_user_model ( ) . objects . filter ( is_staff = True ) : <EOL> email = '<STR_LIT>' . format ( user . first_name , user . last_name , user . email ) <EOL> choices . append ( ( user . id , email ) ) <EOL> return choices <EOL> def transition_name ( self , obj ) : <EOL> return OrderModel . get_transition_name ( obj . transition_target ) <EOL> transition_name . short_description = _ ( "<STR_LIT>" ) <EOL> def num_attachments ( self , obj ) : <EOL> return obj . notificationattachment_set . count ( ) <EOL> num_attachments . short_description = _ ( "<STR_LIT>" ) <EOL> def recipient ( self , obj ) : <EOL> try : <EOL> if obj . mail_to > <NUM_LIT:0> : <EOL> user = get_user_model ( ) . objects . get ( id = obj . mail_to , is_staff = True ) <EOL> return '<STR_LIT>' . format ( user . first_name , user . last_name , user . email ) <EOL> else : <EOL> return OrderedDict ( self . USER_CHOICES ) [ obj . mail_to ] <EOL> except ( get_user_model ( ) . DoesNotExist , KeyError ) : <EOL> return _ ( "<STR_LIT>" ) </s>
