inital keyval db

This commit is contained in:
Jeremy Zhang 2017-04-23 20:02:04 -07:00
parent 8d50f4fa0f
commit b3a219e58d
2 changed files with 52 additions and 0 deletions

View File

@ -6,3 +6,5 @@ from unauthenticated_users import UnauthenticatedUsers
from unauthenticated_bans import UnauthenticatedBans from unauthenticated_bans import UnauthenticatedBans
from authenticated_users import AuthenticatedUsers from authenticated_users import AuthenticatedUsers
from custom_redislite import LimitsRedisLite from custom_redislite import LimitsRedisLite
from keyvalue_properties import set_keyvalproperty, get_keyvalproperty, getexpir_keyvalproperty, setexpir_keyvalproperty

View File

@ -0,0 +1,50 @@
from titanembeds.database import db
from datetime import datetime
def set_keyvalproperty(key, value, expiration=None):
q = db.session.query(KeyValueProperties).filter(KeyValueProperties.key == key)
if q.count() == 0:
db.session.add(KeyValueProperties(key=key, value=value, expiration=expiration))
else:
firstobj = q.first()
firstobj.value = value
firstobj.expiration = expiration
db.session.commit()
def get_keyvalproperty(key):
q = db.session.query(KeyValueProperties).filter(KeyValueProperties.key == key)
now = datetime.now()
if q.count() > 0 and (q.first().expiration is None or q.first().expiration > now):
return q.first().value
return None
def getexpir_keyvalproperty(key):
q = db.session.query(KeyValueProperties).filter(KeyValueProperties.key == key)
now = datetime.now()
if q.count() > 0 and (q.first().expiration is not None and q.first().expiration > now):
return q.first().expiration
return None
def setexpir_keyvalproperty(key, expiration=None):
q = db.session.query(KeyValueProperties).filter(KeyValueProperties.key == key)
if q.count() > 0:
if expiration:
q.first().expiration = datetime.now()
else:
q.first().expiration = None
db.session.commit()
class KeyValueProperties(db.Model):
__tablename__ = "keyvalue_properties"
id = db.Column(db.Integer, primary_key=True) # Auto incremented id
key = db.Column(db.String(32)) # Property Key
value = db.Column(db.Text()) # Property value
expiration = db.Column(db.TIMESTAMP) # Suggested Expiration for value (None = no expire) in secs
def __init__(self, key, value, expiration=None):
self.key = key
self.value = value
if expiration:
self.expiration = datetime.datetime.now() + datetime.timedelta(seconds = expiration)
else:
self.expiration = None