From: Radek Czajka
Date: Thu, 29 Mar 2012 09:59:12 +0000 (+0200)
Subject: Merge branch 'custompdf'
X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/5eeb9dace6068f83e2b70b5222cfab0c0a5e71eb?hp=a6554d68f532f4d15b75f75acd0d85d2b3b7ace2
Merge branch 'custompdf'
Conflicts:
wolnelektury/settings/__init__.py
---
diff --git a/apps/ajaxable/templates/ajaxable/form.html b/apps/ajaxable/templates/ajaxable/form.html
index 84e86e15f..38113dbb6 100755
--- a/apps/ajaxable/templates/ajaxable/form.html
+++ b/apps/ajaxable/templates/ajaxable/form.html
@@ -1,8 +1,14 @@
{% load i18n %}
+
{{ title }}
"
+msgstr ""
+"Uwaga: Generowanie pliku może trwaÄ dÅuższÄ
chwilÄ.\n"
+"Poczekaj cierpliwie, albo dodaj tÄ stronÄ do zakÅadek i wrÃ³Ä później."
+
+#: templates/waiter/wait.html:55
+#, python-format
+msgid ""
+"Something seems to have gone wrong while generating your file.\n"
+" Please order it again or complain to us about it."
+msgstr ""
+"WyglÄ
da na to, że coÅ poszÅo źle podczas generowania Twojego pliku.\n"
+"Spróbuj zamówiÄ go jeszcze raz albo napisz do nas."
+
diff --git a/apps/waiter/migrations/0001_initial.py b/apps/waiter/migrations/0001_initial.py
new file mode 100644
index 000000000..a75884bac
--- /dev/null
+++ b/apps/waiter/migrations/0001_initial.py
@@ -0,0 +1,39 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Adding model 'WaitedFile'
+ db.create_table('waiter_waitedfile', (
+ ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('path', self.gf('django.db.models.fields.CharField')(unique=True, max_length=255, db_index=True)),
+ ('task_id', self.gf('django.db.models.fields.CharField')(db_index=True, max_length=128, null=True, blank=True)),
+ ('task', self.gf('picklefield.fields.PickledObjectField')(null=True)),
+ ('description', self.gf('django.db.models.fields.CharField')(max_length=255, null=True, blank=True)),
+ ))
+ db.send_create_signal('waiter', ['WaitedFile'])
+
+
+ def backwards(self, orm):
+
+ # Deleting model 'WaitedFile'
+ db.delete_table('waiter_waitedfile')
+
+
+ models = {
+ 'waiter.waitedfile': {
+ 'Meta': {'object_name': 'WaitedFile'},
+ 'description': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'path': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}),
+ 'task': ('picklefield.fields.PickledObjectField', [], {'null': 'True'}),
+ 'task_id': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '128', 'null': 'True', 'blank': 'True'})
+ }
+ }
+
+ complete_apps = ['waiter']
diff --git a/apps/waiter/migrations/__init__.py b/apps/waiter/migrations/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/apps/waiter/models.py b/apps/waiter/models.py
new file mode 100644
index 000000000..10f92897e
--- /dev/null
+++ b/apps/waiter/models.py
@@ -0,0 +1,65 @@
+from os.path import join, isfile
+from django.core.urlresolvers import reverse
+from django.db import models
+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)
+ 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.
+
+ `path` is relative to WAITER_ROOT.
+ Won't open a path leading outside of WAITER_ROOT.
+ """
+ abs_path = check_abspath(path)
+ # Pre-fetch objects for deletion to avoid minor race condition
+ relevant = [o.id for o in cls.objects.filter(path=path)]
+ if isfile(abs_path):
+ cls.objects.filter(id__in=relevant).delete()
+ return True
+ else:
+ 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
+ )
+
+ 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 order(cls, path, task_creator, description=None):
+ """
+ Returns an URL for the user to follow.
+ If the file is ready, returns download URL.
+ If not, starts preparing it and returns waiting URL.
+
+ task_creator: function taking a path and generating the file;
+ description: a string or string proxy with a description for user;
+ """
+ already = cls.exists(path)
+ 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_id = waited.task.task_id
+ waited.description = description
+ waited.save()
+ return reverse("waiter", args=[path])
+ return join(WAITER_URL, path)
diff --git a/apps/waiter/settings.py b/apps/waiter/settings.py
new file mode 100644
index 000000000..fef4a7e49
--- /dev/null
+++ b/apps/waiter/settings.py
@@ -0,0 +1,18 @@
+from os.path import join
+from django.conf import settings
+
+try:
+ WAITER_ROOT = settings.WAITER_ROOT
+except AttributeError:
+ WAITER_ROOT = join(settings.MEDIA_ROOT, 'waiter')
+
+try:
+ WAITER_URL = settings.WAITER_URL
+except AttributeError:
+ WAITER_URL = join(settings.MEDIA_URL, 'waiter')
+
+try:
+ WAITER_MAX_QUEUE = settings.WAITER_MAX_QUEUE
+except AttributeError:
+ WAITER_MAX_QUEUE = 20
+
diff --git a/apps/waiter/tasks.py b/apps/waiter/tasks.py
new file mode 100644
index 000000000..4c3933ef1
--- /dev/null
+++ b/apps/waiter/tasks.py
@@ -0,0 +1,7 @@
+from celery.signals import task_postrun
+from waiter.models import WaitedFile
+
+
+def task_delete_after(task_id=None, **kwargs):
+ WaitedFile.objects.filter(task_id=task_id).delete()
+task_postrun.connect(task_delete_after)
diff --git a/apps/waiter/templates/waiter/wait.html b/apps/waiter/templates/waiter/wait.html
new file mode 100644
index 000000000..e15bd6455
--- /dev/null
+++ b/apps/waiter/templates/waiter/wait.html
@@ -0,0 +1,93 @@
+{% extends "base.html" %}
+{% load i18n %}
+{% load url from future %}
+
+{% block titleextra %}
+{% if file_url %}
+ {% trans "The file is ready for download!" %}
+{% else %}
+ {% if waiting %}
+ {% trans "Your file is being prepared, please wait." %}
+ {% else %}
+ {% trans "Something went wrong." %}
+ {% endif %}
+{% endif %}
+{% endblock %}
+
+
+{% block extrahead %}
+{% if file_url %}
+
+{% else %}
+ {% if waiting %}
+
+ {% endif %}
+{% endif %}
+{% endblock %}
+
+
+{% block body %}
+{% if file_url %}
+ {% trans "The file is ready for download!" %}
+
+
+
{% blocktrans %}Your file is ready!
+ If the download doesn't start in a few seconds,
+ feel free to use this direct link.{% endblocktrans %}
+
+{% else %}
+ {% if waiting %}
+
+ {% trans "Your file is being prepared, please wait." %}
+
+
+
{% blocktrans with d=waiting.description %}The file you requested was: {{d}}.{% endblocktrans %}
+
+
{% blocktrans %}Be aware: Generating the file can take a while.
+ Please be patient, or bookmark this page and come back later.
{% endblocktrans %}
+
+ {% else %}
+ {% trans "Something went wrong." %}
+
+
+ {% url 'suggest' as s %}
+
{% blocktrans %}Something seems to have gone wrong while generating your file.
+ Please order it again or complain to us about it.{% endblocktrans %}
+
+ {% endif %}
+{% endif %}
+
+{% endblock %}
+
+{% block extrabody %}
+{% if waiting %}
+
+{% endif %}
+{% endblock %}
diff --git a/apps/waiter/urls.py b/apps/waiter/urls.py
new file mode 100644
index 000000000..484ef3e44
--- /dev/null
+++ b/apps/waiter/urls.py
@@ -0,0 +1,5 @@
+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('waiter.views',
+ url(r'^(?P.*)$', 'wait', name='waiter'),
+)
diff --git a/apps/waiter/utils.py b/apps/waiter/utils.py
new file mode 100644
index 000000000..0957e9d80
--- /dev/null
+++ b/apps/waiter/utils.py
@@ -0,0 +1,17 @@
+from os.path import abspath, join, exists
+from shutil import rmtree
+from waiter.settings import WAITER_ROOT
+
+
+def check_abspath(path):
+ abs_path = abspath(join(WAITER_ROOT, path))
+ if not abs_path.startswith(WAITER_ROOT):
+ raise ValueError('Path not inside WAITER_ROOT.')
+ return abs_path
+
+
+def clear_cache(path):
+ abs_path = check_abspath(path)
+ if exists(abs_path):
+ rmtree(abs_path)
+
diff --git a/apps/waiter/views.py b/apps/waiter/views.py
new file mode 100644
index 000000000..e38bd8f1d
--- /dev/null
+++ b/apps/waiter/views.py
@@ -0,0 +1,19 @@
+from os.path import join
+from waiter.models import WaitedFile
+from waiter.settings import WAITER_URL
+from django.shortcuts import render, get_object_or_404
+from django.http import HttpResponse
+
+def wait(request, path):
+ if WaitedFile.exists(path):
+ file_url = join(WAITER_URL, path)
+ else:
+ file_url = ""
+ waiting = get_object_or_404(WaitedFile, path=path)
+ if waiting.is_stale():
+ waiting = None
+
+ if request.is_ajax():
+ return HttpResponse(file_url)
+ else:
+ return render(request, "waiter/wait.html", locals())
diff --git a/apps/wolnelektury_core/templates/admin/base_site.html b/apps/wolnelektury_core/templates/admin/base_site.html
index 23cfb59d8..ff381b554 100644
--- a/apps/wolnelektury_core/templates/admin/base_site.html
+++ b/apps/wolnelektury_core/templates/admin/base_site.html
@@ -5,7 +5,18 @@
{% block branding %}
{% trans "Site administration" %} - WolneLektury.pl
-{% trans "Translations" %}
+
+
+ promobox |
+ cytaty |
+ info |
+ ksiÄ
żki |
+ tagi |
+ kolekcje |
+ licznik domeny |
+ sponsorzy |
+ sugestie
+
{% endblock %}
{% block nav-global %}{% endblock %}
\ No newline at end of file
diff --git a/apps/wolnelektury_core/templates/admin/catalogue/book/change_list.html b/apps/wolnelektury_core/templates/admin/catalogue/book/change_list.html
index 09e567a5d..0ec95b24d 100644
--- a/apps/wolnelektury_core/templates/admin/catalogue/book/change_list.html
+++ b/apps/wolnelektury_core/templates/admin/catalogue/book/change_list.html
@@ -3,6 +3,7 @@
{% block content %}
{{ block.super }}
diff --git a/apps/wolnelektury_core/templates/auth/login.html b/apps/wolnelektury_core/templates/auth/login.html
index 7fbd570c0..6d71cd5b4 100644
--- a/apps/wolnelektury_core/templates/auth/login.html
+++ b/apps/wolnelektury_core/templates/auth/login.html
@@ -3,6 +3,7 @@
diff --git a/apps/wolnelektury_core/templates/superbase.html b/apps/wolnelektury_core/templates/superbase.html
index a4a7830e2..f67d9f384 100644
--- a/apps/wolnelektury_core/templates/superbase.html
+++ b/apps/wolnelektury_core/templates/superbase.html
@@ -130,6 +130,7 @@