From 5f05450ea4b87928e647e05fba46a3fa1f79f22d Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Mon, 27 Nov 2017 20:12:31 +1100 Subject: [PATCH] Implement PostgreSQL "support". Closes #3 Good god what have I done --- eos/core/db/postgresql.py | 51 ++++++++++++++++++++++++++++++++++++ eos/core/objects/__init__.py | 1 + local_settings.example.py | 6 +++++ requirements.txt | 9 +++++++ 4 files changed, 67 insertions(+) create mode 100644 eos/core/db/postgresql.py diff --git a/eos/core/db/postgresql.py b/eos/core/db/postgresql.py new file mode 100644 index 0000000..1512c81 --- /dev/null +++ b/eos/core/db/postgresql.py @@ -0,0 +1,51 @@ +# 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 . + +import psycopg2 +from psycopg2.sql import SQL, Identifier +import psycopg2.extras + +import eos.core.db + +class PostgreSQLDBProvider(eos.core.db.DBProvider): + def connect(self): + self.conn = psycopg2.connect(self.db_uri, dbname=self.db_name) + self.cur = self.conn.cursor() + + def create_table(self, table): + self.cur.execute(SQL('CREATE TABLE IF NOT EXISTS {} (_id uuid NOT NULL, data json, PRIMARY KEY (_id))').format(Identifier(table))) + self.conn.commit() + + def get_all(self, table): + self.create_table(table) + self.cur.execute(SQL('SELECT data FROM {}').format(Identifier(table))) + return [x[0] for x in self.cur.fetchall()] + + def get_by_id(self, table, _id): + self.create_table(table) + self.cur.execute(SQL('SELECT data FROM {} WHERE _id = %s').format(Identifier(table)), (_id,)) + return self.cur.fetchone()[0] + + def update_by_id(self, table, _id, value): + self.create_table(table) + self.cur.execute(SQL('INSERT INTO {} (_id, data) VALUES (%s, %s) ON CONFLICT (_id) DO UPDATE SET data = excluded.data').format(Identifier(table)), (_id, psycopg2.extras.Json(value))) + self.conn.commit() + + def reset_db(self): + self.cur.execute('DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public') + self.conn.commit() + +eos.core.db.db_providers['postgresql'] = PostgreSQLDBProvider diff --git a/eos/core/objects/__init__.py b/eos/core/objects/__init__.py index f847dec..d60b101 100644 --- a/eos/core/objects/__init__.py +++ b/eos/core/objects/__init__.py @@ -29,6 +29,7 @@ if is_python: __pragma__('skip') import eos.core.db import eos.core.db.mongodb + import eos.core.db.postgresql from bson.binary import UUIDLegacy diff --git a/local_settings.example.py b/local_settings.example.py index 3405a85..4b14d19 100644 --- a/local_settings.example.py +++ b/local_settings.example.py @@ -13,6 +13,12 @@ DB_TYPE = 'mongodb' DB_URI = 'mongodb://localhost:27017/' DB_NAME = 'eos' +# PostgreSQL + +#DB_TYPE = 'postgresql' +#DB_URI = 'postgresql://' +#DB_NAME = 'eos' + # Email SMTP_HOST, SMTP_PORT = 'localhost', 25 diff --git a/requirements.txt b/requirements.txt index 643bc53..6810fe8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,13 +1,22 @@ +certifi==2017.11.5 +chardet==3.0.4 click==6.7 coverage==4.4.1 Flask==0.12.2 +Flask-OAuthlib==0.9.4 +idna==2.6 itsdangerous==0.24 Jinja2==2.10 MarkupSafe==1.0 mypy==0.521 +oauthlib==2.0.6 +psycopg2==2.7.3.2 PyExecJS==1.4.1 pymongo==3.5.1 +requests==2.18.4 +requests-oauthlib==0.8.0 six==1.10.0 Transcrypt==3.6.50 typed-ast==1.0.4 +urllib3==1.22 Werkzeug==0.12.2