本文整理汇总了Python中peewee.IntegrityError方法的典型用法代码示例。如果您正苦于以下问题:Python peewee.IntegrityError方法的具体用法?Python peewee.IntegrityError怎么用?Python peewee.IntegrityError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类peewee
的用法示例。
在下文中一共展示了peewee.IntegrityError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: put
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def put(self, rasp_result_ins):
"""
将rasp_result_ins序列化并插入数据表
Returns:
插入成功返回True, 重复返回False
Raises:
exceptions.DatabaseError - 数据库错误引发此异常
"""
try:
data = {
"data": rasp_result_ins.dump(),
"data_hash": rasp_result_ins.get_hash()
}
await peewee_async.create_object(self.ResultList, **data)
except peewee.IntegrityError as e:
return False
except asyncio.CancelledError as e:
raise e
except Exception as e:
Logger().critical("DB error in method put!", exc_info=e)
else:
return True
示例2: test_unique_pairs
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def test_unique_pairs(add_doc, add_institution):
"""
Don't allow duplicate links between the same doc -> inst pair.
"""
inst = add_institution()
doc = add_doc()
Institution_Document.create(
institution=inst,
document=doc,
)
with pytest.raises(IntegrityError):
Institution_Document.create(
institution=inst,
document=doc,
)
示例3: test_unique_pairs
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def test_unique_pairs(add_subfield, add_doc):
"""
Don't allow duplicate links between the same field -> document.
"""
s = add_subfield()
d = add_doc()
Subfield_Document.create(
subfield=s,
document=d,
offset=1,
snippet='abc'
)
with pytest.raises(IntegrityError):
Subfield_Document.create(
subfield=s,
document=d,
offset=2,
snippet='def'
)
示例4: find_or_create_derived_storage
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def find_or_create_derived_storage(
source_image, transformation_name, preferred_location, varying_metadata=None
):
existing = find_derived_storage_for_image(source_image, transformation_name, varying_metadata)
if existing is not None:
return existing
uniqueness_hash = _get_uniqueness_hash(varying_metadata)
trans = ImageStorageTransformation.get(name=transformation_name)
new_storage = storage.create_v1_storage(preferred_location)
try:
derived = DerivedStorageForImage.create(
source_image=source_image,
derivative=new_storage,
transformation=trans,
uniqueness_hash=uniqueness_hash,
)
except IntegrityError:
# Storage was created while this method executed. Just return the existing.
ImageStoragePlacement.delete().where(ImageStoragePlacement.storage == new_storage).execute()
new_storage.delete_instance()
return find_derived_storage_for_image(source_image, transformation_name, varying_metadata)
return derived
示例5: post
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def post(self):
"""
Star a repository.
"""
user = get_authenticated_user()
req = request.get_json()
namespace = req["namespace"]
repository = req["repository"]
repo = model.repository.get_repository(namespace, repository)
if repo:
try:
model.repository.star_repository(user, repo)
except IntegrityError:
pass
return {"namespace": namespace, "repository": repository,}, 201
示例6: _attach_service
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def _attach_service(login_service, user_obj, lid, lusername):
"""
Attaches the given user account to the given service, with the given service user ID and service
username.
"""
metadata = {
"service_username": lusername,
}
try:
model.user.attach_federated_login(
user_obj, login_service.service_id(), lid, metadata=metadata
)
return _oauthresult(user_obj=user_obj)
except IntegrityError:
err = "%s account %s is already attached to a %s account" % (
login_service.service_name(),
lusername,
app.config["REGISTRY_TITLE_SHORT"],
)
return _oauthresult(service_name=login_service.service_name(), error_message=err)
示例7: get_or_create
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def get_or_create(cls, **kwargs):
defaults = kwargs.pop('defaults', {})
query = cls.select()
for field, value in kwargs.items():
query = query.where(getattr(cls, field) == value)
try:
result = await query.get(), False
except cls.DoesNotExist:
try:
if defaults:
kwargs.update(defaults)
with cls._meta.database.atomic():
result = await cls.create(**kwargs), True
except IntegrityError as exc:
try:
result = await query.get(), False
except cls.DoesNotExist:
raise exc
return result
示例8: write
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def write(self, stories, team=None):
# TODO
# this should be abstracted here and in spreadsheet.py
new_stories = []
for story in stories:
try:
story = Story.create(
name = story['name'],
slug = story['slug'],
date = story['date'],
url = story['url'],
team = team
)
new_stories.append(story)
except IntegrityError:
# Story probably already exists.
logger.info('Not adding %s to database: probably already exists' % (story['name']))
return new_stories
示例9: write
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def write(self, stories, team=None):
"""
Save rows to the database
"""
new_stories = []
for story in stories:
slug = story['official flavor description'] + ' - ' + story['taster']
try:
story = Story.create(
name=story['name'].strip(),
slug=slug,
date=PockyScraper.parse_date(story['date tasted']),
story_type='pocky',
team=team,
)
logger.info('Added {0}'.format(story.name))
new_stories.append(story)
except IntegrityError:
# Story probably already exists.
logger.info('Not adding %s to database: probably already exists' % (slug))
pass
return new_stories
示例10: get_or_create
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def get_or_create(cls, **kwargs):
defaults = kwargs.pop('defaults', {})
query = cls.select()
for field, value in kwargs.items():
if '__' in field:
query = query.filter(**{field: value})
else:
query = query.where(getattr(cls, field) == value)
try:
return await query.get(), False
except cls.DoesNotExist:
try:
params = dict((k, v) for k, v in kwargs.items()
if '__' not in k)
params.update(defaults)
async with cls._meta.database.atomic():
return await cls.create(**params), True
except IntegrityError as exc:
try:
return await query.get(), False
except cls.DoesNotExist:
raise exc
示例11: sync
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def sync(cls):
"""Sync cache by latest data"""
def sync_tables(tables):
for table in tables:
table.sync()
click.echo(".", nl=False, err=True)
cache_table_map = cls.get_cache_tables()
tables = list(cache_table_map.values())
# Inserting version table at start
tables.insert(0, Version)
try:
LOG.info("Updating cache", nl=False)
sync_tables(tables)
except (OperationalError, IntegrityError):
click.echo(" [Fail]")
# init db handle once (recreating db if some schema changes are there)
LOG.info("Removing existing db and updating cache again")
init_db_handle()
LOG.info("Updating cache", nl=False)
sync_tables(tables)
click.echo(" [Done]", err=True)
示例12: create_single
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def create_single(self, model, kwargs):
model = self.get_model(model)
try:
item = model.create(**kwargs)
except IntegrityError:
item = None
return item
示例13: update_single
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def update_single(self, model, item, kwargs):
model = self.get_model(model)
try:
kwargs['updated'] = now()
query = model.update(**kwargs).where(model.id == item.id)
query.execute()
except IntegrityError:
item = None
return item
示例14: create
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def create(self, key, value, ttl=0):
try:
item = Cacheable.create(key=key, value=value.strip(), ttl=ttl)
except IntegrityError:
item = None
return item
示例15: update
# 需要导入模块: import peewee [as 别名]
# 或者: from peewee import IntegrityError [as 别名]
def update(self, item, value, ttl=0):
kwargs = {
'value': value.strip(),
'ttl': ttl,
'updated': now()
}
try:
query = Cacheable.update(**kwargs).where(Cacheable.key == item.key)
query.execute()
except IntegrityError:
pass
return item