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


Python DataBase.insert方法代码示例

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


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

示例1: Action

# 需要导入模块: from database import DataBase [as 别名]
# 或者: from database.DataBase import insert [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
#.........这里部分代码省略.........
开发者ID:acknowledge,项目名称:NFCGolfBallDispenser-UI,代码行数:103,代码来源:action.py


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