Changeset 2052

Show
Ignore:
Timestamp:
06/19/09 15:57:40 (9 months ago)
Author:
JensDiemer
Message:

TODO: Use CSS color scheme ;)

Location:
branches/0.9/pylucid_project
Files:
2 added
9 modified

Legend:

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

    r2040 r2052  
    103103admin.site.register(models.PluginPage, PluginPageAdmin) 
    104104 
     105#----------------------------------------------------------------------------- 
     106 
     107class ColorInline(admin.TabularInline): 
     108    model = models.Color 
     109 
     110class ColorSchemeAdmin(VersionAdmin):     
     111    list_display = ("name",) 
     112    list_display_links = ("name",) 
     113    search_fields = ("name",) 
     114    inlines = [ 
     115        ColorInline, 
     116    ] 
     117     
     118admin.site.register(models.ColorScheme, ColorSchemeAdmin) 
     119 
     120 
    105121 
    106122class DesignAdmin(VersionAdmin):     
    107     list_display = ("name", "template", "lastupdatetime", "lastupdateby") 
     123    list_display = ("name", "template", "colorscheme", "lastupdatetime", "lastupdateby") 
    108124    list_display_links = ("name",) 
    109     list_filter = ("site", "template", "createby", "lastupdateby") 
    110     search_fields = ("name", "template") 
     125    list_filter = ("site", "template", "colorscheme", "createby", "lastupdateby") 
     126    search_fields = ("name", "template", "colorscheme") 
    111127 
    112128admin.site.register(models.Design, DesignAdmin) 
     
    120136admin.site.register(models.EditableHtmlHeadFile, EditableHtmlHeadFileAdmin) 
    121137 
     138#----------------------------------------------------------------------------- 
    122139 
    123140class UserProfileAdmin(VersionAdmin): 
  • branches/0.9/pylucid_project/apps/pylucid/models.py

    r2048 r2052  
    2929from django.db import models 
    3030from django.conf import settings 
     31from django.core import exceptions 
    3132from django.db.models import signals 
    3233from django.core.urlresolvers import reverse 
     
    4142from pylucid_project.utils import crypt 
    4243 
    43 from pylucid.system.auto_model_info import UpdateInfoBaseModel 
     44from pylucid.system.auto_model_info import UpdateInfoBaseModel,AutoSiteM2M 
     45from pylucid.shortcuts import user_message_or_warn 
     46from pylucid.fields import ColorField 
    4447from pylucid.system import headfile 
    45 from pylucid.shortcuts import user_message_or_warn 
    4648 
    4749 
     
    433435        ordering = ("page", "lang") 
    434436 
     437#------------------------------------------------------------------------------ 
     438 
     439class ColorScheme(AutoSiteM2M, UpdateInfoBaseModel): 
     440    """ 
     441    inherited attributes from AutoSiteM2M: 
     442        site    -> ManyToManyField to Site 
     443        on_site -> sites.managers.CurrentSiteManager instance 
     444    """ 
     445    name = models.CharField(max_length=255, help_text="The name of this color scheme.") 
     446     
     447    def update(self, colors): 
     448        assert isinstance(colors, dict) 
     449        new = [] 
     450        updated = [] 
     451        exists = [] 
     452        for name, value in colors.iteritems(): 
     453            color, created = Color.objects.get_or_create( 
     454                colorscheme=self, name=name, 
     455                defaults={"colorscheme":self, "name":name, "value":value} 
     456            ) 
     457            if created: 
     458                new.append(color) 
     459            elif color.value != value: 
     460                color.value = value 
     461                updated.append(color) 
     462                color.save() 
     463            else: 
     464                exists.append(color) 
     465        return new, updated, exists 
     466 
     467    def __unicode__(self): 
     468        sites = [site.name for site in self.site.all()] 
     469        return u"ColorScheme '%s' (on sites: %r)" % (self.name, sites) 
     470 
     471 
     472class Color(AutoSiteM2M, UpdateInfoBaseModel): 
     473    """ 
     474    inherited attributes from AutoSiteM2M: 
     475        site    -> ManyToManyField to Site 
     476        on_site -> sites.managers.CurrentSiteManager instance 
     477    """ 
     478    colorscheme = models.ForeignKey(ColorScheme) 
     479    name = models.CharField(max_length=128, 
     480        help_text="Name if this color (e.g. main_color, head_background)" 
     481    ) 
     482    value = ColorField(help_text="CSS hex color value.") 
     483     
     484    def __unicode__(self): 
     485        sites = [site.name for site in self.site.all()] 
     486        return u"Color '%s' (%s, on sites: %r)" % (self.name, self.colorscheme, sites) 
    435487 
    436488#------------------------------------------------------------------------------ 
     
    439491    pass 
    440492 
    441 class Design(UpdateInfoBaseModel): 
     493class Design(AutoSiteM2M, UpdateInfoBaseModel): 
    442494    """ 
    443495    Page design: template + CSS/JS files 
     496     
     497    inherited attributes from AutoSiteM2M: 
     498        site    -> ManyToManyField to Site 
     499        on_site -> sites.managers.CurrentSiteManager instance 
    444500     
    445501    inherited attributes from UpdateInfoBaseModel: 
     
    451507    objects = DesignManager() 
    452508     
    453     site = models.ManyToManyField(Site, default=[settings.SITE_ID]) 
    454     on_site = CurrentSiteManager() 
    455      
    456509    name = models.CharField(unique=True, max_length=150, help_text="Name of this design combination",) 
    457510    template = models.CharField(max_length=128, help_text="filename of the used template for this page") 
     
    459512        help_text="Static files (stylesheet/javascript) for this page, included in html head via link tag" 
    460513    ) 
     514    colorscheme = models.ForeignKey(ColorScheme, null=True, blank=True) 
    461515 
    462516    def save(self, *args, **kwargs): 
     
    467521 
    468522    def __unicode__(self): 
    469         return u"Page design '%s'" % self.name 
     523        sites = [site.name for site in self.site.all()] 
     524        return u"Page design '%s' (on sites: %r)" % (self.name, sites) 
    470525     
    471526    class Meta: 
     
    484539         
    485540 
    486 class EditableHtmlHeadFile(UpdateInfoBaseModel): 
     541class EditableHtmlHeadFile(AutoSiteM2M, UpdateInfoBaseModel): 
    487542    """ 
    488543    Storage for editable static text files, e.g.: stylesheet / javascript. 
     544     
     545    inherited attributes from AutoSiteM2M: 
     546        site    -> ManyToManyField to Site 
     547        on_site -> sites.managers.CurrentSiteManager instance 
    489548     
    490549    inherited attributes from UpdateInfoBaseModel: 
     
    496555    objects = EditableHtmlHeadFileManager() 
    497556     
    498     site = models.ManyToManyField(Site, default=[settings.SITE_ID]) 
    499     on_site = CurrentSiteManager() 
    500      
    501557    filepath = models.CharField(max_length=256) 
    502558    mimetype = models.CharField(max_length=64) 
    503559    html_attributes = models.CharField(max_length=256, null=False, blank=True, 
     560        # TODO: Use this! 
    504561        help_text='Additional html tag attributes (CSS example: media="screen")' 
     562    ) 
     563    render = models.BooleanField(default = False, 
     564        help_text="Are there CSS ColorScheme entries in the content?" 
    505565    ) 
    506566    description = models.TextField(null=True, blank=True) 
     
    588648 
    589649    def __unicode__(self): 
    590         return u"EditableHtmlHeadFile '%s'" % self.filepath 
     650        sites = [site.name for site in self.site.all()] 
     651        return u"EditableHtmlHeadFile '%s' (on sites: %r)" % (self.filepath, sites) 
    591652 
    592653    class Meta: 
     
    597658 
    598659     
    599 class UserProfile(UpdateInfoBaseModel): 
     660class UserProfile(AutoSiteM2M, UpdateInfoBaseModel): 
    600661    """ 
    601662    Stores additional information about PyLucid users 
     
    603664     
    604665    Created via post_save signal, if a new user created. 
     666     
     667    inherited attributes from AutoSiteM2M: 
     668        site    -> ManyToManyField to Site 
     669        on_site -> sites.managers.CurrentSiteManager instance 
    605670    """ 
    606671    user = models.ForeignKey(User, unique=True, related_name="%(class)s_user") 
     
    613678    ) 
    614679     
    615     site = models.ManyToManyField(Site, 
    616         help_text="User can access only these sites." 
    617     ) 
     680    # TODO: Overwrite help_text: 
     681#    site = models.ManyToManyField(Site, 
     682#        help_text="User can access only these sites." 
     683#    ) 
    618684     
    619685    def set_sha_login_password(self, raw_password): 
     
    650716    if created: 
    651717        user_message_or_warn("UserProfile entry for user '%s' created." % user) 
    652              
    653         if not user.is_superuser: # Info: superuser can automaticly access all sites 
    654             site = Site.objects.get_current() 
    655             userprofile.site.add(site) 
    656             user_message_or_warn("Add site '%s' to '%s' UserProfile." % (site.name, user))             
     718#             
     719#        if not user.is_superuser: # Info: superuser can automaticly access all sites 
     720#            site = Site.objects.get_current() 
     721#            userprofile.site.add(site) 
     722#            user_message_or_warn("Add site '%s' to '%s' UserProfile." % (site.name, user))             
    657723 
    658724signals.post_save.connect(create_user_profile, sender=User) 
  • branches/0.9/pylucid_project/apps/pylucid/system/auto_model_info.py

    r2040 r2052  
    2222 
    2323from django.db import models 
     24from django.conf import settings 
    2425from django.contrib import admin 
    2526from django.http import HttpRequest 
    2627from django.contrib.auth.models import User 
     28from django.contrib.sites.models import Site 
    2729from django.db import transaction, IntegrityError 
     30from pylucid.shortcuts import user_message_or_warn 
     31from django.contrib.sites.managers import CurrentSiteManager 
    2832 
    2933from django_tools.middlewares import ThreadLocal 
     34 
     35 
     36class AutoSiteM2M(models.Model): 
     37    """ 
     38    Add site and on_site to model, and add at least the current site in save method. 
     39    """ 
     40    site = models.ManyToManyField(Site) 
     41    on_site = CurrentSiteManager() 
     42 
     43    def save(self, *args, **kwargs): 
     44        """ Automatic current site, if not exist. """ 
     45        if self.pk==None: 
     46            # instance needs to have a primary key value before a many-to-many relationship can be used. 
     47            super(AutoSiteM2M, self).save(*args, **kwargs) 
     48         
     49        if self.site.count()==0: 
     50            site = Site.objects.get_current() 
     51            if settings.DEBUG: 
     52                user_message_or_warn("Automatic add site '%s' to %r" % (site.name, self))              
     53            self.site.add(site) 
     54             
     55        super(AutoSiteM2M, self).save(*args, **kwargs) 
     56 
     57    class Meta: 
     58        abstract = True 
    3059 
    3160 
     
    3564    The createby and lastupdateby ForeignKey would be automaticly updated. This needs the  
    3665    request object as the first argument in the save method. 
    37      
    38     Important: Every own objects manager should be inherit from UpdateInfoBaseModelManager! 
    39     """     
     66    """ 
     67    objects = models.Manager() 
     68             
    4069    createtime = models.DateTimeField(auto_now_add=True, help_text="Create time",) 
    4170    lastupdatetime = models.DateTimeField(auto_now=True, help_text="Time of the last change.",) 
  • branches/0.9/pylucid_project/apps/pylucid_update/forms.py

    r1875 r2052  
    22 
    33from django import forms 
    4 from django.contrib.sites.models import Site 
    54from django.utils.translation import ugettext as _ 
    65 
     
    87 
    98class UpdateForm(forms.Form): 
    10     site = forms.ModelChoiceField( 
    11         queryset=Site.objects.all(), 
    12         help_text=_("Select the site for the existing pages."), 
    13     ) 
    149    language = forms.ModelChoiceField( 
    1510        queryset=Language.objects.all(), 
  • branches/0.9/pylucid_project/apps/pylucid_update/templates/pylucid_update/menu.html

    r1931 r2052  
    44{% block content %} 
    55<!-- view_content block --> 
     6<p>Update on site: <strong>{{ site }}</strong></p> 
    67Please select: 
    78<ol> 
  • branches/0.9/pylucid_project/apps/pylucid_update/templates/pylucid_update/update08.html

    r1931 r2052  
    66<fieldset><legend>update</legend> 
    77<p>TODO: Insert info text here ;)</p> 
     8<p>Update on site: <strong>{{ site }}</strong></p> 
    89Please select: 
    910<form method="post" action="{{ url }}"> 
  • branches/0.9/pylucid_project/apps/pylucid_update/urls.py

    r2045 r2052  
    22 
    33""" 
    4     Generate dynamicly urls based on the database tree 
     4    urls for update section 
    55""" 
    66 
  • branches/0.9/pylucid_project/apps/pylucid_update/views.py

    r2050 r2052  
    1515""" 
    1616 
     17import re 
    1718import posixpath 
    1819 
     
    2021from django.template import RequestContext 
    2122from django.core.urlresolvers import reverse 
     23from django.contrib.sites.models import Site 
    2224from django.shortcuts import render_to_response 
    2325from django.template.loader import find_template_source 
     
    2628 
    2729from pylucid_project.utils.SimpleStringIO import SimpleStringIO 
    28 from pylucid_project.apps.pylucid.models import PageTree, PageMeta, PageContent, Design, \ 
    29                                                             EditableHtmlHeadFile, UserProfile 
     30from pylucid.models import PageTree, PageMeta, PageContent, ColorScheme, Design, EditableHtmlHeadFile, \ 
     31                                                                                            UserProfile 
    3032from pylucid_project.apps.pylucid_update.models import Page08, Template08, Style08, JS_LoginData08 
     33from pylucid.system.css_color_utils import filter_content, extract_colors 
    3134from pylucid_project.apps.pylucid_update.forms import UpdateForm 
    32  
     35from pylucid.fields import CSS_VALUE_RE 
    3336 
    3437 
     
    3740    context = { 
    3841        "title": "menu", 
     42        "site": Site.objects.get_current() 
    3943    } 
    4044    return render_to_response('pylucid_update/menu.html', context, context_instance=RequestContext(request)) 
    4145 
    4246 
    43 def _do_update(request, site, language): 
     47def _do_update(request, language): 
    4448    out = SimpleStringIO() 
     49    site = Site.objects.get_current() 
    4550    out.write("Starting update and move v0.8 data into v0.9 (on site: %s)" % site) 
    4651 
     
    5358         
    5459        userprofile, created = UserProfile.objects.get_or_create(user = user) 
    55         userprofile.site.add(site)            
     60        #userprofile.site.add(site)            
    5661        if created: 
    5762            out.write("UserProfile for user '%s' created." % user.username) 
     
    100105            } 
    101106        ) 
    102         new_staticfile.site.add(site) 
    103107        cssfiles[style.name] = new_staticfile 
    104108        if created: 
     
    123127        design_key = "%s %s" % (old_page.template.name, old_page.style.name) 
    124128        if design_key not in designs: 
     129            style_name = old_page.style.name 
    125130            # The template/style combination was not created, yet. 
    126             if old_page.template.name == old_page.style.name: 
     131            if old_page.template.name == style_name: 
    127132                new_design_name = old_page.template.name 
    128133            else: 
    129                 new_design_name = "%s + %s" % (old_page.template.name, old_page.style.name) 
     134                new_design_name = "%s + %s" % (old_page.template.name, style_name) 
    130135                 
    131136            design, created = Design.objects.get_or_create( 
     
    135140                } 
    136141            ) 
    137             design.site.add(site) 
    138142            if created: 
    139                 # Add old page css file 
    140                 design.headfiles.add(cssfiles[old_page.style.name]) 
    141143                out.write("New design created: %s" % new_design_name) 
    142              
     144            else: 
     145                out.write("Use existing Design: %r" % design) 
     146 
     147            # Add old page css file 
     148            css_file = cssfiles[style_name] # EditableHtmlHeadFile instance 
     149            assert isinstance(css_file, EditableHtmlHeadFile) 
     150            design.headfiles.add(css_file) 
     151                 
     152            colorscheme, created = ColorScheme.objects.get_or_create(name=style_name) 
     153            if created: 
     154                out.write("Use new color scheme: %s" % colorscheme.name) 
     155                out.write("Colors can be extracted later.") 
     156            else: 
     157                out.write("Use color scheme: %s" % colorscheme.name) 
     158                 
     159            design.colorscheme = colorscheme 
     160            design.save() 
    143161            designs[design_key] = design 
    144162        else: 
    145163            design = designs[design_key] 
     164            out.write("Use existing Design: %r" % design) 
    146165             
    147166        #--------------------------------------------------------------------- 
     
    240259        form = UpdateForm(request.POST) 
    241260        if form.is_valid(): 
    242             site = form.cleaned_data["site"] 
    243261            language = form.cleaned_data["language"] 
    244  
    245             return _do_update(request, site, language) 
     262            return _do_update(request, language) 
    246263    else: 
    247264        form = UpdateForm() 
     
    250267        "title": "update data from PyLucid v0.8 to v0.9", 
    251268        "url": reverse("PyLucidUpdate-update08"), 
     269        "site": Site.objects.get_current(), 
    252270        "form": form, 
    253271    } 
     
    355373 
    356374 
     375 
    357376def update08styles(request): 
    358377    """ 
     
    363382    out = SimpleStringIO() 
    364383     
    365     def replace(content, out, old, new): 
    366         out.write("replace %r with %r" % (old, new)) 
    367         if not old in content: 
    368             out.write("String not found. Updated already?") 
    369         else: 
    370             content = content.replace(old, new) 
    371         return content 
    372      
    373     # Get the file content via django template loader: 
    374     additional_styles, origin = find_template_source("pylucid_update/additional_styles.css") 
    375          
    376     styles = EditableHtmlHeadFile.objects.filter(filepath__istartswith=settings.SITE_TEMPLATE_PREFIX) 
    377     styles = styles.filter(filepath__iendswith=".css") 
    378     for style in styles: 
     384    def update_headfile_colorscheme(design, headfile): 
     385        out.write("\nExtract colors from: '%s'" % headfile.filepath) 
     386 
     387        colorscheme = design.colorscheme 
     388        if colorscheme == None: 
     389            # This design has no color scheme, yet -> create one 
     390            colorscheme=ColorScheme(name=headfile.filepath) 
     391#            colorscheme.site.add(site = Site.objects.get_current()) # FIXME ?!? 
     392            colorscheme.save() 
     393            out.write("Add color scheme %r to %r" % (colorscheme.name, design.name)) 
     394            design.colorscheme = colorscheme 
     395            design.save() 
     396         
     397        out.write("Use color scheme %r" % colorscheme.name) 
     398         
     399        content = headfile.content 
     400        new_content, color_dict = extract_colors(content) 
     401        out.write(repr(new_content)) 
     402        out.write(repr(color_dict)) 
     403 
     404        created, updated, exists = colorscheme.update(color_dict) 
     405        out.write("created colors: %r" % created) 
     406        out.write("updated colors: %r" % updated) 
     407        out.write("exists colors: %r" % exists) 
     408         
     409        colorscheme.save() 
     410         
     411        headfile.content = new_content 
     412        headfile.save() 
     413         
     414         
     415    def update_all_design_colorscheme(design): 
     416        headfiles = design.headfiles.all() 
     417        out.write("\nExisting headfiles: %r" % headfiles) 
     418         
     419        for headfile in headfiles: 
     420            if not headfile.filepath.lower().endswith(".css"): 
     421                out.write("Skip headfile: %r" % headfile) 
     422            else: 
     423                update_headfile_colorscheme(design, headfile) 
     424     
     425    for design in Design.objects.all(): 
    379426        out.write("\n______________________________________________________________") 
    380         out.write("\nUpdate Style: '%s'" % style.filepath) 
    381          
    382         content = style.content 
    383         if additional_styles in content: 
    384             out.write("additional styles allready inserted.") 
    385         else: 
    386             content = additional_styles + content 
    387             style.content = content 
    388             style.save() 
    389             out.write("additional styles inserted.")         
     427        out.write("\nUpdate color scheme for design: '%s'" % design.name) 
     428                 
     429        update_all_design_colorscheme(design) 
     430     
     431     
     432#    styles = EditableHtmlHeadFile.objects.filter(filepath__istartswith=settings.SITE_TEMPLATE_PREFIX) 
     433#    styles = styles.filter(filepath__iendswith=".css") 
     434#    for style in styles: 
     435#        out.write("\n______________________________________________________________") 
     436#        out.write("\nUpdate Style: '%s'" % style.filepath) 
     437#         
     438#        content = style.content 
     439#        filtered_content = filter_content(content) # Skip all pygments styles 
     440#        out.write(filtered_content) 
     441#         
     442#        new_content, color_dict = extract_colors(content) 
     443#        out.write(new_content) 
     444#        out.write(repr(color_dict)) 
     445     
     446#    def replace(content, out, old, new): 
     447#        out.write("replace %r with %r" % (old, new)) 
     448#        if not old in content: 
     449#            out.write("String not found. Updated already?") 
     450#        else: 
     451#            content = content.replace(old, new) 
     452#        return content 
     453#     
     454#    # Get the file content via django template loader: 
     455#    additional_styles, origin = find_template_source("pylucid_update/additional_styles.css") 
     456#         
     457#    styles = EditableHtmlHeadFile.objects.filter(filepath__istartswith=settings.SITE_TEMPLATE_PREFIX) 
     458#    styles = styles.filter(filepath__iendswith=".css") 
     459#    for style in styles: 
     460#        out.write("\n______________________________________________________________") 
     461#        out.write("\nUpdate Style: '%s'" % style.filepath) 
     462#         
     463#        content = style.content 
     464#        if additional_styles in content: 
     465#            out.write("additional styles allready inserted.") 
     466#        else: 
     467#            content = additional_styles + content 
     468#            style.content = content 
     469#            style.save() 
     470#            out.write("additional styles inserted.")         
    390471     
    391472    context = { 
  • branches/0.9/pylucid_project/pylucid_plugins/page_admin/templates/page_admin/edit_page_form.html

    r2037 r2052  
    99    $("#preview_fieldset").hide(); 
    1010     
     11    // from media/PyLucid/pylucid_js_tools.js 
    1112    pylucid_ajax_form_view('#edit_page_form'); 
    1213