<s> from django import http <EOL> from django . db import models <EOL> from django_databrowse . datastructures import EasyModel <EOL> from django_databrowse . sites import DatabrowsePlugin <EOL> from django . shortcuts import render_to_response <EOL> from django . utils . text import capfirst <EOL> from django . utils . encoding import smart_str , force_unicode <EOL> from django . utils . safestring import mark_safe <EOL> import urllib <EOL> from django . core . paginator import Paginator , EmptyPage , PageNotAnInteger <EOL> class FieldChoicePlugin ( DatabrowsePlugin ) : <EOL> def __init__ ( self , field_filter = None ) : <EOL> self . field_filter = field_filter <EOL> def field_dict ( self , model ) : <EOL> """<STR_LIT>""" <EOL> if self . field_filter : <EOL> return dict ( <EOL> [ ( f . name , f ) for f in model . _meta . fields <EOL> if self . field_filter ( f ) ] <EOL> ) <EOL> else : <EOL> return dict ( <EOL> [ ( f . name , f ) for f in model . _meta . fields <EOL> if not f . rel and <EOL> not f . primary_key and <EOL> not f . unique and <EOL> not isinstance ( f , ( models . AutoField , models . TextField ) ) ] <EOL> ) <EOL> def model_index_html ( self , request , model , site ) : <EOL> fields = self . field_dict ( model ) <EOL> if not fields : <EOL> return u'<STR_LIT>' <EOL> return mark_safe ( <EOL> u'<STR_LIT>' % u'<STR_LIT:U+002CU+0020>' . join ( <EOL> [ '<STR_LIT>' % <EOL> ( f . name , force_unicode ( capfirst ( f . verbose_name ) ) ) <EOL> for f in fields . values ( ) ] ) <EOL> ) <EOL> def urls ( self , plugin_name , easy_instance_field ) : <EOL> if easy_instance_field . field in self . field_dict ( easy_instance_field . model . model ) . values ( ) : <EOL> field_value = smart_str ( easy_instance_field . raw_value ) <EOL> return [ mark_safe ( u'<STR_LIT>' % ( <EOL> easy_instance_field . model . url ( ) , <EOL> plugin_name , easy_instance_field . field . name , <EOL> urllib . quote ( field_value , safe = '<STR_LIT>' ) ) ) ] <EOL> def model_view ( self , request , model_databrowse , url ) : <EOL> self . model , self . site = model_databrowse . model , model_databrowse . site <EOL> self . fields = self . field_dict ( self . model ) <EOL> if not self . fields : <EOL> raise http . Http404 ( '<STR_LIT>' ) <EOL> if url is None : <EOL> return self . homepage_view ( request ) <EOL> url_bits = url . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> if url_bits [ <NUM_LIT:0> ] in self . fields : <EOL> return self . field_view ( <EOL> request , <EOL> self . fields [ url_bits [ <NUM_LIT:0> ] ] , <EOL> * url_bits [ <NUM_LIT:1> : ] <EOL> ) <EOL> raise http . Http404 ( '<STR_LIT>' ) <EOL> def homepage_view ( self , request ) : <EOL> easy_model = EasyModel ( self . site , self . model ) <EOL> field_list = self . fields . values ( ) <EOL> field_list . sort ( key = lambda k : k . verbose_name ) <EOL> return render_to_response ( <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : self . site . root_url , <EOL> '<STR_LIT>' : easy_model , <EOL> '<STR_LIT>' : field_list <EOL> } <EOL> ) <EOL> def field_view ( self , request , field , value = None ) : <EOL> easy_model = EasyModel ( self . site , self . model ) <EOL> easy_field = easy_model . field ( field . name ) <EOL> if value is not None : <EOL> obj_list = easy_model . objects ( ** { field . name : value } ) <EOL> else : <EOL> obj_list = [ v [ field . name ] for v in self . model . _default_manager . distinct ( ) . order_by ( field . name ) . values ( field . name ) ] <EOL> numitems = request . GET . get ( '<STR_LIT>' ) <EOL> items_per_page = [ <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT:100> ] <EOL> if numitems and numitems . isdigit ( ) and int ( numitems ) > <NUM_LIT:0> : <EOL> paginator = Paginator ( obj_list , numitems ) <EOL> else : <EOL> paginator = Paginator ( obj_list , items_per_page [ <NUM_LIT:0> ] ) <EOL> page = request . GET . get ( '<STR_LIT>' ) <EOL> try : <EOL> obj_list_page = paginator . page ( page ) <EOL> except PageNotAnInteger : <EOL> obj_list_page = paginator . page ( <NUM_LIT:1> ) <EOL> except EmptyPage : <EOL> obj_list_page = paginator . page ( paginator . num_pages ) <EOL> if value is not None : <EOL> return render_to_response ( <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : self . site . root_url , <EOL> '<STR_LIT>' : easy_model , <EOL> '<STR_LIT>' : easy_field , <EOL> '<STR_LIT:value>' : value , <EOL> '<STR_LIT>' : obj_list_page , <EOL> '<STR_LIT>' : items_per_page , <EOL> } <EOL> ) <EOL> return render_to_response ( <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : self . site . root_url , <EOL> '<STR_LIT>' : easy_model , <EOL> '<STR_LIT>' : easy_field , <EOL> '<STR_LIT>' : obj_list_page , <EOL> '<STR_LIT>' : items_per_page , <EOL> } <EOL> ) </s>
