Пример #1
0
	def _save(self, storable, factory):
		"""
		Internal function that is responsible for doing the
		actual saving.
		"""
		if not(storable.is_dirty()):
			return False
		
		table = storable.get_table()
		data = storable.get_data()
		
		locks_allowed = (not bool(self.req)) or self.req.app.config.get('use_db_locks', True)
		
		use_locks = False
		primary_key = factory.get_primary_key()
		
		if(storable.is_new()):
			if(factory.uses_guids()):
				data[primary_key] = self.fetch_id(storable)
			elif(locks_allowed):
				use_locks = True
				self.pool.runOperation('LOCK TABLES `%s` WRITE' % table)
			else:
				raise RuntimeError("Broken program logic is trying to lock an unlockable database.")
			
			query = sql.build_insert(table, data)
		else:
			query = sql.build_update(table, data, {primary_key:storable.get_id()})
		
		self.log(query)
		
		try:
			self.pool.runOperation(query)
			
			if not(factory.uses_guids()):
				if not(storable.get_id()):
					rows = self.pool.runQuery('SELECT MAX(%s) AS `id` FROM `%s`' % (primary_key, table))
					new_id = rows[0]['id']
					storable.set_id(new_id)
		finally:
			if(use_locks):
				self.pool.runOperation('UNLOCK TABLES')
		
		storable.clean()
		storable.set_new(False)
		storable.set_factory(factory)
		
		return True
Пример #2
0
	def fetch_id(self, storable):
		"""
		Fetch an ID for this object, if possible.
		
		"Predict" the id for this storable. If GUIDs are being used,
		this method will fetch a new GUID, set it, and return
		that ID immediately (assuming that this object will ultimately
		be saved with that ID).
		
		If GUIDs are not being used, this method will return 0 if
		this is an unsaved object. It's not possible to use "predictive"
		IDs in that case.
		
		@see: L{IFactory.fetch_id()}
		
		@param storable: the object whose ID you wish to fetch
		@type storable: L{storable.Storable}
		
		@returns: the item's id, or 0
		@rtype: int
		@raises LookupError: if no factory has been registered for this Storable's table
		"""
		id = storable.get_id()
		if(id == 0):
			table = storable.get_table()
			if not(self.has_factory(table)):
				raise LookupError('There is no factory registered for the table `%s`' % table)
			factory = self.get_factory(table)
			use_locks = (not bool(self.req)) or self.req.app.config.get('use_db_locks', True)
			new_id = factory.get_id(use_locks=use_locks)
			storable.set_id(new_id, set_old=False)
			return new_id
		return id
Пример #3
0
	def get_form_element(self, req, style, storable):
		"""
		Get a FormNode element that represents this field.
		
		The parent itemdef class will call this function, which will
		call the get_element() method, and set a few default values.
		
		@param req: The current request
		@type req: L{modu.web.app.Request}
		
		@param style: Generate for 'listing', 'search', or 'detail' views.
		@type style: str
		
		@param storable: The Storable instance to fill form data with.
		@type storable: L{modu.persist.storable.Storable} subclass
		
		@return: form element
		@rtype: L{modu.util.form.FormNode}
		"""
		frm = self.get_element(req, style, storable)
		
		classes = [self.__class__]
		while(classes):
			for cls in classes:
				if not(hasattr(cls, 'inherited_attributes')):
					continue
				for name in cls.inherited_attributes:
					if(name in self and name not in frm.attributes):
						frm.attributes[name] = self[name]
			classes = cls.__bases__
		
		for name, value in self.get('attributes', {}).iteritems():
			frm.attributes[name] = value
		
		# Since the templates take care of the wrapper, all
		# elements are defined as basic elements unless set
		# explicitly by the field definition.
		if(style != 'search' and 'basic_element' not in frm.attributes):
			frm(basic_element=True)
		
		if(style == 'listing' and self.get('link', False)):
			href = req.get_path(req.prepath, 'detail', storable.get_table(), storable.get_id())
			frm(prefix=tags.a(href=href, __no_close=True), suffix='</a>')
		
		if(self.get('required', False)):
			frm(required=True)
		
		if(callable(self.get('form_alter', None))):
			self['form_alter'](req, style, frm, storable, self)
		
		return frm
Пример #4
0
	def _destroy(self, storable):
		"""
		Internal function that is responsible for doing the
		actual destruction.
		"""
		factory = storable.get_factory()
		if(factory.uses_guids()):
			primary_key = factory.get_primary_key()
			delete_query = sql.build_delete(storable.get_table(), {primary_key:storable.get_id()})
		else:
			delete_query = sql.build_delete(storable.get_table(), storable.get_data())

		self.pool.runOperation(delete_query)
		storable.reset_id()
Пример #5
0
	def delete(self, req, form, storable):
		"""
		The delete function for forms generated from this itemdef.
		
		@param req: The current request
		@type req: L{modu.web.app.Request}
		
		@param frm: The current form
		@type frm: L{modu.util.form.FormNode}
		
		@param storable: The Storable instance to delete.
		@type storable: list(L{modu.persist.storable.Storable})
		
		@return: True if deleted
		@rtype: bool
		"""
		if not(self.deletable(req)):
			req.messages.report('error', "You are not allowed to delete `%s` records." % storable.get_table())
			return False
		
		if('predelete_callback' in self.config):
			result = self.config['predelete_callback'](req, form, storable)
			if(result is False):
				return False
		
		deleted_id = storable.get_id()
		deleted_table = storable.get_table()
		storable.get_store().destroy(storable, self.config.get('delete_related_storables', False))
		
		if('postdelete_callback' in self.config):
			self.config['postdelete_callback'](req, form, storable)
		
		req.messages.report('message', "Record #%d in %s was deleted." % (deleted_id, deleted_table))
		
		if('referer' in form):
			referer = form['referer'].value
		else:
			referer = None
		app.redirect(referer)
		
		return True
Пример #6
0
				req.messages.report('error', "An exception occurred during postwrite: %s" % e)
				reason = failure.Failure()
				reason.printTraceback(req['wsgi.errors'])
				postwrite_succeeded = False
		
		# call postwrite_callback
		if('postwrite_callback' in self.config):
			postwrite_callback = self.config['postwrite_callback']
			if not(isinstance(postwrite_callback, (list, tuple))):
				postwrite_callback = [postwrite_callback]
			for callback in postwrite_callback:
				if not(callback(req, form, storable)):
					postwrite_succeeded = False
		
		if(postwrite_succeeded):
			req.messages.report('message', 'Your changes to %s record #%s have been saved.' % (storable.get_table(), storable.get_id()))
			
			if('referer' in form):
				referer = form['referer'].value
			else:
				referer = None
			if(referer):
				app.redirect(referer)
		else:
			req.messages.report('error', 'There was an error in the postwrite process, but primary record data was saved.')
		
		return postwrite_succeeded
	
	def deletable(self, req):
		allow_delete = True
		if('delete_acl' in self.config):