1 # -*- coding: utf-8 -*-
 
   2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 
   3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 
   5 from os.path import join, isfile
 
   6 from django.core.urlresolvers import reverse
 
   7 from django.db import models
 
   8 from waiter.settings import WAITER_URL, WAITER_MAX_QUEUE
 
   9 from waiter.utils import check_abspath
 
  10 from picklefield import PickledObjectField
 
  13 class WaitedFile(models.Model):
 
  14     path = models.CharField(max_length=255, unique=True, db_index=True)
 
  15     task_id = models.CharField(max_length=128, db_index=True, null=True, blank=True)
 
  16     task = PickledObjectField(null=True, editable=False)
 
  17     description = models.CharField(max_length=255, null=True, blank=True)
 
  20     def exists(cls, path):
 
  21         """Returns opened file or None.
 
  23         `path` is relative to WAITER_ROOT.
 
  24         Won't open a path leading outside of WAITER_ROOT.
 
  26         abs_path = check_abspath(path)
 
  27         # Pre-fetch objects for deletion to avoid minor race condition
 
  28         relevant = [o.id for o in cls.objects.filter(path=path)]
 
  30             cls.objects.filter(id__in=relevant).delete()
 
  36     def can_order(cls, path):
 
  37         return (cls.objects.filter(path=path).exists() or
 
  39                 cls.objects.count() < WAITER_MAX_QUEUE
 
  43     def order(cls, path, task_creator, description=None):
 
  45         Returns an URL for the user to follow.
 
  46         If the file is ready, returns download URL.
 
  47         If not, starts preparing it and returns waiting URL.
 
  49         task_creator: function taking a path and generating the file;
 
  50         description: a string or string proxy with a description for user;
 
  52         already = cls.exists(path)
 
  54             waited, created = cls.objects.get_or_create(path=path)
 
  56                 waited.task = task_creator(check_abspath(path), waited.pk)
 
  57                 waited.task_id = waited.task.task_id
 
  58                 waited.description = description
 
  60             return reverse("waiter", args=[path])
 
  61         return join(WAITER_URL, path)