Merge branch 'master' of http://github.com/fnp/wolnelektury
authorLukasz <lukasz@anwajler.com>
Tue, 11 May 2010 11:57:27 +0000 (13:57 +0200)
committerLukasz <lukasz@anwajler.com>
Tue, 11 May 2010 11:57:27 +0000 (13:57 +0200)
apps/catalogue/migrations/0008_fix_shelf_book_count.py [new file with mode: 0644]
apps/catalogue/views.py
deployment.py [new file with mode: 0644]
wolnelektury.vhost.tmpl [new file with mode: 0644]
wolnelektury.wsgi.tmpl [new file with mode: 0644]

diff --git a/apps/catalogue/migrations/0008_fix_shelf_book_count.py b/apps/catalogue/migrations/0008_fix_shelf_book_count.py
new file mode 100644 (file)
index 0000000..b4ee915
--- /dev/null
@@ -0,0 +1,18 @@
+# -*- coding: utf-8 -*-
+
+from south.db import db
+from django.db import models
+from catalogue.models import Tag, Book
+
+class Migration:
+    
+    def forwards(self):
+        "Write your forwards migration here"
+        for tag in Tag.objects.filter(user__isnull=False):
+            books = Tag.intermediary_table_model.objects.get_intersection_by_model(Book, [tag])
+            tag.book_count = len(books)
+            tag.save()
+    
+    def backwards(self, orm):
+        "Write your backwards migration here"
+        pass
index 6de5a26..60f984a 100644 (file)
@@ -294,12 +294,15 @@ def remove_from_shelf(request, shelf, book):
     book = get_object_or_404(models.Book, slug=book)
     shelf = get_object_or_404(models.Tag, slug=shelf, category='set', user=request.user)
     
-    models.Tag.objects.remove_tag(book, shelf)
-    
-    shelf.book_count -= 1
-    shelf.save()
-    
-    return HttpResponse('Usunieto')
+    if shelf in book.tags:
+        models.Tag.objects.remove_tag(book, shelf)
+
+        shelf.book_count -= 1
+        shelf.save()
+
+        return HttpResponse('Usunięto')
+    else:
+        return HttpResponse('Książki nie ma na półce')
 
 
 def collect_books(books):
diff --git a/deployment.py b/deployment.py
new file mode 100644 (file)
index 0000000..b156b93
--- /dev/null
@@ -0,0 +1,67 @@
+#!/srv/library/wolnelektury/pythonenv/bin/python
+from __future__ import with_statement
+
+import shutil
+import os
+import sys
+
+from string import Template
+
+def render_template(source, dest, context={}):
+    print "Rendering template:",
+    with open(source, 'rb') as source_file:
+        t = Template(source_file.read())
+    with open(dest, 'wb') as dest_file:
+        dest_file.write(t.safe_substitute(context))
+    print "done."
+
+def restart_wsgi():
+    print "Restarting wsgi application:",
+    os.system("touch %s" % WSGI_TARGET)
+    print "done."
+
+def update_application():
+    print "Updating repository.",
+    os.system("cd %s; git pull" % PROJECT_ROOT)
+
+    print "Installing requirements"
+    os.system("%s install -r %s" % (PIP, os.path.join(PROJECT_ROOT, 'requirements.txt')))
+
+    print "Installing local requirements"
+    os.system("%s install -r %s" % (PIP, os.path.join(ROOT, 'etc', 'requirements.txt')))
+    print "done."
+
+ROOT = os.path.dirname(os.path.abspath(__file__))
+
+PYTHON = os.path.join(ROOT, 'pythonenv', 'bin', 'python')
+PIP = os.path.join(ROOT, 'pythonenv', 'bin', 'pip')
+PYTHON_SITE = os.path.join(ROOT, 'pythonenv', 'lib', 'python2.6', 'site-packages')
+
+PROJECT_NAME = 'wolnelektury'
+PROJECT_ROOT = os.path.join(ROOT, 'application')
+
+MEDIA_ROOT = os.path.join(ROOT, 'www', 'media')
+
+ADMIN_EMAIL = 'lrekucki@gmail.com'
+
+WSGI_TARGET = os.path.join(ROOT, 'www', 'wsgi', PROJECT_NAME + '.wsgi')
+WSGI_DIR = os.path.dirname(WSGI_TARGET)
+
+WSGI_USER = PROJECT_NAME
+WSGI_PROCESSES = 5
+WSGI_THREADS = 1
+
+DOMAIN = 'lektury.staging.nowoczesnapolska.org.pl'
+
+#
+# Load local configuration
+#
+sys.path = [ os.path.join(ROOT, 'etc') ] + sys.path
+
+from local_deployment import *
+
+if __name__ == '__main__':
+    update_application()
+    render_template(os.path.join(PROJECT_ROOT, PROJECT_NAME + '.wsgi.tmpl'), WSGI_TARGET, context=globals())
+    render_template(os.path.join(PROJECT_ROOT, PROJECT_NAME + '.vhost.tmpl'), os.path.join(ROOT, 'etc', PROJECT_NAME + '.vhost'), context=globals())
+    restart_wsgi()
diff --git a/wolnelektury.vhost.tmpl b/wolnelektury.vhost.tmpl
new file mode 100644 (file)
index 0000000..fbd21a9
--- /dev/null
@@ -0,0 +1,25 @@
+<VirtualHost *:80>
+    ServerName $DOMAIN
+    ServerAlias $DOMAIN_ALIASES
+    ServerAdmin $ADMIN_EMAIL
+
+    WSGIDaemonProcess $PROJECT_NAME user=$WSGI_USER group=$WSGI_USER processes=$WSGI_PROCESSES threads=$WSGI_THREADS display-name=%{GROUP}
+    WSGIProcessGroup $PROJECT_NAME
+
+    WSGIScriptAlias / $WSGI_TARGET
+    <Directory $WSGI_DIR>
+        Order allow,deny
+        allow from all
+    </Directory>
+
+    Alias /media $MEDIA_ROOT
+    <Directory $MEDIA_ROOT >
+        Options Indexes, FollowLinks
+        Order allow,deny
+        Allow from all
+    </Directory>
+
+    LogLevel warn
+    ErrorLog /var/log/apache2/$PROJECT_NAME/error.log
+    CustomLog /var/log/apache2/$PROJECT_NAME/access.log combined
+</VirtualHost>
diff --git a/wolnelektury.wsgi.tmpl b/wolnelektury.wsgi.tmpl
new file mode 100644 (file)
index 0000000..6b772f3
--- /dev/null
@@ -0,0 +1,24 @@
+#!$PYTHON
+import site
+site.addsitedir('$PYTHON_SITE')
+
+import os
+from os.path import abspath, dirname, join
+import sys
+
+# Redirect sys.stdout to sys.stderr for bad libraries like geopy that use
+# print statements for optional import exceptions.
+sys.stdout = sys.stderr
+
+# Add apps and lib directories to PYTHONPATH
+sys.path = [
+    '$PROJECT_ROOT',
+       '$PROJECT_ROOT/lib',
+       '$PROJECT_ROOT/apps',
+] + sys.path
+
+# Run Django
+os.environ['DJANGO_SETTINGS_MODULE'] = '$PROJECT_NAME.settings'
+
+from django.core.handlers.wsgi import WSGIHandler
+application = WSGIHandler()