source: products/quintagroup.referencedatagridfield/trunk/quintagroup/referencedatagridfield/_field.py @ 2360

Last change on this file since 2360 was 2360, checked in by mylan, 14 years ago

Toggle trunk for plone4 release.
Plone-3 release moved to brances/plone3.

Merged revisions 3345-3351,3355-3359,3361-3370,3372-3390,3397 via svnmerge from
http://svn.quintagroup.com/products/quintagroup.referencedatagridfield/branches/plone4

Fixed minor conflicts in:

  • ./setup.py
  • ./quintagroup/referencedatagridfield/configure.zcml

........

r3345 | mylan | 2010-05-13 15:51:53 +0300 (Thu, 13 May 2010) | 1 line


Update requirements: in setup.py, included its in configure.zcml

........

r3346 | mylan | 2010-05-13 17:42:07 +0300 (Thu, 13 May 2010) | 1 line


Added refdatagridbrowser_popup view with popup customization for the referencedatagridfield

........

r3347 | mylan | 2010-05-13 17:50:14 +0300 (Thu, 13 May 2010) | 1 line


Added and registered datagridref_popup named template for the ReferenceDataGridWidget?

........

r3348 | mylan | 2010-05-13 18:59:34 +0300 (Thu, 13 May 2010) | 1 line


Fix typo in named_template_adapter

........

r3349 | mylan | 2010-05-13 19:00:21 +0300 (Thu, 13 May 2010) | 1 line


Prevent breakage of datagridwidget_manipulators.pt

........

r3350 | mylan | 2010-05-13 20:01:59 +0300 (Thu, 13 May 2010) | 1 line


Initial reference popup integration

........

r3351 | mylan | 2010-05-13 22:05:47 +0300 (Thu, 13 May 2010) | 1 line


Update default referencebrowser javascript / datagrid field to popup work correctly for new added row

........

r3355 | mylan | 2010-05-14 17:21:23 +0300 (Fri, 14 May 2010) | 1 line


Fix reference browser url calculation on the view - pass additional paramters

........

r3356 | mylan | 2010-05-14 17:26:49 +0300 (Fri, 14 May 2010) | 1 line


Update datagridwidget_manipulators template for additional params pass to datagridref_popup view

........

r3357 | mylan | 2010-05-14 17:27:59 +0300 (Fri, 14 May 2010) | 1 line


Remove order_idx from the additional parmeters - it will be calulated with jQuery

........

r3358 | mylan | 2010-05-14 17:29:20 +0300 (Fri, 14 May 2010) | 1 line


Updated jQuery processor on UID adding

........

r3359 | mylan | 2010-05-14 17:31:54 +0300 (Fri, 14 May 2010) | 1 line


Added focus and blur event-handlers to referencedatagridwidget javascripts

........

r3361 | mylan | 2010-05-17 18:08:44 +0300 (Mon, 17 May 2010) | 1 line


Fixed all overlays bindings to same target issue

........

r3362 | mylan | 2010-05-17 18:29:07 +0300 (Mon, 17 May 2010) | 1 line


Fix error with everlay windows autoclosing

........

r3363 | mylan | 2010-05-17 18:29:51 +0300 (Mon, 17 May 2010) | 1 line


Fix error with everlay windows autoclosing

........

r3364 | mylan | 2010-05-17 18:35:25 +0300 (Mon, 17 May 2010) | 1 line


Fixed link, title and uid data set in refbrowser_setReference

........

r3365 | mylan | 2010-05-17 18:38:15 +0300 (Mon, 17 May 2010) | 1 line


Fix error with everlay windows autoclosing

........

r3366 | mylan | 2010-05-17 18:43:18 +0300 (Mon, 17 May 2010) | 1 line


Fix addReferenceDataGridRowAfter newly added method of dataGridFieldFunctions

........

r3367 | mylan | 2010-05-17 18:46:10 +0300 (Mon, 17 May 2010) | 1 line


Implemented addReferenceDataGridRowAfter method of dataGridFieldFunctions object

........

r3368 | mylan | 2010-05-17 18:47:22 +0300 (Mon, 17 May 2010) | 1 line


Override updateOrderIndex method of dataGridFieldFunctions

........

r3369 | mylan | 2010-05-17 19:14:01 +0300 (Mon, 17 May 2010) | 1 line


Fixed styles toggling on title column edition

........

r3370 | mylan | 2010-05-17 20:35:10 +0300 (Mon, 17 May 2010) | 1 line


Little clean-up

........

r3372 | mylan | 2010-05-18 19:02:12 +0300 (Tue, 18 May 2010) | 1 line


Fix Deprication warning for plone4 consider importing

........

r3373 | mylan | 2010-05-18 19:04:12 +0300 (Tue, 18 May 2010) | 1 line


Added base tests structure, move tests into the tests package

........

r3374 | mylan | 2010-05-18 19:04:51 +0300 (Tue, 18 May 2010) | 1 line


Added installation tests

........

r3375 | mylan | 2010-05-18 20:08:00 +0300 (Tue, 18 May 2010) | 1 line


Fixed package installation, updated demo content creation to MixIn? class

........

r3376 | mylan | 2010-05-18 22:20:41 +0300 (Tue, 18 May 2010) | 1 line


Added basic field test

........

r3377 | mylan | 2010-05-18 23:29:24 +0300 (Tue, 18 May 2010) | 1 line


Added test for link, uid, title for the field

........

r3378 | mylan | 2010-05-19 10:54:46 +0300 (Wed, 19 May 2010) | 1 line


Prevent changing security manager on tested content adding

........

r3379 | mylan | 2010-05-19 13:58:29 +0300 (Wed, 19 May 2010) | 1 line


Added tests for possible bugs in field processing

........

r3380 | mylan | 2010-05-19 13:58:54 +0300 (Wed, 19 May 2010) | 1 line


Updated testing content creation

........

r3381 | mylan | 2010-05-19 13:59:49 +0300 (Wed, 19 May 2010) | 1 line


Simplify field name for example content type

........

r3382 | mylan | 2010-05-19 14:01:16 +0300 (Wed, 19 May 2010) | 1 line


Fixed bug on getting data for uninitialized field

........

r3383 | mylan | 2010-05-19 15:22:37 +0300 (Wed, 19 May 2010) | 1 line


Added tests for widget in view mode

........

r3384 | mylan | 2010-05-19 16:47:06 +0300 (Wed, 19 May 2010) | 1 line


Update comments for TestWidgetView?, some code cleanup

........

r3385 | mylan | 2010-05-19 17:02:29 +0300 (Wed, 19 May 2010) | 1 line


Added TestWidgetEditPresence? tests for presence of columns and buttons in edit mode of the widget

........

r3386 | mylan | 2010-05-19 18:00:01 +0300 (Wed, 19 May 2010) | 1 line


Remove records with links to unexistent objects on getting data from the field

........

r3387 | mylan | 2010-05-19 18:13:57 +0300 (Wed, 19 May 2010) | 1 line


Minor changes

........

r3388 | mylan | 2010-05-19 18:14:42 +0300 (Wed, 19 May 2010) | 1 line


Added tests popup related adapters registration

........

r3389 | mylan | 2010-05-19 19:10:49 +0300 (Wed, 19 May 2010) | 1 line


Added tests for pupup related staff in edit mode of ReferenceDataGridWidget?

........

r3390 | mylan | 2010-05-19 19:12:06 +0300 (Wed, 19 May 2010) | 1 line


Added tests of widget resources

........

r3397 | mylan | 2010-05-19 21:02:51 +0300 (Wed, 19 May 2010) | 1 line


Added MANIFEST.in, updated author in package information

........

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1#from Products.Archetypes import atapi
2import re
3import logging
4import urlparse
5from urllib import quote
6from types import ListType, TupleType
7
8from AccessControl import ClassSecurityInfo
9
10from Products.CMFCore.utils import getToolByName
11from Products.validation import validation #validators import baseValidators
12from Products.Archetypes.Field import encode, ReferenceField
13from Products.Archetypes.Registry import registerField, registerWidget
14
15from archetypes.referencebrowserwidget.widget import ReferenceBrowserWidget
16
17from Products.DataGridField.Column import Column
18from Products.DataGridField.DataGridField import DataGridField
19from Products.DataGridField.DataGridWidget import DataGridWidget
20
21from quintagroup.referencedatagridfield.columns import BlockColumn
22from quintagroup.referencedatagridfield.columns import HiddenColumn
23from quintagroup.referencedatagridfield.columns import StyledColumn
24
25# Logger object
26#logger = logging.getLogger('ReferenceDataGridField')
27#logger.debug("ReferenceDataGrid loading")
28
29class ReferenceDataGridWidget(DataGridWidget, ReferenceBrowserWidget):
30    _properties = ReferenceBrowserWidget._properties.copy()
31    _properties.update(DataGridWidget._properties.copy())
32    _properties.update({
33        'macro': "referencedatagridwidget",
34        'helper_css': ('datagridwidget.css','referencedatagridwidget.css'),
35        'helper_js': ('referencebrowser.js', 'datagridwidget.js', 'referencedatagridwidget.js'),
36        'force_close_on_insert': True,
37        'popup_name': 'datagridref_popup',
38        'columns': {
39            'title': StyledColumn("Title", trigger_key="default_title",
40                                  blur_handler="triggerTitleClass",
41                                  focus_handler="triggerOnFocusStyles",
42                                  class_no=None,
43                                  class_changed="changed-title-field",
44                                  class_not_changed="not-changed-title-field"),
45            'link': BlockColumn("Link", column_on_class="hidden-field",
46                                columns=['link','uid'], read_only=True),
47            'uid': HiddenColumn("UID", visible=False)},
48        })
49
50isURL = validation.validatorFor('isURL')
51
52class ReferenceDataGridField(DataGridField, ReferenceField):
53    _properties = ReferenceField._properties.copy()
54    _properties.update(DataGridField._properties.copy())
55    _properties.update({
56        'columns': ('title', 'link', 'uid'),
57        'widget': ReferenceDataGridWidget,
58        'multiValued' : True,
59        })
60
61    security = ClassSecurityInfo()
62
63    security.declarePrivate('isRemoteURL')
64    def isRemoteURL(self, url):
65        return isURL(url) == 1 and True or False
66
67    security.declarePrivate('set')
68    def set(self, instance, value, **kwargs):
69        """
70        The passed in object should be a records object, or a sequence of dictionaries
71        About link data:
72          * interpretations:
73            * if data not starts with standard protocol names (http://, ftp://) than
74              *uid* field data will be used as reference
75          * record removed if:
76            * no data;
77            * data contains UID of not existent object
78        About title:
79          * if there is UID of existent object and record has same title to the original
80            object - title will not be saved.
81        """
82        catalog = getToolByName(instance, "uid_catalog")
83
84        if value is None:
85            value = ()
86
87        if not isinstance(value, (ListType, TupleType)):
88            value = value,
89
90        result = []
91        for row in value:
92            data = {"title":"", "link":"", "uid":""}
93
94            uid = str(row.get("uid", "")).strip()
95            link = str(row.get("link", "")).strip()
96            title = str(row.get('title', ""))
97
98            if not title == "":
99                data["title"] = title
100
101            if link == "":
102                continue
103            elif self.isRemoteURL(link):
104                data["link"] = urlparse.urlunparse(urlparse.urlparse(link))
105            else:
106                if uid == '':
107                    continue
108
109                brains = catalog(UID=uid)
110                if len(brains) == 0:
111                    continue
112                # Found objects with pointed UID
113                brain = brains[0]
114                data["uid"] = uid
115                # Fix title for uid
116                if data['title'] == getattr(brain, "Title", ""):
117                    data['title'] = ""
118            result.append(data)
119
120        DataGridField.set(self, instance, result, **kwargs)
121
122        uids = [r['uid'] for r in result if r['uid']!=""]
123        ReferenceField.set(self, instance, uids, **kwargs)
124       
125    security.declarePrivate('get')
126    def get(self, instance, **kwargs):
127        """ Return DataGridField value
128
129        Value is a list object of rows.
130        Row id dictionary object with standard 'link', 'uid' and 'title' keys
131        plus extra informal *url* and *url_title* keys
132        """
133        purl = getToolByName(instance, "portal_url")
134        # use portal_catalog to hide protected object for the logged in user.
135        catalog = getToolByName(instance, "portal_catalog")
136
137        result = []
138        uids = {}
139        rows = DataGridField.get(self, instance, **kwargs)
140        for row in rows:
141            uid = row.get("uid","")
142            link = row.get("link","")
143            title = row.get("title","")
144            result.append({
145                # DataGridField row data
146                "uid": uid, "link": link, "title": title,
147                # View data
148                "url": "", "default_title": None})
149            data = result[-1]
150            # Process remote URL and collect UIDs
151            if link:
152                data["url"] = quote(link, safe='?$#@/:=+;$,&%')
153                data["default_title"] = link
154                # if title not set for remote url - set it equals to url
155                if not data["title"]:
156                    data["title"] = data["default_title"]
157            else:
158                uids[uid] = data
159        # Process UIDs
160        if uids:
161            brains = catalog(UID=uids.keys())
162            for b in brains:
163                data = uids[b.UID]
164                data["url"] = b.getURL()
165                data["link"] = b.getPath()
166                data["default_title"] = self._brains_title_or_id(b, instance)
167                # If title not set - get it from the brain
168                if not data["title"]:
169                    data["title"] = data["default_title"]
170            # Remove records with links to unexistent objects
171            del_uids = set(uids.keys()) - set([b.UID for b in brains])
172            result = filter(lambda r: not r["uid"] in del_uids, result)
173
174        return result
175
176
177registerWidget(
178    ReferenceDataGridWidget,
179    title='DataGrid Reference',
180    used_for=('quintagroup.referencedatagridfield.ReferenceDataGridField',)
181    )
182
183registerField(
184    ReferenceDataGridField,
185    title="DataGrid Reference Field",
186    description=("Reference DataGrid field.")
187    )
Note: See TracBrowser for help on using the repository browser.