コード例 #1
0
ファイル: comment.py プロジェクト: nvarini/aiida_core
    def __init__(self, **kwargs):

        # If no arguments are passed, then create a new DbComment
        if not kwargs:
            self.dbcomment = DbComment()

        # If a DbComment is passed as argument. Just use it and
        # wrap it with a Comment object
        elif 'dbcomment' in kwargs:
            # When a dbcomment is passed as argument, then no other arguments
            # should be passed.
            if len(kwargs) > 1:
                raise ValueError("When a DbComment is passed as argument, no"
                                 "further arguments are accepted.")
            dbcomment = kwargs.pop('dbcomment')
            if not isinstance(dbcomment, DbComment):
                raise ValueError("Expected a DbComment. Object of a different"
                                 "class was given as argument.")
            self.dbcomment = dbcomment

        else:
            id = kwargs.pop('id', None)
            if id is None:
                id = kwargs.pop('pk', None)
            user = kwargs.pop('user', None)
            dbnode = kwargs.pop('dbnode', None)

            # Constructing the default query
            dbcomment_query = DbComment.query

            # If an id is specified then we add it to the query
            if id is not None:
                dbcomment_query = dbcomment_query.filter_by(id=id)

            # If a user is specified then we add it to the query
            if user is not None:
                dbcomment_query = dbcomment_query.filter_by(user=user)

            # If a dbnode is specified then we add it to the query
            if dbnode is not None:
                dbcomment_query = dbcomment_query.filter_by(dbnode=dbnode)

            ccount = dbcomment_query.count()
            if ccount > 1:
                raise MultipleObjectsError(
                    "The arguments that you specified were too vague. More "
                    "than one comments with this data were found in the "
                    "database")
            elif ccount == 0:
                raise NotExistent("No comments were found with the given "
                                  "arguments")

            self.dbcomment = dbcomment_query.first()
コード例 #2
0
    def add_comment(self, content, user=None):
        from aiida.backends.sqlalchemy import session
        if self._to_be_stored:
            raise ModificationNotAllowed("Comments can be added only after "
                                         "storing the node")

        comment = DbComment(dbnode=self._dbnode, user=user, content=content)
        session.add(comment)
        session.commit()
コード例 #3
0
    def add_comment(self, content, user=None):
        from aiida.backends.sqlalchemy import get_scoped_session
        session = get_scoped_session()

        if self._to_be_stored:
            raise ModificationNotAllowed("Comments can be added only after "
                                         "storing the node")

        comment = DbComment(dbnode=self._dbnode, user=user, content=content)
        session.add(comment)
        try:
            session.commit()
        except:
            from aiida.backends.sqlalchemy import get_scoped_session
            session = get_scoped_session()
            session.rollback()
            raise