def create_or_get(cls, **kwargs):
blob = kwargs.pop('blob')
sha256 = kwargs.pop('sha256', hashlib.sha256(blob).hexdigest())
try:
with cls._meta.database.atomic():
return cls.create(blob=blob, sha256=sha256, **kwargs), True
except peewee.IntegrityError:
try:
return cls.get(sha256=sha256, **kwargs), False
except cls.DoesNotExist: # this could happen with master-slave sync delay
return None, False
评论列表
文章目录