本文整理汇总了Python中sngconnect.database.DBSession类的典型用法代码示例。如果您正苦于以下问题:Python DBSession类的具体用法?Python DBSession怎么用?Python DBSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DBSession类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: upload_log
def upload_log(request):
"""
Store new piece of log sent by tinyputer. Logs can be sent as a response to 'upload_log' command
which created placeholder LogRequest objects.
Parameters:
log_request_id
log_request_hash
"""
try:
log_request_id = int(request.matchdict['log_request_id'])
log_request_hash = str(request.matchdict['log_request_hash'])
except (KeyError, ValueError):
raise httpexceptions.HTTPNotFound("Invalid request arguments.")
try:
log_request = DBSession.query(LogRequest).filter(
LogRequest.id == log_request_id,
LogRequest.hash == log_request_hash,
LogRequest.log == None
).one()
except database_exceptions.NoResultFound:
raise httpexceptions.HTTPNotFound("Log request not found.")
log_request.log = request.body
DBSession.add(log_request)
return Response()
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:25,代码来源:views.py
示例2: activate
def activate(request):
successful_activation = False
try:
user = DBSession.query(User).filter(
User.email == request.matchdict['email'],
User.email_activation_code ==
request.matchdict['email_activation_code'],
User.activated == None
).one()
except database_exceptions.NoResultFound:
raise httpexceptions.HTTPNotFound()
activation_form = forms.ActivationForm(csrf_context=request)
if request.method == 'POST':
activation_form.process(request.POST)
if activation_form.validate():
if (user.phone_activation_code.upper() !=
activation_form.phone_activation_code.data.upper()):
request.session.flash(
_("The activation code you entered is invalid."),
queue='error'
)
else:
# Regenerate activation code so user cannot reactivate his
# account after being banned.
user.regenerate_email_activation_code()
user.activated = pytz.utc.localize(datetime.datetime.utcnow())
DBSession.add(user)
successful_activation = True
return {
'activation_form': activation_form,
'successful_activation': successful_activation,
}
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:32,代码来源:views.py
示例3: setUp
def setUp(self):
super(TestActivate, self).setUp()
feed_template = FeedTemplate(
id=1,
name='Feed template 1',
modbus_bandwidth=9600,
modbus_port='/dev/ttyS0',
modbus_parity='EVEN',
modbus_data_bits=8,
modbus_stop_bits=1,
modbus_timeout=5,
modbus_endianness='BIG',
modbus_polling_interval=120,
dashboard_layout='IMAGE',
)
feed = Feed(
id=1,
template=feed_template,
name=u"Feed 1",
description=u"Description",
latitude=20.5,
longitude=15.3,
created=pytz.utc.localize(datetime.datetime.utcnow()),
api_key='some-api-key',
activation_code='some-activation-code',
activation_code_regenerated=pytz.utc.localize(
datetime.datetime.utcnow()
),
)
self.activation_code = feed.activation_code
DBSession.add_all([
feed_template,
feed,
])
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:34,代码来源:views.py
示例4: test_normal_operation
def test_normal_operation(self):
request = self.get_request(1)
response = views.commands(request)
self.assertEqual(response.status_code, 200)
self.assertDictEqual(response.json_body, {
'commands': [],
})
command = Command(
feed_id=1,
date=_utc_datetime(2012, 5, 17, 23, 4, 11),
command='upload_log',
arguments={
'url': 'http://example.org',
}
)
#with transaction.manager:
DBSession.add(command)
request = self.get_request(1)
response = views.commands(request)
self.assertEqual(response.status_code, 200)
self.assertDictEqual(response.json_body, {
u'commands': [
{
u'command': u'upload_log',
u'arguments': {
u'url': u'http://example.org',
},
},
],
})
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:30,代码来源:views.py
示例5: __call__
def __call__(self):
update_chart_form = forms.UpdateChartDefinitionForm(
self.chart_definition.id,
self.feed,
self.feed.template.data_stream_templates,
csrf_context=self.request
)
update_chart_form.process(self.request.POST)
data_stream_templates_dict = dict((
(template.id, template)
for template in self.feed.template.data_stream_templates
))
if update_chart_form.validate():
update_chart_form.populate_obj(self.chart_definition)
self.chart_definition.data_stream_templates = []
for id in update_chart_form.data_stream_template_ids.data:
self.chart_definition.data_stream_templates.append(
data_stream_templates_dict[id]
)
DBSession.add(self.chart_definition)
return Response(
json.dumps({
'redirect': None,
}),
content_type='application/json'
)
return Response(
json.dumps({
'errors': update_chart_form.errors,
}),
content_type='application/json'
)
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:32,代码来源:charts.py
示例6: test_reset_requested_value
def test_reset_requested_value(self):
# Request value in data stream.
self.assertEqual(
DBSession.query(DataStream).filter(
DataStream.id == 2
).value('requested_value'),
1234
)
# Pretend value has not yet been set by tinyputer - requested_value
# still exists.
request = self.get_request(1, 'data_stream_2', json_body={
'datapoints': [
{
'at': '2012-10-13T17:01:00.345123Z',
'value': '134.2344',
},
{
'at': '2012-10-13T17:02:00.425Z',
'value': '-23.24525',
},
]
})
response = views.feed_data_stream(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(
DBSession.query(DataStream).filter(
DataStream.id == 2
).value('requested_value'),
1234
)
# Pretend value has been set by tinyputer - requested_value reset.
request = self.get_request(1, 'data_stream_2', json_body={
'datapoints': [
{
'at': '2012-10-13T17:02:30.345123Z',
'value': '1234',
},
]
})
#with transaction.manager
response = views.feed_data_stream(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(
DBSession.query(DataStream).filter(
DataStream.id == 2
).value('requested_value'),
None
)
request = self.get_request(1, 'data_stream_2', json_body={
'datapoints': [
{
'at': '2012-10-13T17:03:30.345123Z',
'value': '4321',
},
]
})
response = views.feed_data_stream(request)
self.assertEqual(response.status_code, 200)
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:58,代码来源:views.py
示例7: main
def main(argv=sys.argv):
if len(argv) != 2:
usage(argv)
config_uri = argv[1]
setup_logging(config_uri)
settings = get_appsettings(config_uri)
database_engine = sqlalchemy.engine_from_config(settings, 'database.')
DBSession.configure(bind=database_engine)
generate_data()
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:9,代码来源:create_test_users.py
示例8: insert_data_points
def insert_data_points(feed_id, data_stream, data_points, message_service):
Measurements().insert_data_points(data_stream.id, data_points)
# FIXME This may be not wise for production use due to race condition
# concerns.
dates = map(lambda x: x[0], data_points)
HourlyAggregates().recalculate_aggregates(data_stream.id, dates)
DailyAggregates().recalculate_aggregates(data_stream.id, dates)
MonthlyAggregates().recalculate_aggregates(data_stream.id, dates)
LastDataPoints().update(feed_id, data_stream.id)
last_date, last_value = LastDataPoints().get_last_data_stream_data_point(
feed_id,
data_stream.id
)
last_value = decimal.Decimal(last_value)
# Turn alarms associated with datastreams on/off
alarm_definitions = DBSession.query(AlarmDefinition).filter(
AlarmDefinition.data_stream_id == data_stream.id
)
alarm_messages = []
alarms_on = []
alarms_off = []
for alarm_definition in alarm_definitions:
message_content = alarm_definition.check_value(last_value)
if message_content is None:
alarms_off.append(alarm_definition.id)
else:
alarms_on.append(alarm_definition.id)
alarm_messages.append(
Message(
feed=alarm_definition.data_stream.feed,
data_stream=alarm_definition.data_stream,
message_type='ERROR',
date=last_date,
content=message_content
)
)
Alarms().set_alarms_on(feed_id, data_stream.id, alarms_on, last_date)
Alarms().set_alarms_off(feed_id, data_stream.id, alarms_off)
for message in alarm_messages:
message_service.create_message(message)
# Set requested value to None if applied.
if data_stream.writable and data_stream.requested_value is not None:
error = abs(data_stream.requested_value - last_value)
maximal_error = (
decimal.Decimal(str(sys.float_info.epsilon)) * max((
2 ** -1022,
abs(data_stream.requested_value),
abs(last_value))
)
)
if error <= maximal_error:
DBSession.query(DataStream).filter(
DataStream.id == data_stream.id
).update({
'requested_value': None,
'value_requested_at': None,
})
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:57,代码来源:views.py
示例9: main
def main(argv=sys.argv):
if len(argv) != 2:
usage(argv)
config_uri = argv[1]
setup_logging(config_uri)
settings = get_appsettings(config_uri)
engine = engine_from_config(settings, 'database.')
DBSession.configure(bind=engine)
ModelBase.metadata.create_all(engine)
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:9,代码来源:initialize_database.py
示例10: feed_template_delete
def feed_template_delete(request):
try:
feed_template = DBSession.query(FeedTemplate).filter(
FeedTemplate.id == request.matchdict['feed_template_id']
).one()
except database_exceptions.NoResultFound:
raise httpexceptions.HTTPNotFound()
delete_form = forms.DeleteFeedTemplateForm(
feed_template_id=feed_template.id,
csrf_context=request
)
delete_form.process(request.POST)
if delete_form.validate():
dependent_feed_count = DBSession.query(Feed).filter(
Feed.template == feed_template
).count()
dependent_data_stream_template_count = DBSession.query(
DataStreamTemplate
).filter(
DataStreamTemplate.feed_template == feed_template
).count()
if dependent_feed_count > 0:
request.session.flash(
_(
"Device template cannot be deleted as there are"
" already devices based on it. Contact the system"
" support for further information."
),
queue='error'
)
elif dependent_data_stream_template_count > 0:
request.session.flash(
_(
"Device template cannot be deleted as it has parameter"
" templates assigned to it. Contact the system support for"
" further information."
),
queue='error'
)
else:
DBSession.delete(feed_template)
request.session.flash(
_("Device template has been successfully deleted."),
queue='success'
)
else:
request.session.flash(
_(
"There were some problems with your request."
" Contact the system support."
),
queue='error'
)
return httpexceptions.HTTPFound(
request.route_url('sngconnect.devices.feed_templates')
)
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:56,代码来源:views.py
示例11: data_stream_template_delete
def data_stream_template_delete(request):
try:
feed_template, data_stream_template = DBSession.query(
FeedTemplate,
DataStreamTemplate
).filter(
FeedTemplate.id == request.matchdict['feed_template_id'],
(DataStreamTemplate.id ==
request.matchdict['data_stream_template_id']),
DataStreamTemplate.feed_template_id == FeedTemplate.id
).one()
except database_exceptions.NoResultFound:
raise httpexceptions.HTTPNotFound()
delete_form = forms.DeleteDataStreamTemplateForm(
data_stream_template_id=data_stream_template.id,
csrf_context=request,
locale=get_locale_name(request)
)
delete_form.process(request.POST)
if delete_form.validate():
dependent_count = DBSession.query(DataStream).filter(
DataStream.template == data_stream_template
).count()
if dependent_count == 0:
DBSession.delete(data_stream_template)
request.session.flash(
_("Parameter template has been successfully deleted."),
queue='success'
)
else:
request.session.flash(
_(
"Parameter template cannot be deleted as there are"
" already parameters based on it. Contact the system"
" support for further information."
),
queue='error'
)
else:
request.session.flash(
_(
"There were some problems with your request."
" Contact the system support."
),
queue='error'
)
return httpexceptions.HTTPFound(
request.route_url(
'sngconnect.devices.feed_template',
feed_template_id=feed_template.id
)
)
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:52,代码来源:views.py
示例12: sing_up
def sing_up(request):
sign_up_form = forms.SignUpForm(csrf_context=request)
successful_submission = False
if request.method == 'POST':
sign_up_form.process(request.POST)
if sign_up_form.validate():
user = User(
email=sign_up_form.email.data.lower(),
phone=sign_up_form.phone.data,
name=sign_up_form.user_name.data,
company_name=(
sign_up_form.company_name.data
if sign_up_form.company_name.data else
None
),
timezone_tzname=str(request.registry['default_timezone'])
)
user.set_password(sign_up_form.password.data)
DBSession.add(user)
template = request.registry['jinja2_environment'].get_template(
'sngconnect.accounts:templates/emails/account_activation.txt'
)
activation_email = EmailMessage(
subject=_("Activate your account at SNG Connect"),
sender=request.registry['settings']['mail.sender'],
recipients=[user.email],
body=template.render(
activation_url=request.route_url(
'sngconnect.accounts.activate',
email=user.email,
email_activation_code=user.email_activation_code
)
)
)
get_mailer(request).send(activation_email)
sms_service = SMSService(request.registry)
sms_service.send_sms(
[user.phone],
_(
"Your SNG:connect confirmation code is: ${code}",
mapping={
'code': user.phone_activation_code,
}
)
)
successful_submission = True
return {
'sign_up_form': sign_up_form,
'successful_submission': successful_submission,
}
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:50,代码来源:views.py
示例13: set_requested_value
def set_requested_value(self, data_stream, value):
DBSession.query(DataStream).filter(
DataStream.id == data_stream.id
).update({
'requested_value': value,
'value_requested_at': pytz.utc.localize(
datetime.datetime.utcnow()
),
})
self.registry['scheduler'].add_date_job(
DataStreamService.assert_requested_value,
datetime.datetime.now() + self._requested_value_timeout,
[self.registry, data_stream.id]
)
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:14,代码来源:data_stream.py
示例14: validate_name
def validate_name(self, field):
if field.errors:
return
try:
DBSession.query(ChartDefinition).filter(
ChartDefinition.feed_template == self.feed_template,
ChartDefinition.feed == None,
ChartDefinition.name == field.data
).one()
except database_exceptions.NoResultFound:
pass
else:
raise validators.ValidationError(
_("This chart name is already taken.")
)
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:15,代码来源:forms.py
示例15: generate_data
def generate_data():
user = User(
name='User',
email='[email protected]',
phone='+48123456789',
activated=pytz.utc.localize(datetime.datetime.utcnow()),
role_user=True,
timezone_tzname='Europe/Warsaw'
)
user.set_password('user')
kid = User(
name='Kid',
email='[email protected]',
phone='+48123456789',
activated=pytz.utc.localize(datetime.datetime.utcnow()),
role_user=True,
timezone_tzname='Europe/Warsaw'
)
kid.set_password('kid')
maintainer = User(
name='Maintainer',
email='[email protected]',
phone='+48123456789',
activated=pytz.utc.localize(datetime.datetime.utcnow()),
role_maintainer=True,
timezone_tzname='Europe/Warsaw'
)
maintainer.set_password('maintainer')
supplier = User(
name='Supplier',
email='[email protected]',
phone='+48123456789',
activated=pytz.utc.localize(datetime.datetime.utcnow()),
role_supplier=True,
timezone_tzname='Europe/Warsaw'
)
supplier.set_password('supplier')
admin = User(
name='Admin',
email='[email protected]',
phone='+48123456789',
activated=pytz.utc.localize(datetime.datetime.utcnow()),
role_administrator=True,
timezone_tzname='Europe/Warsaw'
)
admin.set_password('admin')
DBSession.add_all([user, kid, maintainer, supplier, admin])
transaction.commit()
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:48,代码来源:create_test_users.py
示例16: feed_templates
def feed_templates(request):
feed_template_form = forms.AddFeedTemplateForm(csrf_context=request)
if request.method == 'POST':
feed_template_form.process(request.POST)
if feed_template_form.validate():
feed_template = FeedTemplate()
feed_template_form.populate_obj(feed_template)
DBSession.add(feed_template)
request.session.flash(
_("Device template has been successfully added."),
queue='success'
)
return httpexceptions.HTTPFound(
request.route_url('sngconnect.devices.feed_templates')
)
else:
request.session.flash(
_(
"There were some problems with your request."
" Please check the form for error messages."
),
queue='error'
)
feed_templates = DBSession.query(FeedTemplate).order_by(
FeedTemplate.name
)
return {
'feed_template_form': feed_template_form,
'feed_templates': [
{
'id': feed_template.id,
'name': feed_template.name,
'url': request.route_url(
'sngconnect.devices.feed_template',
feed_template_id=feed_template.id
),
'delete_url': request.route_url(
'sngconnect.devices.feed_template_delete',
feed_template_id=feed_template.id
),
'delete_form': forms.DeleteFeedTemplateForm(
csrf_context=request,
feed_template_id=feed_template.id
),
}
for feed_template in feed_templates
]
}
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:48,代码来源:views.py
示例17: index
def index(request):
order_maximum = int(
request.registry['settings']['sngconnect.payments.order_maximum']
)
token_prices = _get_token_prices(request.registry['settings'])
buy_form = forms.BuyForm(
order_maximum=order_maximum,
csrf_context=request
)
if request.method == 'POST':
buy_form.process(request.POST)
if buy_form.validate():
tokens = buy_form.tokens.data
user_service = UserService(request.registry)
user = user_service.get_user(authenticated_userid(request))
price = _get_price(tokens, token_prices)
order = Order(
status='PLACED',
placed=datetime.datetime.utcnow(),
user=user,
client_email=user.email,
audit_data=json.dumps({
'ip': request.client_addr,
'user_agent': request.user_agent,
'cookies': request.cookies.values(),
'accept_language': str(request.accept_language),
}),
tokens=tokens,
price_net=price['price_net'],
price_tax=price['price_tax'],
price_gross=price['price_gross'],
value_net=price['price_net'] * tokens,
value_tax=price['price_tax'] * tokens,
value_gross=price['price_gross'] * tokens
)
DBSession.add(order)
payment_backend = PayUPaymentBackend(
request,
request.route_url('sngconnect.payments.index'),
request.route_url('sngconnect.payments.confirmation')
)
payment_backend.process_order(order)
return httpexceptions.HTTPSeeOther(
)
return {
'token_prices': token_prices,
'buy_form': buy_form,
}
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:48,代码来源:views.py
示例18: commands
def commands(request):
"""
Issue commands FOR the device
"""
try:
feed_id = int(request.matchdict['feed_id'])
except (KeyError, ValueError):
raise httpexceptions.HTTPNotFound("Invalid request arguments.")
authorize_request(request, feed_id)
commands = DBSession.query(
Command.command,
Command.arguments,
).filter(
Command.feed_id == feed_id,
)
cstruct = schemas.GetCommandsResponse().serialize({
'commands': [
{
'command': command.command,
'arguments': command.arguments,
}
for command in commands
]
})
return Response(
json.dumps(cstruct),
content_type='application/json'
)
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:28,代码来源:views.py
示例19: get_all_feed_users
def get_all_feed_users(self, feed):
return DBSession.query(User).join(
FeedUser
).filter(
FeedUser.feed == feed
).order_by(
self.default_order
).all()
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:8,代码来源:user.py
示例20: validate_email
def validate_email(self, field):
count = DBSession.query(User).filter(
User.email == field.data
).count()
if count != 0:
raise validators.ValidationError(
_("There is already an account registered with this e-mail"
" address.")
)
开发者ID:fikander,项目名称:sngtec-sngconnect,代码行数:9,代码来源:forms.py
注:本文中的sngconnect.database.DBSession类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论