本文整理汇总了Python中tap.models.DBSession.add方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.add方法的具体用法?Python DBSession.add怎么用?Python DBSession.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tap.models.DBSession
的用法示例。
在下文中一共展示了DBSession.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: user_save
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def user_save(self):
result = dict(success=0, message="")
try:
with transaction.manager:
user = None
user_id = self.request.params.get('id')
if user_id:
user = DBSession.query(TapUser).get(user_id)
else:
name_valid(self.request.params['name'])
user = DBSession.query(TapUser).filter_by(
name=self.request.params['name']).first()
if user:
raise Exception("用户名重复")
user = TapUser()
DBSession.add(user)
user.name = self.request.params.get('name').strip()
obj_setattr(user, 'full_name', self.request)
obj_setattr(user, 'description', self.request)
if 'is_admin' in self.request.params:
user.is_admin = int(self.request.params['is_admin'])
if 'password' in self.request.params:
password = self.request.params.get('password').strip()
assert len(password) >= 6, '密码长度最少为6'
if 'password-repeat' in self.request.params:
password_repeat = self.request.params['password-repeat']
assert password == password_repeat, "两次输入密码不匹配"
user.password = encrypt_password(password.encode('utf8'))
result["success"] = 1
except BaseException, e:
result["message"] = str(e)
示例2: permission_save
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def permission_save(self):
result = dict(success=0, message="")
with transaction.manager:
user_id = self.request.params['user_id']
permissions = self.request.params['permissions']
db_list = DBSession.query(TapUserPermission).filter_by(
user_id=user_id)
for permission in db_list:
DBSession.delete(permission)
# permissionid-view:1,add:0,delete,0,edit,0;
for section in permissions.split(';'):
permission_id, permission_str = section.split('-')
permission = DBSession.query(TapPermission).get(permission_id)
if not permission:
result["message"] = "不存在的Permission Id: %s" \
% permission_id
return result
upermission = TapUserPermission(user_id=user_id,
permission_id=permission_id)
DBSession.add(upermission)
for detail in permission_str.split(','):
print detail
permission_type, value = detail.split(':')
value = int(value)
if permission_type == 'view':
upermission.a_view = value
elif permission_type == 'add':
upermission.a_add = value
elif permission_type == 'delete':
upermission.a_delete = value
elif permission_type == 'edit':
upermission.a_edit = value
result['success'] = 1
return result
示例3: flush_log_stats
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def flush_log_stats(occurrence_time, api_id, client_id, elapse):
with transaction.manager:
api = DBSession.query(TapApi).get(api_id)
if not api:
warnings.warn("API:%s is not exist." % api_id)
return
q = DBSession.query(TapApiStats)\
.filter_by(api_id=api_id, occurrence_time=occurrence_time,
client_id=client_id)
stats = q.first()
if not stats:
stats = TapApiStats(api_id=api_id, project_id=api.project_id,
occurrence_time=occurrence_time,
client_id=client_id)
DBSession.add(stats)
DBSession.flush()
# row-lock update
print '\tName:', api.name, ', ClientId:', client_id, \
', Occurrence:', elapse['occurrence_total']
stats = DBSession.query(TapApiStats).with_lockmode('update')\
.filter(TapApiStats.id == stats.id).first()
stats.occurrence_time = occurrence_time
stats.occurrence_total += elapse['occurrence_total']
stats.exception_total += elapse['exception_total']
if stats.elapse_max < elapse['elapse_max']:
stats.elapse_max = elapse['elapse_max']
if stats.elapse_min < elapse['elapse_min']:
stats.elapse_min = elapse['elapse_min']
stats.elapse_sum += elapse['elapse_sum']
# print stats.occurrence_total, elapse
stats.elapse_avg = stats.elapse_sum / stats.occurrence_total
示例4: project_save
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def project_save(self):
result = dict(success=0, message="")
try:
with transaction.manager:
project = None
project_id = self.request.params.get('id')
if project_id:
project = DBSession.query(TapProject).get(project_id)
else:
name_valid(self.request.params.get('name'))
assert self.request.params.get('cnname') not in ('', None), "名称不能为空"
project = TapProject()
DBSession.add(project)
project.uid_create = self.request.userid
project.uid_owner = self.request.userid
project.name = self.request.params.get('name').strip().upper()
project.cnname = self.request.params.get('cnname').strip()
project.description = self.request.params.get('description') or ''
permission = add_permission(
project.name, u'项目:' + project.name)
DBSession.flush()
add_user_permission(self.request.user, permission,
True, True, True, True)
if 'name' in self.request.params:
name_valid(self.request.params.get('name'))
project.name = self.request.params.get('name').strip().upper()
obj_setattr(project, 'cnname', self.request)
obj_setattr(project, 'description', self.request)
result["success"] = 1
except BaseException, e:
result["message"] = str(e)
示例5: release_save
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def release_save(self):
result = dict(success=0, message="")
request = self.request
try:
with transaction.manager:
api_id = request.params.get('api_id')
api_id = int(api_id)
api = DBSession.query(TapApi).get(api_id)
# auth type, change api
auth_type = request.params['auth_type']
if auth_type != api.auth_type:
api.auth_type = auth_type
release = TapApiRelease()
release.api_id = api.id
release.project_name = api.project.name
release.api_name = api.name
# release.dbconn_id = api.dbconn_id
release.version = request.params['version']
release.notes = request.params['notes']
release.uid_release = request.userid
release.content = json.dumps(api2dict(api), cls=TapEncoder)
DBSession.add(release)
api.status = 'RELEASE'
result["success"] = 1
except BaseException, e:
import traceback; traceback.print_exc()
result["message"] = str(e)
示例6: gen_captcha
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def gen_captcha():
with transaction.manager:
code = random.sample(chars, 5)
captcha = CaptchaCode()
captcha.code = unicode(''.join(code))
DBSession.add(captcha)
DBSession.flush()
assert captcha.id is not None
return captcha.id
示例7: add_permission
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def add_permission(name, desc):
permission = DBSession.query(TapPermission).filter_by(
name=name).first()
if permission:
return
permission = TapPermission()
permission.name = name
permission.description = desc
DBSession.add(permission)
return permission
示例8: add_user_permission
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def add_user_permission(user, permission, view=False, edit=False,
add=False, delete=False):
user_permission = TapUserPermission()
user_permission.user_id = user.id
user_permission.permission_id = permission.id
user_permission.a_view = view
user_permission.a_edit = edit
user_permission.a_add = add
user_permission.a_delete = delete
DBSession.add(user_permission)
示例9: require_key
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def require_key(request):
"""
:param request: token[token string], expire[expire time int]
:return: access_key, expire_time
"""
token = request.params['token']
expire = int(request.params.get('expire', 600))
if expire < 600:
expire = 600
with transaction.manager:
# access_key 不应该与 api 挂钩
client = DBSession.query(TapApiClient).filter_by(token=token).first()
if not client:
raise TapAuthFail
if client.auth_type == 'TOKEN':
pass
elif client.auth_type == 'CUSTOM':
container = {}
source = client.custom_auth.source.encode('utf8')
paras = ParaHandler.prepare(request.params,
client.custom_auth.paras)
variables = {}
for k, v in paras.items():
if k in ('main', '__builtins__'):
continue
variables[k] = v
exec source in container
container.update(variables)
check = False
try:
check = container['main']()
except BaseException as error:
return response_err(str(error), 500)
if check is not True:
return response_err('Auth Failed', 403)
key = str(uuid.uuid4())
access_key = TapApiAccessKey()
# access_key.api_id = auth.api_id
# access_key.auth_id = auth.id
access_key.client_id = client.id
access_key.access_key = key
access_key.access_expire = int(time.time()) + expire
DBSession.add(access_key)
# TODO Performance improvement. support redis/memcached/mongodb
# insert access_key to redis/memcached/mongodb
set_val(key, client.id)
result = dict(access_key=key, expire_time=access_key.access_expire)
response = Response(json.dumps(result),
headerlist=[('Access-Control-Allow-origin', '*',)])
response.content_type = "application/json"
return response
示例10: setUp
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def setUp(self):
self.config = testing.setUp()
from sqlalchemy import create_engine
engine = create_engine('sqlite://')
from .models import (
Base,
MyModel,
)
DBSession.configure(bind=engine)
Base.metadata.create_all(engine)
with transaction.manager:
model = MyModel(name='one', value=55)
DBSession.add(model)
示例11: para_new
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def para_new(self):
result = dict(success=0, message="")
request = self.request
api_id = request.params.get('id')
try:
with transaction.manager:
api_id = int(api_id)
para = TapParameter(api_id=api_id, name='__None__')
DBSession.add(para)
DBSession.flush()
result["para_id"] = para.id
result["para_name"] = '__None__'
result["success"] = 1
except BaseException, e:
result["message"] = str(e)
示例12: client_paranew
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def client_paranew(self):
result = dict(success=0, message="")
request = self.request
auth_id = request.params.get('id')
try:
with transaction.manager:
auth_id = int(auth_id)
para = TapApiClientCustomPara(customauth_id=auth_id,
name='__None__')
DBSession.add(para)
DBSession.flush()
result["para_id"] = para.id
result["para_name"] = '__None__'
result["success"] = 1
except BaseException, e:
result["message"] = str(e)
示例13: authclient_add
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def authclient_add(self):
result = dict(success=0, message="")
try:
with transaction.manager:
client_id = self.request.params['id']
api_id = int(self.request.params['api_id'])
client = DBSession.query(TapApiClient).get(client_id)
if DBSession.query(TapApiAuth).filter_by(client_id=client.id,
api_id=api_id).first():
result['message'] = u'重复添加: %s' % client.name
return result
auth = TapApiAuth()
auth.client_id = client.id
auth.api_id = api_id
auth.uid_auth = self.request.userid
auth.token = client.token
DBSession.add(auth)
result['success'] = 1
except BaseException, e:
result["message"] = str(e)
示例14: conn_save
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def conn_save(self):
result = dict(success=0, message="")
request = self.request
conn_id = request.params.get('id')
try:
with transaction.manager:
conn = None
name_valid(request.params.get('name'))
if conn_id:
conn = DBSession.query(TapDBConn).get(conn_id)
else:
# 检查是否存在
name = request.params.get('name').strip()
conn = DBSession.query(TapDBConn).filter_by(
name=name).first()
if conn:
raise Exception("名称重复: %s" % name.encode('utf8'))
conn = TapDBConn()
DBSession.add(conn)
conn.uid_create = request.userid
conn.name = name
conn.dbtype = request.params.get('dbtype')
conn.connstring = request.params.get('connstring')
assert request.params.get('dbtype') not in ('', None), \
'数据库类型不能为空'
assert request.params.get('connstring') not in ('', None), \
'连接字符串不能为空'
obj_setattr(conn, 'name', request)
obj_setattr(conn, 'dbtype', request)
obj_setattr(conn, 'connstring', request)
obj_setattr(conn, 'description', request)
obj_setattr(conn, 'options', request)
result["success"] = 1
except BaseException, e:
result["message"] = str(e)
示例15: client_save
# 需要导入模块: from tap.models import DBSession [as 别名]
# 或者: from tap.models.DBSession import add [as 别名]
def client_save(self):
result = dict(success=0, message="")
request = self.request
try:
with transaction.manager:
client_id = request.params.get('client_id')
client = None
name = self.request.params.get('name')
if client_id:
client = DBSession.query(TapApiClient).get(client_id)
else:
assert name not in ('', None), '名称不能为空'
client = TapApiClient(name=name)
client.uid_create = self.request.userid
client.token = str(uuid.uuid4())
custom_auth = TapApiClientCustomAuth()
custom_auth.uid_create = self.request.userid
client.custom_auth = custom_auth
DBSession.add(client)
obj_setattr(client, 'name', self.request)
obj_setattr(client, 'description', self.request)
obj_setattr(client, 'auth_type', self.request)
obj_setattr(client.custom_auth, 'source', self.request)
for para in client.custom_auth.paras:
changed = para_setattr(para, 'name', request)
changed = para_setattr(para, 'val_type', request) or changed
changed = para_setattr(para, 'default', request) or changed
changed = (para_setattr(para, 'absent_type', request) or
changed)
result["success"] = 1
except BaseException, e:
import traceback; traceback.print_exc()
result["message"] = str(e)