<s> from django . contrib import admin <EOL> from django . db . models import Q <EOL> from django . http import HttpResponseRedirect <EOL> from django . utils . translation import get_language <EOL> from i18n . utils import normalize_language_code <EOL> from nouns . models import Noun , Keyword , Relation , Channel <EOL> from premises . models import Contention <EOL> class KeywordInline ( admin . TabularInline ) : <EOL> model = Keyword <EOL> extra = <NUM_LIT:0> <EOL> class ContentionInline ( admin . TabularInline ) : <EOL> model = Contention . nouns . through <EOL> extra = <NUM_LIT:0> <EOL> raw_id_fields = ( '<STR_LIT>' , ) <EOL> class RelationInline ( admin . TabularInline ) : <EOL> model = Relation <EOL> extra = <NUM_LIT:0> <EOL> raw_id_fields = ( '<STR_LIT:target>' , '<STR_LIT:user>' ) <EOL> fk_name = '<STR_LIT:source>' <EOL> class ActionInChangeFormMixin ( object ) : <EOL> def response_action ( self , request , queryset ) : <EOL> """<STR_LIT>""" <EOL> _super = super ( ActionInChangeFormMixin , self ) <EOL> response = _super . response_action ( request , queryset ) <EOL> if isinstance ( response , HttpResponseRedirect ) : <EOL> response [ '<STR_LIT>' ] = request . META . get ( <EOL> '<STR_LIT>' , response . url ) <EOL> return response <EOL> def change_view ( self , request , object_id , extra_context = None ) : <EOL> actions = self . get_actions ( request ) <EOL> if actions : <EOL> action_form = self . action_form ( auto_id = None ) <EOL> choices = self . get_action_choices ( request ) <EOL> action_form . fields [ '<STR_LIT:action>' ] . choices = choices <EOL> else : <EOL> action_form = None <EOL> extra_context = extra_context or { } <EOL> extra_context [ '<STR_LIT>' ] = action_form <EOL> return super ( ActionInChangeFormMixin , self ) . change_view ( <EOL> request , object_id , extra_context = extra_context ) <EOL> class NounAdmin ( ActionInChangeFormMixin , admin . ModelAdmin ) : <EOL> list_display = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> list_filter = ( '<STR_LIT>' , ) <EOL> inlines = [ KeywordInline , ContentionInline , RelationInline ] <EOL> actions = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> search_fields = [ '<STR_LIT:text>' , '<STR_LIT>' ] <EOL> actions_on_top = True <EOL> actions_on_bottom = True <EOL> save_on_top = True <EOL> def get_form ( self , request , obj = None , ** kwargs ) : <EOL> form = super ( NounAdmin , self ) . get_form ( request , obj , ** kwargs ) <EOL> form . base_fields [ '<STR_LIT>' ] . initial = normalize_language_code ( get_language ( ) ) <EOL> return form <EOL> def get_queryset ( self , request ) : <EOL> qs = super ( NounAdmin , self ) . get_queryset ( request ) <EOL> return qs . prefetch_related ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def update_contentions ( self , request , qs ) : <EOL> q = Q ( ) <EOL> for noun in qs : <EOL> q |= Q ( title__icontains = noun . text ) <EOL> for keyword in noun . keywords . all ( ) : <EOL> q |= Q ( title__icontains = keyword . text ) <EOL> contentions = Contention . objects . filter ( q ) <EOL> for contention in contentions : <EOL> contention . save_nouns ( ) <EOL> def reset_contentions ( self , request , qs ) : <EOL> contentions = Contention . objects . filter ( nouns = qs ) <EOL> for contention in contentions : <EOL> contention . nouns . clear ( ) <EOL> def update_with_wordnet ( self , request , qs ) : <EOL> for noun in qs : <EOL> noun . update_with_wordnet ( ) <EOL> def hypernyms_as_text ( self , noun ) : <EOL> return '<STR_LIT:U+002CU+0020>' . join ( [ <EOL> '<STR_LIT>' % { <EOL> '<STR_LIT:id>' : hypernym . target . id , <EOL> '<STR_LIT:text>' : hypernym . target . text <EOL> } for hypernym in noun . hypernyms ( ) ] ) <EOL> hypernyms_as_text . allow_tags = True <EOL> def make_active ( self , request , qs ) : <EOL> return qs . update ( is_active = True ) <EOL> def make_passive ( self , request , qs ) : <EOL> return qs . update ( is_active = False ) <EOL> class ChannelAdmin ( admin . ModelAdmin ) : <EOL> filter_horizontal = ( '<STR_LIT>' , ) <EOL> admin . site . register ( Noun , NounAdmin ) <EOL> admin . site . register ( Channel , ChannelAdmin ) </s>
