pfwb: change app_settings implementation

This commit is contained in:
Benjamin Dauvergne 2014-01-30 15:17:33 +01:00
parent d2d22cf381
commit a4b67b5bf3
3 changed files with 51 additions and 27 deletions

View File

@ -2,15 +2,15 @@ import os.path
import json
import datetime
from app_settings import PFWB_GED_DIRECTORY
from ..docbow.models import AttachedFile
import models
from . import app_settings, models
def push_document(signal, sender, instance, **kwargs):
'''post-save signal handler, to push new documents inside the GED directory'''
attached_file = instance
document = attached_file.document
if not PFWB_GED_DIRECTORY:
if not app_settings.PFWB_GED_DIRECTORY:
return
try:
plone_file_type = document.filetype.plonefiletype
@ -31,8 +31,8 @@ def push_document(signal, sender, instance, **kwargs):
name = os.path.basename(attached_file.name)
name = '%s-%s-%s' % (document.id, datetime.datetime.now().isoformat(), name)
json_name = name + '.json'
path = os.path.join(PFWB_GED_DIRECTORY, name)
path_json = os.path.join(PFWB_GED_DIRECTORY, json_name)
path = os.path.join(app_settings.PFWB_GED_DIRECTORY, name)
path_json = os.path.join(app_settings.PFWB_GED_DIRECTORY, json_name)
with open(path, 'w') as f:
f.write(attached_file.content.read())
with open(path_json, 'w') as f:

View File

@ -1,21 +1,48 @@
from django.conf import settings
# -*- coding: utf-8 -*-
# directory where ged files are stored
PFWB_GED_DIRECTORY = getattr(settings, 'DOCBOW_PFWB_GED_DIRECTORY', None)
# default type id for documents received by SMTP when given type does not exist
PFWB_SENDMAIL_DEFAULT_TYPE_ID = getattr(settings, 'DOCBOW_PFWB_SENDMAIL_DEFAULT_TYPE_ID', None)
# default type name if default type id does not exist
PFWB_SENDMAIL_DEFAULT_TYPE_NAME = getattr(settings, 'DOCBOW_PFWB_SENDMAIL_DEFAULT_TYPE_NAME', 'Divers')
# sender email for document received from tabellio expedition by SMTP
PFWB_SENDMAIL_TABELLIO_EXPEDITION_EMAIL = getattr(settings,
'DOCBOW_PFWB_SENDMAIL_TABELLIO_EXPEDITION_EMAIL', 'commande.documents@pfwb.be')
# user id of senders for document received from tabellio expedition by SMTP
PFWB_SENDMAIL_TABELLIO_EXPEDITION_USER_ID = getattr(settings,
'DOCBOW_PFWB_SENDMAIL_TABELLIO_EXPEDITION_USER_ID', None)
# sender email for document received by SMTP (generic code)
PFWB_SENDMAIL_ATTACHED_FILE_EMAIL = getattr(settings,
'DOCBOW_PFWB_SENDMAIL_ATTACHED_FILE_EMAIL', None)
# user id of senders for document received by SMTP (generic code)
PFWB_SENDMAIL_ATTACHED_FILE_USER_ID = getattr(settings,
'DOCBOW_PFWB_SENDMAIL_ATTACHED_FILE_USER_ID', None)
class AppSettings(object):
'''Thanks django-allauth'''
__DEFAULTS = dict(
# directory where ged files are stored
PFWB_GED_DIRECTORY = None,
# default type id for documents received by SMTP when given type does
# not exist
PFWB_SENDMAIL_DEFAULT_TYPE_ID = None,
# default type name if default type id does not exist
PFWB_SENDMAIL_DEFAULT_TYPE_NAME = 'Divers',
# sender email for document received from tabellio expedition by SMTP
PFWB_SENDMAIL_TABELLIO_EXPEDITION_EMAIL = 'commande.documents@pfwb.be',
# user id of senders for document received from tabellio expedition by SMTP
PFWB_SENDMAIL_TABELLIO_EXPEDITION_USER_ID = None,
# sender email for document received by SMTP (generic code)
PFWB_SENDMAIL_ATTACHED_FILE_EMAIL = None,
# user id of senders for document received by SMTP (generic code)
PFWB_SENDMAIL_ATTACHED_FILE_USER_ID = None,
)
def __init__(self, prefix):
self.prefix = prefix
@property
def settings(self):
from django.conf import settings
return settings
def __getattr__(self, key):
if key in self.__DEFAULTS:
return getattr(self.settings,
self.prefix+key, self.__DEFAULTS[key])
else:
from django.core.exceptions import ImproperlyConfigured
try:
return getattr(self.settings, self.prefix+key)
except AttributeError:
raise ImproperlyConfigured('settings %s is missing' % self.prefix+key)
app_settings = AppSettings('DOCBOW_')
app_settings.__name__ = __name__
app_settings.__file__ = __file__
import sys
sys.modules[__name__] = app_settings

View File

@ -53,9 +53,6 @@ RECIPIENT_LIST_EMAIL = 'liste-ma-liste@example.com'
@override_settings(PFWB_GED_DIRECTORY='/tmp')
class SendMailTestCase(TestCase):
def setUp(self):
import app_settings
reload(app_settings)
self.pjd_filetype = FileType.objects.create(name='PJD', id=2)
self.tabellio_doc_type = TabellioDocType.objects.create(filetype=self.pjd_filetype,
tabellio_doc_type='PJD')