Implement PostgreSQL "support". Closes #3

Good god what have I done
This commit is contained in:
RunasSudo 2017-11-27 20:12:31 +11:00
parent 72e08c01ff
commit 5f05450ea4
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 67 additions and 0 deletions

51
eos/core/db/postgresql.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
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

View File

@ -29,6 +29,7 @@ if is_python:
__pragma__('skip') __pragma__('skip')
import eos.core.db import eos.core.db
import eos.core.db.mongodb import eos.core.db.mongodb
import eos.core.db.postgresql
from bson.binary import UUIDLegacy from bson.binary import UUIDLegacy

View File

@ -13,6 +13,12 @@ DB_TYPE = 'mongodb'
DB_URI = 'mongodb://localhost:27017/' DB_URI = 'mongodb://localhost:27017/'
DB_NAME = 'eos' DB_NAME = 'eos'
# PostgreSQL
#DB_TYPE = 'postgresql'
#DB_URI = 'postgresql://'
#DB_NAME = 'eos'
# Email # Email
SMTP_HOST, SMTP_PORT = 'localhost', 25 SMTP_HOST, SMTP_PORT = 'localhost', 25

View File

@ -1,13 +1,22 @@
certifi==2017.11.5
chardet==3.0.4
click==6.7 click==6.7
coverage==4.4.1 coverage==4.4.1
Flask==0.12.2 Flask==0.12.2
Flask-OAuthlib==0.9.4
idna==2.6
itsdangerous==0.24 itsdangerous==0.24
Jinja2==2.10 Jinja2==2.10
MarkupSafe==1.0 MarkupSafe==1.0
mypy==0.521 mypy==0.521
oauthlib==2.0.6
psycopg2==2.7.3.2
PyExecJS==1.4.1 PyExecJS==1.4.1
pymongo==3.5.1 pymongo==3.5.1
requests==2.18.4
requests-oauthlib==0.8.0
six==1.10.0 six==1.10.0
Transcrypt==3.6.50 Transcrypt==3.6.50
typed-ast==1.0.4 typed-ast==1.0.4
urllib3==1.22
Werkzeug==0.12.2 Werkzeug==0.12.2