<s> from __future__ import unicode_literals <EOL> from future . builtins import str <EOL> try : <EOL> from urllib . parse import urljoin <EOL> except ImportError : <EOL> from urlparse import urljoin <EOL> from django . apps import apps <EOL> from django . core . urlresolvers import resolve , reverse <EOL> from django . db import models <EOL> from django . utils . encoding import python_2_unicode_compatible <EOL> from django . utils . translation import ugettext_lazy as _ , ugettext <EOL> from mezzanine . conf import settings <EOL> from mezzanine . core . models import Displayable , Orderable , RichText <EOL> from mezzanine . pages . fields import MenusField <EOL> from mezzanine . pages . managers import PageManager <EOL> from mezzanine . utils . urls import path_to_slug <EOL> class BasePage ( Orderable , Displayable ) : <EOL> """<STR_LIT>""" <EOL> objects = PageManager ( ) <EOL> class Meta : <EOL> abstract = True <EOL> @ python_2_unicode_compatible <EOL> class Page ( BasePage ) : <EOL> """<STR_LIT>""" <EOL> parent = models . ForeignKey ( "<STR_LIT>" , blank = True , null = True , <EOL> related_name = "<STR_LIT>" ) <EOL> in_menus = MenusField ( _ ( "<STR_LIT>" ) , blank = True , null = True ) <EOL> titles = models . CharField ( editable = False , max_length = <NUM_LIT:1000> , null = True ) <EOL> content_model = models . CharField ( editable = False , max_length = <NUM_LIT:50> , null = True ) <EOL> login_required = models . BooleanField ( _ ( "<STR_LIT>" ) , default = False , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> ordering = ( "<STR_LIT>" , ) <EOL> order_with_respect_to = "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> return self . titles <EOL> def get_absolute_url ( self ) : <EOL> """<STR_LIT>""" <EOL> slug = self . slug <EOL> if self . content_model == "<STR_LIT>" : <EOL> slug = urljoin ( '<STR_LIT:/>' , slug ) <EOL> return slug <EOL> if slug == "<STR_LIT:/>" : <EOL> return reverse ( "<STR_LIT>" ) <EOL> else : <EOL> return reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT>" : slug } ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . id is None : <EOL> self . content_model = self . _meta . object_name . lower ( ) <EOL> titles = [ self . title ] <EOL> parent = self . parent <EOL> while parent is not None : <EOL> titles . insert ( <NUM_LIT:0> , parent . title ) <EOL> parent = parent . parent <EOL> self . titles = "<STR_LIT>" . join ( titles ) <EOL> super ( Page , self ) . save ( * args , ** kwargs ) <EOL> def description_from_content ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __class__ == Page : <EOL> content_model = self . get_content_model ( ) <EOL> if content_model : <EOL> return content_model . description_from_content ( ) <EOL> return super ( Page , self ) . description_from_content ( ) <EOL> def get_ascendants ( self , for_user = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . parent_id : <EOL> return [ ] <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> if self . slug : <EOL> kwargs = { "<STR_LIT>" : for_user } <EOL> pages = Page . objects . with_ascendants_for_slug ( self . slug , <EOL> ** kwargs ) <EOL> self . _ascendants = pages [ <NUM_LIT:0> ] . _ascendants <EOL> else : <EOL> self . _ascendants = [ ] <EOL> if not self . _ascendants : <EOL> child = self <EOL> while child . parent_id is not None : <EOL> self . _ascendants . append ( child . parent ) <EOL> child = child . parent <EOL> return self . _ascendants <EOL> @ classmethod <EOL> def get_content_models ( cls ) : <EOL> """<STR_LIT>""" <EOL> return [ m for m in apps . get_models ( ) <EOL> if m is not Page and issubclass ( m , Page ) ] <EOL> def get_content_model ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self , self . content_model , None ) <EOL> def get_slug ( self ) : <EOL> """<STR_LIT>""" <EOL> slug = super ( Page , self ) . get_slug ( ) <EOL> if self . parent is not None : <EOL> return "<STR_LIT>" % ( self . parent . slug , slug ) <EOL> return slug <EOL> def set_slug ( self , new_slug ) : <EOL> """<STR_LIT>""" <EOL> slug_prefix = "<STR_LIT>" % self . slug <EOL> for page in Page . objects . filter ( slug__startswith = slug_prefix ) : <EOL> if not page . overridden ( ) : <EOL> page . slug = new_slug + page . slug [ len ( self . slug ) : ] <EOL> page . save ( ) <EOL> self . slug = new_slug <EOL> self . save ( ) <EOL> def set_parent ( self , new_parent ) : <EOL> """<STR_LIT>""" <EOL> self_slug = self . slug <EOL> old_parent_slug = self . parent . slug if self . parent else "<STR_LIT>" <EOL> new_parent_slug = new_parent . slug if new_parent else "<STR_LIT>" <EOL> parent = new_parent <EOL> while parent is not None : <EOL> if parent . pk == self . pk : <EOL> raise AttributeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parent = parent . parent <EOL> self . parent = new_parent <EOL> self . save ( ) <EOL> if self_slug : <EOL> if not old_parent_slug : <EOL> self . set_slug ( "<STR_LIT:/>" . join ( ( new_parent_slug , self . slug ) ) ) <EOL> elif self . slug . startswith ( old_parent_slug ) : <EOL> new_slug = self . slug . replace ( old_parent_slug , <EOL> new_parent_slug , <NUM_LIT:1> ) <EOL> self . set_slug ( new_slug . strip ( "<STR_LIT:/>" ) ) <EOL> def overridden ( self ) : <EOL> """<STR_LIT>""" <EOL> from mezzanine . pages . views import page <EOL> page_url = reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT>" : self . slug } ) <EOL> resolved_view = resolve ( page_url ) [ <NUM_LIT:0> ] <EOL> return resolved_view != page <EOL> def can_add ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return self . slug != "<STR_LIT:/>" <EOL> def can_change ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def can_delete ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def can_move ( self , request , new_parent ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def set_helpers ( self , context ) : <EOL> """<STR_LIT>""" <EOL> current_page = context [ "<STR_LIT>" ] <EOL> current_page_id = getattr ( current_page , "<STR_LIT:id>" , None ) <EOL> current_parent_id = getattr ( current_page , "<STR_LIT>" , None ) <EOL> self . is_current_child = self . parent_id == current_page_id <EOL> self . is_child = self . is_current_child <EOL> self . is_current_sibling = self . parent_id == current_parent_id <EOL> try : <EOL> request = context [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> self . is_current = False <EOL> else : <EOL> self . is_current = self . slug == path_to_slug ( request . path_info ) <EOL> def is_c_or_a ( page_id ) : <EOL> parent_id = context . get ( "<STR_LIT>" , { } ) . get ( page_id ) <EOL> return self . id == page_id or ( parent_id and is_c_or_a ( parent_id ) ) <EOL> self . is_current_or_ascendant = lambda : bool ( is_c_or_a ( current_page_id ) ) <EOL> self . is_current_parent = self . id == current_parent_id <EOL> self . is_primary = self . parent_id is None <EOL> self . html_id = self . slug . replace ( "<STR_LIT:/>" , "<STR_LIT:->" ) <EOL> self . branch_level = <NUM_LIT:0> <EOL> def in_menu_template ( self , template_name ) : <EOL> if self . in_menus is not None : <EOL> for i , l , t in settings . PAGE_MENU_TEMPLATES : <EOL> if not str ( i ) in self . in_menus and t == template_name : <EOL> return False <EOL> return True <EOL> def get_template_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> class RichTextPage ( Page , RichText ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> class Link ( Page ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> class PageMoveException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , msg = None ) : <EOL> self . msg = msg or ugettext ( "<STR_LIT>" ) <EOL> def __str__ ( self ) : <EOL> return self . msg <EOL> __unicode__ = __str__ </s>
