Ejemplo n.º 1
0
def put(bag):
    data = {"type": bag["type"]}
    del bag["type"]
    if '_created' in bag:
        del bag["_created"]
    if '_deleted' in bag:
        del bag["_deleted"]

    table_name = data["type"]
    table = getattr(db, table_name)

    if table is None or not issubclass(table, (db.Base, db.CouchSync)):
        raise CbsException(TABLE_NOT_FOUND)

    for key in bag:
        data[key] = bag[key]
    # del_columns(data)

    for column in table.metadata.tables.get(table_name.lower()).columns._data:
        nullable = table.metadata.tables.get(
            table_name.lower()).columns._data[column].nullable
        if not nullable and not column.startswith(
                "_") and not column == "entry_user_id" and column not in data:
            raise CbsException(KEY_ERROR,
                               MESSAGE.get(KEY_ERROR).format(column))
        elif not column.startswith(
                "_") and not column == "entry_user_id" and column not in data:
            data[column] = None

    pg_db = PostgresDatabase()
    _id, _rev = pg_db.store(data, new_edits=True)
    return {"ok": True, "id": _id, "rev": _rev}
Ejemplo n.º 2
0
    def saveapproval(self, bag):
        if '_created' in bag:
            del bag["_created"]
        if '_deleted' in bag:
            del bag["_deleted"]
        if 'date' not in bag['data']:
            bag["data"]['date'] = str(datetime.now())

        if '_id' in bag:
            query = g.tran.query(db.Document).filter_by(_id=bag['_id']) \
                .filter(db.Document.document_status != 'committed')
            document = query.first()
            if document is None:
                raise CbsException(USER_NO_ACCESS)

        pg_db = PostgresDatabase()
        bag['type'] = 'Document'
        bag['document_status'] = 'draft'
        if len(g.user.roles_id) > 0:
            bag['approval']['approval_roles'] = []
            for role_id in bag['approval']['roles_id']:
                bag['approval']['approval_roles'].append({
                    'role_id': role_id
                })
        _id, _rev = pg_db.store(bag, new_edits=True)
        return {"ok": True, "id": _id, "rev": _rev}
Ejemplo n.º 3
0
    def save(self, bag):
        if '_created' in bag:
            del bag["_created"]
        if '_deleted' in bag:
            del bag["_deleted"]
        if 'date' not in bag['data']:
            bag["data"]['date'] = str(datetime.now())

        if '_id' in bag:
            query = g.tran.query(db.Document).filter_by(_id=bag['_id']) \
                .filter(db.Document.data.contains(type_coerce({"user_id": g.user.id}, JSONB)),
                        db.Document.document_status != 'committed')
            document = query.first()
            if document is None:
                raise CbsException(USER_NO_ACCESS)
        if g.user.roles_id is None:
            raise CbsException(GENERIC_ERROR,
                               u'Вам необходимо получить права')
        else:
            pg_db = PostgresDatabase()
            bag['type'] = 'Document'
            bag['document_status'] = 'draft'
            bag['data']['user_id'] = g.user.id
            if len(g.user.roles_id) > 0:
                bag['approval']['approval_roles'] = []
                for role_id in bag['approval']['roles_id']:
                    bag['approval']['approval_roles'].append({
                        'role_id': role_id
                    })
            _id, _rev = pg_db.store(bag, new_edits=True)
        return {"ok": True, "id": _id, "rev": _rev}
Ejemplo n.º 4
0
    def approve(self, bag, approve, comment):
        roles_filter = []
        for role_id in g.user.roles_id:
            roles_filter.append(db.Document.approval.contains(type_coerce({'approval_roles': [{"role_id": role_id}]}, JSONB)))
        query = g.tran.query(db.Document).filter_by(_id=bag[ID] if ID in bag else bag['_id'], _deleted='infinity') \
            .filter(or_(and_(db.Document.document_status == "approval", or_(*roles_filter)),
                        and_(db.Document.document_status == "approved",
                             db.Document.data.contains(type_coerce({'executor_id': g.user.id}, JSONB))),
                        and_(db.Document.document_status == 'draft',
                             db.Document.data.contains(type_coerce({'user_id': g.user.id}, JSONB)))))
        document = query.first()
        if document is None:
            raise CbsException(USER_NO_ACCESS)
        doc_data = orm_to_json(document)

        del doc_data["_created"]
        del doc_data["_deleted"]
        del doc_data["entry_user_id"]

        if bag['document_status'] == 'draft' and doc_data['document_status'] == 'draft':
            doc_data['document_status'] = 'approval'if \
                'required' in doc_data['approval'] and doc_data['approval']['required'] == True else 'approved'
            pg_db = PostgresDatabase()
            doc_data['type'] = 'Document'
            _id, _rev = pg_db.store(doc_data)
            return {"id": _id, "rev": _rev}
        elif bag['document_status'] == 'approval' and doc_data['document_status'] == 'approval':
            approval_position = 0
            for approval_role in doc_data['approval']["approval_roles"]:
                if g.user.roles_id.index(approval_role["role_id"]) >= 0 and "approved" not in approval_role:
                    if "approval_users" in approval_role:
                        for approval_users in approval_role["approval_users"]:
                            if approval_users["user_id"] == g.user.id:
                                raise CbsException(GENERIC_ERROR, u'Вы уже одобряли документ')
                        approval_role["approval_users"].append({
                            "user_id": g.user.id,
                            "approved": approve
                        })
                    else:
                        approval_role["approval_users"] = []
                        approval_role["approval_users"].append({
                            "user_id": g.user.id,
                            "approved": approve
                        })
                    if approve is True:
                        if doc_data['approval']["approval_type"] == 'all':
                            users_count = g.tran.query(db.User).filter_by(_deleted='infinity') \
                                .filter(db.User.roles_id.contains(type_coerce(approval_role["role_id"], JSONB)))
                            if users_count == len(approval_role["approval_users"]):
                                approval_role["approved"] = True
                                try:
                                    if approval_position == len(doc_data['approval']["approval_roles"]) - 1:
                                        doc_data['document_status'] = 'approved'
                                except IndexError:
                                    pass
                        else:
                            approval_role["approved"] = True
                            try:
                                if approval_position == len(doc_data['approval']["approval_roles"]) - 1:
                                    doc_data['document_status'] = 'approved'
                            except IndexError:
                                pass
                    else:
                        approval_role["approved"] = False
                        doc_data['document_status'] = 'rejected'
                    break
                approval_position += 1
            pg_db = PostgresDatabase()

            if comment:
                pg_db.store({
                    "type": "DocumentComments",
                    "document_id": doc_data['_id'],
                    "data": {
                        "comment_type": "approval" if approve is True else "rejection",
                        "comment": comment
                    }
                })

            doc_data['type'] = 'Document'
            _id, _rev = pg_db.store(doc_data)
            return {"id": _id, "rev": _rev}
        elif bag['document_status'] == 'approved' and doc_data['document_status'] == 'approved':
            pg_db = PostgresDatabase()

            if comment:
                pg_db.store({
                    "type": "DocumentComments",
                    "document_id": doc_data['_id'],
                    "data": {
                        "comment_type": "commit",
                        "comment": comment
                    }
                })

            if doc_data['data']["executor_id"] == g.user.id and \
                ('roles_id' not in doc_data['approval']):
                return self.commit(bag)
            elif doc_data['data']["executor_id"] == g.user.id:
                for role_id in doc_data['approval']['roles_id']:
                    doc_data['approval']['approval_roles'].append({
                        "role_id": role_id
                    })
                bag['type'] = 'Document'
                bag['document_status'] = 'approval'
                bag['approval'] = doc_data['approval']
                bag['data']['executor_id'] = doc_data['data']['executor_id']
                bag['data']['user_id'] = doc_data['data']['user_id']
                _id, _rev = pg_db.store(bag)
                return {"id": _id, "rev": _rev}

        raise CbsException(GENERIC_ERROR, u'Не подходящий статус документа')