mirror of
https://github.com/TitanEmbeds/Titan.git
synced 2024-12-24 14:07:03 +01:00
inital keyval db
This commit is contained in:
parent
8d50f4fa0f
commit
b3a219e58d
@ -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
|
||||||
|
50
titanembeds/database/keyvalue_properties.py
Normal file
50
titanembeds/database/keyvalue_properties.py
Normal 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
|
Loading…
Reference in New Issue
Block a user