~divmod-dev/divmod.org/dangling-1091

« back to all changes in this revision

Viewing changes to Mantissa/xmantissa/webapp.py

  • Committer: glyph
  • Date: 2005-07-28 22:09:16 UTC
  • Revision ID: svn-v4:866e43f7-fbfc-0310-8f2a-ec88d1da2979:trunk:2
move this repository to a more official-looking URL

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
"""
 
3
 
 
4
This module is the basis for Mantissa-based web applications.  It provides
 
5
several basic pluggable application-level features, most notably Powerup-based
 
6
integration of the extensible hierarchical navigation system defined in
 
7
xmantissa.webnav
 
8
 
 
9
"""
 
10
 
 
11
 
 
12
from xmantissa.webtheme import getAllThemes
 
13
from xmantissa.webnav import getTabs
 
14
 
 
15
def _reorderForPreference(themeList, preferredThemeName):
 
16
    """
 
17
    Re-order the input themeList according to the preferred theme.
 
18
 
 
19
    Returns None.
 
20
    """
 
21
    for t in themeList:
 
22
        if preferredThemeName == t.name:
 
23
            themeList.remove(t)
 
24
            themeList.insert(0,t)
 
25
            return
 
26
 
 
27
 
 
28
class NavFragment(Fragment):
 
29
    def __init__(self, docFactory, navigation):
 
30
        Fragment.__init__(self, docFactory)
 
31
        self.navigation = navigation
 
32
 
 
33
    def data_navigation(self, ctx):
 
34
        return self.navigation
 
35
 
 
36
    def render_tab(self, ctx, data):
 
37
        ctx.fillSlots('name', self.stuff.name)
 
38
        ctx.fillSlots('link', self.linkTarget)
 
39
        return ctx.tag
 
40
 
 
41
    def render_subtabs(self, ctx, data):
 
42
        return NavFragment(docFactory=self.docFactory, navigation=data.subtabs)
 
43
 
 
44
 
 
45
class NavMixin(object):
 
46
 
 
47
    fragmentName = 'main'
 
48
 
 
49
    def __init__(self, siteconf, navigation):
 
50
        self.siteconf = siteconf
 
51
        self.navigation = navigation
 
52
 
 
53
    def getDocFactory(self):
 
54
        return self.siteconf.getDocFactory(self.fragmentName)
 
55
 
 
56
    def render_content(self, ctx, data):
 
57
        raise NotImplementedError("implement render_context in subclasses")
 
58
 
 
59
    def render_navigation(self, ctx, data):
 
60
        return NavFragment(self.getDocFactory('navigation'), self.navigation)
 
61
 
 
62
 
 
63
class PrivatePage(Page, NavMixin):
 
64
    def __init__(self, siteConf, navigation):
 
65
        docFactory = siteConf.getDocFactory('shell')
 
66
        Page.__init__(self)
 
67
        NavMixin.__init__(self, siteConf, navigation)
 
68
 
 
69
 
 
70
class PrivateApplication(Item):
 
71
    # Installed on Avatar stores.
 
72
    implements(ISiteRootPlugin)
 
73
 
 
74
    typeName = 'private_web_application'
 
75
    schemaVersion = 1
 
76
 
 
77
    preferredTheme = text()
 
78
    hitCount = integer()
 
79
 
 
80
    def install(self):
 
81
        self.store.powerUp(self, ISiteRootPlugin)
 
82
 
 
83
    def resourceFactory(self, segments):
 
84
        self.hitCount += 1
 
85
        if segments and segments[0] == 'private':
 
86
            nav = getTabs(self.store.powerupsFor(INavigableElement))
 
87
            return PrivatePage(self, nav), segments[1:]
 
88
 
 
89
    def getDocFactory(self, fragmentName):
 
90
        l = list(getAllThemes())
 
91
        _reorderForPreference(l, self.preferredTheme)
 
92
        for t in l:
 
93
            fact = t.getDocFactory(fragmentName)
 
94
            if fact is not None:
 
95
                return fact
 
96
        raise KeyError("No such theme element: %r in themes: %r" %
 
97
                       (fragmentName, l))