<s> from django . conf . urls import url <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . contrib . staticfiles import finders <EOL> from django . contrib . auth import get_permission_codename <EOL> from django . utils . functional import cached_property <EOL> from widgy import registry <EOL> from widgy . views import ( <EOL> NodeView , <EOL> ContentView , <EOL> ShelfView , <EOL> NodeEditView , <EOL> NodeTemplatesView , <EOL> NodeParentsView , <EOL> CommitView , <EOL> HistoryView , <EOL> RevertView , <EOL> DiffView , <EOL> ResetView , <EOL> ) <EOL> from widgy . exceptions import ( <EOL> MutualRejection , <EOL> ParentWasRejected , <EOL> ChildWasRejected , <EOL> ) <EOL> class WidgySite ( object ) : <EOL> def get_registry ( self ) : <EOL> return registry <EOL> def get_all_content_classes ( self ) : <EOL> return self . get_registry ( ) <EOL> def get_urls ( self ) : <EOL> urlpatterns = [ <EOL> url ( '<STR_LIT>' , self . node_view ) , <EOL> url ( '<STR_LIT>' , self . node_view ) , <EOL> url ( '<STR_LIT>' , self . shelf_view ) , <EOL> url ( '<STR_LIT>' , self . node_edit_view ) , <EOL> url ( '<STR_LIT>' , self . node_templates_view ) , <EOL> url ( '<STR_LIT>' , self . node_parents_view ) , <EOL> url ( '<STR_LIT>' , self . content_view ) , <EOL> url ( '<STR_LIT>' , self . revert_view ) , <EOL> url ( '<STR_LIT>' , self . commit_view ) , <EOL> url ( '<STR_LIT>' , self . history_view ) , <EOL> url ( '<STR_LIT>' , self . reset_view ) , <EOL> url ( '<STR_LIT>' , self . diff_view ) , <EOL> ] <EOL> return urlpatterns <EOL> @ property <EOL> def urls ( self ) : <EOL> return self . get_urls ( ) <EOL> def reverse ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return reverse ( * args , ** kwargs ) <EOL> def authorize_view ( self , request , view ) : <EOL> if not ( request . user . is_authenticated ( ) and request . user . is_staff ) : <EOL> raise PermissionDenied <EOL> def has_add_permission ( self , request , parent , created_obj_cls ) : <EOL> codename = get_permission_codename ( '<STR_LIT>' , created_obj_cls . _meta ) <EOL> return request . user . has_perm ( '<STR_LIT>' % ( created_obj_cls . _meta . app_label , codename ) ) <EOL> def has_change_permission ( self , request , obj_or_class ) : <EOL> codename = get_permission_codename ( '<STR_LIT>' , obj_or_class . _meta ) <EOL> return request . user . has_perm ( '<STR_LIT>' % ( obj_or_class . _meta . app_label , codename ) ) <EOL> def has_delete_permission ( self , request , obj_or_class ) : <EOL> def has_perm ( o ) : <EOL> codename = get_permission_codename ( '<STR_LIT>' , o . _meta ) <EOL> return request . user . has_perm ( '<STR_LIT>' % ( o . _meta . app_label , codename ) ) <EOL> if isinstance ( obj_or_class , type ) : <EOL> return has_perm ( obj_or_class ) <EOL> else : <EOL> return all ( has_perm ( o ) for o in obj_or_class . depth_first_order ( ) ) <EOL> @ cached_property <EOL> def node_view ( self ) : <EOL> return NodeView . as_view ( site = self ) <EOL> @ cached_property <EOL> def content_view ( self ) : <EOL> return ContentView . as_view ( site = self ) <EOL> @ cached_property <EOL> def shelf_view ( self ) : <EOL> return ShelfView . as_view ( site = self ) <EOL> @ cached_property <EOL> def node_edit_view ( self ) : <EOL> return NodeEditView . as_view ( site = self ) <EOL> @ cached_property <EOL> def node_templates_view ( self ) : <EOL> return NodeTemplatesView . as_view ( site = self ) <EOL> @ cached_property <EOL> def node_parents_view ( self ) : <EOL> return NodeParentsView . as_view ( site = self ) <EOL> @ cached_property <EOL> def commit_view ( self ) : <EOL> return CommitView . as_view ( site = self ) <EOL> @ cached_property <EOL> def history_view ( self ) : <EOL> return HistoryView . as_view ( site = self ) <EOL> @ cached_property <EOL> def revert_view ( self ) : <EOL> return RevertView . as_view ( site = self ) <EOL> @ cached_property <EOL> def diff_view ( self ) : <EOL> return DiffView . as_view ( site = self ) <EOL> @ cached_property <EOL> def reset_view ( self ) : <EOL> return ResetView . as_view ( site = self ) <EOL> def valid_parent_of ( self , parent , child_class , child = None ) : <EOL> return parent . valid_parent_of ( child_class , child ) <EOL> def valid_child_of ( self , parent , child_class , child = None ) : <EOL> return child_class . valid_child_of ( parent , child ) <EOL> def validate_relationship ( self , parent , child ) : <EOL> if isinstance ( child , type ) : <EOL> child_class = child <EOL> child = None <EOL> else : <EOL> child_class = type ( child ) <EOL> bad_child = not self . valid_parent_of ( parent , child_class , child ) <EOL> bad_parent = not self . valid_child_of ( parent , child_class , child ) <EOL> if bad_parent and bad_child : <EOL> raise MutualRejection ( "<STR_LIT>" . format ( <EOL> child = child_class . __name__ , <EOL> parent = type ( parent ) . __name__ , <EOL> ) ) <EOL> elif bad_parent : <EOL> raise ParentWasRejected ( "<STR_LIT>" . format ( <EOL> child = child_class . __name__ , <EOL> parent = type ( parent ) . __name__ , <EOL> ) ) <EOL> elif bad_child : <EOL> raise ChildWasRejected ( "<STR_LIT>" . format ( <EOL> child = child_class . __name__ , <EOL> parent = type ( parent ) . __name__ , <EOL> ) ) <EOL> def get_version_tracker_model ( self ) : <EOL> from widgy . models import VersionTracker <EOL> return VersionTracker <EOL> def filter_existing_staticfiles ( self , filename ) : <EOL> path = finders . find ( filename ) <EOL> return bool ( path ) <EOL> def find_media_files ( self , extension , hierarchy = [ '<STR_LIT>' ] ) : <EOL> files = set ( ) <EOL> for widget in self . get_all_content_classes ( ) : <EOL> files . update ( widget . get_templates_hierarchy ( <EOL> hierarchy = hierarchy , <EOL> extension = extension , <EOL> ) ) <EOL> return sorted ( filter ( self . filter_existing_staticfiles , files ) ) <EOL> @ cached_property <EOL> def scss_files ( self ) : <EOL> return self . find_media_files ( '<STR_LIT>' ) <EOL> @ cached_property <EOL> def js_files ( self ) : <EOL> return self . find_media_files ( '<STR_LIT>' ) <EOL> @ cached_property <EOL> def admin_scss_files ( self ) : <EOL> return self . find_media_files ( <EOL> extension = '<STR_LIT>' , <EOL> hierarchy = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def valid_root_of ( self , owner_field , root_class , root_choices ) : <EOL> return issubclass ( root_class , root_choices ) </s>
