1 Star 0 Fork 0

aspirefhaha / udisk_man_web

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
models.py 2.29 KB
Copy Edit Web IDE Raw Blame History
aspirefhaha authored 2016-08-25 16:11 . chang timestamp bug
# -*- coding:utf-8 -*-
from sqlalchemy import *
from sqlalchemy import event
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
import hashlib
password_prefix = "Ad%cvcsadefr^!deaf"
engine=create_engine('sqlite:///./udisk.db', echo=True)
def bindSQL():
return scoped_session(sessionmaker(bind=engine))
Base = declarative_base()
class GDisk(Base):
__tablename__ = 'gdisk'
id = Column(Integer,primary_key=True)
name = Column(String(20),unique=True)
address = Column(String(20),unique=True)
starttime = Column(TIMESTAMP,nullable=False,server_default=func.now())
endtime = Column(TIMESTAMP,nullable=False,server_default=func.now())
areatype = Column(String(32),nullable=True)
area = Column(String(2048),nullable=True)
curloc = Column(String(128),nullable=True)
class User(Base):
__tablename__ = 'user'
id = Column(Integer,primary_key=True)
name = Column(String(20),unique=True)
email = Column(String(32),unique=True)
password = Column(String(32))
superuser = Column(Boolean,default=False)
metadata = Base.metadata
def calcPassword(value):
return hashlib.md5("%s%s" % (password_prefix,value)).hexdigest()
def setPassword(target, value, oldvalue,initiator):
if value == oldvalue:
return oldvalue
return hashlib.md5("%s%s" % (password_prefix,value)).hexdigest()
event.listen(User.password,"set",setPassword,retval=True)
def get_or_create(session,model,**kwargs):
if "defaults" in kwargs:
defaults = kwargs["defaults"]
del kwargs["defaults"]
else :
defaults = {}
instance = session.query(model).filter_by(**kwargs).first()
if instance:
return instance, False
else:
kwargs.update(defaults)
instance = model(**kwargs)
session.add(instance)
session.flush()
session.refresh(instance)
return instance, True
def initModel():
metadata.create_all(engine)
db = bindSQL()
obj, created = get_or_create(
db,
User,
name="administrator",
defaults={
"email":"1945520@qq.com",
"password":"administrator",
"superuser":True
}
)
db.commit()
db.remove()
if __name__ == '__main__':
#metadata.create_all(engine)
initModel()

Comment ( 0 )

Sign in for post a comment

1
https://gitee.com/fhaha/udisk_man_web.git
git@gitee.com:fhaha/udisk_man_web.git
fhaha
udisk_man_web
udisk_man_web
master

Search