From 959584d988257f893789c830d780fa1653f78262 Mon Sep 17 00:00:00 2001 From: Eduardo Hernandez Date: Tue, 21 Apr 2020 20:51:42 -0500 Subject: [PATCH 1/9] Specify python version Is a good practice specify the python version when you start working with more people in a project. With this, we can ensure that all of us are using the same python version, and avoid confusion with the development process. --- .gitignore | 3 --- .python-version | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) create mode 100644 .python-version 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 From 99842995433bcc345a85081fd4fc925578ef4a45 Mon Sep 17 00:00:00 2001 From: Eduardo Hernandez Date: Tue, 21 Apr 2020 20:51:42 -0500 Subject: [PATCH 2/9] Update dependencies --- README.md | 5 +++++ requirements.txt | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b395080..71e9180 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # viveros Prototipo de proyecto viveros para la Feria de la Transparencia 2019. + +## 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: diff --git a/requirements.txt b/requirements.txt index 19ee86e..807e6b1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -Flask -pymongo[srv] +flask +pymongo qrcode gunicorn From d6c644f42557a2e266937bf2ea366a55c8a36978 Mon Sep 17 00:00:00 2001 From: Eduardo Hernandez Date: Tue, 21 Apr 2020 20:51:42 -0500 Subject: [PATCH 3/9] README Updates This adds: - Pre-requisites - Installation instructions - Running instructions --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 71e9180..408e314 100644 --- a/README.md +++ b/README.md @@ -1,7 +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``` From fa3f9b25bb94c7a0bd411863a0fe6e2faacb1387 Mon Sep 17 00:00:00 2001 From: Eduardo Hernandez Date: Tue, 21 Apr 2020 20:51:42 -0500 Subject: [PATCH 4/9] Load dotenv in app.py At this point, parsed key/value from the .env file is now present as system environment variable and they can be conveniently accessed via os.getenv(): --- app.py | 2 ++ requirements.txt | 1 + 2 files changed, 3 insertions(+) diff --git a/app.py b/app.py index 353ec2f..1c8c040 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,7 @@ #!/usr/bin/python # -*- coding: utf-8 -*- +from dotenv import load_dotenv +load_dotenv() from flask import Flask from flask import render_template diff --git a/requirements.txt b/requirements.txt index 807e6b1..ece7dd8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ flask pymongo +python-dotenv qrcode gunicorn From e64447ff1b1dd9afd14abd1c3b8973a96eaa814e Mon Sep 17 00:00:00 2001 From: Eduardo Hernandez Date: Tue, 21 Apr 2020 20:51:42 -0500 Subject: [PATCH 5/9] Add dependencies to retrieve google sheets data --- requirements.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index ece7dd8..67a5f41 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,8 @@ flask +google-api-python-client +google-auth +google-auth-httplib2 +gunicorn pymongo python-dotenv qrcode -gunicorn From d52c0e374affe252adbac4cac49dd9bf59d02dd5 Mon Sep 17 00:00:00 2001 From: Eduardo Hernandez Date: Tue, 21 Apr 2020 20:51:42 -0500 Subject: [PATCH 6/9] GSheets wrapper This class help us to create a uniq auth instance with google api, and get the data only passing the the range. --- gsheet_utils.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 gsheet_utils.py 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 From dda78156bf79863520f78d08b0fea35bd966e518 Mon Sep 17 00:00:00 2001 From: Eduardo Hernandez Date: Tue, 21 Apr 2020 20:51:42 -0500 Subject: [PATCH 7/9] Show real donations amount in homepage --- app.py | 6 +++++- templates/index.html | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 1c8c040..7818ff1 100644 --- a/app.py +++ b/app.py @@ -7,6 +7,8 @@ from flask import render_template import qrcode +from gsheet_utils import GSheet + from io import BytesIO import base64 @@ -18,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/templates/index.html b/templates/index.html index 076f5e8..8edf8fd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -74,7 +74,7 @@
-

19,104

+

{{total_donations}}

nature Árboles y plantas donadas

From 2880124036063728468b886215cf057f0fc8e39a Mon Sep 17 00:00:00 2001 From: Eduardo Hernandez Date: Tue, 21 Apr 2020 20:51:42 -0500 Subject: [PATCH 8/9] Retreive MONGO_URI via env vars --- dbutils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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): From e83b5e802929dac1dc4844ffbd2614bfeb5b160b Mon Sep 17 00:00:00 2001 From: Eduardo Hernandez Date: Tue, 21 Apr 2020 20:51:42 -0500 Subject: [PATCH 9/9] Update start.sh permissions --- start.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 start.sh diff --git a/start.sh b/start.sh old mode 100644 new mode 100755