Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ google-auth = "*"
flask-sqlalchemy = "*"
requests = "*"
flask-migrate = "*"
gpiozero = "*"
flask-cors = "*"

[dev-packages]
yapf = "*"
Expand Down
73 changes: 48 additions & 25 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ Be sure to set the following parameters in the file you just created:
- GOOGLE_CLIENT_ID: what you just got from google
- SQLALCHEMY_DATABASE_URI: URI to your database

Install pipenv by following this tuto: https://pypi.org/project/pipenv/

Create database with sqlite command:
```
$ sqlite3 test.db
>> .databases
```
.databases should list the existing databases like this:
``
main: /home/raspberry/porton/test.db
``

Having done that, we can install the app:

```
Expand All @@ -34,3 +46,8 @@ export FLASK_APP=aper
flask db upgrade
python run.py
```

## RaspberryPI connection
Pin 1 —> +3V -> Rele DC+
Pin 9 —> GND -> Rele DC-
Pin 11 —> GPIO 17 -> Rele IN
4 changes: 3 additions & 1 deletion aper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from flask import Flask
from flask_login import LoginManager
from flask_cors import CORS

app = Flask(__name__, instance_relative_config=True)

app.config.from_object('config')
app.config.from_pyfile('config.py', silent=True)

CORS(app)

login_manager = LoginManager()
login_manager.init_app(app)

Expand All @@ -19,6 +22,5 @@
def shutdown_session(exception=None):
db_session.remove()


import aper.auth
import aper.views
35 changes: 26 additions & 9 deletions aper/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .db import db_session
from google.oauth2 import id_token
from google.auth.transport import requests
import json



@login_manager.request_loader
Expand All @@ -28,32 +28,49 @@ def load_user(request):

try:
# Specify the CLIENT_ID of the app that accesses the backend:
#idinfo = id_token.verify_oauth2_token(token, requests.Request(),
# app.config['GOOGLE_CLIENT_ID'])

idinfo = json.loads(dummy_info)
idinfo = id_token.verify_oauth2_token(token, requests.Request(),
app.config['GOOGLE_CLIENT_ID'])

#idinfo = json.loads(dummy_info)

# Or, if multiple clients access the backend server:
# idinfo = id_token.verify_oauth2_token(token, requests.Request())
# if idinfo['aud'] not in [CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]:
# raise ValueError('Could not verify audience.')

if idinfo['iss'] not in [
'accounts.google.com', 'https://accounts.google.com'
]:
raise ValueError('Wrong issuer.')

# If auth request is from a G Suite domain:
# if idinfo['hd'] != GSUITE_DOMAIN_NAME:
# raise ValueError('Wrong hosted domain.')

# ID token is valid. Get the user's Google Account ID from the decoded token.

# whitelist = app.config['USERS']
#
# if idinfo['email'] in whitelist:
# user = User.query.filter(User.email == idinfo['email']).first()
# if not user:
# newuser = User(idinfo['name'], idinfo['email'], idinfo['picture'])
# db_session.add(newuser)
# else:
# return user
# else:
# return None
# except ValueError:
# return None

user = User.query.filter(User.email == idinfo['email']).first()

if not user:
user = User(idinfo['name'], idinfo['email'])
user = User(idinfo['name'], idinfo['email'], idinfo['picture'], "GUEST")
db_session.add(user)
db_session.commit()
else:
user.avatar = idinfo['picture']
db_session.commit()
return user
except ValueError:
# Invalid token
return None
return None
12 changes: 9 additions & 3 deletions aper/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ class User(UserMixin, Base):
name = Column(String(100))
order = Column(Integer, nullable=False)
absent_on = Column(Date)
avatar = Column(String(100))
role = Column(String(100))

def __init__(self, name, email):
def __init__(self, name, email, avatar, role):
self.email = email
self.name = name
max_order = db_session.query(func.max(User.order)).scalar()
self.order = max_order + 1 if max_order else 1
self.absent_on = None
self.avatar = avatar
self.role = role

def serialize(self):
"""Return object data in serializeable format"""
Expand All @@ -30,7 +34,9 @@ def serialize(self):
'name': self.name,
'email': self.email,
'order': self.order,
'absent_on': self.absent_on
'absent_on': self.absent_on,
'avatar': self.avatar,
'role': self.role,
}

@classmethod
Expand All @@ -44,4 +50,4 @@ def __repr__(self):
return '<User {}[{}]>'.format(self.name, self.email)

def __eq__(self, value):
return self.id == value.id and self.name == value.name and self.order == value.order and self.absent_on == value.absent_on
return self.id == value.id and self.name == value.name and self.order == value.order and self.absent_on == value.absent_on and self.avatar == value.avatar and self.role == value.role
Loading