本文整理汇总了Python中database.DataBase.find_one方法的典型用法代码示例。如果您正苦于以下问题:Python DataBase.find_one方法的具体用法?Python DataBase.find_one怎么用?Python DataBase.find_one使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类database.DataBase
的用法示例。
在下文中一共展示了DataBase.find_one方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Action
# 需要导入模块: from database import DataBase [as 别名]
# 或者: from database.DataBase import find_one [as 别名]
class Action(QObject):
"""! @brief
Binding between the card reader and the database.
@author CASAS Jacky
@date 22.06.14
@version 1.0
"""
## Signal used to display the action status on the UI
status = Signal(str)
## Signal used to display the last transactions on the UI
transactionsLoaded = Signal(list)
def __init__(self, pifacecontrol):
"""! @brief Initialize the instance and get the two collections of the database.
@param self the Action instance
"""
QObject.__init__(self)
## contains a link to the database collection *user*
self.users = DataBase().user
## contains a link to the database collection *transaction*
self.transactions = DataBase().transaction
self.piFace = pifacecontrol
self.piFace.activateButtonListener()
def transaction(self, deviceId, amount):
"""! @brief Method to make a transaction (withdraw an amount from an account).
@param self the Action instance
@param deviceId identifier of the device (smartcard or smartphone)
@param amount amount to withdraw
"""
canWithdraw = False
user = self.users.find_one({"devices.uid": deviceId})
for device in user['devices']:
if device['uid'] == deviceId:
if device['status'] == STA_DEVICE_ACTIVE:
if amount > 0:
if user['balance'] >= amount:
canWithdraw = True
else:
self.status.emit('Please recharge your account, you don\'t have enough money in it.')
else:
self.status.emit('The amount must be greater than 0.')
elif device['status'] == STA_DEVICE_LOST:
self.status.emit('Impossible transaction : Lost device.')
elif device['status'] == STA_DEVICE_STOLEN:
self.status.emit('Impossible transaction : Stolen device.')
elif device['status'] == STA_DEVICE_DELETED:
self.status.emit('Impossible transaction : Deleted device.')
else:
self.status.emit('Impossible transaction : Device status unknown.')
if canWithdraw:
# create transaction
userId = user['uid']
dispenserId = DISPENSER_ID
transactionDate = datetime.now()
transaction = {"userId":userId, "deviceId":deviceId, "dispenserId":dispenserId, "transactionType":WITHDRAWAL, "amount":amount, "transactionDate":transactionDate}
self.transactions.insert(transaction)
# debit credit in account
query = {"uid": user['uid'] }
update = {"$inc": { "balance": -amount}}
self.users.update(query, update)
self.status.emit('You withdraw ' + `amount` + ' CHF. You have now ' + `user['balance'] - amount` + ' CHF on your account.')
self.piFace.actionValidated()
else:
self.piFace.actionDenied()
break
def recharge(self, deviceId, amount):
"""! @brief Method to make a recharge (put an amount in an account).
@param self the Action instance
@param deviceId identifier of the device (smartcard or smartphone)
@param amount amount to recharge
"""
canRecharge = False
user = self.users.find_one({"devices.uid": deviceId})
for device in user['devices']:
if device['uid'] == deviceId:
if device['status'] == STA_DEVICE_ACTIVE:
if amount > 0:
canRecharge = True
else:
self.status.emit('The amount must be greater than 0.')
elif device['status'] == STA_DEVICE_LOST:
self.status.emit('Impossible recharge : Lost device.')
elif device['status'] == STA_DEVICE_STOLEN:
self.status.emit('Impossible recharge : Stolen device.')
elif device['status'] == STA_DEVICE_DELETED:
self.status.emit('Impossible recharge : Deleted device.')
else:
self.status.emit('Impossible recharge : Device status unknown.')
if canRecharge:
# create transaction
userId = user['uid']
dispenserId = DISPENSER_ID
#.........这里部分代码省略.........