def getFieldSetHTML(self): """ Liefert HTML fuer das Edit Formular """ formerror = self.request.get('formerror', None) if formerror: return formerror else: pre = "" kwargs = {} defaults = {} form = self.form if self.defaults: if callable(self.defaults): of = self.defaults(self.context) else: of = self.defaults if self.create: # Vgl. Filter Defaults for k in of.keys(): pre = pre + "<input type='hidden' id='%s--%s' name='%s--%s:int' value='%s' />" % (self.sd.__name__, k, self.sd.__name__, k, of[k]) else: for key in of: if hasattr(self.sd, key) and getattr(self.sd, key) is None: setattr(self.sd, key, of[key]) try: bf = form.bind(self.sd,**kwargs) html = bf.render() except Exception, e: # Hier gibt es leider einen Unterschied zwischen Elixir Entities und gemappten SA Klassen log_exc(e) html = form.bind(self.sd(),**kwargs).render() return pre + html
def _migrateSetValue(self, name, value, old_schema=None, **kw): """Try to set an object value using a variety of methods.""" schema = self.Schema() # Migrate pre-AT 1.3 schemas. schema = fixSchema(schema) field = schema.get(name, None) # Try using the field's mutator if field: mutator = field.getMutator(self) if mutator is not None: try: args = [value,] mapply(mutator, *args, **kw) return except (ConflictError, KeyboardInterrupt): raise except: log_exc() else: # Try setting an existing attribute if shasattr(self, name): setattr(self, name, value) return raise ValueError, 'name = %s, value = %s' % (name, value)
def getCFieldSetHTML(self): """ Liefert HTML fuer das Create Formular """ kwargs = {} kwargs['session'] = __session__ form = self.form # print "CFS" if self.defaults: # print "YES" if callable(self.defaults): of = self.defaults(self.context) else: of = self.defaults #print "Defaults", of #print type(self.sd) for key in of: # print key if getattr(self.sd, key) is None: # print of[key] setattr(self.sd, key, of[key]) try: return form.bind(self.sd,**kwargs).render() except Exception, e: # Hier gibt es leider einen Unterschied zwischen Elixir Entities und gemappten SA Klassen log_exc(e) return form.bind(self.sd(),**kwargs).render()
def abort(self): dbc, DB__ = self._get_dbc() try: DB__.tpc_abort() except ConflictError: raise except: log_exc(msg='Database abort failed')
def createScales(self, instance, value=_marker): sizes = self.getAvailableSizes(instance) if not HAS_PIL or not sizes: return # get data from the original size if value is None if value is _marker: img = self.getRaw(instance) if not img: return data = str(img.data) else: data = value # empty string - stop rescaling because PIL fails on an empty string if not data: return filename = self.getFilename(instance) for n, size in sizes.items(): if size == (0,0): continue w, h = size id = self.getName() + "_" + n __traceback_info__ = (self, instance, id, w, h) try: #XXX here is the difference between the base class #use the convention that if the word "square" is in the #name of the size, then we scale to aspect ratio and crop if 'square' in n: imgdata, format = self.cropscale(data, w, h) else: imgdata, format = self.scale(data, w, h) except (ConflictError, KeyboardInterrupt): raise except: if not self.swallowResizeExceptions: raise else: log_exc() # scaling failed, don't create a scaled version continue mimetype = 'image/%s' % format.lower() image = self.content_class(id, self.getName(), imgdata, mimetype ) # nice filename: filename_sizename.ext #fname = "%s_%s%s" % (filename, n, ext) #image.filename = fname image.filename = filename # manually use storage delattr(image, 'title') self.getStorage(instance).set(id, instance, image, mimetype=mimetype, filename=filename)
def getUrlsToPurge(self, object): """Get a list of URLs to be purged when the given object is added / modified / deleted""" # if nothing to purge or cachefu disabled, return an empty list if not self.hasPurgeableProxy() or not self.getEnabled(): return [] relative_urls = sets.Set() rules = self.getRules().objectValues() for rule in rules: try: rule.getRelativeUrlsToPurge(object, relative_urls) except ConflictError: raise except: log_exc() for adapter in zope.component.subscribers([aq_inner(object)], IPurgeUrls): relative_urls.union_update(adapter.getRelativeUrls()) relative_urls = list(relative_urls) absolute_urls=[] for adapter in zope.component.subscribers([aq_inner(object)], IPurgeUrls): absolute_urls.extend(adapter.getAbsoluteUrls(relative_urls)) if relative_urls: proxy_purge_config = self.getProxyPurgeConfig() if proxy_purge_config == 'vhm-rewrite': # unless defined otherwise, we assume urls passed to the cache proxy # are of the standard form expected by the VirtualHostMonster: # [squid_url]/VirtualHostBase/[protocol]/[host]:[port]/[path to portal root]/VirtualHostRoot/[path] url_tool = getToolByName(self, 'portal_url') portal_path = '/'.join(url_tool.getPortalObject().getPhysicalPath()) domains = self.getDomains() prefixes = [] for d in domains: p = urlparse.urlparse(d) protocol = p[0] host = p[1] split_host = host.split(':') host = split_host[0] port = split_host[1] prefixes.append('VirtualHostBase/%s/%s:%s%s/VirtualHostRoot/' \ % (protocol, host, port, portal_path)) relative_urls = [prefix+url for prefix in prefixes for url in relative_urls] elif proxy_purge_config == 'custom-rewrite': domains = [urlparse.urlparse(d) for d in self.getDomains()] relative_urls = self.rewritePurgeUrls(relative_urls, domains) return relative_urls + absolute_urls
def initializeArchetype(self, **kwargs): # Called by the generated add* factory in types tool. try: self.initializeLayers() self.markCreationFlag() self.setDefaults() if kwargs: kwargs['_initializing_'] = True self.edit(**kwargs) self._signature = self.Schema().signature() except (ConflictError, KeyboardInterrupt): raise except: log_exc()
def getRuleAndHeaderSet(self, request, object, view, member): """Get the caching rule that applies here and the header set specified by the rule""" if not self.getEnabled(): return (None, None) rules = self.getRules().objectValues() for rule in rules: try: header_set = rule.getHeaderSet(request, object, view, member) if header_set is not None: return (rule, header_set) except ConflictError: raise except: log_exc() return (None, None)
def get_portal_metadata(self, field): # Returns the portal_metadata for a field. pmt = getToolByName(self, 'portal_metadata') policy = None try: schema = getattr(pmt, 'DCMI', None) spec = schema.getElementSpec(field.accessor) policy = spec.getPolicy(self.portal_type) except (ConflictError, KeyboardInterrupt): raise except: log_exc() return None, False if not policy: policy = spec.getPolicy(None) return DisplayList(map(lambda x: (x, x), policy.allowedVocabulary())), \ policy.enforceVocabulary()
def _migrateSetValue(self, name, value, old_schema=None, **kw): """Try to set an object value using a variety of methods.""" schema = self.Schema() # Migrate pre-AT 1.3 schemas. schema = fixSchema(schema) field = schema.get(name, None) # Try using the field's mutator if field: mutator = field.getMutator(self) if mutator is not None: try: args = [value] mapply(mutator, *args, **kw) return except (ConflictError, KeyboardInterrupt): raise except: log_exc() else: # Try setting an existing attribute if shasattr(self, name): setattr(self, name, value) return raise ValueError, 'name = %s, value = %s' % (name, value)
query = query.encode(db_encoding) else: try: query = query.encode(site_encoding) except UnicodeEncodeError: query = query.encode("UTF-8") if context.cache_time_ > 0 and context.max_cache_ > 0: result = self._cached_result(DB__, (query, context.max_rows_)) else: try: result = DB__.query(query, context.max_rows_) except ConflictError: raise except: log_exc(msg="Database query failed", reraise=1) if hasattr(context, "_v_sql_brain"): brain = context._v_sql_brain else: brain = context._v_sql_brain = getBrain(context.class_file_, context.class_name_) if type(result) is type(""): f = StringIO() f.write(result) f.seek(0) result = RDB.File(f, brain, p, None) else: if db_encoding: # Encode result before we wrap it in Result object # We will change the encoding from source to either the specified target_encoding
class SQLMethod(Aqueduct.BaseQuery): _arg = None _col = None def __init__(self, context): self.context = context self.id = str(context.__class__.__name__) self.title = '' for k, v in _defaults.items(): if not hasattr(context, k): setattr(context, k, v) def edit(self, connection_id, arguments, template): """Change database method properties The 'connection_id' argument is the id of a database connection that resides in the current folder or in a folder above the current folder. The database should understand SQL. The 'arguments' argument is a string containing an arguments specification, as would be given in the SQL method cration form. The 'template' argument is a string containing the source for the SQL Template. """ context = self.context self.connection_id = str(connection_id) arguments = str(arguments) self.arguments_src = arguments self._arg = Aqueduct.parse(arguments) if not isinstance(template, (str, unicode)): template = str(template) self.src = template self.template = t = context.template_class(template) t.cook() context._v_query_cache = {}, Bucket() def advanced_edit(self, max_rows=1000, max_cache=100, cache_time=0, class_name='', class_file='', REQUEST=None): """Change advanced properties The arguments are: max_rows -- The maximum number of rows to be returned from a query. max_cache -- The maximum number of results to cache cache_time -- The maximum amound of time to use a cached result. class_name -- The name of a class that provides additional attributes for result record objects. This class will be a base class of the result record class. class_file -- The name of the file containing the class definition. The class file normally resides in the 'Extensions' directory, however, the file name may have a prefix of 'product.', indicating that it should be found in a product directory. For example, if the class file is: 'ACMEWidgets.foo', then an attempt will first be made to use the file 'lib/python/Products/ACMEWidgets/Extensions/foo.py'. If this failes, then the file 'Extensions/ACMEWidgets.foo.py' will be used. """ context = self.context # paranoid type checking if type(max_rows) is not type(1): max_rows = atoi(max_rows) if type(max_cache) is not type(1): max_cache = atoi(max_cache) if type(cache_time) is not type(1): cache_time = atoi(cache_time) class_name = str(class_name) class_file = str(class_file) context.max_rows_ = max_rows context.max_cache_, context.cache_time_ = max_cache, cache_time context._v_sql_cache = {}, Bucket() context.class_name_, context.class_file_ = class_name, class_file context._v_sql_brain = getBrain(context.class_file_, context.class_name_, 1) def _cached_result(self, DB__, query): context = self.context # Try to fetch from cache if hasattr(context, '_v_sql_cache'): cache = context._v_sql_cache else: cache = context._v_sql_cache = {}, Bucket() cache, tcache = cache max_cache = context.max_cache_ now = time() t = now - context.cache_time_ if len(cache) > max_cache / 2: keys = tcache.keys() keys.reverse() while keys and (len(keys) > max_cache or keys[-1] < t): key = keys[-1] q = tcache[key] del tcache[key] if int(cache[q][0]) == key: del cache[q] del keys[-1] if query in cache: k, r = cache[query] if k > t: return r result = apply(DB__.query, query) if context.cache_time_ > 0: tcache[int(now)] = query cache[query] = now, result return result def _get_dbc(self): """Get the database connection""" context = self.context try: dbc = getattr(context, self.connection_id) except AttributeError: raise AttributeError, ( "The database connection <em>%s</em> cannot be found." % (self.connection_id)) try: DB__ = dbc() except ConflictError: raise except: raise 'Database Error', ('%s is not connected to a database' % self.id) return dbc, DB__ def __call__(self, src__=0, test__=0, **kw): """Call the database method The arguments to the method should be passed via keyword arguments, or in a single mapping object. If no arguments are given, and if the method was invoked through the Web, then the method will try to acquire and use the Web REQUEST object as the argument mapping. The returned value is a sequence of record objects. """ context = self.context dbc, DB__ = self._get_dbc() p = None argdata = self._argdata(kw) argdata['sql_delimiter'] = '\0' argdata['sql_quote__'] = dbc.sql_quote__ # TODO: Review the argdata dictonary. The line bellow is receiving unicode # strings, mixed with standard strings. It is insane! Archetypes needs a policy # about unicode, and lots of tests on this way. I prefer to not correct it now, # only doing another workarround. We need to correct the cause of this problem, # not its side effects :-( try: query = apply(self.template, (p, ), argdata) except TypeError, msg: msg = str(msg) if 'client' in msg: raise NameError("'client' may not be used as an " + "argument name in this context") else: raise __traceback_info__ = query if src__: return query # Get the encoding arguments # We have two possible kw arguments: # db_encoding: The encoding used in the external database # site_encoding: The uncoding used for the site # If not specified, we use sys.getdefaultencoding() db_encoding = kw.get('db_encoding', None) site_encoding = kw.get('site_encoding', 'utf-8') if type(query) == type(u''): if db_encoding: query = query.encode(db_encoding) else: try: query = query.encode(site_encoding) except UnicodeEncodeError: query = query.encode('UTF-8') if context.cache_time_ > 0 and context.max_cache_ > 0: result = self._cached_result(DB__, (query, context.max_rows_)) else: try: result = DB__.query(query, context.max_rows_) except ConflictError: raise except: log_exc(msg='Database query failed', reraise=1) if hasattr(context, '_v_sql_brain'): brain = context._v_sql_brain else: brain = context._v_sql_brain = getBrain(context.class_file_, context.class_name_) if type(result) is type(''): f = StringIO() f.write(result) f.seek(0) result = RDB.File(f, brain, p, None) else: if db_encoding: # Encode result before we wrap it in Result object # We will change the encoding from source to either the specified target_encoding # or the site default encoding # The data is a list of tuples of column data encoded_result = [] for row in result[1]: columns = () for col in row: if isinstance(col, types.StringType): # coerce column to unicode with database encoding newcol = unicode(col, db_encoding) # Encode column as string with site_encoding newcol = newcol.encode(site_encoding) else: newcol = col columns += newcol, encoded_result.append(columns) result = (result[0], encoded_result) result = Results(result, brain, p, None) columns = result._searchable_result_columns() if test__ and columns != self._col: self._col = columns # If run in test mode, return both the query and results so # that the template doesn't have to be rendered twice! if test__: return query, result return result
# FIXME: do some testing with this result return result except xmlrpclib.Fault, ef: LOG.warn('%s: %s' % (sys.exc_info()[0], ef)) if retry: # take care of hexadecimal Unicode escape sequences submission = submission.decode('unicode_escape') # retry appending this submission return self.appendJob(backend, submission, inputFields, tests, False) except Exception, e: log_exc() return (-1, 'Internal error: %s: %s' % (sys.exc_info()[0], e)) security.declarePublic('getResult') def getResult(self, jobId): """ Returns the result for a check job with the given job id. """ handle = self._getSpoolerHandle() username = self.portal_properties.ecaab_properties.username password = self.portal_properties.ecaab_properties.password AUTH = {'username':username, 'password':password} return handle.getResult(AUTH, jobId)
query = query.encode(db_encoding) else: try: query = query.encode(site_encoding) except UnicodeEncodeError: query = query.encode('UTF-8') if context.cache_time_ > 0 and context.max_cache_ > 0: result = self._cached_result(DB__, (query, context.max_rows_)) else: try: result = DB__.query(query, context.max_rows_) except ConflictError: raise except: log_exc(msg='Database query failed', reraise=1) if hasattr(context, '_v_sql_brain'): brain = context._v_sql_brain else: brain = context._v_sql_brain = getBrain(context.class_file_, context.class_name_) if type(result) is type(''): f = StringIO() f.write(result) f.seek(0) result = RDB.File(f, brain, p, None) else: if db_encoding: # Encode result before we wrap it in Result object