Python mongoengine.DoesNotExist() Examples

The following are 30 code examples of mongoengine.DoesNotExist(). 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: event.py    From cascade-server with Apache License 2.0 6 votes vote down vote up
def update_host(self):
        try:
            host = self.host
        except DoesNotExist:
            host = None
        hostname = self.state.hostname.upper()

        if not isinstance(host, Host):
            host = Host.update_existing(hostname=hostname)
            self.modify(host=host)

        if self.state.fqdn is not None and host.fqdn is None:
            host.update(fqdn=self.state.fqdn)

        elif host.fqdn is not None and self.state.fqdn is None:
            self.modify(state__fqdn=host.fqdn.upper())

        if 'user' in self.fields and self and self.state.user:
            host.update(add_to_set__users=self.state.user) 
Example #2
Source File: service.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #3
Source File: service.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #4
Source File: views.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def update_news():
    try:
        post_data = request.get_json()
        news = News.objects.get(id=post_data['news_id'])
        news.update(
            title=post_data.get('title', news.title),
            content=post_data.get('content', news.content),
            author=post_data.get('author', news.author),
            tags=post_data.get('tags', news.tags),
        )
        news.reload()
        response_object = {
            'status': 'success',
            'news': news,
        }
        return jsonify(response_object), 200
    except mongoengine.DoesNotExist:
        return jsonify(response_object), 404 
Example #5
Source File: service.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #6
Source File: services.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #7
Source File: services.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #8
Source File: services.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #9
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 #10
Source File: models.py    From mykrobe with MIT License 6 votes vote down vote up
def _get_or_create_variant(self, record):
        try:
            var_hash = make_var_hash(record.REF, record.POS, [
                str(a) for a in record.ALT])
            v = Variant.objects.get(var_hash=var_hash)
            v.add_to_variant_set(self.vcf_variant_set)
        except DoesNotExist:
            try:
                reference = self.references[record.CHROM]
            except KeyError as e:
                raise KeyError(
                    "Reference %s cannot be found in reference set %s (%s). Please add it to the database." %
                    (record.CHROM, self.reference_set.id, self.reference_set.name))
            v = Variant.create_and_save(
                variant_sets=self.variant_sets,
                start=record.POS,
                reference_bases=record.REF,
                alternate_bases=[
                    str(a) for a in record.ALT],
                reference=reference,
                names=[record.ID])
        return v 
Example #11
Source File: views.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def update_news():
    try:
        post_data = request.get_json()
        news = News.objects.get(id=post_data['news_id'])
        news.update(
            title=post_data.get('title', news.title),
            content=post_data.get('content', news.content),
            author=post_data.get('author', news.author),
            tags=post_data.get('tags', news.tags),
        )
        news.reload()
        response_object = {
            'status': 'success',
            'news': news,
        }
        return jsonify(response_object), 200
    except mongoengine.DoesNotExist:
        return jsonify(response_object), 404 
Example #12
Source File: services.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #13
Source File: target_test.py    From Arsenal with GNU General Public License v3.0 6 votes vote down vote up
def test_target_rename(self):
        """
        Tests the RenameTarget API function.
        """
        target = Database.create_target('NOTTHIS')
        data = APIClient.rename_target(self.client, target.name, 'TEST')
        self.assertEqual(data['error'], False)
        target = Database.get_target('TEST')
        self.assertIsNotNone(target)
        self.assertEqual(target.name, 'TEST')
        with self.assertRaises(DoesNotExist):
            Database.get_target('NOTTHIS')

        target2 = Database.create_target()
        data = APIClient.rename_target(self.client, target2.name, 'TEST')
        self.assertEqual(data['error'], True)
        self.assertEqual(data['error_type'], 'cannot-rename-target')
        self.assertIsNotNone(Database.get_target(target2.name)) 
Example #14
Source File: target_test.py    From Arsenal with GNU General Public License v3.0 6 votes vote down vote up
def test_create(self):
        """
        This test will pass if the target is created.
        """

        with self.assertRaises(DoesNotExist):
            Database.get_target('TEST Target')

        data = APIClient.create_target(
            self.client,
            'TEST Target',
            'AA:BB:CC:DD:EE:FF',
            {'test_fact': 'hello'})

        self.assertEqual(False, data['error'])

        target = Database.get_target('TEST Target')
        self.assertIsNotNone(target)
        self.assertEqual(target.name, 'TEST Target')
        self.assertEqual('AA:BB:CC:DD:EE:FF', target.uuid)
        self.assertDictEqual({'test_fact': 'hello'}, target.facts) 
Example #15
Source File: services.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #16
Source File: service.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #17
Source File: service.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #18
Source File: service.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #19
Source File: service.py    From Microservice-Patterns-and-Best-Practices with MIT License 6 votes vote down vote up
def normalize_db(self, data):
        try:
            news = QueryNewsModel.objects.get(
                id=data['id']
            )
            news.update(
                version=data.get('version', news.version),
                title=data.get('title', news.title),
                content=data.get('content', news.content),
                author=data.get('author', news.author),
                published_at=data.get('published_at', news.published_at),
                tags=data.get('tags', news.tags),
            )
            news.reload()
        except mongoengine.DoesNotExist:
            QueryNewsModel(
                id=data['id'],
                version=data['version'],
                title=data.get('title'),
                content=data.get('content'),
                author=data.get('author'),
                tags=data.get('tags'),
            ).save()
        except Exception as e:
            return e 
Example #20
Source File: aiohttp_fetcher.py    From web_develop with GNU General Public License v3.0 6 votes vote down vote up
def fetch(retry=0):
    proxy = 'http://{}'.format(Proxy.get_random()['address'])
    headers = {'user-agent': get_user_agent()}
    conn = aiohttp.ProxyConnector(proxy=proxy)

    url = 'http://httpbin.org/ip'

    try:
        with aiohttp.ClientSession(connector=conn) as session:
            with aiohttp.Timeout(TIMEOUT):
                async with session.get(url, headers=headers) as resp:
                    return await resp.json()
    except (ProxyConnectionError, TimeoutError):
        try:
            p = Proxy.objects.get(address=proxy)
            if p:
                p.delete()
        except DoesNotExist:
            pass
        retry += 1
        if retry > 5:
            raise TimeoutError()
        await asyncio.sleep(1)
        return await fetch(retry=retry) 
Example #21
Source File: service.py    From Microservice-Patterns-and-Best-Practices with MIT License 5 votes vote down vote up
def get_news(self, id):
        try:
            news = QueryNewsModel.objects.get(id=id)
            return news.to_json()
        except mongoengine.DoesNotExist as e:
            return e
        except Exception as e:
            return e 
Example #22
Source File: service.py    From Microservice-Patterns-and-Best-Practices with MIT License 5 votes vote down vote up
def get_news(self, id):
        try:
            news = QueryNewsModel.objects.get(id=id)
            return news.to_json()
        except mongoengine.DoesNotExist as e:
            return e
        except Exception as e:
            return e 
Example #23
Source File: views.py    From Microservice-Patterns-and-Best-Practices with MIT License 5 votes vote down vote up
def publish_news(news_id):
    try:
        news = News.objects.get(id=news_id)
        news.update(published_at=datetime.datetime.now)
        news.reload()
        response_object = {
            'status': 'success',
            'news': news,
        }
        return jsonify(response_object), 200
    except mongoengine.DoesNotExist:
        return jsonify(response_object), 404 
Example #24
Source File: views.py    From Microservice-Patterns-and-Best-Practices with MIT License 5 votes vote down vote up
def get_single_news(news_id):
    """Get single user details"""
    response_object = {
        'status': 'fail',
        'message': 'User does not exist'
    }
    try:
        news = News.objects.get(id=news_id)
        response_object = {
            'status': 'success',
            'data': news,
        }
        return jsonify(response_object), 200
    except mongoengine.DoesNotExist:
        return jsonify(response_object), 404 
Example #25
Source File: services.py    From Microservice-Patterns-and-Best-Practices with MIT License 5 votes vote down vote up
def get_news(self, id):
        try:
            news = QueryNewsModel.objects.get(id=id)
            return news.to_json()
        except mongoengine.DoesNotExist as e:
            return e
        except Exception as e:
            return e 
Example #26
Source File: views.py    From Microservice-Patterns-and-Best-Practices with MIT License 5 votes vote down vote up
def publish_news(news_id):
    try:
        news = News.objects.get(id=news_id)
        news.update(published_at=datetime.datetime.now)
        news.reload()
        response_object = {
            'status': 'success',
            'news': news,
        }
        return jsonify(response_object), 200
    except mongoengine.DoesNotExist:
        return jsonify(response_object), 404 
Example #27
Source File: service.py    From Microservice-Patterns-and-Best-Practices with MIT License 5 votes vote down vote up
def get_news(self, id):
        try:
            news = QueryNewsModel.objects.get(id=id)
            return news.to_json()
        except mongoengine.DoesNotExist as e:
            return e
        except Exception as e:
            return e 
Example #28
Source File: views.py    From Microservice-Patterns-and-Best-Practices with MIT License 5 votes vote down vote up
def get_single_news(news_id):
    """Get single user details"""
    response_object = {
        'status': 'fail',
        'message': 'User does not exist'
    }
    try:
        news = News.objects.get(id=news_id)
        response_object = {
            'status': 'success',
            'data': news,
        }
        return jsonify(response_object), 200
    except mongoengine.DoesNotExist:
        return jsonify(response_object), 404 
Example #29
Source File: services.py    From Microservice-Patterns-and-Best-Practices with MIT License 5 votes vote down vote up
def get_news(self, id):
        try:
            news = QueryNewsModel.objects.get(id=id)
            return news.to_json()
        except mongoengine.DoesNotExist as e:
            return e
        except Exception as e:
            return e 
Example #30
Source File: service.py    From Microservice-Patterns-and-Best-Practices with MIT License 5 votes vote down vote up
def get_news(self, id):
        try:
            news = QueryNewsModel.objects.get(id=id)
            return news.to_json()
        except mongoengine.DoesNotExist as e:
            return e
        except Exception as e:
            return e