-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
64 lines (50 loc) · 1.89 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import logging
from google.appengine.ext import ndb
class AccountSettings(ndb.Model):
"""Accound settings DB model to keep roomba settings for user"""
userid = ndb.StringProperty()
agent_url = ndb.StringProperty()
device_mac = ndb.StringProperty()
@classmethod
def get_user_by_id(cls, user_id):
return cls.query(cls.userid == user_id)
class AccountSettingsHelper():
"""add new user to a database if user is not yet created
will create [UsersNdbModel(key=Key('UsersNdbModel', 6473924464345088), user_id=u'185804764220139124118', user_role=u'admin')]
"""
def add_new_user(self, user_id):
usr = AccountSettings(key=ndb.Key(AccountSettings, user_id))
usr.userid = user_id
usr.put()
def user_exists(self, user_id):
"""Check if user already exists
Returns: True if exists False otherwise
"""
qry = AccountSettings().get_user_by_id(user_id)
res = qry.fetch()
if res:
return True
else:
return False
def update_device_information(self, user_id, agent_url=None, device_mac=None):
""" update agent url and/or device_mac """
if self.user_exists(user_id):
usr_key = ndb.Key(AccountSettings, user_id)
usr = usr_key.get()
if agent_url:
usr.agent_url = agent_url
if device_mac:
usr.device_mac = device_mac
usr.put()
return True
else:
logging.error("Cannot update information for non-existing user")
return False
def get_user_info(self, user_id):
""" Returns information about user form db"""
if self.user_exists:
usr_key = ndb.Key(AccountSettings, user_id)
usr = usr_key.get()
return usr
else:
logging.error("User %s doesn't exist in db" % user_id)