72 encoding = vreg.property_value('ui.encoding') |
72 encoding = vreg.property_value('ui.encoding') |
73 except: # no vreg or property not registered |
73 except: # no vreg or property not registered |
74 encoding = 'utf-8' |
74 encoding = 'utf-8' |
75 self.encoding = encoding |
75 self.encoding = encoding |
76 # cache result of execution for (rql expr / eids), |
76 # cache result of execution for (rql expr / eids), |
77 # should be emptied on commit/rollback of the server session / web |
77 # should be emptied on commit/rollback of the server session / web |
78 # connection |
78 # connection |
79 self.local_perm_cache = {} |
79 self.local_perm_cache = {} |
80 |
80 |
81 def property_value(self, key): |
81 def property_value(self, key): |
82 if self.user: |
82 if self.user: |
83 return self.user.property_value(key) |
83 return self.user.property_value(key) |
84 return self.vreg.property_value(key) |
84 return self.vreg.property_value(key) |
85 |
85 |
86 def etype_rset(self, etype, size=1): |
86 def etype_rset(self, etype, size=1): |
87 """return a fake result set for a particular entity type""" |
87 """return a fake result set for a particular entity type""" |
88 from cubicweb.rset import ResultSet |
88 from cubicweb.rset import ResultSet |
89 rset = ResultSet([('A',)]*size, '%s X' % etype, |
89 rset = ResultSet([('A',)]*size, '%s X' % etype, |
90 description=[(etype,)]*size) |
90 description=[(etype,)]*size) |
112 return rset.get_entity(0, 0) |
112 return rset.get_entity(0, 0) |
113 else: |
113 else: |
114 return None |
114 return None |
115 |
115 |
116 # url generation methods ################################################## |
116 # url generation methods ################################################## |
117 |
117 |
118 def build_url(self, method, base_url=None, **kwargs): |
118 def build_url(self, method, base_url=None, **kwargs): |
119 """return an absolute URL using params dictionary key/values as URL |
119 """return an absolute URL using params dictionary key/values as URL |
120 parameters. Values are automatically URL quoted, and the |
120 parameters. Values are automatically URL quoted, and the |
121 publishing method to use may be specified or will be guessed. |
121 publishing method to use may be specified or will be guessed. |
122 """ |
122 """ |
128 else: |
128 else: |
129 path = method |
129 path = method |
130 if not kwargs: |
130 if not kwargs: |
131 return u'%s%s' % (base_url, path) |
131 return u'%s%s' % (base_url, path) |
132 return u'%s%s?%s' % (base_url, path, self.build_url_params(**kwargs)) |
132 return u'%s%s?%s' % (base_url, path, self.build_url_params(**kwargs)) |
133 |
133 |
134 |
134 |
135 def build_url_params(self, **kwargs): |
135 def build_url_params(self, **kwargs): |
136 """return encoded params to incorporate them in an URL""" |
136 """return encoded params to incorporate them in an URL""" |
137 args = [] |
137 args = [] |
138 for param, values in kwargs.items(): |
138 for param, values in kwargs.items(): |
152 return unicode(quoted, self.encoding) |
152 return unicode(quoted, self.encoding) |
153 return urlquote(str(value), safe=safe) |
153 return urlquote(str(value), safe=safe) |
154 |
154 |
155 def url_unquote(self, quoted): |
155 def url_unquote(self, quoted): |
156 """returns a unicode unquoted string |
156 """returns a unicode unquoted string |
157 |
157 |
158 decoding is based on `self.encoding` which is the encoding |
158 decoding is based on `self.encoding` which is the encoding |
159 used in `url_quote` |
159 used in `url_quote` |
160 """ |
160 """ |
161 if isinstance(quoted, unicode): |
161 if isinstance(quoted, unicode): |
162 quoted = quoted.encode(self.encoding) |
162 quoted = quoted.encode(self.encoding) |
163 try: |
163 try: |
164 return unicode(urlunquote(quoted), self.encoding) |
164 return unicode(urlunquote(quoted), self.encoding) |
165 except UnicodeDecodeError: # might occurs on manually typed URLs |
165 except UnicodeDecodeError: # might occurs on manually typed URLs |
166 return unicode(urlunquote(quoted), 'iso-8859-1') |
166 return unicode(urlunquote(quoted), 'iso-8859-1') |
167 |
167 |
168 |
168 |
169 # session's user related methods ##################################### |
169 # session's user related methods ##################################### |
170 |
170 |
171 @cached |
171 @cached |
172 def user_data(self): |
172 def user_data(self): |
173 """returns a dictionnary with this user's information""" |
173 """returns a dictionnary with this user's information""" |
174 userinfo = {} |
174 userinfo = {} |
175 if self.is_internal_session: |
175 if self.is_internal_session: |
195 def is_internal_session(self): |
195 def is_internal_session(self): |
196 """overrided on the server-side""" |
196 """overrided on the server-side""" |
197 return False |
197 return False |
198 |
198 |
199 # abstract methods to override according to the web front-end ############# |
199 # abstract methods to override according to the web front-end ############# |
200 |
200 |
201 def base_url(self): |
201 def base_url(self): |
202 """return the root url of the application""" |
202 """return the root url of the application""" |
203 raise NotImplementedError |
203 raise NotImplementedError |
204 |
204 |
205 def decorate_rset(self, rset): |
205 def decorate_rset(self, rset): |
206 """add vreg/req (at least) attributes to the given result set """ |
206 """add vreg/req (at least) attributes to the given result set """ |
207 raise NotImplementedError |
207 raise NotImplementedError |
208 |
208 |
209 def describe(self, eid): |
209 def describe(self, eid): |
210 """return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
210 """return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
211 raise NotImplementedError |
211 raise NotImplementedError |
212 |
212 |
213 |
213 |
214 # XXX 2.45 is allowing nicer entity type names, use this map for bw compat |
214 # XXX 2.45 is allowing nicer entity type names, use this map for bw compat |
215 ETYPE_NAME_MAP = {# 3.2 migration |
215 ETYPE_NAME_MAP = {# 3.2 migration |
216 'ECache': 'CWCache', |
216 'ECache': 'CWCache', |
217 'EUser': 'CWUser', |
217 'EUser': 'CWUser', |
218 'EGroup': 'CWGroup', |
218 'EGroup': 'CWGroup', |
219 'EProperty': 'CWProperty', |
219 'EProperty': 'CWProperty', |
266 'eworkcase': 'workcase', |
266 'eworkcase': 'workcase', |
267 'eworkorder': 'workorder', |
267 'eworkorder': 'workorder', |
268 'ezone': 'zone', |
268 'ezone': 'zone', |
269 'i18ncontent': 'i18ncontent', |
269 'i18ncontent': 'i18ncontent', |
270 'svnfile': 'vcsfile', |
270 'svnfile': 'vcsfile', |
271 |
271 |
272 'eclassschemes': 'keyword', |
272 'eclassschemes': 'keyword', |
273 'eclassfolders': 'folder', |
273 'eclassfolders': 'folder', |
274 'eclasstags': 'tag', |
274 'eclasstags': 'tag', |
275 |
275 |
276 'jpl': 'jpl', |
276 'jpl': 'jpl', |