<s> from django . template import loader <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . decorators import method_decorator <EOL> from django . views . decorators . csrf import csrf_protect <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db import transaction <EOL> from django . db . models import Q <EOL> from django . forms import ModelChoiceField <EOL> from django . http import QueryDict <EOL> from xadmin . sites import site <EOL> from xadmin . views import ModelAdminView , BaseAdminPlugin , ListAdminView <EOL> from xadmin . views . list import COL_LIST_VAR , ORDER_VAR <EOL> from xadmin . views . dashboard import widget_manager , BaseWidget , PartialBaseWidget <EOL> from xadmin . filters import FILTER_PREFIX , SEARCH_VAR <EOL> from xadmin . plugins . relate import RELATE_PREFIX <EOL> from xadmin . models import Bookmark <EOL> csrf_protect_m = method_decorator ( csrf_protect ) <EOL> class BookmarkPlugin ( BaseAdminPlugin ) : <EOL> list_bookmarks = [ ] <EOL> show_bookmarks = True <EOL> def has_change_permission ( self , obj = None ) : <EOL> if not obj or self . user . is_superuser : <EOL> return True <EOL> else : <EOL> return obj . user == self . user <EOL> def get_context ( self , context ) : <EOL> if not self . show_bookmarks : <EOL> return context <EOL> bookmarks = [ ] <EOL> current_qs = '<STR_LIT:&>' . join ( [ '<STR_LIT>' % ( k , v ) for k , v in sorted ( <EOL> filter ( lambda i : bool ( i [ <NUM_LIT:1> ] and ( i [ <NUM_LIT:0> ] in ( COL_LIST_VAR , ORDER_VAR , SEARCH_VAR ) or i [ <NUM_LIT:0> ] . startswith ( FILTER_PREFIX ) <EOL> or i [ <NUM_LIT:0> ] . startswith ( RELATE_PREFIX ) ) ) , self . request . GET . items ( ) ) ) ] ) <EOL> model_info = ( self . opts . app_label , self . opts . model_name ) <EOL> has_selected = False <EOL> menu_title = _ ( u"<STR_LIT>" ) <EOL> list_base_url = reverse ( '<STR_LIT>' % <EOL> model_info , current_app = self . admin_site . name ) <EOL> for bk in self . list_bookmarks : <EOL> title = bk [ '<STR_LIT:title>' ] <EOL> params = dict ( <EOL> [ ( FILTER_PREFIX + k , v ) for ( k , v ) in bk [ '<STR_LIT>' ] . items ( ) ] ) <EOL> if '<STR_LIT>' in bk : <EOL> params [ ORDER_VAR ] = '<STR_LIT:.>' . join ( bk [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in bk : <EOL> params [ COL_LIST_VAR ] = '<STR_LIT:.>' . join ( bk [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in bk : <EOL> params [ SEARCH_VAR ] = bk [ '<STR_LIT>' ] <EOL> def check_item ( i ) : <EOL> return bool ( i [ <NUM_LIT:1> ] ) or i [ <NUM_LIT:1> ] == False <EOL> bk_qs = '<STR_LIT:&>' . join ( [ '<STR_LIT>' % ( k , v ) for k , v in sorted ( filter ( check_item , params . items ( ) ) ) ] ) <EOL> url = list_base_url + '<STR_LIT:?>' + bk_qs <EOL> selected = ( current_qs == bk_qs ) <EOL> bookmarks . append ( <EOL> { '<STR_LIT:title>' : title , '<STR_LIT>' : selected , '<STR_LIT:url>' : url } ) <EOL> if selected : <EOL> menu_title = title <EOL> has_selected = True <EOL> content_type = ContentType . objects . get_for_model ( self . model ) <EOL> bk_model_info = ( Bookmark . _meta . app_label , Bookmark . _meta . model_name ) <EOL> bookmarks_queryset = Bookmark . objects . filter ( <EOL> content_type = content_type , <EOL> url_name = '<STR_LIT>' % model_info <EOL> ) . filter ( Q ( user = self . user ) | Q ( is_share = True ) ) <EOL> for bk in bookmarks_queryset : <EOL> selected = ( current_qs == bk . query ) <EOL> if self . has_change_permission ( bk ) : <EOL> change_or_detail = '<STR_LIT>' <EOL> else : <EOL> change_or_detail = '<STR_LIT>' <EOL> bookmarks . append ( { '<STR_LIT:title>' : bk . title , '<STR_LIT>' : selected , '<STR_LIT:url>' : bk . url , '<STR_LIT>' : <EOL> reverse ( '<STR_LIT>' % ( bk_model_info [ <NUM_LIT:0> ] , bk_model_info [ <NUM_LIT:1> ] , change_or_detail ) , <EOL> args = ( bk . id , ) ) } ) <EOL> if selected : <EOL> menu_title = bk . title <EOL> has_selected = True <EOL> post_url = reverse ( '<STR_LIT>' % model_info , <EOL> current_app = self . admin_site . name ) <EOL> new_context = { <EOL> '<STR_LIT>' : menu_title , <EOL> '<STR_LIT>' : bookmarks , <EOL> '<STR_LIT>' : current_qs , <EOL> '<STR_LIT>' : has_selected , <EOL> '<STR_LIT>' : list_base_url , <EOL> '<STR_LIT>' : post_url , <EOL> '<STR_LIT>' : self . admin_view . request . user . has_perm ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . admin_view . request . user . has_perm ( '<STR_LIT>' ) <EOL> } <EOL> context . update ( new_context ) <EOL> return context <EOL> def get_media ( self , media ) : <EOL> return media + self . vendor ( '<STR_LIT>' ) <EOL> def block_nav_menu ( self , context , nodes ) : <EOL> if self . show_bookmarks : <EOL> nodes . insert ( <NUM_LIT:0> , loader . render_to_string ( '<STR_LIT>' , context = context ) ) <EOL> class BookmarkView ( ModelAdminView ) : <EOL> @ csrf_protect_m <EOL> @ transaction . atomic <EOL> def post ( self , request ) : <EOL> model_info = ( self . opts . app_label , self . opts . model_name ) <EOL> url_name = '<STR_LIT>' % model_info <EOL> bookmark = Bookmark ( <EOL> content_type = ContentType . objects . get_for_model ( self . model ) , <EOL> title = request . POST [ <EOL> '<STR_LIT:title>' ] , user = self . user , query = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> is_share = request . POST . get ( '<STR_LIT>' , <NUM_LIT:0> ) , url_name = url_name ) <EOL> bookmark . save ( ) <EOL> content = { '<STR_LIT:title>' : bookmark . title , '<STR_LIT:url>' : bookmark . url } <EOL> return self . render_response ( content ) <EOL> class BookmarkAdmin ( object ) : <EOL> model_icon = '<STR_LIT>' <EOL> list_display = ( '<STR_LIT:title>' , '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> list_display_links = ( '<STR_LIT:title>' , ) <EOL> user_fields = [ '<STR_LIT:user>' ] <EOL> hidden_menu = True <EOL> def queryset ( self ) : <EOL> if self . user . is_superuser : <EOL> return Bookmark . objects . all ( ) <EOL> return Bookmark . objects . filter ( Q ( user = self . user ) | Q ( is_share = True ) ) <EOL> def get_list_display ( self ) : <EOL> list_display = super ( BookmarkAdmin , self ) . get_list_display ( ) <EOL> if not self . user . is_superuser : <EOL> list_display . remove ( '<STR_LIT:user>' ) <EOL> return list_display <EOL> def has_change_permission ( self , obj = None ) : <EOL> if not obj or self . user . is_superuser : <EOL> return True <EOL> else : <EOL> return obj . user == self . user <EOL> @ widget_manager . register <EOL> class BookmarkWidget ( PartialBaseWidget ) : <EOL> widget_type = _ ( '<STR_LIT>' ) <EOL> widget_icon = '<STR_LIT>' <EOL> description = _ ( <EOL> '<STR_LIT>' ) <EOL> template = "<STR_LIT>" <EOL> bookmark = ModelChoiceField ( <EOL> label = _ ( '<STR_LIT>' ) , queryset = Bookmark . objects . all ( ) , required = False ) <EOL> def setup ( self ) : <EOL> BaseWidget . setup ( self ) <EOL> bookmark = self . cleaned_data [ '<STR_LIT>' ] <EOL> model = bookmark . content_type . model_class ( ) <EOL> data = QueryDict ( bookmark . query ) <EOL> self . bookmark = bookmark <EOL> if not self . title : <EOL> self . title = unicode ( bookmark ) <EOL> req = self . make_get_request ( "<STR_LIT>" , data . items ( ) ) <EOL> self . list_view = self . get_view_class ( <EOL> ListAdminView , model , list_per_page = <NUM_LIT:10> , list_editable = [ ] ) ( req ) <EOL> def has_perm ( self ) : <EOL> return True <EOL> def context ( self , context ) : <EOL> list_view = self . list_view <EOL> list_view . make_result_list ( ) <EOL> base_fields = list_view . base_list_display <EOL> if len ( base_fields ) > <NUM_LIT:5> : <EOL> base_fields = base_fields [ <NUM_LIT:0> : <NUM_LIT:5> ] <EOL> context [ '<STR_LIT>' ] = [ c for c in list_view . result_headers ( <EOL> ) . cells if c . field_name in base_fields ] <EOL> context [ '<STR_LIT>' ] = [ [ o for i , o in <EOL> enumerate ( filter ( lambda c : c . field_name in base_fields , r . cells ) ) ] <EOL> for r in list_view . results ( ) ] <EOL> context [ '<STR_LIT>' ] = list_view . result_count <EOL> context [ '<STR_LIT>' ] = self . bookmark . url <EOL> site . register ( Bookmark , BookmarkAdmin ) <EOL> site . register_plugin ( BookmarkPlugin , ListAdminView ) <EOL> site . register_modelview ( r'<STR_LIT>' , BookmarkView , name = '<STR_LIT>' ) </s>
