From 2b26d94dd3646d9d9322c7448e80152629113e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <22278039+afk@users.noreply.github.com> Date: Wed, 13 Mar 2019 15:07:43 +0300 Subject: [PATCH 1/3] voting system init --- server_side/voting-system/__init__.py | 21 +++++++++++++++++++++ server_side/voting-system/db.py | 18 ++++++++++++++++++ server_side/voting-system/schema.sql | 9 +++++++++ 3 files changed, 48 insertions(+) create mode 100644 server_side/voting-system/__init__.py create mode 100644 server_side/voting-system/db.py create mode 100644 server_side/voting-system/schema.sql diff --git a/server_side/voting-system/__init__.py b/server_side/voting-system/__init__.py new file mode 100644 index 0000000..64a9132 --- /dev/null +++ b/server_side/voting-system/__init__.py @@ -0,0 +1,21 @@ +import os +from flask import Flask + +def create_app(): + app = Flask(__name__) + app.config.from_mapping( + SECRET_KEY='dev', + DATABASE=os.path.join(app.instance_path, 'voting-system.sqlite') + ) + + app.config.from_pyfile('config.py', silent=True) + try: + os.makedirs(app.instance_path) + except OSError: + pass # Already exists + + @app.route('/votings') + def hello(): + return 'Hello, world!' + + return app \ No newline at end of file diff --git a/server_side/voting-system/db.py b/server_side/voting-system/db.py new file mode 100644 index 0000000..b54b0af --- /dev/null +++ b/server_side/voting-system/db.py @@ -0,0 +1,18 @@ +import sqlite3 + +import click +from flask import current_app, g +from flask.cli import with_appcontext + +def get_db(): + if 'db' not in g: + g.db = sqlite3.connect(current_app.config['DATABASE']) + g.db.row_factory = sqlite3.Row + + return g.db + +def close_db() + db = g.pop('db', None) + + if db is not None: + db.close() \ No newline at end of file diff --git a/server_side/voting-system/schema.sql b/server_side/voting-system/schema.sql new file mode 100644 index 0000000..2c9e7b6 --- /dev/null +++ b/server_side/voting-system/schema.sql @@ -0,0 +1,9 @@ +DROP TABLE IF EXISTS votings; + +CREATE TABLE votings ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + desc TEXT, + img TEXT, + votes +) \ No newline at end of file From 049504cb5d554d7169f49cca10bb40d8659d1c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Wed, 13 Mar 2019 18:26:09 +0300 Subject: [PATCH 2/3] voting system api --- server_side/api/requirements.txt | 12 ++++++++ server_side/api/voting_system/app.py | 43 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 server_side/api/requirements.txt create mode 100644 server_side/api/voting_system/app.py diff --git a/server_side/api/requirements.txt b/server_side/api/requirements.txt new file mode 100644 index 0000000..2bbbdf0 --- /dev/null +++ b/server_side/api/requirements.txt @@ -0,0 +1,12 @@ +aniso8601==6.0.0 +Click==7.0 +dnspython==1.16.0 +Flask==1.0.2 +Flask-RESTful==0.3.7 +itsdangerous==1.1.0 +Jinja2==2.10 +MarkupSafe==1.1.1 +pymongo==3.7.2 +pytz==2018.9 +six==1.12.0 +Werkzeug==0.14.1 diff --git a/server_side/api/voting_system/app.py b/server_side/api/voting_system/app.py new file mode 100644 index 0000000..b5fc631 --- /dev/null +++ b/server_side/api/voting_system/app.py @@ -0,0 +1,43 @@ +import ssl + +from flask import Flask +from flask_restful import Resource, Api, reqparse, abort + +from pymongo import MongoClient +from bson.objectid import ObjectId + +app = Flask(__name__) +api = Api(app) + +client = MongoClient("mongodb+srv://mycity:mycity123@mycity-3v9y3.mongodb.net/test?retryWrites=true", ssl_cert_reqs=ssl.CERT_NONE) +db = client.voting_system +collection = db.votings + +class Votings(Resource): + def get(self): + votings = [ + { + 'id' : str(doc['_id']), + 'name': doc['name'], + 'desc': doc['desc'], + 'img' : doc['img'] + } + for doc in collection.find({}) + ] + return votings + +class Vote(Resource): + def get(self, voting_id): + try: + doc = collection.find_one({'_id': ObjectId(voting_id)}) + doc['_id'] = str(doc['_id']) + return doc + except: + abort(404, error="Voting {} doesn't exist".format(voting_id)) + + +api.add_resource(Votings, '/votings') +api.add_resource(Vote, '/votings/') + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file From c93f9930ab66a39461a1a58990b6155261597304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Wed, 13 Mar 2019 18:28:09 +0300 Subject: [PATCH 3/3] remove previous voting system --- server_side/voting-system/__init__.py | 21 --------------------- server_side/voting-system/db.py | 18 ------------------ server_side/voting-system/schema.sql | 9 --------- 3 files changed, 48 deletions(-) delete mode 100644 server_side/voting-system/__init__.py delete mode 100644 server_side/voting-system/db.py delete mode 100644 server_side/voting-system/schema.sql diff --git a/server_side/voting-system/__init__.py b/server_side/voting-system/__init__.py deleted file mode 100644 index 64a9132..0000000 --- a/server_side/voting-system/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -import os -from flask import Flask - -def create_app(): - app = Flask(__name__) - app.config.from_mapping( - SECRET_KEY='dev', - DATABASE=os.path.join(app.instance_path, 'voting-system.sqlite') - ) - - app.config.from_pyfile('config.py', silent=True) - try: - os.makedirs(app.instance_path) - except OSError: - pass # Already exists - - @app.route('/votings') - def hello(): - return 'Hello, world!' - - return app \ No newline at end of file diff --git a/server_side/voting-system/db.py b/server_side/voting-system/db.py deleted file mode 100644 index b54b0af..0000000 --- a/server_side/voting-system/db.py +++ /dev/null @@ -1,18 +0,0 @@ -import sqlite3 - -import click -from flask import current_app, g -from flask.cli import with_appcontext - -def get_db(): - if 'db' not in g: - g.db = sqlite3.connect(current_app.config['DATABASE']) - g.db.row_factory = sqlite3.Row - - return g.db - -def close_db() - db = g.pop('db', None) - - if db is not None: - db.close() \ No newline at end of file diff --git a/server_side/voting-system/schema.sql b/server_side/voting-system/schema.sql deleted file mode 100644 index 2c9e7b6..0000000 --- a/server_side/voting-system/schema.sql +++ /dev/null @@ -1,9 +0,0 @@ -DROP TABLE IF EXISTS votings; - -CREATE TABLE votings ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, - desc TEXT, - img TEXT, - votes -) \ No newline at end of file