205 lines
6.8 KiB
Python
205 lines
6.8 KiB
Python
# w.c.s. - web application for online forms
|
|
# Copyright (C) 2005-2010 Entr'ouvert
|
|
#
|
|
# 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
import cPickle
|
|
import os
|
|
import sys
|
|
import zipfile
|
|
|
|
from Defaults import *
|
|
|
|
try:
|
|
from wcs_cfg import *
|
|
except ImportError:
|
|
pass
|
|
|
|
import qommon
|
|
|
|
from qommon.publisher import get_publisher_class, set_publisher_class, QommonPublisher, get_cfg
|
|
|
|
# this is terribly ugly but import RootDirectory will import a bunch of things,
|
|
# and some of them need a publisher to be set
|
|
class StubWcsPublisher(QommonPublisher):
|
|
pass
|
|
set_publisher_class(StubWcsPublisher)
|
|
|
|
|
|
from root import RootDirectory
|
|
from backoffice import RootDirectory as BackofficeRootDirectory
|
|
from admin import RootDirectory as AdminRootDirectory
|
|
import sessions
|
|
|
|
from users import User
|
|
|
|
import pickle
|
|
|
|
class UnpicklerClass(pickle.Unpickler):
|
|
def find_class(self, module, name):
|
|
if module == 'wcs.form':
|
|
module = 'qommon.form'
|
|
__import__(module)
|
|
mod = sys.modules[module]
|
|
klass = getattr(mod, name)
|
|
return klass
|
|
|
|
|
|
class WcsPublisher(StubWcsPublisher):
|
|
APP_NAME = 'wcs'
|
|
APP_DIR = APP_DIR
|
|
DATA_DIR = DATA_DIR
|
|
ERROR_LOG = ERROR_LOG
|
|
USE_LONG_TRACES = USE_LONG_TRACES
|
|
auto_create_appdir = AUTO_CREATE_VHOSTS
|
|
missing_appdir_redirect = REDIRECT_ON_UNKNOWN_VHOST
|
|
|
|
admin_help_url = {
|
|
'fr': 'http://doc.entrouvert.org/wcs/dev/',
|
|
}
|
|
|
|
supported_languages = ['fr', 'es']
|
|
|
|
root_directory_class = RootDirectory
|
|
backoffice_directory_class = BackofficeRootDirectory
|
|
admin_directory_class = AdminRootDirectory
|
|
|
|
session_manager_class = sessions.StorageSessionManager
|
|
user_class = User
|
|
unpickler_class = UnpicklerClass
|
|
|
|
def get_backoffice_module(cls):
|
|
import backoffice
|
|
return backoffice
|
|
get_backoffice_module = classmethod(get_backoffice_module)
|
|
|
|
def get_admin_module(cls):
|
|
import admin
|
|
return admin
|
|
get_admin_module = classmethod(get_admin_module)
|
|
|
|
def configure(cls, config, extra=[]):
|
|
for directory in extra:
|
|
cls.register_extra_dir(directory)
|
|
|
|
if config.has_option("main", "app_dir"):
|
|
cls.APP_DIR = config.get("main", "app_dir")
|
|
if config.has_option("main", "data_dir"):
|
|
cls.DATA_DIR = config.get("main", "data_dir")
|
|
if config.has_option("main", "error_log"):
|
|
cls.ERROR_LOG = config.get("main", "error_log")
|
|
if config.has_option("main", "use_long_traces"):
|
|
cls.USE_LONG_TRACES = config.getboolean("main",
|
|
"use_long_traces")
|
|
if config.has_option("main", "auto_create_appdir"):
|
|
cls.auto_create_appdir = config.getboolean("main",
|
|
"auto_create_appdir")
|
|
if config.has_option("main", "missing_appdir_redirect"):
|
|
cls.missing_appdir_redirect = config.get("main",
|
|
"missing_appdir_redirect")
|
|
configure = classmethod(configure)
|
|
|
|
def set_config(self, request = None):
|
|
QommonPublisher.set_config(self, request = request)
|
|
filename = os.path.join(self.app_dir, 'config.pck')
|
|
if os.path.exists(filename):
|
|
# a configuration file exists, it may not have a charset
|
|
# specified so we default to iso-8859-15 as it was the only
|
|
# possible value
|
|
self.site_charset = self.cfg.get('misc', {}).get('charset', 'iso-8859-15')
|
|
else:
|
|
# new site, set the charset, and write it down for further
|
|
# requests
|
|
self.site_charset = 'utf-8'
|
|
self.cfg['misc'] = {'charset': 'utf-8'}
|
|
self.write_cfg()
|
|
if request:
|
|
request.response.charset = self.site_charset
|
|
|
|
import wcs.workflows
|
|
wcs.workflows.load_extra()
|
|
|
|
if self.has_site_option('postgresql') and get_cfg('postgresql', {}):
|
|
import sql
|
|
self.user_class = sql.SqlUser
|
|
sql.get_connection(new=True)
|
|
else:
|
|
self.user_class = User
|
|
|
|
def import_zip(self, fd):
|
|
z = zipfile.ZipFile(fd)
|
|
results = {'formdefs': 0, 'workflows': 0, 'categories': 0, 'roles': 0, 'settings': 0}
|
|
for f in z.namelist():
|
|
path = os.path.join(self.app_dir, f)
|
|
data = z.read(f)
|
|
if not os.path.exists(os.path.dirname(path)):
|
|
os.mkdir(os.path.dirname(path))
|
|
if f == 'config.pck':
|
|
results['settings'] = 1
|
|
d = cPickle.loads(data)
|
|
if self.cfg.has_key('sp'):
|
|
current_sp = self.cfg['sp']
|
|
else:
|
|
current_sp = None
|
|
self.cfg = d
|
|
if current_sp:
|
|
self.cfg['sp'] = current_sp
|
|
elif self.cfg.has_key('sp'):
|
|
del self.cfg['sp']
|
|
self.write_cfg()
|
|
continue
|
|
open(path, 'w').write(data)
|
|
if results.has_key(os.path.split(f)[0]):
|
|
results[os.path.split(f)[0]] += 1
|
|
|
|
# rebuild indexes for imported objects
|
|
for k, v in results.items():
|
|
if k == 'settings':
|
|
continue
|
|
if v == 0:
|
|
continue
|
|
klass = None
|
|
if k == 'formdefs':
|
|
from formdef import FormDef
|
|
klass = FormDef
|
|
elif k == 'categories':
|
|
from categories import Category
|
|
klass = Category
|
|
elif k == 'roles':
|
|
from roles import Role
|
|
klass = Role
|
|
elif k == 'workflows':
|
|
from wcs.workflows import Workflow
|
|
klass = Workflow
|
|
if klass:
|
|
klass.rebuild_indexes()
|
|
|
|
if k ==' formdefs':
|
|
# in case of formdefs, we store them anew in case SQL changes
|
|
# are required.
|
|
for formdef in FormDef.select():
|
|
formdef.store()
|
|
|
|
z.close()
|
|
return results
|
|
|
|
def try_publish(self, request):
|
|
if request.get_header('X_WCS_IFRAME_MODE', '') in ('true', 'yes'):
|
|
request.response.iframe_mode = True
|
|
return QommonPublisher.try_publish(self, request)
|
|
|
|
set_publisher_class(WcsPublisher)
|
|
WcsPublisher.register_extra_dir(os.path.join(os.path.dirname(__file__), 'extra'))
|
|
|