%PDF- %PDF-
Direktori : /opt/imunify360/venv/lib64/python3.8/site-packages/defence360agent/model/ |
Current File : //opt/imunify360/venv/lib64/python3.8/site-packages/defence360agent/model/__init__.py |
""" This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. Copyright © 2019 Cloud Linux Software Inc. This software is also available under ImunifyAV commercial license, see <https://www.imunify360.com/legal/eula> """ from peewee import IntegrityError, Model as BaseModel class Model(BaseModel): """ Common Model class that fix create_or_get method with using CompositeKey. https://stackoverflow.com/questions/35167628/peewee-create-or-get-error-in-model-with-compositekey """ @classmethod def create_or_get(cls, **kwargs): try: with cls._meta.database.atomic(): return cls.create(**kwargs), True except IntegrityError: query = [] for field_name, value in kwargs.items(): field = getattr(cls, field_name) field_is_primary_key = ( field.name in cls._meta.primary_key.field_names if cls._meta.composite_key else field.primary_key ) if field.unique or field_is_primary_key: query.append(field == value) return cls.get(*query), False