diff --git a/.gitignore b/.gitignore index 73bec35..13052cc 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ __pycache__ refs bower_components eosjs.js +local_settings.py \#* .#* diff --git a/eos/base/tests.py b/eos/base/tests.py index bd35477..2c6be96 100644 --- a/eos/base/tests.py +++ b/eos/base/tests.py @@ -23,7 +23,8 @@ from eos.core.objects import * class ElectionTestCase(EosTestCase): @classmethod def setUpClass(cls): - client.drop_database('test') + db_connect('test') + dbinfo.client.drop_database('test') def do_task_assert(self, election, task, next_task): self.assertEqual(election.workflow.get_task(task).status, WorkflowTask.Status.READY) @@ -65,8 +66,8 @@ class ElectionTestCase(EosTestCase): election.save() # Check that it saved - self.assertEqual(db[Election._db_name].find_one()['value'], election.serialise()) - self.assertEqual(EosObject.deserialise_and_unwrap(db[Election._db_name].find_one()).serialise(), election.serialise()) + self.assertEqual(dbinfo.db[Election._db_name].find_one()['value'], election.serialise()) + self.assertEqual(EosObject.deserialise_and_unwrap(dbinfo.db[Election._db_name].find_one()).serialise(), election.serialise()) self.assertEqualJSON(EosObject.deserialise_and_unwrap(EosObject.serialise_and_wrap(election)).serialise(), election.serialise()) diff --git a/eos/core/objects/__init__.py b/eos/core/objects/__init__.py index 077b290..6f18e1c 100644 --- a/eos/core/objects/__init__.py +++ b/eos/core/objects/__init__.py @@ -49,9 +49,16 @@ else: # Database # ======== -if is_python: - client = pymongo.MongoClient() - db = client['test'] +class DBInfo: + def __init__(self): + self.client = None + self.db = None + +dbinfo = DBInfo() + +def db_connect(db_name, mongo_uri='mongodb://localhost:27017/'): + dbinfo.client = pymongo.MongoClient(mongo_uri) + dbinfo.db = dbinfo.client[db_name] # Fields # ====== @@ -369,15 +376,15 @@ class DocumentObject(EosObject, metaclass=DocumentObjectType): class TopLevelObject(DocumentObject): def save(self): #res = db[self._name].replace_one({'_id': self.serialise()['_id']}, self.serialise(), upsert=True) - res = db[self._db_name].replace_one({'_id': self._fields['_id'].serialise(self._id)}, EosObject.serialise_and_wrap(self), upsert=True) + res = dbinfo.db[self._db_name].replace_one({'_id': self._fields['_id'].serialise(self._id)}, EosObject.serialise_and_wrap(self), upsert=True) @classmethod def get_all(cls): - return [EosObject.deserialise_and_unwrap(x) for x in db[cls._db_name].find()] + return [EosObject.deserialise_and_unwrap(x) for x in dbinfo.db[cls._db_name].find()] @classmethod def get_by_id(cls, _id): - return EosObject.deserialise_and_unwrap(db[cls._db_name].find_one(_id)) + return EosObject.deserialise_and_unwrap(dbinfo.db[cls._db_name].find_one(_id)) class EmbeddedObject(DocumentObject): pass diff --git a/eos/psr/tests.py b/eos/psr/tests.py index 3e69941..0718012 100644 --- a/eos/psr/tests.py +++ b/eos/psr/tests.py @@ -212,7 +212,8 @@ class MixnetTestCase(EosTestCase): class ElectionTestCase(EosTestCase): @classmethod def setUpClass(cls): - client.drop_database('test') + db_connect('test') + dbinfo.client.drop_database('test') def do_task_assert(self, election, task, next_task): self.assertEqual(election.workflow.get_task(task).status, WorkflowTask.Status.READY) diff --git a/eosweb/core/main.py b/eosweb/core/main.py index b08bdfa..0b1dafd 100644 --- a/eosweb/core/main.py +++ b/eosweb/core/main.py @@ -30,10 +30,26 @@ import eosweb from datetime import datetime import functools +import importlib import json +import os app = flask.Flask(__name__) +# Load config +app.config.from_object('eosweb.core.settings') +if 'EOSWEB_SETTINGS' in os.environ: + app.config.from_envvar('EOSWEB_SETTINGS') + +# Load app config +for app_name in app.config['APPS']: + app.config.from_object(app_name + '.settings') +if 'EOSWEB_SETTINGS' in os.environ: + app.config.from_envvar('EOSWEB_SETTINGS') + +# Connect to database +db_connect(app.config['DB_NAME'], app.config['MONGO_URI']) + @app.cli.command('test') @click.option('--prefix', default=None) @click.option('--lang', default=None) @@ -45,7 +61,7 @@ def run_tests(prefix, lang): @app.cli.command('drop_db_and_setup') def setup_test_election(): # DANGER! - client.drop_database('test') + dbinfo.client.drop_database(app.config['DB_NAME']) # Set up election election = PSRElection() diff --git a/eosweb/core/settings.py b/eosweb/core/settings.py new file mode 100644 index 0000000..112752e --- /dev/null +++ b/eosweb/core/settings.py @@ -0,0 +1,28 @@ +# Eos - Verifiable elections +# Copyright © 2017 RunasSudo (Yingtong Li) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +BASE_URI = 'http://localhost:5000' + +MONGO_URI = 'mongodb://localhost:27017/' +DB_NAME = 'eos' + +APPS = [ + 'eosweb.redditauth' +] + +AUTH_METHODS = [] + +SECRET_KEY = 'FIXME'