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