File: FormulatorFormFile.py

package info (click to toggle)
zope-formulator 1.7.0-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 736 kB
  • ctags: 1,025
  • sloc: python: 4,617; sh: 50; makefile: 44
file content (134 lines) | stat: -rw-r--r-- 4,645 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Filesystem Formulator Form module

Zope object encapsulating a Formulator Form from the filesystem,
after PageTemplateFile example.
"""

import os
from AccessControl import ClassSecurityInfo
from Globals import package_home, DevelopmentMode, InitializeClass
from AccessControl import getSecurityManager
from Products.Formulator.Form import ZMIForm
from OFS.SimpleItem import Item_w__name__

class FormulatorFormFile(Item_w__name__, ZMIForm):
    meta_type = 'Formulator Form (File)'

    _v_last_read = 0

    security = ClassSecurityInfo()

    def __init__(self, filename, _prefix=None, **kw):
        if _prefix is None:
            # in Zope 2.7, could use getConfiguration()
            _prefix = SOFTWARE_HOME
        elif type(_prefix) is not type(''):
            _prefix = package_home(_prefix)
        name = kw.get('__name__')
        basepath, ext = os.path.splitext(filename)
        if name:
            self._need__name__ = 0
            self.__name__ = name
        else:
            self.__name__ = os.path.basename(basepath)
        self.filename = os.path.join(_prefix, filename)

    def _refresh_check(self):
        if self._v_last_read and not DevelopmentMode:
            return
        __traceback_info__ = self.filename
        try:
            mtime = os.path.getmtime(self.filename)
        except OSError:
            mtime = 0
        if mtime == self._v_last_read:
            return
        f = open(self.filename, "rb")
        text = f.read()
        f.close()
        # set time in advance, so no recursive reading will occur
        self._v_last_read = mtime
        self.set_xml(text)

    security.declareProtected('View management screens', 'refresh_form')
    def refresh_form(self):
        """Trigger refresh check explicitly.
        """
        self._refresh_check()
        
    security.declareProtected('View', 'has_field')
    def has_field(self, id, include_disabled):
        """Check whether the form has a field of a certain id.
        """
        self._refresh_check()
        return FormulatorFormFile.inheritedAttribute(
            'has_field')(self, id, include_disabled)

    security.declareProtected('View', 'get_field')
    def get_field(self, id, include_disabled):
        """Get a field of a certain id.
        """
        self._refresh_check()
        return FormulatorFormFile.inheritedAttribute(
            'get_field')(self, id, include_disabled)

    security.declareProtected('View', 'get_fields')    
    def get_fields(self, include_disabled=0):
        """Get all the fields for all groups (in display order).
        """
        self._refresh_check()
        return FormulatorFormFile.inheritedAttribute(
            'get_fields')(self, include_disabled)
    
    security.declareProtected('View', 'get_field_ids')
    def get_field_ids(self, include_disabled=0):
        """Get all the ids of the fields in the form.
        """
        self._refresh_check()
        return FormulatorFormFile.inheritedAttribute(
            'get_field_ids')(self, include_disabled)

    security.declareProtected('View', 'get_fields_in_group')     
    def get_fields_in_group(self, group, include_disabled=0):
        self._refresh_check()
        return FormulatorFormFile.inheritedAttribute(
            'get_fields_in_group')(self, group, include_disabled)

    security.declareProtected('View', 'get_groups')
    def get_groups(self, include_empty=0):
        """Get a list of all groups, in display order.
        """
        self._refresh_check()
        return FormulatorFormFile.inheritedAttribute(
            'get_groups')(self, include_empty)
    
    security.declareProtected('View', 'render')
    def render(self, dict=None, REQUEST=None):
        """Render form.
        """
        self._refresh_check()
        return FormulatorFormFile.inheritedAttribute(
            'render')(self, dict, REQUEST)

    security.declareProtected('View', 'render_view')
    def render_view(self, dict=None):
        """Render contents (default simplistic way).
        """
        self._refresh_check()
        return FormulatorFormFile.inheritedAttribute(
            'render_view')(self, dict)
    
    def getOwner(self, info=0):
        """Gets the owner of the executable object.

        Since this object came from the
        filesystem, it is owned by no one managed by Zope.
        """
        return None

    def __getstate__(self):
        from ZODB.POSException import StorageError
        raise StorageError, ("Instance of AntiPersistent class %s "
                             "cannot be stored." % self.__class__.__name__)

InitializeClass(FormulatorFormFile)