doing a better job of creating and updating db records
parent
baf32f857d
commit
0ec6698db7
50
app.py
50
app.py
|
@ -155,6 +155,8 @@ def index():
|
|||
een.get_list_of_cameras()
|
||||
een.get_list_of_accounts()
|
||||
|
||||
save_list_of_cameras(een=een)
|
||||
|
||||
if len(een.accounts) > 0 and een.active_account == None:
|
||||
# they need to pick and account
|
||||
|
||||
|
@ -211,13 +213,21 @@ def login_callback():
|
|||
try:
|
||||
user = User.query.filter(User.email == een.current_user['email']).first()
|
||||
if user is None:
|
||||
logging.debug(f"no user found with email {een.current_user['email']}, creating new user")
|
||||
user = User(account_id=een.current_user['accountId'],\
|
||||
user_id=een.current_user['id'],\
|
||||
email=een.current_user['email'],\
|
||||
timezone=een.current_user['timeZone']['timeZone'],\
|
||||
access_token=een.access_token,\
|
||||
refresh_token=een.refresh_token,\
|
||||
last_login=None)
|
||||
last_login=datetime.now())
|
||||
else:
|
||||
logging.debug(f"found user with {een.current_user['email']}, updating")
|
||||
user.last_login = datetime.now()
|
||||
user.access_token = een.access_token
|
||||
user.refresh_token = een.refresh_token
|
||||
|
||||
logging.debug(f"saving user object")
|
||||
user.save()
|
||||
except Exception as e:
|
||||
logging.warn(e)
|
||||
|
@ -240,6 +250,32 @@ def logout():
|
|||
return redirect(url_for('index'))
|
||||
|
||||
|
||||
|
||||
def save_list_of_cameras(een=None):
|
||||
user = User.query.filter(User.email == een.current_user['email']).first()
|
||||
if user:
|
||||
user_id = user.id
|
||||
else:
|
||||
user_id = None
|
||||
|
||||
for c in een.cameras:
|
||||
try:
|
||||
check_camera = Camera.query.filter(Camera.device_id == c.id).first()
|
||||
if check_camera is None:
|
||||
logging.debug(f"no camera found for {c.id}, creating new camera")
|
||||
check_camera = Camera(device_id=c.id,\
|
||||
timezone=None, \
|
||||
name=c.name,
|
||||
user_id=user_id)
|
||||
else:
|
||||
logging.debug(f"found camera for {c.id}")
|
||||
|
||||
logging.debug(f"saving camera object")
|
||||
check_camera.save()
|
||||
except Exception as e:
|
||||
logging.warn(e)
|
||||
|
||||
|
||||
@app.route('/cameras')
|
||||
@login_required
|
||||
def cameras():
|
||||
|
@ -249,16 +285,8 @@ def cameras():
|
|||
|
||||
logging.debug(een.get_list_of_cameras())
|
||||
|
||||
for c in een.cameras:
|
||||
try:
|
||||
check_camera = Camera.query.filter(Camera.device_id == c.id).first()
|
||||
if check_camera is None:
|
||||
new_camera = Camera(device_id=c.id,\
|
||||
timezone=None, \
|
||||
name=c.name)
|
||||
new_camera.save()
|
||||
except Exception as e:
|
||||
logging.warn(e)
|
||||
|
||||
save_list_of_cameras(een=een)
|
||||
|
||||
values = {
|
||||
"current_user": een.current_user,
|
||||
|
|
Loading…
Reference in New Issue