def f_actions(self): # if using actions, we need to flush coprs into db, so that we can get # their ids self.f_db() self.a1 = models.Action( action_type=helpers.ActionTypeEnum("rename"), object_type="copr", object_id=self.c1.id, old_value="{0}/{1}".format(self.c1.user.name, self.c1.name), new_value="{0}/new_name".format(self.c1.user.name), created_on=int(time.time())) self.a2 = models.Action( action_type=helpers.ActionTypeEnum("rename"), object_type="copr", object_id=self.c2.id, old_value="{0}/{1}".format(self.c2.user.name, self.c2.name), new_value="{0}/new_name2".format(self.c2.user.name), created_on=int(time.time())) self.a3 = models.Action(action_type=helpers.ActionTypeEnum("delete"), object_type="copr", object_id=100, old_value="asd/qwe", new_value=None, result=helpers.BackendResultEnum("success"), created_on=int(time.time())) self.db.session.add_all([self.a1, self.a2, self.a3])
def get_waiting(cls): """ Return actions that aren't finished """ query = (models.Action.query.filter( models.Action.result == helpers.BackendResultEnum("waiting") ).filter( models.Action.action_type != helpers.ActionTypeEnum("legal-flag")). order_by(models.Action.created_on.asc())) return query
def unfinished_blocking_actions_for(cls, copr): blocking_actions = [helpers.ActionTypeEnum("rename"), helpers.ActionTypeEnum("delete")] actions = (models.Action.query .filter(models.Action.object_type == "copr") .filter(models.Action.object_id == copr.id) .filter(models.Action.result == helpers.BackendResultEnum("waiting")) .filter(models.Action.action_type.in_(blocking_actions))) return actions
class Action(db.Model, helpers.Serializer): """ Representation of a custom action that needs backends cooperation/admin attention/... """ id = db.Column(db.Integer, primary_key=True) # delete, rename, ...; see helpers.ActionTypeEnum action_type = db.Column(db.Integer, nullable=False) # copr, ...; downcase name of class of modified object object_type = db.Column(db.String(20)) # id of the modified object object_id = db.Column(db.Integer) # old and new values of the changed property old_value = db.Column(db.String(255)) new_value = db.Column(db.String(255)) # additional data data = db.Column(db.Text) # result of the action, see helpers.BackendResultEnum result = db.Column(db.Integer, default=helpers.BackendResultEnum("waiting")) # optional message from the backend/whatever message = db.Column(db.Text) # time created as returned by int(time.time()) created_on = db.Column(db.Integer) # time ended as returned by int(time.time()) ended_on = db.Column(db.Integer) def __str__(self): return self.__unicode__() def __unicode__(self): if self.action_type == helpers.ActionTypeEnum("delete"): return "Deleting {0} {1}".format(self.object_type, self.old_value) elif self.action_type == helpers.ActionTypeEnum("rename"): return "Renaming {0} from {1} to {2}.".format( self.object_type, self.old_value, self.new_value) elif self.action_type == helpers.ActionTypeEnum("legal-flag"): return "Legal flag on copr {0}.".format(self.old_value) return "Action {0} on {1}, old value: {2}, new value: {3}.".format( self.action_type, self.object_type, self.old_value, self.new_value)
def still_forking(self): return bool( Action.query.filter( Action.result == helpers.BackendResultEnum("waiting")).filter( Action.action_type == helpers.ActionTypeEnum("fork")). filter(Action.new_value == self.full_name).all())