diff --git a/.gitignore b/.gitignore index f717486..718bfbe 100644 --- a/.gitignore +++ b/.gitignore @@ -81,9 +81,6 @@ target/ profile_default/ ipython_config.py -# pyenv -.python-version - # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # However, in case of collaboration, if having platform-specific dependencies or dependencies diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..f280719 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.8.1 diff --git a/README.md b/README.md index b395080..408e314 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ # viveros Prototipo de proyecto viveros para la Feria de la Transparencia 2019. + +# Getting Started + +## Pre-requisites +- ```MongoDB```:leaves: - [Installation guide](https://docs.mongodb.com/manual/installation/) +- ```pyenv``` [It is optional, but would improve your development experience](https://github.com/pyenv/pyenv) +- ```Python 3.8.1```:snake: + +## Installation +```pip install -r requirements.txt``` + +## Running +```python app.py``` diff --git a/app.py b/app.py index 353ec2f..7818ff1 100644 --- a/app.py +++ b/app.py @@ -1,10 +1,14 @@ #!/usr/bin/python # -*- coding: utf-8 -*- +from dotenv import load_dotenv +load_dotenv() from flask import Flask from flask import render_template import qrcode +from gsheet_utils import GSheet + from io import BytesIO import base64 @@ -16,10 +20,12 @@ app = Flask(__name__) donations = db_connect_to_collection(MONGO_URI, 'viveros', 'donaciones') +gsheet = GSheet() @app.route('/') def index(): - return render_template('index.html') + total_donations = gsheet.get_data("'donaciones 2020'!I1")[0][0] + return render_template('index.html', total_donations=f"{int(total_donations):,}") @app.route('/dashboard') diff --git a/dbutils.py b/dbutils.py index c9bdea2..b94a3c7 100644 --- a/dbutils.py +++ b/dbutils.py @@ -2,10 +2,11 @@ # -*- coding: utf-8 -*- from pymongo import MongoClient +import os # import pandas as pd # Access to MongoDB remote cluster: -MONGO_URI = "" +MONGO_URI = os.getenv("MONGO_URI") def db_connect_to_collection(MONGO_URI, database, collection): diff --git a/gsheet_utils.py b/gsheet_utils.py new file mode 100644 index 0000000..2b00ab9 --- /dev/null +++ b/gsheet_utils.py @@ -0,0 +1,43 @@ +import json +import googleapiclient.discovery +from google.oauth2 import service_account +import os + + +GOOGLE_SPREADSHEET_ID = os.getenv("GOOGLE_SPREADSHEET_ID") + +class GSheet: + google_credentials_json = os.getenv("GOOGLE_CREDENTIALS_JSON") + gkeys = json.loads(google_credentials_json) + + def __init__(self): + self.service = self.__get_service() + + def __get_credentials(self): + scopes = ["https://www.googleapis.com/auth/spreadsheets.readonly"] + GOOGLE_PRIVATE_KEY = self.gkeys["private_key"] + # The environment variable has escaped newlines, so remove the extra backslash + GOOGLE_PRIVATE_KEY = GOOGLE_PRIVATE_KEY.replace('\\n', '\n') + + account_info = { + "private_key": GOOGLE_PRIVATE_KEY, + "client_email": self.gkeys["client_email"], + "token_uri": "https://accounts.google.com/o/oauth2/token", + } + + credentials = service_account.Credentials.from_service_account_info(account_info, scopes=scopes) + return credentials + + def __get_service(self): + service_name='sheets' + api_version='v4' + credentials = self.__get_credentials() + service = googleapiclient.discovery.build(service_name, api_version, credentials=credentials) + return service + + def get_data(self, range_name): + result = self.service.spreadsheets().values().get( + spreadsheetId=GOOGLE_SPREADSHEET_ID, range=range_name + ).execute() + values = result.get('values', []) + return values diff --git a/requirements.txt b/requirements.txt index 19ee86e..67a5f41 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,8 @@ -Flask -pymongo[srv] -qrcode +flask +google-api-python-client +google-auth +google-auth-httplib2 gunicorn +pymongo +python-dotenv +qrcode diff --git a/start.sh b/start.sh old mode 100644 new mode 100755 diff --git a/templates/index.html b/templates/index.html index 076f5e8..8edf8fd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -74,7 +74,7 @@