当前位置: 首页>>代码示例>>Python>>正文


Python ErrorHandler.get_error方法代码示例

本文整理汇总了Python中errorhandler.ErrorHandler.get_error方法的典型用法代码示例。如果您正苦于以下问题:Python ErrorHandler.get_error方法的具体用法?Python ErrorHandler.get_error怎么用?Python ErrorHandler.get_error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在errorhandler.ErrorHandler的用法示例。


在下文中一共展示了ErrorHandler.get_error方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Model

# 需要导入模块: from errorhandler import ErrorHandler [as 别名]
# 或者: from errorhandler.ErrorHandler import get_error [as 别名]
class Model(object):
	""" Base class to all Models """
	def __init__(self):
		self.db_name = current_app.config["DB"]["name"]
		self.collection_name = ""
		self.content = None
		self.error_handle = ErrorHandler()

	def set_content(self):
		""" 
			There should be a definition of this function in every child class.
			This function prepares a dictionary of the child class object's parameters which
			are to be saved, and set it to self.content
		"""
		pass
	def set_objprops(self, data=None):
		"""
			This function should have a definition in a child class for when 
			a single result is expected from the DB query. (when findone=True).
			Eg: When fetching a user from the DB.

			This function should then populate the appropriate properties of the object
			with the data fetched from the DB
		"""
		pass


	def get_db_handler(self):
		""" Return DB handler for the appropriate collection, returns None if no collection name provided """
		if not self.collection_name=="":
			return MongoClient()[self.db_name][self.collection_name]
		else:
			return None

	def check_duplicate(self, fields):
		"""
			provided fields, this function will return true if there is a duplicate entry
			false if no duplicate value found
			eg: fields = {"name": "John", "age": 20}
		"""
		try:
			dbh = self.get_db_handler()
			if dbh.find(fields).count()>0:
				return True
			return False
		except Exception as e:
			return self.error_handle.get_error(error=str(e), occurred_at="mad.lib.Model.check_duplicate()")

	def get(self, conditions=None, sort_by=[("_id", 1)], distinct=False, distinct_fieldname=None, limit=0, findone=False):
		""" Return db items fullfilled by conditions, and sorted by ID """
		try:
			dbh = self.get_db_handler()
			content = None
			if conditions and "_id" in conditions:
				if not "noobjectify" in conditions or conditions["noobjectify"]==False:
					conditions.update({"_id": ObjectId(conditions["_id"])})
				if "noobjectify" in conditions:
					del conditions["noobjectify"]

			if distinct==True and distinct_fieldname:
				content = dbh.find(conditions).sort(sort_by).distinct(distinct_fieldname)
			elif findone==True:
				content = dbh.find_one(conditions)
			else:
				content = dbh.find(conditions).sort(sort_by)

			if content and limit>0:
				content = content.limit(limit)

			return content
		except Exception as e:
			return self.error_handle.get_error(error=str(e), occurred_at="mad.lib.Model.get()")

	def save(self, duplicate_fields_check=None, allow_duplicate=False):
		""" Add a new item to the collection """
		try:
			# duplicate_fields_check should be a dict, with field and its corresponding value
			self.set_content()

			if not self.content==None:
				if allow_duplicate==False and not duplicate_fields_check==None:
					if self.check_duplicate(fields=duplicate_fields_check)==True:
						return {"status": "failed", "message": "Could not save item. Duplicate entry found."}

				dbh = self.get_db_handler()
				dbh.save(self.content)
				return {"status": "success", "message": "Successfully added item to the DB"}

			return {"status": "failed", "message": "Content is empty" }

		except Exception as e:
			return self.error_handle.get_error(error=str(e), occurred_at="mad.lib.Model.save()")

	def update(self, conditions={}, overwrite=False, upsert=False, noobjectify=False, multi=False):
		""" Update a DB entry """
		try:
			self.set_content()

			dbh = self.get_db_handler()
			if "_id" in conditions or "_id" in self.content:
#.........这里部分代码省略.........
开发者ID:bnkamalesh,项目名称:flaav,代码行数:103,代码来源:model.py

示例2: Controller

# 需要导入模块: from errorhandler import ErrorHandler [as 别名]
# 或者: from errorhandler.ErrorHandler import get_error [as 别名]
class Controller(object):
	""" Parent class to all Controller classes. """
	def __init__(self):
		self.error_handle = ErrorHandler()
		self.cache_handle = Cache

	def get_db_handler(self):
		""" Returns the DB handler """
		mc = MongoClient()
		return mc[current_app.config["DB"]["name"]]

	def add_db_item(self, collection=None, content=None, _id=None, upsert=False):
		""" Save an item to the DB """
		try:
			if collection and content:
				db = self.get_db_handler()
				_id = db[collection].save(content)
				return {"status": "success", "message": "Successfully added item to the DB", "_id": _id}
			return None
		except Exception as e:
			return self.error_handle.get_error(error=str(e), occurred_at="mad.lib.controller.add_db_item()")

	def update_db_item(self, collection=None, conditions=None, content=None, upsert=False, overwrite=False, noobjectify=False, multi=False):
		""" Update/upsert a DB item """
		try:
			if collection and conditions and content:
				db = self.get_db_handler()
				if noobjectify==False and "_id" in conditions:
					conditions["_id"] = ObjectId(conditions["_id"])
				if overwrite:
					db[collection].update(conditions, {"$set": content}, upsert=upsert, multi=multi)
				else:
					db[collection].update(conditions, content, upsert=upsert, multi=multi)
			return {"status": "success", "message": "DB item updated successfully."}
		except Exception as e:
			return self.error_handle.get_error(error=str(e), occurred_at="mad.lib.controller.update_db_item()")


	def get_db_items(self, collection=None, conditions=None, sort_by=[("_id", 1)]):
		""" Get items from a collection in the DB and return results """
		try:
			if collection:
				if conditions and "_id" in conditions:
					# By default, if an _id is provided, it'll be converted into an ObjectId instance.
					# "noobjectify" would prevent it from making use of the _id directly instead of 
					# converting it into an ObjectId instance.
					if not "noobjectify" in conditions or conditions["noobjectify"]==False:
						conditions.update({"_id": ObjectId(conditions["_id"])})
				items = self.get_db_handler()[collection].find(conditions).sort(sort_by)
				if items:
					return items
			return None
		except Exception as e:
			return self.error_handle.get_error(error=str(e), occurred_at="mad.lib.controller.get_db_items()")

	def get_db_distinct_items(self, collection=None, fieldname=None, conditions=None, sort_by=[("_id", 1)]):
		""" Get unique/distinct values from all records of the given field """
		try:
			if collection and fieldname:
				return self.get_db_handler()[collection].find(conditions).sort(sort_by).distinct(fieldname)
			return None
		except Exception as e:
			return self.error_handle.get_error(error=str(e), occurred_at="mad.lib.controller.get_db_distinct_items()")
			

	def remove_db_item(self, collection=None, conditions=None, remove_all=False):
		""" Remove an or all items from the collection, based on given conditions """
		try:
			if remove_all:
				conditions = {}

			if collection and conditions:
				self.get_db_handler()[collection].remove(conditions)

			return {"status": "success", "message": "Successfully removed item(s) from the DB"}
		except Exception as e:
			return self.error_handle.get_error(error=str(e), occurred_at="mad.lib.controller.remove_db_item()")
开发者ID:bnkamalesh,项目名称:flaav,代码行数:79,代码来源:controller.py


注:本文中的errorhandler.ErrorHandler.get_error方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。