本文整理汇总了Python中gateway.models.DBSession.merge方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.merge方法的具体用法?Python DBSession.merge怎么用?Python DBSession.merge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gateway.models.DBSession
的用法示例。
在下文中一共展示了DBSession.merge方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_tokens
# 需要导入模块: from gateway.models import DBSession [as 别名]
# 或者: from gateway.models.DBSession import merge [as 别名]
def update_tokens(self):
"""
We need to record these updates to token states and provide a
way to view this in the Gateway Interface.
"""
session = DBSession()
data = simplejson.loads(self.request.body)
if not 'device_id' in data:
return json_response('You must provide an device_id')
device = session.query(Device)\
.filter_by(device_id=data['device_id']).first()
if device:
for i in data['tokens']:
token = session.query(Token)\
.filter_by(token=i['token_id']).first()
if token:
circuit = session.query(Circuit)\
.filter_by(pin=i['account_id']).first()
if circuit:
job = AddCredit(token.value, circuit, token)
session.add(job)
token.state = 'used'
session.merge(token)
session.flush()
return json_response('ok')
else:
return json_response('You must provide a valid device_id')
示例2: MessageHandler
# 需要导入模块: from gateway.models import DBSession [as 别名]
# 或者: from gateway.models.DBSession import merge [as 别名]
class MessageHandler(object):
def __init__(self, request):
self.session = DBSession()
self.request = request
self.message = self.session.\
query(Message).filter_by(
uuid=self.request.matchdict["id"]).first()
@action(renderer='sms/index_msg.mako')
def index(self):
global breadcrumbs
breadcrumbs.extend({})
return {
'breadcrumbs': breadcrumbs,
'message': self.message }
@action(request_method="POST")
def remove(self):
self.message.sent = True
self.session.merge(self.message)
return Response("ok")
@action(renderer='sms/delete_msg.mako')
def delete(self):
if self.request.method == 'POST':
return Response("Removed Message")
elif self.request.method == 'GET':
return {'message': self.message }
示例3: MessageHandler
# 需要导入模块: from gateway.models import DBSession [as 别名]
# 或者: from gateway.models.DBSession import merge [as 别名]
class MessageHandler(object):
""" Handler that allows the system to manage messages
"""
def __init__(self, request):
self.session = DBSession()
self.request = request
self.message = self.session.\
query(Message).filter_by(
uuid=self.request.matchdict["id"]).first()
@action(renderer='sms/index_msg.mako', permission='admin')
def index(self):
global breadcrumbs
breadcrumbs.extend({})
return {
'breadcrumbs': breadcrumbs,
'message': self.message}
@action(request_method="POST")
def remove(self):
self.message.sent = True
self.session.merge(self.message)
return Response("ok")
@action(permission='admin')
def delete(self):
self.session.delete(self.message)
self.session.flush()
return HTTPFound(location='/')
示例4: job
# 需要导入模块: from gateway.models import DBSession [as 别名]
# 或者: from gateway.models.DBSession import merge [as 别名]
def job(self):
session = DBSession()
job = session.query(Job).get(self.request.matchdict["id"])
if self.request.method == "DELETE":
job.state = False
job.end = datetime.now()
session.merge(job)
return Response(str(job))
else:
return Response(simplejson.dumps(job.toDict()))
示例5: set_primary_contact
# 需要导入模块: from gateway.models import DBSession [as 别名]
# 或者: from gateway.models.DBSession import merge [as 别名]
def set_primary_contact(message):
"""Allows users to set their primary contact number"""
session = DBSession()
circuit = get_circuit(message)
if circuit:
interface = circuit.meter.communication_interface
new_number = message.text.split(delimiter)[2]
old_number = circuit.account.phone
messageBody = make_message_body("tel.txt", lang=message.langauge, old_number=old_number, new_number=new_number)
interface.sendMessage(message.number, messageBody, incoming=message.uuid)
if new_number != message.number:
interface.sendMessage(new_number, messageBody, incoming=message.uuid)
account = circuit.account
account.phone = new_number
session.merge(account)
示例6: add_credit
# 需要导入模块: from gateway.models import DBSession [as 别名]
# 或者: from gateway.models.DBSession import merge [as 别名]
def add_credit(message):
"""Allows consumer to add credit to their account.
Sends an outgoing message to the consumer.
"""
session = DBSession()
circuit = get_circuit(message)
token = get_token(message)
if circuit:
interface = circuit.meter.communication_interface
if token:
job = AddCredit(circuit=circuit, credit=token.value, token=token)
session.add(job)
session.flush()
interface.sendJob(job,
incoming=message.uuid)
session.merge(circuit)
session.flush()
示例7: pp
# 需要导入模块: from gateway.models import DBSession [as 别名]
# 或者: from gateway.models.DBSession import merge [as 别名]
def pp(self):
"""
Primary log action. Should force the meter to provide authentication
"""
params = parse_qs(self.request.body)
session = DBSession()
if not self.meter or not self.circuit:
return Response(status=404)
log = PrimaryLog(circuit=self.circuit,
watthours=params["wh"][0],
use_time=params["tu"][0],
credit=params["cr"][0],
status=int(params["status"][0]))
self.circuit.credit = float(log.credit)
self.circuit.status = int(params["status"][0]) # fix
session.add(log)
session.merge(self.circuit)
return Response("ok")
示例8: UserHandler
# 需要导入模块: from gateway.models import DBSession [as 别名]
# 或者: from gateway.models.DBSession import merge [as 别名]
class UserHandler(object):
"""
Handler to manage user system.
Allows users to log in and out.
Also allows users to
"""
def __init__(self, request):
self.request = request
self.breadcrumbs = breadcrumbs[:]
self.session = DBSession()
def findUser(self):
name = authenticated_userid(self.request)
return self.session.query(Users).filter_by(name=name).first()
@action(renderer='users/profile.mako', permission='view')
def profile(self):
"""
Allows users to edit their current password.
"""
error = None
user = self.findUser()
breadcrumbs = self.breadcrumbs[:]
breadcrumbs.append({'text': 'Edit your profile'})
if self.request.method == 'POST':
user = self.findUser()
old_password = hashlib\
.md5(self.request.params['old_password']).hexdigest()
new_password = hashlib\
.md5(self.request.params['new_password']).hexdigest()
verify = hashlib\
.md5(self.request.params['verify_password']).hexdigest()
if unicode(old_password) == user.password:
if new_password == verify:
user.password = unicode(new_password)
self.session.merge(user)
self.session.flush()
return HTTPFound(location='/')
else:
error = 'You entered two different new passwords'
else:
error = 'Old password did not match'
return {'user': user,
'error': error,
'breadcrumbs': breadcrumbs}
@action(renderer='users/add.mako', permission='admin')
def add(self):
errors = None
session = DBSession()
groups = session.query(Groups).all()
breadcrumbs = self.breadcrumbs[:]
breadcrumbs.append({'text': 'Add a new user'})
if self.request.method == 'GET':
return {'breadcrumbs': breadcrumbs,
'groups': groups,
'errors': errors}
elif self.request.method == 'POST':
name = self.request.params['name']
email = self.request.params['email']
password = self.request.params['password']
group = self.request.params['group']
user = Users(name=name, email=email,
group_id=group,
password=password)
session.add(user)
session.flush()
return HTTPFound(location='/')
@action(permission='admin')
def show(self):
session = DBSession()
users = session.query(Users).all()
return Response(str([(user.name, user.group.name) for user in users]))
@action(renderer='login.mako')
def login(self):
session = DBSession()
came_from = self.request.params.get('came_from', '/')
message = ''
login = ''
password = ''
if 'form.submitted' in self.request.params:
name = self.request.params['name']
hash = hashlib.md5(self.request.params['password']).hexdigest()
user = session.query(Users)\
.filter_by(name=name)\
.filter_by(password=unicode(hash)).first()
if user:
print(name, hash)
headers = remember(self.request, user.name)
return HTTPFound(
location="%s" % came_from,
headers=headers)
message = 'Failed login'
return {
'message': message,
'url': self.request.application_url + '/login',
'came_from': came_from,
#.........这里部分代码省略.........
示例9: CircuitHandler
# 需要导入模块: from gateway.models import DBSession [as 别名]
# 或者: from gateway.models.DBSession import merge [as 别名]
class CircuitHandler(object):
"""
Circuit handler. Has all of the most
important urls for managing circuits
"""
def __init__(self, request):
self.session = DBSession()
self.request = request
self.circuit = self.session.\
query(Circuit).get(self.request.matchdict["id"])
self.meter = self.circuit.meter
self.breadcrumbs = breadcrumbs[:]
@action(renderer="circuit/index.mako", permission="admin")
def index(self):
breadcrumbs = self.breadcrumbs[:]
breadcrumbs.extend([
{"text": "Meter Overview", "url": self.meter.getUrl()},
{"text": "Circuit Overview"}])
return {
"logged_in": authenticated_userid(self.request),
"breadcrumbs": breadcrumbs,
"jobs": self.circuit.get_jobs(),
"fields": get_fields(self.circuit),
"circuit": self.circuit }
@action(renderer="circuit/edit.mako", permission="admin")
def edit(self):
breadcrumbs = self.breadcrumbs
breadcrumbs.extend([
{"text": "Meter Overview", "url": self.meter.getUrl()},
{"text": "Circuit Overview", "url": self.circuit.url()},
{"text": "Circuit Edit"}])
return {
"logged_in": authenticated_userid(self.request),
"breadcrumbs": breadcrumbs,
"fields": get_fields(self.circuit),
"circuit": self.circuit }
@action(permission="admin")
def update(self):
circuit = model_from_request(
self.request, self.circuit)
self.session.merge(circuit)
return HTTPFound(
location="%s%s" % (self.request.application_url,
self.circuit.getUrl()))
@action(permission="admin")
def turn_off(self):
self.circuit.turnOff()
return HTTPFound(location=self.circuit.getUrl())
@action(permission="admin")
def turn_on(self):
self.circuit.turnOn()
return HTTPFound(location=self.circuit.getUrl())
@action(permission="admin")
def ping(self):
self.circuit.ping()
return HTTPFound(location=self.circuit.getUrl())
@action(permission="admin")
def remove_jobs(self):
[self.session.delete(job) for job in self.circuit.get_jobs()]
return HTTPFound(
location="%s%s" % (self.request.application_url,
self.circuit.getUrl()))
@action(renderer="circuit/build_graph.mako", permission="admin")
def build_graph(self):
return {
"logged_in": authenticated_userid(self.request),
"circuit": self.circuit }
@action(renderer="circuit/show_graph.mako", permission="admin")
def show_graph(self):
query = self.session.query(PrimaryLog)
params = self.request.params
# parse the date from the request
origin = parser.parse(params["from"])
to = parser.parse(params["to"])
yaxis = params["yaxis"]
logs = [x for x in query.all() if x.created > origin]
logs = [x for x in logs if x.created < to]
return {
"logged_in": authenticated_userid(self.request),
"data": [{"time": str(x.created.ctime()),
"value": x.get_property(yaxis)} for x in logs ],
"y_units": simplejson.dumps(params["yaxis"]),
"origin": simplejson.dumps(params["from"]),
"to": simplejson.dumps(params["to"])}
@action()
def jobs(self):
return Response([x.toJSON() for x in self.circuit.get_jobs()])
@action(permission="admin")
#.........这里部分代码省略.........
示例10: MeterHandler
# 需要导入模块: from gateway.models import DBSession [as 别名]
# 或者: from gateway.models.DBSession import merge [as 别名]
class MeterHandler(object):
"""
Meter handler, allows for user to edit and manage meters
"""
def __init__(self, request):
self.request = request
self.session = DBSession()
self.meter = self.session.query(Meter).\
filter_by(slug=self.request.matchdict['slug']).one()
self.breadcrumbs = breadcrumbs[:]
@action(renderer="meter/index.mako", permission="admin")
def index(self):
breadcrumbs = self.breadcrumbs[:]
breadcrumbs.append({"text": "Meter Overview"})
circuit_data = make_table_data(self.meter.get_circuits())
return {
"logged_in": authenticated_userid(self.request),
"meter": self.meter,
"circuit_header": make_table_header(Circuit),
"circuit_data": circuit_data,
"fields": get_fields(self.meter),
"breadcrumbs": breadcrumbs }
@action(request_method='POST', permission="admin")
def add_circuit(self):
params = self.request.params
pin = params.get("pin")
if len(pin) == 0:
pin = Circuit.get_pin()
account = Account(
lang=params.get("lang"),
phone=params.get("phone"))
circuit = Circuit(meter=self.meter,
account=account,
pin=pin,
ip_address=params.get("ip_address"),
energy_max=int(params.get("energy_max")),
power_max=int(params.get("power_max")))
self.session.add(account)
self.session.add(circuit)
self.session.flush()
return HTTPFound(location="%s%s" % (
self.request.application_url, self.meter.getUrl()))
@action(renderer="meter/edit.mako", permission="admin")
def edit(self):
return {
"fields": get_fields(self.meter),
"meter": self.meter }
@action(renderer="meter/build_graph.mako", permission="admin")
def build_graph(self):
return {
"logged_in": authenticated_userid(self.request),
"meter": self.meter }
@action(renderer="meter/show_graph.mako", permission="admin")
def show_graph(self):
#needs to be implemented
return {}
@action(permission="admin")
def logs(self):
days = int(self.request.params.get('days', 10))
date = datetime.now()
logs = find_meter_logs(meter=self.meter,
date=date, session=self.session,
days=days)
return Response(
simplejson.dumps(logs),
content_type='application/json')
@action(permission="admin")
def update(self):
meter = model_from_request(self.request,
self.meter)
self.session.merge(meter)
return HTTPFound(
location="%s%s" % (self.request.application_url,
self.meter.getUrl()))
@action(permission="admin")
def remove(self):
self.session.delete(self.meter)
[self.session.delete(x)
for x in self.session.query(Circuit).filter_by(meter=self.meter)]
return HTTPFound(location="/")
@action(permission="admin")
def ping(self):
job = Mping(self.meter)
self.session.add(job)
self.session.flush()
msgClass = self.meter.getMessageType(job=True)
self.session.add(msgClass(job, self.meter.phone, incoming=""))
return HTTPFound(location=self.meter.getUrl())