<s> """<STR_LIT>""" <EOL> from django . apps import apps as django_apps <EOL> from django . core . urlresolvers import reverse <EOL> from django . forms . utils import flatatt <EOL> from django . utils . itercompat import is_iterable <EOL> from django . utils . text import capfirst <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from admin_tools . utils import AppListElementMixin , uniquify <EOL> class DashboardModule ( object ) : <EOL> """<STR_LIT>""" <EOL> template = '<STR_LIT>' <EOL> enabled = True <EOL> draggable = True <EOL> collapsible = True <EOL> deletable = True <EOL> show_title = True <EOL> title = '<STR_LIT>' <EOL> title_url = None <EOL> css_classes = None <EOL> pre_content = None <EOL> post_content = None <EOL> children = None <EOL> id = None <EOL> def __init__ ( self , title = None , ** kwargs ) : <EOL> if title is not None : <EOL> self . title = title <EOL> for key in kwargs : <EOL> if hasattr ( self . __class__ , key ) : <EOL> setattr ( self , key , kwargs [ key ] ) <EOL> self . children = self . children or [ ] <EOL> self . css_classes = self . css_classes or [ ] <EOL> self . _initialized = False <EOL> def init_with_context ( self , context ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def is_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . pre_content is None and self . post_content is None and len ( self . children ) == <NUM_LIT:0> <EOL> def render_css_classes ( self ) : <EOL> """<STR_LIT>""" <EOL> ret = [ '<STR_LIT>' ] <EOL> if not self . enabled : <EOL> ret . append ( '<STR_LIT>' ) <EOL> if self . draggable : <EOL> ret . append ( '<STR_LIT>' ) <EOL> if self . collapsible : <EOL> ret . append ( '<STR_LIT>' ) <EOL> if self . deletable : <EOL> ret . append ( '<STR_LIT>' ) <EOL> ret += self . css_classes <EOL> return '<STR_LIT:U+0020>' . join ( ret ) <EOL> def _prepare_children ( self ) : <EOL> pass <EOL> class Group ( DashboardModule ) : <EOL> """<STR_LIT>""" <EOL> force_show_title = True <EOL> template = '<STR_LIT>' <EOL> display = '<STR_LIT>' <EOL> def init_with_context ( self , context ) : <EOL> if self . _initialized : <EOL> return <EOL> for module in self . children : <EOL> module . collapsible = False <EOL> module . draggable = False <EOL> module . deletable = False <EOL> if self . force_show_title : <EOL> module . show_title = ( self . display == '<STR_LIT>' ) <EOL> module . init_with_context ( context ) <EOL> self . _initialized = True <EOL> def is_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> if super ( Group , self ) . is_empty ( ) : <EOL> return True <EOL> for child in self . children : <EOL> if not child . is_empty ( ) : <EOL> return False <EOL> return True <EOL> def _prepare_children ( self ) : <EOL> seen = set ( ) <EOL> for id , module in enumerate ( self . children ) : <EOL> proposed_id = "<STR_LIT>" % ( self . id , module . id or id + <NUM_LIT:1> ) <EOL> module . id = uniquify ( proposed_id , seen ) <EOL> module . _prepare_children ( ) <EOL> class LinkList ( DashboardModule ) : <EOL> """<STR_LIT>""" <EOL> title = _ ( '<STR_LIT>' ) <EOL> template = '<STR_LIT>' <EOL> layout = '<STR_LIT>' <EOL> def init_with_context ( self , context ) : <EOL> if self . _initialized : <EOL> return <EOL> new_children = [ ] <EOL> for link in self . children : <EOL> if isinstance ( link , ( tuple , list , ) ) : <EOL> link_dict = { '<STR_LIT:title>' : link [ <NUM_LIT:0> ] , '<STR_LIT:url>' : link [ <NUM_LIT:1> ] } <EOL> if len ( link ) >= <NUM_LIT:3> : <EOL> link_dict [ '<STR_LIT>' ] = link [ <NUM_LIT:2> ] <EOL> if len ( link ) >= <NUM_LIT:4> : <EOL> link_dict [ '<STR_LIT:description>' ] = link [ <NUM_LIT:3> ] <EOL> if len ( link ) >= <NUM_LIT:5> : <EOL> link_dict [ '<STR_LIT>' ] = link [ <NUM_LIT:4> ] <EOL> link = link_dict <EOL> if '<STR_LIT>' not in link : <EOL> link [ '<STR_LIT>' ] = { } <EOL> link [ '<STR_LIT>' ] [ '<STR_LIT>' ] = link [ '<STR_LIT:url>' ] <EOL> if link . get ( '<STR_LIT:description>' , '<STR_LIT>' ) : <EOL> link [ '<STR_LIT>' ] [ '<STR_LIT:title>' ] = link [ '<STR_LIT:description>' ] <EOL> if link . get ( '<STR_LIT>' , False ) : <EOL> link [ '<STR_LIT>' ] [ '<STR_LIT:class>' ] = '<STR_LIT:U+0020>' . join ( <EOL> [ '<STR_LIT>' ] + link [ '<STR_LIT>' ] . get ( '<STR_LIT:class>' , '<STR_LIT>' ) . split ( ) <EOL> ) . strip ( ) <EOL> link [ '<STR_LIT>' ] = flatatt ( link [ '<STR_LIT>' ] ) <EOL> new_children . append ( link ) <EOL> self . children = new_children <EOL> self . _initialized = True <EOL> class AppList ( DashboardModule , AppListElementMixin ) : <EOL> """<STR_LIT>""" <EOL> title = _ ( '<STR_LIT>' ) <EOL> template = '<STR_LIT>' <EOL> models = None <EOL> exclude = None <EOL> include_list = None <EOL> exclude_list = None <EOL> def __init__ ( self , title = None , ** kwargs ) : <EOL> self . models = list ( kwargs . pop ( '<STR_LIT>' , [ ] ) ) <EOL> self . exclude = list ( kwargs . pop ( '<STR_LIT>' , [ ] ) ) <EOL> self . include_list = kwargs . pop ( '<STR_LIT>' , [ ] ) <EOL> self . exclude_list = kwargs . pop ( '<STR_LIT>' , [ ] ) <EOL> super ( AppList , self ) . __init__ ( title , ** kwargs ) <EOL> def init_with_context ( self , context ) : <EOL> if self . _initialized : <EOL> return <EOL> items = self . _visible_models ( context [ '<STR_LIT>' ] ) <EOL> apps = { } <EOL> for model , perms in items : <EOL> app_label = model . _meta . app_label <EOL> if app_label not in apps : <EOL> apps [ app_label ] = { <EOL> '<STR_LIT:title>' : <EOL> django_apps . get_app_config ( app_label ) . verbose_name , <EOL> '<STR_LIT:url>' : self . _get_admin_app_list_url ( model , context ) , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> model_dict = { } <EOL> model_dict [ '<STR_LIT:title>' ] = model . _meta . verbose_name_plural <EOL> if perms [ '<STR_LIT>' ] : <EOL> model_dict [ '<STR_LIT>' ] = self . _get_admin_change_url ( <EOL> model , <EOL> context <EOL> ) <EOL> if perms [ '<STR_LIT>' ] : <EOL> model_dict [ '<STR_LIT>' ] = self . _get_admin_add_url ( model , context ) <EOL> apps [ app_label ] [ '<STR_LIT>' ] . append ( model_dict ) <EOL> for app in sorted ( apps . keys ( ) ) : <EOL> apps [ app ] [ '<STR_LIT>' ] . sort ( key = lambda x : x [ '<STR_LIT:title>' ] ) <EOL> self . children . append ( apps [ app ] ) <EOL> self . _initialized = True <EOL> class ModelList ( DashboardModule , AppListElementMixin ) : <EOL> """<STR_LIT>""" <EOL> template = '<STR_LIT>' <EOL> models = None <EOL> exclude = None <EOL> include_list = None <EOL> exclude_list = None <EOL> def __init__ ( self , title = None , models = None , exclude = None , ** kwargs ) : <EOL> self . models = list ( models or [ ] ) <EOL> self . exclude = list ( exclude or [ ] ) <EOL> self . include_list = kwargs . pop ( '<STR_LIT>' , [ ] ) <EOL> self . exclude_list = kwargs . pop ( '<STR_LIT>' , [ ] ) <EOL> if '<STR_LIT>' in kwargs : <EOL> self . extra = kwargs . pop ( '<STR_LIT>' ) <EOL> else : <EOL> self . extra = [ ] <EOL> super ( ModelList , self ) . __init__ ( title , ** kwargs ) <EOL> def init_with_context ( self , context ) : <EOL> if self . _initialized : <EOL> return <EOL> items = self . _visible_models ( context [ '<STR_LIT>' ] ) <EOL> if not items : <EOL> return <EOL> for model , perms in items : <EOL> model_dict = { } <EOL> model_dict [ '<STR_LIT:title>' ] = model . _meta . verbose_name_plural <EOL> if perms [ '<STR_LIT>' ] : <EOL> model_dict [ '<STR_LIT>' ] = self . _get_admin_change_url ( <EOL> model , <EOL> context <EOL> ) <EOL> if perms [ '<STR_LIT>' ] : <EOL> model_dict [ '<STR_LIT>' ] = self . _get_admin_add_url ( model , context ) <EOL> self . children . append ( model_dict ) <EOL> if self . extra : <EOL> for extra_url in self . extra : <EOL> model_dict = { } <EOL> model_dict [ '<STR_LIT:title>' ] = extra_url [ '<STR_LIT:title>' ] <EOL> model_dict [ '<STR_LIT>' ] = extra_url [ '<STR_LIT>' ] <EOL> model_dict [ '<STR_LIT>' ] = extra_url . get ( '<STR_LIT>' , None ) <EOL> self . children . append ( model_dict ) <EOL> self . _initialized = True <EOL> class RecentActions ( DashboardModule ) : <EOL> """<STR_LIT>""" <EOL> title = _ ( '<STR_LIT>' ) <EOL> template = '<STR_LIT>' <EOL> limit = <NUM_LIT:10> <EOL> include_list = None <EOL> exclude_list = None <EOL> def __init__ ( self , title = None , limit = <NUM_LIT:10> , include_list = None , <EOL> exclude_list = None , ** kwargs ) : <EOL> self . include_list = include_list or [ ] <EOL> self . exclude_list = exclude_list or [ ] <EOL> kwargs . update ( { '<STR_LIT>' : limit } ) <EOL> super ( RecentActions , self ) . __init__ ( title , ** kwargs ) <EOL> def init_with_context ( self , context ) : <EOL> if self . _initialized : <EOL> return <EOL> from django . db . models import Q <EOL> from django . contrib . admin . models import LogEntry <EOL> request = context [ '<STR_LIT>' ] <EOL> def get_qset ( list ) : <EOL> from django . contrib . contenttypes . models import ContentType <EOL> qset = None <EOL> for contenttype in list : <EOL> if isinstance ( contenttype , ContentType ) : <EOL> current_qset = Q ( content_type__id = contenttype . id ) <EOL> else : <EOL> try : <EOL> app_label , model = contenttype . split ( '<STR_LIT:.>' ) <EOL> except : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % contenttype <EOL> ) <EOL> current_qset = Q ( <EOL> content_type__app_label = app_label , <EOL> content_type__model = model <EOL> ) <EOL> if qset is None : <EOL> qset = current_qset <EOL> else : <EOL> qset = qset | current_qset <EOL> return qset <EOL> if request . user is None : <EOL> qs = LogEntry . objects . all ( ) <EOL> else : <EOL> qs = LogEntry . objects . filter ( user__id__exact = request . user . id ) <EOL> if self . include_list : <EOL> qs = qs . filter ( get_qset ( self . include_list ) ) <EOL> if self . exclude_list : <EOL> qs = qs . exclude ( get_qset ( self . exclude_list ) ) <EOL> self . children = qs . select_related ( '<STR_LIT>' , '<STR_LIT:user>' ) [ : self . limit ] <EOL> if not len ( self . children ) : <EOL> self . pre_content = _ ( '<STR_LIT>' ) <EOL> self . _initialized = True <EOL> class Feed ( DashboardModule ) : <EOL> """<STR_LIT>""" <EOL> title = _ ( '<STR_LIT>' ) <EOL> template = '<STR_LIT>' <EOL> feed_url = None <EOL> limit = None <EOL> def __init__ ( self , title = None , feed_url = None , limit = None , ** kwargs ) : <EOL> kwargs . update ( { '<STR_LIT>' : feed_url , '<STR_LIT>' : limit } ) <EOL> super ( Feed , self ) . __init__ ( title , ** kwargs ) <EOL> def init_with_context ( self , context ) : <EOL> if self . _initialized : <EOL> return <EOL> import datetime <EOL> if self . feed_url is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> try : <EOL> import feedparser <EOL> except ImportError : <EOL> self . children . append ( { <EOL> '<STR_LIT:title>' : ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> return <EOL> feed = feedparser . parse ( self . feed_url ) <EOL> if self . limit is not None : <EOL> entries = feed [ '<STR_LIT>' ] [ : self . limit ] <EOL> else : <EOL> entries = feed [ '<STR_LIT>' ] <EOL> for entry in entries : <EOL> entry . url = entry . link <EOL> try : <EOL> entry . date = datetime . date ( * entry . published_parsed [ <NUM_LIT:0> : <NUM_LIT:3> ] ) <EOL> except : <EOL> pass <EOL> self . children . append ( entry ) <EOL> self . _initialized = True </s>
