Fix for unclickable cites in admin.
[wolnelektury.git] / apps / waiter / models.py
index 59eeea6..bffaf7f 100644 (file)
@@ -1,21 +1,25 @@
+# -*- coding: utf-8 -*-
+# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
 from os.path import join, isfile
 from django.core.urlresolvers import reverse
 from django.db import models
 from os.path import join, isfile
 from django.core.urlresolvers import reverse
 from django.db import models
-from djcelery.models import TaskMeta
-from waiter.settings import WAITER_URL
+from waiter.settings import WAITER_URL, WAITER_MAX_QUEUE
 from waiter.utils import check_abspath
 from picklefield import PickledObjectField
 
 
 class WaitedFile(models.Model):
     path = models.CharField(max_length=255, unique=True, db_index=True)
 from waiter.utils import check_abspath
 from picklefield import PickledObjectField
 
 
 class WaitedFile(models.Model):
     path = models.CharField(max_length=255, unique=True, db_index=True)
+    task_id = models.CharField(max_length=128, db_index=True, null=True, blank=True)
     task = PickledObjectField(null=True, editable=False)
     description = models.CharField(max_length=255, null=True, blank=True)
 
     @classmethod
     def exists(cls, path):
         """Returns opened file or None.
     task = PickledObjectField(null=True, editable=False)
     description = models.CharField(max_length=255, null=True, blank=True)
 
     @classmethod
     def exists(cls, path):
         """Returns opened file or None.
-        
+
         `path` is relative to WAITER_ROOT.
         Won't open a path leading outside of WAITER_ROOT.
         """
         `path` is relative to WAITER_ROOT.
         Won't open a path leading outside of WAITER_ROOT.
         """
@@ -28,13 +32,12 @@ class WaitedFile(models.Model):
         else:
             return False
 
         else:
             return False
 
-    def is_stale(self):
-        if self.task is None:
-            # Race; just let the other task roll. 
-            return False
-        if self.task.status not in (u'PENDING', u'STARTED', u'SUCCESS', u'RETRY'):
-            return True
-        return False
+    @classmethod
+    def can_order(cls, path):
+        return (cls.objects.filter(path=path).exists() or
+                cls.exists(path) or
+                cls.objects.count() < WAITER_MAX_QUEUE
+                )
 
     @classmethod
     def order(cls, path, task_creator, description=None):
 
     @classmethod
     def order(cls, path, task_creator, description=None):
@@ -50,7 +53,8 @@ class WaitedFile(models.Model):
         if not already:
             waited, created = cls.objects.get_or_create(path=path)
             if created or waited.is_stale():
         if not already:
             waited, created = cls.objects.get_or_create(path=path)
             if created or waited.is_stale():
-                waited.task = task_creator(check_abspath(path))
+                waited.task = task_creator(check_abspath(path), waited.pk)
+                waited.task_id = waited.task.task_id
                 waited.description = description
                 waited.save()
             return reverse("waiter", args=[path])
                 waited.description = description
                 waited.save()
             return reverse("waiter", args=[path])