Changeset 2064

Show
Ignore:
Timestamp:
06/25/09 11:21:23 (9 months ago)
Author:
JensDiemer
Message:
 
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/0.9/pylucid_project/apps/pylucid/models.py

    r2060 r2064  
    4343from pylucid_project.utils import crypt 
    4444 
    45 from pylucid.system.auto_model_info import UpdateInfoBaseModel,AutoSiteM2M 
     45from pylucid.system.auto_model_info import UpdateInfoBaseModel, AutoSiteM2M 
    4646from pylucid.shortcuts import user_message_or_warn 
    4747from pylucid.fields import ColorValueField 
     
    5252    """ 
    5353    Manager class for PageTree model 
    54      
     54 
    5555    inherited from models.Manager: 
    5656        get_or_create() method, witch expected a request object as the first argument. 
     
    6767                raise 
    6868        return root_page 
    69      
     69 
    7070    def get_model_instance(self, request, ModelClass, pagetree=None): 
    7171        """ 
     
    7979        # default Language instance set in system preferences: 
    8080        default_lang_entry = request.PYLUCID.default_lang_entry 
    81          
     81 
    8282        lang_entry = request.PYLUCID.lang_entry 
    8383        default_lang_entry = request.PYLUCID.default_lang_entry 
    84          
     84 
    8585        if not pagetree: 
    8686            # current pagetree instance 
    8787            pagetree = request.PYLUCID.pagetree 
    88              
     88 
    8989        queryset = ModelClass.objects.all().filter(page=pagetree) 
    9090        try: 
     
    9494            # Get the PageContent entry in the system default language 
    9595            return queryset.get(lang=default_lang_entry) 
    96      
     96 
    9797    def get_pagemeta(self, request, pagetree=None): 
    9898        """ 
     
    102102        """ 
    103103        return self.get_model_instance(request, PageMeta, pagetree) 
    104      
     104 
    105105    def get_pagecontent(self, request, pagetree=None): 
    106106        """ 
     
    110110        """ 
    111111        return self.get_model_instance(request, PageContent, pagetree) 
    112      
     112 
    113113    def get_page_from_url(self, url_path): 
    114114        """ returns a tuple the page tree instance from the given url_path""" 
     
    124124            if page.type == PageTree.PLUGIN_TYPE: 
    125125                # It's a plugin 
    126                 prefix_url = "/".join(path[:no+1]) 
    127                 rest_url = "/".join(path[no+1:]) 
     126                prefix_url = "/".join(path[:no + 1]) 
     127                rest_url = "/".join(path[no + 1:]) 
    128128#                if not rest_url.endswith("/"): 
    129129#                    rest_url += "/" 
     
    141141        if pagetree == None: 
    142142            pagetree = request.PYLUCID.pagetree 
    143          
     143 
    144144        pagemeta = self.get_pagemeta(request, pagetree) 
    145145        url = pagemeta.get_absolute_url() 
    146146        title = pagemeta.title_or_slug() 
    147          
     147 
    148148        backlist = [{"url": url, "title": title}] 
    149          
     149 
    150150        parent = pagetree.parent 
    151         if parent:  
     151        if parent: 
    152152            # insert parent links 
    153153            backlist = self.get_backlist(request, parent) + backlist 
     
    160160    """ 
    161161    The CMS page tree 
    162      
     162 
    163163    inherited attributes from UpdateInfoBaseModel: 
    164164        createtime     -> datetime of creation 
     
    198198    permitViewGroup = models.ForeignKey(Group, related_name="%(class)s_permitViewGroup", 
    199199        help_text="Limit viewable to a group?", 
    200         null=True, blank=True,  
     200        null=True, blank=True, 
    201201    ) 
    202202    permitEditGroup = models.ForeignKey(Group, related_name="%(class)s_permitEditGroup", 
     
    217217 
    218218    class Meta: 
    219         unique_together=(("site", "slug", "parent"),) 
    220          
     219        unique_together = (("site", "slug", "parent"),) 
     220 
    221221        # FIXME: It would be great if we can order by get_absolute_url() 
    222222        ordering = ("site", "id", "position") 
     
    232232    permitViewGroup = models.ForeignKey(Group, related_name="%(class)s_permitViewGroup", 
    233233        help_text="Limit viewable to a group for a complete language section?", 
    234         null=True, blank=True,  
     234        null=True, blank=True, 
    235235    ) 
    236236 
     
    250250    page = models.ForeignKey(PageTree) 
    251251    lang = models.ForeignKey(Language) 
    252      
     252 
    253253    def get_absolute_url(self): 
    254254        """ absolute url *with* language code (without domain/host part) """ 
     
    256256        page_url = self.page.get_absolute_url() 
    257257        return "/" + lang_code + page_url 
    258      
     258 
    259259    def get_site(self): 
    260260        return self.page.site 
    261      
     261 
    262262    class Meta: 
    263263        abstract = True 
     
    268268    """ 
    269269    Meta data for PageContent or PluginPage 
    270      
     270 
    271271    inherited attributes from i18nPageTreeBaseModel: 
    272272        page -> ForeignKey to PageTree 
    273273        lang -> ForeignKey to Language 
    274274        get_absolute_url() 
    275      
     275 
    276276    inherited attributes from UpdateInfoBaseModel: 
    277277        createtime     -> datetime of creation 
     
    293293    permitViewGroup = models.ForeignKey(Group, related_name="%(class)s_permitViewGroup", 
    294294        help_text="Limit viewable to a group?", 
    295         null=True, blank=True,  
     295        null=True, blank=True, 
    296296    ) 
    297297 
     
    302302    def __unicode__(self): 
    303303        return u"PageMeta for page: '%s' (lang: '%s')" % (self.page.slug, self.lang.code) 
    304      
    305     class Meta: 
    306         unique_together = (("page","lang"),) 
     304 
     305    class Meta: 
     306        unique_together = (("page", "lang"),) 
    307307        ordering = ("page", "lang") 
    308308 
     
    313313    """ 
    314314    A plugin page 
    315      
     315 
    316316    inherited attributes from i18nPageTreeBaseModel: 
    317317        page -> ForeignKey to PageTree 
    318318        lang -> ForeignKey to Language 
    319319        get_absolute_url() 
    320      
     320 
    321321    inherited attributes from UpdateInfoBaseModel: 
    322322        createtime     -> datetime of creation 
     
    325325        lastupdateby   -> ForeignKey to user who has edited this entry 
    326326    """ 
    327     APP_LABEL_CHOICES = [(app,app) for app in settings.INSTALLED_APPS] 
    328      
     327    APP_LABEL_CHOICES = [(app, app) for app in settings.INSTALLED_APPS] 
     328 
    329329    pagemeta = models.ForeignKey(PageMeta) 
    330      
     330 
    331331    app_label = models.CharField(max_length=256, choices=APP_LABEL_CHOICES, 
    332332        help_text="The app lable witch is in settings.INSTALLED_APPS" 
    333333    ) 
    334      
     334 
    335335    def title_or_slug(self): 
    336336        """ The page title is optional, if not exist, used the slug from the page tree """ 
    337337        return self.pagemeta.title or self.page.slug 
    338      
     338 
    339339    def get_plugin_name(self): 
    340340        return self.app_label.split(".")[-1] 
    341      
     341 
    342342    def save(self, *args, **kwargs): 
    343343        if not self.page.type == self.page.PLUGIN_TYPE: 
     
    345345            raise AssertionError("Plugin can only exist on a plugin type tree entry!") 
    346346        return super(PluginPage, self).save(*args, **kwargs) 
    347      
     347 
    348348    def __unicode__(self): 
    349349        return u"PluginPage '%s' (page: %s)" % (self.app_label, self.page) 
    350      
    351     class Meta: 
    352         unique_together = (("page","lang"),) 
     350 
     351    class Meta: 
     352        unique_together = (("page", "lang"),) 
    353353        ordering = ("page", "lang") 
    354354 
     
    360360    """ 
    361361    Manager class for PageContent model 
    362      
     362 
    363363    inherited from models.Manager: 
    364364        get_or_create() method, witch expected a request object as the first argument. 
    365     """     
     365    """ 
    366366    def get_sub_pages(self, pagecontent): 
    367367        """ 
     
    373373        current_page = pagecontent.page 
    374374        sub_pages = PageContent.objects.all().filter(page__parent=current_page, lang=current_lang) 
    375         return sub_pages    
     375        return sub_pages 
    376376 
    377377 
     
    392392    """ 
    393393    # IDs used in other parts of PyLucid, too 
    394     MARKUP_CREOLE       = 6 
    395     MARKUP_HTML         = 0 
    396     MARKUP_HTML_EDITOR  = 1 
    397     MARKUP_TINYTEXTILE  = 2 
    398     MARKUP_TEXTILE      = 3 
    399     MARKUP_MARKDOWN     = 4 
    400     MARKUP_REST         = 5 
     394    MARKUP_CREOLE = 6 
     395    MARKUP_HTML = 0 
     396    MARKUP_HTML_EDITOR = 1 
     397    MARKUP_TINYTEXTILE = 2 
     398    MARKUP_TEXTILE = 3 
     399    MARKUP_MARKDOWN = 4 
     400    MARKUP_REST = 5 
    401401 
    402402    MARKUP_CHOICES = ( 
     
    411411    MARKUP_DICT = dict(MARKUP_CHOICES) 
    412412    #-------------------------------------------------------------------------- 
    413      
     413 
    414414    objects = PageContentManager() 
    415415 
     
    433433 
    434434    class Meta: 
    435         unique_together = (("page","lang"),) 
     435        unique_together = (("page", "lang"),) 
    436436        ordering = ("page", "lang") 
    437437 
     
    445445    """ 
    446446    name = models.CharField(max_length=255, help_text="The name of this color scheme.") 
    447      
     447 
    448448    def update(self, colors): 
    449449        assert isinstance(colors, dict) 
     
    474474        colors = self.all().filter(colorscheme=colorscheme) 
    475475        color_list = colors.values_list('name', 'value') 
    476         return dict([(name, "#%s" % value) for name,value in color_list]) 
     476        return dict([(name, "#%s" % value) for name, value in color_list]) 
    477477 
    478478class Color(AutoSiteM2M, UpdateInfoBaseModel): 
     
    483483    """ 
    484484    objects = ColorManager() 
    485      
     485 
    486486    colorscheme = models.ForeignKey(ColorScheme) 
    487487    name = models.CharField(max_length=128, 
     
    489489    ) 
    490490    value = ColorValueField(help_text="CSS hex color value.") 
    491      
     491 
    492492    def save(self, *args, **kwargs): 
    493493        self.name = self.name.replace(" ", "_") 
    494494        new_name = self.name 
    495         old_name = Color.objects.get(id=self.id).name 
    496         if new_name != old_name: 
    497             # Color name has been changed -> Rename template placeholder in every headfile, too. 
    498             designs = Design.objects.all().filter(colorscheme=self.colorscheme) 
    499             for design in designs: 
    500                 headfiles = design.headfiles.all() 
    501                 for headfile in headfiles: 
    502                     if headfile.render != True: # File used no color placeholder 
    503                         continue 
    504  
    505                     old_content = headfile.content 
    506                     # FIXME: Use flexibler regexp. for this: 
    507                     new_content = old_content.replace("{{ %s }}" % old_name, "{{ %s }}" % new_name) 
    508                     if old_content == new_content: 
    509                         # content not changed?!? 
    510                         user_message_or_warn( 
    511                             "Color '{{ %s }}' not exist in headfile %r" % (old_name, headfile) 
    512                         ) 
    513                         continue 
    514                      
    515                     if settings.DEBUG: 
    516                         user_message_or_warn( 
    517                             "change color name from '%s' to '%s' in %r" % (old_name, new_name, headfile) 
    518                         ) 
    519                     headfile.content = new_content 
    520                     headfile.save() 
     495        try: 
     496            old_name = Color.objects.get(id=self.id).name 
     497        except Color.DoesNotExist: 
     498            # New color 
     499            pass 
     500        else: 
     501            if new_name != old_name: 
     502                # Color name has been changed -> Rename template placeholder in every headfile, too. 
     503                designs = Design.objects.all().filter(colorscheme=self.colorscheme) 
     504                for design in designs: 
     505                    headfiles = design.headfiles.all() 
     506                    for headfile in headfiles: 
     507                        if headfile.render != True: # File used no color placeholder 
     508                            continue 
     509 
     510                        old_content = headfile.content 
     511                        # FIXME: Use flexibler regexp. for this: 
     512                        new_content = old_content.replace("{{ %s }}" % old_name, "{{ %s }}" % new_name) 
     513                        if old_content == new_content: 
     514                            # content not changed?!? 
     515                            user_message_or_warn( 
     516                                "Color '{{ %s }}' not exist in headfile %r" % (old_name, headfile) 
     517                            ) 
     518                            continue 
     519 
     520                        if settings.DEBUG: 
     521                            user_message_or_warn( 
     522                                "change color name from '%s' to '%s' in %r" % (old_name, new_name, headfile) 
     523                            ) 
     524                        headfile.content = new_content 
     525                        headfile.save() 
    521526 
    522527        return super(Color, self).save(*args, **kwargs) 
    523      
     528 
    524529    def __unicode__(self): 
    525530        return u"Color '%s' #%s (%s)" % (self.name, self.value, self.colorscheme) 
    526      
    527     class Meta: 
    528         unique_together=(("colorscheme", "name"),) 
     531 
     532    class Meta: 
     533        unique_together = (("colorscheme", "name"),) 
    529534        ordering = ("colorscheme", "name") 
    530535 
     
    537542    """ 
    538543    Page design: template + CSS/JS files 
    539      
     544 
    540545    inherited attributes from AutoSiteM2M: 
    541546        site    -> ManyToManyField to Site 
    542547        on_site -> sites.managers.CurrentSiteManager instance 
    543      
     548 
    544549    inherited attributes from UpdateInfoBaseModel: 
    545550        createtime     -> datetime of creation 
     
    549554    """ 
    550555    objects = DesignManager() 
    551      
     556 
    552557    name = models.CharField(unique=True, max_length=150, help_text="Name of this design combination",) 
    553558    template = models.CharField(max_length=128, help_text="filename of the used template for this page") 
     
    566571        sites = [site.name for site in self.site.all()] 
    567572        return u"Page design '%s' (on sites: %r)" % (self.name, sites) 
    568      
     573 
    569574    class Meta: 
    570575        ordering = ("template",) 
     
    580585        db_instance = self.get(filename=filename) 
    581586        return headfile.HeadfileLink(filename=db_instance.filename)#, content=db_instance.content) 
    582          
     587 
    583588 
    584589class EditableHtmlHeadFile(AutoSiteM2M, UpdateInfoBaseModel): 
    585590    """ 
    586591    Storage for editable static text files, e.g.: stylesheet / javascript. 
    587      
     592 
    588593    inherited attributes from AutoSiteM2M: 
    589594        site    -> ManyToManyField to Site 
    590595        on_site -> sites.managers.CurrentSiteManager instance 
    591      
     596 
    592597    inherited attributes from UpdateInfoBaseModel: 
    593598        createtime     -> datetime of creation 
     
    597602    """ 
    598603    objects = EditableHtmlHeadFileManager() 
    599      
     604 
    600605    filepath = models.CharField(max_length=256) 
    601606    mimetype = models.CharField(max_length=64) 
     
    604609        help_text='Additional html tag attributes (CSS example: media="screen")' 
    605610    ) 
    606     render = models.BooleanField(default = False, 
     611    render = models.BooleanField(default=False, 
    607612        help_text="Are there CSS ColorScheme entries in the content?" 
    608613    ) 
    609614    description = models.TextField(null=True, blank=True) 
    610615    content = models.TextField() 
    611      
     616 
    612617    def get_color_filepath(self, colorscheme): 
    613618        """ Colorscheme + filepath """ 
    614619        assert isinstance(colorscheme, ColorScheme) 
    615620        return os.path.join("ColorScheme_%s" % colorscheme.pk, self.filepath) 
    616          
     621 
    617622    def get_path(self, colorscheme): 
    618623        """ Path for filesystem cache path and link url. """ 
     
    621626            self.get_color_filepath(colorscheme) 
    622627        ) 
    623      
     628 
    624629    def get_cachepath(self, colorscheme): 
    625630        """ 
     
    628633        """ 
    629634        return os.path.join(settings.MEDIA_ROOT, self.get_path(colorscheme)) 
    630          
     635 
    631636    def get_rendered(self, colorscheme): 
    632637        color_dict = Color.objects.get_color_dict(colorscheme) 
    633         return render.render_string_template(self.content, color_dict)         
    634          
     638        return render.render_string_template(self.content, color_dict) 
     639 
    635640    def save_cache_file(self, colorscheme): 
    636641        """ 
     
    639644        """ 
    640645        cachepath = self.get_cachepath(colorscheme) 
    641          
     646 
    642647        def _save_cache_file(auto_create_dir=True): 
    643648            rendered_content = self.get_rendered(colorscheme) 
     
    648653            except IOError, err: 
    649654                if auto_create_dir and err.errno == errno.ENOENT: # No 2: No such file or directory 
    650                     # Try to create the out dir and save the cache file   
     655                    # Try to create the out dir and save the cache file 
    651656                    path = os.path.dirname(cachepath) 
    652657                    if not os.path.isdir(path): 
     
    722727 
    723728 
    724      
     729 
    725730class UserProfile(AutoSiteM2M, UpdateInfoBaseModel): 
    726731    """ 
    727732    Stores additional information about PyLucid users 
    728733    http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users 
    729      
     734 
    730735    Created via post_save signal, if a new user created. 
    731      
     736 
    732737    inherited attributes from AutoSiteM2M: 
    733738        site    -> ManyToManyField to Site 
     
    735740    """ 
    736741    user = models.ForeignKey(User, unique=True, related_name="%(class)s_user") 
    737      
     742 
    738743    sha_login_checksum = models.CharField(max_length=192, 
    739744        help_text="Checksum for PyLucid JS-SHA-Login" 
     
    742747        help_text="Salt value for PyLucid JS-SHA-Login" 
    743748    ) 
    744      
     749 
    745750    # TODO: Overwrite help_text: 
    746751#    site = models.ManyToManyField(Site, 
    747752#        help_text="User can access only these sites." 
    748753#    ) 
    749      
     754 
    750755    def set_sha_login_password(self, raw_password): 
    751756        """ 
     
    766771        sites = self.site.all() 
    767772        return ", ".join([site.name for site in sites]) 
    768      
     773 
    769774    class Meta: 
    770775        ordering = ("user",) 
     
    775780def cache_headfiles(sender, **kwargs): 
    776781    """ 
    777     One colorscheme was changes: resave all cache headfiles with new color values.  
     782    One colorscheme was changes: resave all cache headfiles with new color values. 
    778783    """ 
    779784    colorscheme = kwargs["instance"] 
     
    784789        for headfile in headfiles: 
    785790            headfile.save_cache_file(colorscheme) 
    786      
     791 
    787792signals.post_save.connect(cache_headfiles, sender=ColorScheme) 
    788793 
     
    793798    """ signal handler: creating user profile, after a new user created. """ 
    794799    user = kwargs["instance"] 
    795          
     800 
    796801    userprofile, created = UserProfile.objects.get_or_create(user=user) 
    797802    if created: 
    798803        user_message_or_warn("UserProfile entry for user '%s' created." % user) 
    799 #             
     804# 
    800805#        if not user.is_superuser: # Info: superuser can automaticly access all sites 
    801806#            site = Site.objects.get_current() 
    802807#            userprofile.site.add(site) 
    803 #            user_message_or_warn("Add site '%s' to '%s' UserProfile." % (site.name, user))             
     808#            user_message_or_warn("Add site '%s' to '%s' UserProfile." % (site.name, user)) 
    804809 
    805810signals.post_save.connect(create_user_profile, sender=User) 
     
    826831    # Use the original method to set the django User password: 
    827832    orig_set_password(user, raw_password) 
    828      
     833 
    829834    userprofile, created = UserProfile.objects.get_or_create(user=user) 
    830835    if created: 
     
    836841 
    837842 
    838 # replace the method  
     843# replace the method 
    839844User.set_password = set_password