From d3fa3d63e7f71780c98b8e703f8eff1b46dced58 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 7 Mar 2018 19:05:43 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E4=BF=AE=E6=94=B9Model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/terminal/models.py | 4 ++-- apps/terminal/tasks.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/terminal/models.py b/apps/terminal/models.py index 91ac8e052..5829fa2e1 100644 --- a/apps/terminal/models.py +++ b/apps/terminal/models.py @@ -127,7 +127,7 @@ class Session(models.Model): is_finished = models.BooleanField(default=False) has_replay = models.BooleanField(default=False, verbose_name=_("Replay")) has_command = models.BooleanField(default=False, verbose_name=_("Command")) - terminal = models.ForeignKey(Terminal, null=True, on_delete=models.CASCADE) + terminal = models.ForeignKey(Terminal, null=True, on_delete=models.SET_NULL) date_last_active = models.DateTimeField(verbose_name=_("Date last active"), default=timezone.now) date_start = models.DateTimeField(verbose_name=_("Date start"), db_index=True) date_end = models.DateTimeField(verbose_name=_("Date end"), null=True) @@ -148,7 +148,7 @@ class Task(models.Model): id = models.UUIDField(default=uuid.uuid4, primary_key=True) name = models.CharField(max_length=128, choices=NAME_CHOICES, verbose_name=_("Name")) args = models.CharField(max_length=1024, verbose_name=_("Args")) - terminal = models.ForeignKey(Terminal, null=True, on_delete=models.CASCADE) + terminal = models.ForeignKey(Terminal, null=True, on_delete=models.SET_NULL) is_finished = models.BooleanField(default=False) date_created = models.DateTimeField(auto_now_add=True) date_finished = models.DateTimeField(null=True) diff --git a/apps/terminal/tasks.py b/apps/terminal/tasks.py index 89f9cc5cc..e267b30b7 100644 --- a/apps/terminal/tasks.py +++ b/apps/terminal/tasks.py @@ -31,6 +31,6 @@ def delete_terminal_status_period(): def clean_orphan_session(): active_sessions = Session.objects.filter(is_finished=False) for session in active_sessions: - if not session.terminal.is_active: + if not session.terminal or not session.terminal.is_active: session.is_finished = True session.save()