[library-web] Rewrote web application serving symbols in Django (GNOME bug 500378)



commit afe58e4ee8f488bd1fd974edd3426b6dd8d9fe7a
Author: Frédéric Péters <fpeters 0d be>
Date:   Fri Dec 3 16:51:31 2010 +0100

    Rewrote web application serving symbols in Django (GNOME bug 500378)

 web/README            |    8 +++
 web/__init__.py       |   18 ++++++
 web/api.py            |   60 ++++++++++++++++++++
 web/lgoweb.wsgi       |    8 +++
 web/manage.py         |   30 ++++++++++
 web/settings.py       |   98 +++++++++++++++++++++++++++++++++
 web/symbols_server.py |  145 -------------------------------------------------
 web/urls.py           |   25 +++++++++
 8 files changed, 247 insertions(+), 145 deletions(-)
---
diff --git a/web/README b/web/README
new file mode 100644
index 0000000..2922fa2
--- /dev/null
+++ b/web/README
@@ -0,0 +1,8 @@
+This web application should typically be run as WSGI, with such a line in the
+<VirtualHost> definition:
+
+  WSGIScriptAlias /symbols /path/to/library-web/lgoweb/lgoweb.wsgi
+
+It needs to have LIBRARY_WEB_CONFIG_FILE defined in its settings.py file (well,
+local_settings.py is better), pointing to an existing library-web configuration
+file.
diff --git a/web/__init__.py b/web/__init__.py
new file mode 100644
index 0000000..dcdab2f
--- /dev/null
+++ b/web/__init__.py
@@ -0,0 +1,18 @@
+# libgo - script to build library.gnome.org
+# Copyright (C) 2007-2010  Frederic Peters
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301  USA
+
diff --git a/web/api.py b/web/api.py
new file mode 100644
index 0000000..84a2a4b
--- /dev/null
+++ b/web/api.py
@@ -0,0 +1,60 @@
+# libgo - script to build library.gnome.org
+# Copyright (C) 2007-2010  Frederic Peters
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301  USA
+
+import os
+import sys
+from pysqlite2 import dbapi2 as sqlite
+sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'src'))
+
+from django.conf import settings
+from config import Config
+
+from django.http import HttpResponse, HttpResponseRedirect
+
+sqlcon = None
+
+def get_sqlconn():
+    global sqlcon
+    if sqlcon:
+        return sqlcon
+    config = Config(filename=settings.LIBRARY_WEB_CONFIG_FILE)
+    sqlcon = sqlite.connect(config.symbols_sqlite_filepath, isolation_level=None)
+    return sqlcon
+
+
+def lookup(request):
+    sqlcon = get_sqlconn()
+    symbol = request.path_info.split('/')[-1]
+    cur = sqlcon.cursor()
+    cur.execute('select symbol from symbols where symbol like ? escape "\\" '
+                'order by symbol limit 50',
+                ('%' + symbol.replace('\\', '\\\\').replace('_', '\\_') + '%',))
+    response = '\n'.join([x[0] for x in cur.fetchall()])
+    cur.close()
+    return HttpResponse(response, mimetype="text/plain")
+
+def redirect(request):
+    symbol = request.REQUEST['q']
+    sqlcon = get_sqlconn()
+    cur = sqlcon.cursor()
+    cur.execute('select path from symbols where symbol = ? limit 1', (symbol,))
+    path = cur.fetchone()
+    if path:
+        path = path[0]
+        return HttpResponseRedirect(path)
+    return HttpResponse('Not found')
diff --git a/web/lgoweb.wsgi b/web/lgoweb.wsgi
new file mode 100644
index 0000000..8e393ad
--- /dev/null
+++ b/web/lgoweb.wsgi
@@ -0,0 +1,8 @@
+import os
+import sys
+sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
+sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '.'))
+os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
+import django.core.handlers.wsgi
+application = django.core.handlers.wsgi.WSGIHandler()
+
diff --git a/web/manage.py b/web/manage.py
new file mode 100755
index 0000000..1498601
--- /dev/null
+++ b/web/manage.py
@@ -0,0 +1,30 @@
+#!/usr/bin/python
+
+# libgo - script to build library.gnome.org
+# Copyright (C) 2007-2010  Frederic Peters
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301  USA
+
+from django.core.management import execute_manager
+try:
+    import settings # Assumed to be in the same directory.
+except ImportError:
+    import sys
+    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
+    sys.exit(1)
+
+if __name__ == "__main__":
+    execute_manager(settings)
diff --git a/web/settings.py b/web/settings.py
new file mode 100644
index 0000000..a80e41d
--- /dev/null
+++ b/web/settings.py
@@ -0,0 +1,98 @@
+# libgo - script to build library.gnome.org
+# Copyright (C) 2007-2010  Frederic Peters
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301  USA
+
+# Django settings for lgoweb project.
+
+DEBUG = False
+TEMPLATE_DEBUG = DEBUG
+
+ADMINS = (
+)
+
+MANAGERS = ADMINS
+
+DATABASES = {
+}
+
+# Local time zone for this installation. Choices can be found here:
+# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
+# although not all choices may be available on all operating systems.
+# On Unix systems, a value of None will cause Django to use the same
+# timezone as the operating system.
+# If running in a Windows environment this must be set to the same as your
+# system time zone.
+TIME_ZONE = 'Europe/Berlin'
+
+# Language code for this installation. All choices can be found here:
+# http://www.i18nguy.com/unicode/language-identifiers.html
+LANGUAGE_CODE = 'en-us'
+
+SITE_ID = 1
+
+# If you set this to False, Django will make some optimizations so as not
+# to load the internationalization machinery.
+USE_I18N = False
+
+# If you set this to False, Django will not format dates, numbers and
+# calendars according to the current locale
+USE_L10N = False
+
+# Absolute path to the directory that holds media.
+# Example: "/home/media/media.lawrence.com/"
+MEDIA_ROOT = ''
+
+# URL that handles the media served from MEDIA_ROOT. Make sure to use a
+# trailing slash if there is a path component (optional in other cases).
+# Examples: "http://media.lawrence.com";, "http://example.com/media/";
+MEDIA_URL = ''
+
+# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
+# trailing slash.
+# Examples: "http://foo.com/media/";, "/media/".
+ADMIN_MEDIA_PREFIX = '/media/'
+
+# Make this unique, and don't share it with anybody.
+SECRET_KEY = '#bex#%rx4x*yd*ek4=g0_c(vg#hbsyt*=zd)fk&gv-w308$z=7'
+
+# List of callables that know how to import templates from various sources.
+TEMPLATE_LOADERS = (
+)
+
+MIDDLEWARE_CLASSES = (
+)
+
+ROOT_URLCONF = 'urls'
+
+TEMPLATE_DIRS = (
+    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
+    # Always use forward slashes, even on Windows.
+    # Don't forget to use absolute paths, not relative paths.
+)
+
+INSTALLED_APPS = (
+)
+
+LIBRARY_WEB_CONFIG_FILE = None
+
+try:
+    from local_settings import *
+except ImportError:
+    pass
+
+if LIBRARY_WEB_CONFIG_FILE is None:
+    raise Exception('Missing definition of LIBRARY_WEB_CONFIG_FILE')
diff --git a/web/urls.py b/web/urls.py
new file mode 100644
index 0000000..1c1638e
--- /dev/null
+++ b/web/urls.py
@@ -0,0 +1,25 @@
+# libgo - script to build library.gnome.org
+# Copyright (C) 2007-2010  Frederic Peters
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301  USA
+
+from django.conf.urls.defaults import *
+import api
+
+urlpatterns = patterns('',
+    url(r'^lookup', api.lookup),
+    url(r'^', api.redirect),
+)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]