本文整理汇总了Python中common.database.Database类的典型用法代码示例。如果您正苦于以下问题:Python Database类的具体用法?Python Database怎么用?Python Database使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Database类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save_to_mongo
def save_to_mongo(request_json):
try:
Database.insert(GraphConstants.COLLECTION, request_json)
except:
GraphErrors.GraphAlreadyExistsError(
"The graph you tried to create with name {} already exists. Please pick a different 'unique_name'.".format(
request_json['unique_name']))
示例2: updatedb
def updatedb(self):
try:
db = Database()
smsen = []
smsen.append(self.smsdict)
smsgwglobals.wislogger.debug("WATCHDOG: " +
"UPDATING " + str(self.smsdict["smsid"]) + " " + str(self.smsdict["status"]))
db.update_sms(smsen)
except error.DatabaseError as e:
smsgwglobals.wislogger.debug(e.message)
示例3: delete_months
def delete_months(group, months_to_keep):
# Find data in the database
# Those timestamps older than `months`, delete them.
if months_to_keep < 1:
return
else:
Database.remove(DataConstants.COLLECTION,
{"group": group,
"date":
{"$lte": datetime.datetime.utcnow() - datetime.timedelta(days=months_to_keep * 31)}
})
示例4: run
def run(self):
# load the configuration
# Create default root user
db = Database()
abspath = path.abspath(path.join(path.dirname(__file__), path.pardir))
configfile = abspath + '/conf/smsgw.conf'
cfg = SmsConfig(configfile)
wisglobals.wisid = cfg.getvalue('wisid', 'nowisid', 'wis')
wisglobals.wisipaddress = cfg.getvalue('ipaddress', '127.0.0.1', 'wis')
wisglobals.wisport = cfg.getvalue('port', '7777', 'wis')
wisglobals.cleanupseconds = cfg.getvalue('cleanupseconds', '86400', 'wis')
wisglobals.ldapserver = cfg.getvalue('ldapserver', None, 'wis')
wisglobals.ldapbasedn = cfg.getvalue('ldapbasedn', None, 'wis')
wisglobals.ldapenabled = cfg.getvalue('ldapenabled', None, 'wis')
ldapusers = cfg.getvalue('ldapusers', '[]', 'wis')
wisglobals.ldapusers = json.loads(ldapusers)
wisglobals.ldapusers = [item.lower() for item in wisglobals.ldapusers]
smsgwglobals.wislogger.debug("WIS:" + str(wisglobals.ldapusers))
password = cfg.getvalue('password', '20778ba41791cdc8ac54b4f1dab8cf7602a81f256cbeb9e782263e8bb00e01794d47651351e5873f9ac82868ede75aa6719160e624f02bba4df1f94324025058', 'wis')
salt = cfg.getvalue('salt', 'changeme', 'wis')
# write the default user on startup
db.write_users('root', password, salt)
# check if ssl is enabled
wisglobals.sslenabled = cfg.getvalue('sslenabled', None, 'wis')
wisglobals.sslcertificate = cfg.getvalue('sslcertificate', None, 'wis')
wisglobals.sslprivatekey = cfg.getvalue('sslprivatekey', None, 'wis')
wisglobals.sslcertificatechain = cfg.getvalue('sslcertificatechain', None, 'wis')
smsgwglobals.wislogger.debug("WIS: SSL " + str(wisglobals.sslenabled))
if wisglobals.sslenabled is not None and 'true' in wisglobals.sslenabled.lower():
smsgwglobals.wislogger.debug("WIS: STARTING SSL")
cherrypy.config.update({'server.ssl_module':
'builtin'})
cherrypy.config.update({'server.ssl_certificate':
wisglobals.sslcertificate})
cherrypy.config.update({'server.ssl_private_key':
wisglobals.sslprivatekey})
if wisglobals.sslcertificatechain is not None:
cherrypy.config.update({'server.ssl_certificate_chain':
wisglobals.sslcertificatechain})
cherrypy.config.update({'server.socket_host':
wisglobals.wisipaddress})
cherrypy.config.update({'server.socket_port':
int(wisglobals.wisport)})
cherrypy.quickstart(Root(), '/smsgateway',
'wis-web.conf')
示例5: writetodb
def writetodb(self):
try:
db = Database()
db.insert_sms(self.smsdict["modemid"],
self.smsdict["targetnr"],
self.smsdict["content"],
self.smsdict["priority"],
self.smsdict["appid"],
self.smsdict["sourceip"],
self.smsdict["xforwardedfor"],
self.smsdict["smsintime"],
self.smsdict["status"],
self.smsdict["statustime"])
except error.DatabaseError as e:
smsgwglobals.wislogger.debug(e.message)
示例6: from_name
def from_name(cls, name, **kwargs):
data = Database.find_one(GraphConstants.COLLECTION, {'unique_name': name})
if not data:
raise GraphErrors.GraphNotFoundError("The graph you requested '{}' was not found.".format(name))
data.update(kwargs)
graph = cls.from_json(data)
return graph.run(data)
示例7: checkpassword
def checkpassword(username, password):
try:
db = Database()
erg = db.read_users(username)
if erg is None or len(erg) == 0:
raise error.UserNotFoundError()
retpasswordhash = erg[0]["password"]
retpasswordsalt = erg[0]["salt"]
givpasswordhash = hashlib.sha512((password +
retpasswordsalt)
.encode('utf-8')).hexdigest()
if retpasswordhash == givpasswordhash:
return True
else:
return False
except error.DatabaseError as e:
smsgwglobals.wislogger.debug(e.message)
示例8: login_valid_user
def login_valid_user(email, password):
"""
:param email: user's email
:param password: user's password
:return: True if valid, False otherwise
"""
userData = Database.find_one("users", {'email': email})
if userData is None:
raise UserNotExistsError("this user does not existed!")
return False
if not Utils.check_hashed_password(password, userData['password']):
raise UserIncorrectPasswordError("Incorrect password!")
return False
return True
示例9: register_user
def register_user(email, password):
"""
This method registers a user using e-mail and password.
The password already come with sha-512 hash algorithm.
:param email: user's email (might be invalid)
:param password: sha512-hashed password
:return: True if registered successfully, of False otherwise
"""
userData = Database.find_one("users", {'email': email})
if userData is not None:
raise UserExistsError("user already existed!")
#if email is invalid, then what?
if not Utils.email_is_valid(email):
raise UserEmailInvalid("invalid email!")
#hash password, create new object, then insert to db
User(email, Utils.hash_password(password)).save_to_db()
return True
示例10: is_login_valid
def is_login_valid(cls, email, password):
"""
This method verifies that an e-mail/password combo (as sent by the site forms) is valid or not.
Checks that the e-mail exists, and that the password associated to that e-mail is correct.
:param email: The user's email
:param password: A sha512 hashed password
:return: True if valid, False otherwise
"""
user_data = Database.find_one(UserConstants.COLLECTION, {"email": email}) # Password in sha512 -> pbkdf2_sha512
if user_data is None:
# Tell the user that their e-mail doesn't exist
raise UserErrors.UserNotExistsError("Your user does not exist.")
if not Utils.check_hashed_password(password, user_data['password']):
# Tell the user that their password is wrong
raise UserErrors.IncorrectPasswordError("Your password was wrong.")
return cls(**user_data)
示例11: register_user
def register_user(email, password):
"""
This method registers a user using e-mail and password.
The password already comes hashed as sha-512.
:param email: user's e-mail (might be invalid)
:param password: sha512-hashed password
:return: True if registered successfully, or False otherwise (exceptions can also be raised)
"""
user_data = Database.find_one(UserConstants.COLLECTION, {"email": email})
if user_data is not None:
raise UserErrors.UserAlreadyRegisteredError("The e-mail you used to register already exists.")
if not Utils.email_is_valid(email):
raise UserErrors.InvalidEmailError("The e-mail does not have the right format.")
user = User(email, Utils.hash_password(password))
user.save_to_db()
return user
示例12: find_by_id
def find_by_id(cls, id):
return cls(**Database.find_one(UserConstants.COLLECTION, {'_id': id}))
示例13: save_to_db
def save_to_db(self):
Database.insert(UserConstants.COLLECTION, self.json())
示例14: initialize_database
def initialize_database():
Database.initialize()
示例15: from_mongo
def from_mongo(cls, id):
post_data = Database.find_one(collection="posts", query={"_id": id})
return cls(**post_data)