Exemplo n.º 1
0
    def delete(self):
        """Delete model object

        :return: Deleted model object
        """
        db.session.delete(self)
        error = session_commit()
        if error:
            raise DataException(message=error, errorcode=404)
        return self
Exemplo n.º 2
0
    def assign(cls, **kwargs):
        """Assign mapping table values or files to model object

        :param kwargs: Dictionary containing references to objects and file objects
        :return: files assigned object
        """
        obj = ModelMixin.__assign_field_values(cls(), **kwargs)
        db.session.add(obj)
        error = session_commit()
        if error:
            raise DataException(message=error, errorcode=404)
        return obj
Exemplo n.º 3
0
    def update(self, **kwargs):
        """Update model object

        :param kwargs: Dictionary containing fields to be updated
        :return: Updated model object
        """
        self = ModelMixin.__assign_field_values(self, **kwargs)
        db.session.add(self)
        error = session_commit()
        if error:
            raise DataException(message=error, errorcode=404)
        return self
Exemplo n.º 4
0
    def add(cls, **kwargs):
        """Add new model object

        :param kwargs: Dictionary containing fields to be assigned to object
        :return: added model object
        """
        obj = ModelMixin.__assign_field_values(cls(), **kwargs)
        obj.created = obj.updated = datetime.utcnow().strftime('%s')
        db.session.add(obj)
        error = session_commit()
        if error:
            raise DataException(message=error, errorcode=404)
        if not obj.id:
            raise DataException(message="Could not add {}".format(
                obj.__class__.__name__),
                                errorcode=403)
        return obj
Exemplo n.º 5
0
    def save_bulk(cls, object_list):
        """ Save all the objects present in object list

        :param object_list: List of objects
        :return: True if successful
        """
        if object_list:
            try:
                db.session.bulk_save_objects(object_list)
            except Exception as e:
                app.logger.exception(traceback.print_stack())
                db.session.rollback()
                return False
            error = session_commit()
            if error:
                raise DataException(message=error, errorcode=403)
        return True