Implement flexible configurations

This commit is contained in:
RunasSudo 2017-11-25 22:37:20 +11:00
parent 11e6c4a7aa
commit bf1b8cee09
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
6 changed files with 65 additions and 11 deletions

1
.gitignore vendored
View File

@ -7,6 +7,7 @@ __pycache__
refs
bower_components
eosjs.js
local_settings.py
\#*
.#*

View File

@ -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())

View File

@ -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

View File

@ -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)

View File

@ -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()

28
eosweb/core/settings.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
BASE_URI = 'http://localhost:5000'
MONGO_URI = 'mongodb://localhost:27017/'
DB_NAME = 'eos'
APPS = [
'eosweb.redditauth'
]
AUTH_METHODS = []
SECRET_KEY = 'FIXME'