本文整理汇总了Python中multiproduct.model.Product.update方法的典型用法代码示例。如果您正苦于以下问题:Python Product.update方法的具体用法?Python Product.update怎么用?Python Product.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiproduct.model.Product
的用法示例。
在下文中一共展示了Product.update方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ProductTestCase
# 需要导入模块: from multiproduct.model import Product [as 别名]
# 或者: from multiproduct.model.Product import update [as 别名]
class ProductTestCase(unittest.TestCase):
"""Unit tests covering the Product model"""
INITIAL_PREFIX = 'tp'
INITIAL_NAME = 'test project'
INITIAL_DESCRIPTION = 'a test project'
def setUp(self):
self.env = EnvironmentStub(enable=['trac.*', 'multiproduct.*'])
self.env.path = tempfile.mkdtemp(prefix='bh-product-tempenv-')
self.mpsystem = MultiProductSystem(self.env)
try:
self.mpsystem.upgrade_environment(self.env.db_transaction)
except self.env.db_exc.OperationalError:
# table remains but database version is deleted
pass
self.listener = self._enable_resource_change_listener()
self.default_data = {'prefix':self.INITIAL_PREFIX,
'name':self.INITIAL_NAME,
'description':self.INITIAL_DESCRIPTION}
self.global_env = self.env
self.product = Product(self.env)
self.product._data.update(self.default_data)
self.product.insert()
def tearDown(self):
shutil.rmtree(self.env.path)
self.env.reset_db()
def _enable_resource_change_listener(self):
listener = TestResourceChangeListener(self.env)
listener.resource_type = Product
listener.callback = self.listener_callback
return listener
def listener_callback(self, action, resource, context, old_values = None):
# pylint: disable=unused-argument
# pylint: disable=attribute-defined-outside-init
self.prefix = resource.prefix
self.name = resource.name
self.description = resource.description
def test_set_table_field(self):
"""tests that table.field style update works"""
test = {'prefix': 'td',
'name': 'test field access',
'description': 'product to test field setting'}
product = Product(self.env)
# attempt to set the fields from the data
product.prefix = test['prefix']
product.name = test['name']
product.description = test['description']
self.assertEqual(product._data['prefix'], test['prefix'])
self.assertEqual(product._data['name'], test['name'])
self.assertEqual(product._data['description'], test['description'])
def test_select(self):
"""tests that select can search Products by fields"""
p2_data = {'prefix':'tp2',
'name':'test project 2',
'description':'a different test project'}
p3_data = {'prefix':'tp3',
'name':'test project 3',
'description':'test project'}
product2 = Product(self.env)
product2._data.update(p2_data)
product3 = Product(self.env)
product3._data.update(p3_data)
product2.insert()
product3.insert()
products = list(Product.select(self.env, where={'prefix':'tp'}))
self.assertEqual(1, len(products))
products = list(Product.select(self.env,
where={'name':'test project'}))
self.assertEqual(1, len(products))
products = list(Product.select(self.env,
where={'prefix':'tp3', 'name':'test project 3'}))
self.assertEqual(1, len(products))
def test_update(self):
"""tests that we can use update to push data to the database"""
product = list(Product.select(self.env, where={'prefix':'tp'}))[0]
self.assertEqual('test project', product._data['name'])
new_data = {'prefix':'tp',
'name':'updated',
'description':'nothing'}
product._data.update(new_data)
product.update()
comp_product = list(Product.select(self.env, where={'prefix':'tp'}))[0]
#.........这里部分代码省略.........
示例2: _render_admin_panel
# 需要导入模块: from multiproduct.model import Product [as 别名]
# 或者: from multiproduct.model.Product import update [as 别名]
def _render_admin_panel(self, req, cat, page, product):
req.perm.require('PRODUCT_VIEW')
name = req.args.get('name')
description = req.args.get('description','')
prefix = req.args.get('prefix') if product is None else product
owner = req.args.get('owner')
keys = {'prefix':prefix}
field_data = {'name':name,
'description':description,
'owner':owner,
}
data = {}
# Detail view?
if product:
prod = Product(self.env, keys)
if req.method == 'POST':
if req.args.get('save'):
req.perm.require('PRODUCT_MODIFY')
prod.update_field_dict(field_data)
prod.update(req.authname)
add_notice(req, _('Your changes have been saved.'))
req.redirect(req.href.admin(cat, page))
elif req.args.get('cancel'):
req.redirect(req.href.admin(cat, page))
Chrome(self.env).add_wiki_toolbars(req)
data = {'view': 'detail', 'product': prod}
else:
default = self.config.get('ticket', 'default_product')
if req.method == 'POST':
# Add Product
if req.args.get('add'):
req.perm.require('PRODUCT_CREATE')
if not (prefix and name and owner):
if not prefix:
add_warning(req, _("You must provide a prefix "
"for the product."))
if not name:
add_warning(req, _("You must provide a name "
"for the product."))
if not owner:
add_warning(req, _("You must provide an owner "
"for the product."))
data['prefix'] = prefix
data['name'] = name
data['owner'] = owner
else:
try:
prod = Product(self.env, keys)
except ResourceNotFound:
prod = Product(self.env)
prod.update_field_dict(keys)
prod.update_field_dict(field_data)
prod.insert()
add_notice(req, _('The product "%(id)s" has been '
'added.', id=prefix))
req.redirect(req.href.admin(cat, page))
else:
if prod.prefix is None:
raise TracError(_('Invalid product id.'))
raise TracError(_("Product %(id)s already exists.",
id=prefix))
# Remove product
elif req.args.get('remove'):
raise TracError(_('Product removal is not allowed!'))
# Set default product
elif req.args.get('apply'):
prefix = req.args.get('default')
if prefix and prefix != default:
self.log.info("Setting default product to %s",
prefix)
self.config.set('ticket', 'default_product',
prefix)
_save_config(self.config, req, self.log)
req.redirect(req.href.admin(cat, page))
# Clear default product
elif req.args.get('clear'):
self.log.info("Clearing default product")
self.config.set('ticket', 'default_product', '')
_save_config(self.config, req, self.log)
req.redirect(req.href.admin(cat, page))
data['view'] = 'list'
data['products'] = Product.select(self.env)
data['default'] = default
if self.config.getbool('ticket', 'restrict_owner'):
perm = PermissionSystem(self.env)
def valid_owner(username):
return perm.get_user_permissions(username).get('TICKET_MODIFY')
data['owners'] = [username for username, name, email
in self.env.get_known_users()
if valid_owner(username)]
data['owners'].insert(0, '')
data['owners'].sort()
else:
#.........这里部分代码省略.........