headfile.py :  » Content-Management-Systems » PyLucid » PyLucid_standalone » pylucid_project » apps » pylucid » system » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Content Management Systems » PyLucid 
PyLucid » PyLucid_standalone » pylucid_project » apps » pylucid » system » headfile.py
# coding: utf-8

"""
    PyLucid headfile
    ~~~~~~~~~~~~~~~~

    Stores the information about CSS/JS links or content for html head.

    Used in pylucid.models and pylucid_plugins.head_files.context_middleware 

    Last commit info:
    ~~~~~~~~~~~~~~~~~
    $LastChangedDate: $
    $Rev: $
    $Author: $

    :copyleft: 2009 by the PyLucid team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""

import os
import mimetypes
from xml.sax.saxutils import escape

from django.conf import settings
from django.utils import safestring
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.template.loader import render_to_string


TYPE_CSS = "css"
TYPE_JS = "js"
KNOWN_TYPES = (TYPE_CSS, TYPE_JS)

DEFAULT_INLINE_ATTRIBUTES = {
    TYPE_CSS:{"type":"text/css"},
    TYPE_JS: {"type":"text/javascript"},
}
DEFAULT_LINK_ATTRIBUTES = {
    TYPE_CSS:{"type":"text/css", "rel":"stylesheet"},
    TYPE_JS: {"type":"text/javascript"},
}


class HeadfileBase(object):
    def check_type(self):
        if self.data_type not in KNOWN_TYPES:
            raise RuntimeError("Data type %r unknown!" % self.data_type)

    def get_head_tag(self):
        return render_to_string(self.template_name, self.context)



class HeadfileInline(HeadfileBase):
    """ CSS/JS content in the html head """
    def __init__(self, data_type, content, tag_attrs={}):
        self.data_type = data_type
        self.check_type()
        
        self.content = content
        
        self.tag_attrs = DEFAULT_INLINE_ATTRIBUTES[data_type]
        self.tag_attrs.update(tag_attrs)
        
        self.template_name = settings.PYLUCID.HEADFILE_INLINE_TEMPLATES % self.data_type
        self.context = {"tag_attrs": self.tag_attrs, "content": self.content,}


class HeadfileLink(HeadfileBase):
    """
    Links to CSS/JS files in html head
    TODO: Should check if the file was saved into media path
    """
    def __init__(self, url, tag_attrs={}):
        self.url = url
        if "?" in url:
            path = url.split("?",1)[0]
        else:
            path = url
        self.data_type = os.path.splitext(path)[1].lstrip(".")
        self.check_type()

        self.tag_attrs = self.make_tag_attrs(tag_attrs)
        
        self.template_name = settings.PYLUCID.HEADFILE_LINK_TEMPLATES % self.data_type      
        self.context = {"url": self.url, "tag_attrs": self.tag_attrs}
        
    def make_tag_attrs(self, tag_attrs):
        attributes = DEFAULT_LINK_ATTRIBUTES[self.data_type]
        
        if settings.DEBUG:
            attributes["onerror"] = safestring.mark_safe(
                "JavaScript:alert('Error loading file [%s] !');" % escape(self.url)
            )
        
        attributes.update(tag_attrs)
        return attributes

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.