<s> import json <EOL> from itertools import groupby <EOL> from django import forms <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . translation import ugettext as _ <EOL> from ralph . admin import RalphAdmin , register <EOL> from ralph . admin . mixins import RalphAdminForm <EOL> from ralph . dashboards . models import Dashboard , Graph <EOL> class GraphForm ( RalphAdminForm ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ) . __init__ ( * args , ** kwargs ) <EOL> choices = [ <EOL> ct for ct in ContentType . objects . all ( ) <EOL> if getattr ( ct . model_class ( ) , '<STR_LIT>' , False ) <EOL> ] <EOL> def keyfunc ( x ) : <EOL> return x . app_label <EOL> data = sorted ( choices , key = keyfunc ) <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( choices = ( <EOL> ( k . capitalize ( ) , list ( map ( lambda x : ( x . id , x ) , g ) ) ) <EOL> for k , g in groupby ( data , keyfunc ) <EOL> ) ) <EOL> self . initial [ '<STR_LIT>' ] = json . dumps ( <EOL> self . initial . get ( '<STR_LIT>' , { } ) , <EOL> indent = <NUM_LIT:4> , <EOL> sort_keys = True <EOL> ) <EOL> def clean_model ( self ) : <EOL> ct_id = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> return ContentType . objects . get ( pk = ct_id ) <EOL> def clean_params ( self ) : <EOL> params = self . cleaned_data . get ( '<STR_LIT>' , '<STR_LIT:{}>' ) <EOL> try : <EOL> params_dict = json . loads ( params ) <EOL> except ValueError as e : <EOL> raise forms . ValidationError ( e . msg ) <EOL> if not params_dict . get ( '<STR_LIT>' , None ) : <EOL> raise forms . ValidationError ( '<STR_LIT>' ) <EOL> if not params_dict . get ( '<STR_LIT>' , None ) : <EOL> raise forms . ValidationError ( '<STR_LIT>' ) <EOL> return params <EOL> class Meta : <EOL> model = Graph <EOL> fields = [ <EOL> '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> @ register ( Graph ) <EOL> class GraphAdmin ( RalphAdmin ) : <EOL> form = GraphForm <EOL> list_display = [ '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT>' ] <EOL> @ register ( Dashboard ) <EOL> class DashboardAdmin ( RalphAdmin ) : <EOL> list_display = [ '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get_link ( self , obj ) : <EOL> return _ ( '<STR_LIT>' . format ( reverse ( <EOL> '<STR_LIT>' , args = ( obj . pk , ) <EOL> ) ) ) <EOL> get_link . short_description = _ ( '<STR_LIT>' ) <EOL> get_link . allow_tags = True </s>
