Python mongoengine.NotUniqueError() Examples

The following are 18 code examples of mongoengine.NotUniqueError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mongoengine , or try the search function .
Example #1
Source File: makeprobes.py    From mykrobe with MIT License 6 votes vote down vote up
def run_make_probes_from_vcf_file(args):
    # Make VariantSet from vcf
    reference = os.path.basename(args.reference_filepath).split(".fa")[0]
    try:
        reference_set = ReferenceSet.objects.get(name=reference)
    except DoesNotExist:
        reference_set = ReferenceSet.create_and_save(name=reference)
        # Hack
    try:
        reference = Reference.create_and_save(
            name=reference, reference_sets=[reference_set], md5checksum=reference
        )
    except NotUniqueError:
        pass
    vcf = VCF(
        args.vcf,
        reference_set.id,
        method="tmp",
        force=True,
        append_to_global_variant_set=False,
    )
    vcf.add_to_database() 
Example #2
Source File: proxy_fetcher_with_queue.py    From web_develop with GNU General Public License v3.0 6 votes vote down vote up
def save_proxies(url):
    proxies = []
    try:
        r = fetch(url)
    except requests.exceptions.RequestException:
        return False
    addresses = re.findall(PROXY_REGEX, r.text)
    for address in addresses:
        proxy = Proxy(address=address)
        try:
            proxy.save()
        except NotUniqueError:
            pass
        else:
            proxies.append(address)
    return proxies 
Example #3
Source File: search_result.py    From web_develop with GNU General Public License v3.0 6 votes vote down vote up
def save_article(article_):
    img_url = article_.find(class_='img_box2').find(
        'img').attrs['src'].split('url=')[1]
    text_box = article_.find(class_='txt-box')
    title = text_box.find('h4').find('a').text
    article_url = text_box.find('h4').find('a').attrs['href']
    summary = text_box.find('p').text
    create_at = datetime.fromtimestamp(float(text_box.find(
        class_='s-p').attrs['t']))
    publisher_name = text_box.find(class_='s-p').find('a').attrs['title']

    article = Article(img_url=img_url, title=title, article_url=article_url,
                      summary=summary, create_at=create_at,
                      publisher=Publisher.get_or_create(publisher_name))
    try:
        article.save()
    except (NotUniqueError, InvalidBSON):
        pass 
Example #4
Source File: search_result_with_lock.py    From web_develop with GNU General Public License v3.0 6 votes vote down vote up
def save_article(article_):
    img_url = article_.find(class_='img_box2').find(
        'img').attrs['src'].split('url=')[1]
    text_box = article_.find(class_='txt-box')
    title = text_box.find('h4').find('a').text
    article_url = text_box.find('h4').find('a').attrs['href']
    summary = text_box.find('p').text
    create_at = datetime.fromtimestamp(float(text_box.find(
        class_='s-p').attrs['t']))
    publisher_name = text_box.find(class_='s-p').find('a').attrs['title']

    article = Article(img_url=img_url, title=title, article_url=article_url,
                      summary=summary, create_at=create_at,
                      publisher=Publisher.get_or_create(publisher_name))
    try:
        article.save()
    except (NotUniqueError, InvalidBSON):
        pass 
Example #5
Source File: migrations.py    From st2 with Apache License 2.0 6 votes vote down vote up
def insert_system_roles():
    """
    Migration which inserts the default system roles.
    """
    system_roles = SystemRole.get_valid_values()

    LOG.debug('Inserting system roles (%s)' % (str(system_roles)))

    for role_name in system_roles:
        description = role_name
        role_db = RoleDB(name=role_name, description=description, system=True)

        try:
            role_db.save()
        except (StackStormDBObjectConflictError, NotUniqueError):
            # Role already exists error is not fatal
            pass 
Example #6
Source File: models.py    From mykrobe with MIT License 6 votes vote down vote up
def _create_call_sets(self):
        for sample in self.vcf_reader.samples:
            try:
                cs = VariantCallSet.create_and_save(
                    name="_".join(
                        [
                            sample,
                            self.method]),
                    variant_sets=self.variant_sets,
                    sample_id=sample,
                    info={
                        "variant_caller": self.method})
            except NotUniqueError:
                raise ValueError(
                    "There is already a call set for sample %s with method %s " %
                    (sample, self.method))
            else:
                self.call_sets[sample] = cs 
Example #7
Source File: target_test.py    From Arsenal with GNU General Public License v3.0 6 votes vote down vote up
def test_create_dup_uuid_fail(self):
        """
        This test will attempt to create targets with the same uuids,
        and it will fail as mongo should throw a not unique exception.
        """

        # basic test
        with self.assertRaises(NotUniqueError):
            target1 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF')
            target2 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF')
            self.assertEqual(target1.uuid, target2.uuid)

        # different encoding
        with self.assertRaises(NotUniqueError):
            target1 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF'.encode('utf-8'))
            target2 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF'.encode('ascii'))
            self.assertEqual(target1.uuid, target2.uuid) 
Example #8
Source File: group_management.py    From yeti with Apache License 2.0 5 votes vote down vote up
def create_group(groupname):
    try:
        return Group(groupname=groupname).save()
    except NotUniqueError:
        return False 
Example #9
Source File: proxy_fetcher.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def save_proxies(url):
    try:
        r = fetch(url)
    except requests.exceptions.RequestException:
        return False
    addresses = re.findall(PROXY_REGEX, r.text)
    for address in addresses:
        proxy = Proxy(address=address)
        try:
            proxy.save()
        except NotUniqueError:
            pass 
Example #10
Source File: base.py    From st2 with Apache License 2.0 5 votes vote down vote up
def insert(cls, model_object, publish=True, dispatch_trigger=True,
               log_not_unique_error_as_debug=False):
        # Late import to avoid very expensive in-direct import (~1 second) when this function
        # is not called / used
        from mongoengine import NotUniqueError

        if model_object.id:
            raise ValueError('id for object %s was unexpected.' % model_object)
        try:
            model_object = cls._get_impl().insert(model_object)
        except NotUniqueError as e:
            if log_not_unique_error_as_debug:
                LOG.debug('Conflict while trying to save in DB: %s.', six.text_type(e))
            else:
                LOG.exception('Conflict while trying to save in DB.')
            # On a conflict determine the conflicting object and return its id in
            # the raised exception.
            conflict_object = cls._get_by_object(model_object)
            conflict_id = str(conflict_object.id) if conflict_object else None
            message = six.text_type(e)
            raise StackStormDBObjectConflictError(message=message, conflict_id=conflict_id,
                                                  model_object=model_object)

        # Publish internal event on the message bus
        if publish:
            try:
                cls.publish_create(model_object)
            except:
                LOG.exception('Publish failed.')

        # Dispatch trigger
        if dispatch_trigger:
            try:
                cls.dispatch_create_trigger(model_object)
            except:
                LOG.exception('Trigger dispatch failed.')

        return model_object 
Example #11
Source File: triggers.py    From st2 with Apache License 2.0 5 votes vote down vote up
def _register_internal_trigger_type(trigger_definition):
    try:
        trigger_type_db = create_trigger_type_db(trigger_type=trigger_definition,
                                                 log_not_unique_error_as_debug=True)
    except (NotUniqueError, StackStormDBObjectConflictError):
        # We ignore conflict error since this operation is idempotent and race is not an issue
        LOG.debug('Internal trigger type "%s" already exists, ignoring error...' %
                  (trigger_definition['name']))

        ref = ResourceReference.to_string_reference(name=trigger_definition['name'],
                                                    pack=trigger_definition['pack'])
        trigger_type_db = get_trigger_type_db(ref)

    if trigger_type_db:
        LOG.debug('Registered internal trigger: %s.', trigger_definition['name'])

    # trigger types with parameters do no require a shadow trigger.
    if trigger_type_db and not trigger_type_db.parameters_schema:
        try:
            trigger_db = create_shadow_trigger(trigger_type_db,
                log_not_unique_error_as_debug=True)

            extra = {'trigger_db': trigger_db}
            LOG.audit('Trigger created for parameter-less internal TriggerType. Trigger.id=%s' %
                      (trigger_db.id), extra=extra)
        except (NotUniqueError, StackStormDBObjectConflictError):
            LOG.debug('Shadow trigger "%s" already exists. Ignoring.',
                      trigger_type_db.get_reference().ref, exc_info=True)

        except (ValidationError, ValueError):
            LOG.exception('Validation failed in shadow trigger. TriggerType=%s.',
                          trigger_type_db.get_reference().ref)
            raise

    return trigger_type_db 
Example #12
Source File: triggers.py    From st2 with Apache License 2.0 5 votes vote down vote up
def register_internal_trigger_types():
    """
    Register internal trigger types.

    NOTE 1: This method blocks until all the trigger types have been registered.

    NOTE 2: We log "NotUniqueError" errors under debug and not error. Those errors are not fatal
    because this operation is idempotent and NotUniqueError simply means internal trigger type
    has already been registered by some other service.
    """
    action_sensor_enabled = cfg.CONF.action_sensor.enable

    registered_trigger_types_db = []

    for _, trigger_definitions in six.iteritems(INTERNAL_TRIGGER_TYPES):
        for trigger_definition in trigger_definitions:
            LOG.debug('Registering internal trigger: %s', trigger_definition['name'])

            is_action_trigger = trigger_definition['name'] == ACTION_SENSOR_TRIGGER['name']
            if is_action_trigger and not action_sensor_enabled:
                continue
            try:
                trigger_type_db = _register_internal_trigger_type(
                    trigger_definition=trigger_definition)
            except Exception:
                LOG.exception('Failed registering internal trigger: %s.', trigger_definition)
                raise
            else:
                registered_trigger_types_db.append(trigger_type_db)

    return registered_trigger_types_db 
Example #13
Source File: target_test.py    From Arsenal with GNU General Public License v3.0 5 votes vote down vote up
def test_create_dup_name_fail(self):
        """
        This test will attempt to create two targets with identitical names,
        and it will fail as Mongo should throw a not unique exception.
        """
        with self.assertRaises(NotUniqueError):
            target1 = Database.create_target(self.TEST_NAME)
            target2 = Database.create_target(self.TEST_NAME)
            self.assertEqual(target1.name, target2.name) 
Example #14
Source File: crispr.py    From SelfTarget with MIT License 5 votes vote down vote up
def map_ids_from_line(self, line: CrisprLine, mode, wges_dict=None):
        crispr: Crispr = self.create_crispr_from_crispr_line(line)
        try:
            crispr.extract_and_save_wge(mode, wges_dict)
        except NoWGEException as ex:
            logger.error(ex.msg())
            pass
        except (DuplicateKeyError, NotUniqueError) as ex:
            logger.warning("Skipping duplicate wge_id")
            pass
        except Exception as e:
            traceback.print_exc()
            logger.error(f"A problem happened for {self.filename}, oligo_id {crispr.crispr_line.get_oligo_id}")
            pass 
Example #15
Source File: models.py    From mykrobe with MIT License 5 votes vote down vote up
def _create_new_variant_set(self):
        variant_set_name = os.path.basename(
            self.f)
        if VariantSet.objects(
                name=variant_set_name,
                reference_set=self.reference_set):
            if not self.force:
                raise NotUniqueError(
                    "VariantSet %s already exists. Rerun with -f to recreate." %
                    variant_set_name)
            else:
                self._remove_variant_set(variant_set_name)
        self.vcf_variant_set = VariantSet.create_and_save(
            name=variant_set_name,
            reference_set=self.reference_set) 
Example #16
Source File: base.py    From st2 with Apache License 2.0 4 votes vote down vote up
def add_or_update(cls, model_object, publish=True, dispatch_trigger=True, validate=True,
                      log_not_unique_error_as_debug=False):
        # Late import to avoid very expensive in-direct import (~1 second) when this function
        # is not called / used
        from mongoengine import NotUniqueError

        pre_persist_id = model_object.id
        try:
            model_object = cls._get_impl().add_or_update(model_object, validate=True)
        except NotUniqueError as e:
            if log_not_unique_error_as_debug:
                LOG.debug('Conflict while trying to save in DB: %s.', six.text_type(e))
            else:
                LOG.exception('Conflict while trying to save in DB.')
            # On a conflict determine the conflicting object and return its id in
            # the raised exception.
            conflict_object = cls._get_by_object(model_object)
            conflict_id = str(conflict_object.id) if conflict_object else None
            message = six.text_type(e)
            raise StackStormDBObjectConflictError(message=message, conflict_id=conflict_id,
                                                  model_object=model_object)

        is_update = str(pre_persist_id) == str(model_object.id)

        # Publish internal event on the message bus
        if publish:
            try:
                if is_update:
                    cls.publish_update(model_object)
                else:
                    cls.publish_create(model_object)
            except:
                LOG.exception('Publish failed.')

        # Dispatch trigger
        if dispatch_trigger:
            try:
                if is_update:
                    cls.dispatch_update_trigger(model_object)
                else:
                    cls.dispatch_create_trigger(model_object)
            except:
                LOG.exception('Trigger dispatch failed.')

        return model_object 
Example #17
Source File: api.py    From cascade-server with Apache License 2.0 4 votes vote down vote up
def api(uri, login=False, **kwargs):
    def decorator(f):
        @wraps(f)
        def wrapped_f(*func_args, **func_kwargs):
            if login:
                user_token = request.cookies.get('user-token')
                if user_token is not None:
                    try:
                        func_kwargs['user'] = users.validate_token(user_token)
                    except utils.AuthenticationError:
                        return JSONResponse(status=httplib.UNAUTHORIZED)
                else:
                    return JSONResponse(status=httplib.UNAUTHORIZED)

            try:
                results, status_code = f(*func_args, **func_kwargs)
                if 'count' in request.args:
                    if isinstance(results, BaseQuerySet):
                        results = results.count()
                    else:
                        results = len(results)

            except mongoengine.ValidationError:
                traceback.print_exc()
                status_code = httplib.BAD_REQUEST
                results = {"error": "invalid input"}

            except mongoengine.NotUniqueError:
                traceback.print_exc()
                status_code = httplib.BAD_REQUEST
                results = {"error": "not unique"}

            except Exception as e:
                traceback.print_exc()
                status_code = httplib.INTERNAL_SERVER_ERROR
                results = None

            if request.args.get('format', 'json') == 'bson':
                output = bsonify(results)  # .json_util.dumps(results, indent=2, sort_keys=True)
                return Response(output, status=status_code, content_type='application/json')
            else:
                return JSONResponse(results, status=status_code)

        endpoint = kwargs.pop('endpoint', f.__name__)  # + str(len(api_endpoints)))
        app.add_url_rule(uri, endpoint, wrapped_f, **kwargs)
        assert endpoint not in api_endpoints
        api_endpoints[endpoint] = f, uri, kwargs.get('methods', ['GET']), rest_doc(f)
        return f
    return decorator 
Example #18
Source File: generic.py    From yeti with Apache License 2.0 4 votes vote down vote up
def handle_form(self, id=None, klass=None, skip_validation=False):
        if klass:  # create
            obj = klass()
            form = klass.get_form()(request.form)
        else:  # update
            obj = self.klass.objects.get(id=id)
            klass = obj.__class__
            form = klass.get_form()(request.form, initial=obj._data)
        if form.validate():
            form.populate_obj(obj)
            try:
                obj = self.create_obj(obj, skip_validation)
                if form.formdata.get("sharing") and hasattr(klass, "sharing_permissions"):
                    obj.sharing_permissions(form.formdata["sharing"], invest_id=obj.id)
            except GenericValidationError as e:
                # failure - redirect to edit page
                form.errors['General Error'] = [e]
                return render_template(
                    "{}/edit.html".format(self.klass.__name__.lower()),
                    form=form,
                    obj_type=klass.__name__,
                    obj=None,
                    groups=get_user_groups())
            except NotUniqueError:
                form.errors['Duplicate'] = [
                    'Entity "{}" is already in the database'.format(obj)
                ]
                return render_template(
                    "{}/edit.html".format(self.klass.__name__.lower()),
                    form=form,
                    obj_type=klass.__name__,
                    obj=None,
                    groups=get_user_groups())

            # success - redirect to view page
            return redirect(
                url_for(
                    'frontend.{}:get'.format(self.__class__.__name__),
                    id=obj.id))
        else:
            return render_template(
                "{}/edit.html".format(self.klass.__name__.lower()),
                form=form,
                obj_type=klass.__name__,
                obj=obj)