Move the robot deletion code into a delete_instance method on the User object

This commit is contained in:
Joseph Schorr 2014-11-10 13:18:17 -05:00
parent 158acd4f41
commit 72fedef097
2 changed files with 14 additions and 9 deletions

View file

@ -118,6 +118,19 @@ class User(BaseModel):
invalid_login_attempts = IntegerField(default=0)
last_invalid_login = DateTimeField(default=datetime.utcnow)
def delete_instance(self, recursive=False, delete_nullable=False):
# If we are deleting a robot account, only execute the subset of queries necessary.
if self.robot:
# For all the model dependencies, only delete those that allow robots.
for query, fk in self.dependencies(search_nullable=True):
if isinstance(fk, QuayUserField) and fk.allows_robots:
model = fk.model_class
model.delete().where(query).execute()
# Delete the instance itself.
super(User, self).delete_instance(recursive=False, delete_nullable=False)
else:
super(User, self).delete_instance(recursive=recursive, delete_nullable=delete_nullable)
class TeamRole(BaseModel):
name = CharField(index=True)

View file

@ -287,15 +287,7 @@ def regenerate_robot_token(robot_shortname, parent):
def delete_robot(robot_username):
try:
robot = User.get(username=robot_username, robot=True)
# For all the model dependencies, only delete those that allow robots.
for query, fk in robot.dependencies(search_nullable=True):
if isinstance(fk, QuayUserField) and fk.allows_robots:
model = fk.model_class
model.delete().where(query).execute()
# Delete the robot itself.
robot.delete_instance(recursive=False)
robot.delete_instance(recursive=True, delete_nullable=True)
except User.DoesNotExist:
raise InvalidRobotException('Could not find robot with username: %s' %