Example #1
0
    def add(self, req):
        if not self.items:
            return (EMPTY_ITEMS, 0)

        m = driver(req)
        c = m._lock(req)                        # lock database first
        not_enought_items = []
        try:
            m._add(self, c)                     # get id
            for item_id, it in self.items:   # record to store
                item = Item(item_id)
                action = Action.from_order(self.id, it['count'])
                try:
                    if item._action(req, c, action) is None:
                        raise RuntimeError('Item not found')
                except ValueError as e:
                    not_enought_items.append(e.args[1])
            if not_enought_items:
                m._rollback(c)
                return (NOT_ENOUGH_ITEMS, not_enought_items)
        except BaseException as e:
            m._rollback(c)
            return None
        m._commit(c)
        return self
Example #2
0
    def set_state(self, req, state, note='', usernote=''):
        m = driver(req)
        c = m._lock(req)        # lock the table first

        try:
            if m._get(self, c) is None:
                raise LookupError('not found in backend')

            record = [(self.state, state)]
            self.state = state

            if note:
                record.append(('note', note))          # append storno note
            if usernote:
                record.append(('usernote', usernote))  # append storno usernote

            self.history.append((int(time()), record))

            if m._mod(self, c) is None:
                raise LookupError('not found in backend')

            if state == STATE_STORNED:          # "return" items to store
                for item_id, it in self.items:
                    item = Item(item_id)
                    action = Action.from_order(self.id, -it['count'])
                    if item._action(req, c, action) is None:
                        raise RuntimeError('Item not found')
            # endif

        except Exception as e:
            req.log_error(str(e))
            m._rollback(c)
            return None

        m._commit(c)
        return self