Python django.db.utils.DataError() Examples

The following are 14 code examples of django.db.utils.DataError(). 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 django.db.utils , or try the search function .
Example #1
Source File: effort.py    From richie with MIT License 6 votes vote down vote up
def from_db_value(value, *_args):
        """Convert a database value to a list value."""
        if not value:
            return None

        try:
            duration, unit, reference = value.split("|")
        except ValueError:
            raise DataError(
                "Value in database should be of the form '{duration:d}|{unit:s}|{reference:s}'"
            )

        if not duration:
            return None

        return int(duration), unit, reference 
Example #2
Source File: effort.py    From richie with MIT License 6 votes vote down vote up
def to_python(self, value):
        """Convert a string value to a list value. Used for deserialization and in clean forms."""
        if isinstance(value, (list, tuple)):
            return value

        if not value:
            return None

        if isinstance(value, str):
            try:
                duration, unit, reference = value.split("|")
            except ValueError:
                raise DataError(
                    "Value in database should be of the form '{duration:d}|{unit:s}|{reference:s}'"
                )

            return int(duration.strip()), unit.strip(), reference.strip()

        return value 
Example #3
Source File: duration.py    From richie with MIT License 6 votes vote down vote up
def from_db_value(value, *_args):
        """Convert a database value to a list value."""
        if not value:
            return None

        try:
            duration, unit = value.split("|")
        except ValueError:
            raise DataError(
                "Value in database should be of the form '{duration:d}|{unit:s}'"
            )

        if not duration:
            return None

        return int(duration), unit 
Example #4
Source File: duration.py    From richie with MIT License 6 votes vote down vote up
def to_python(self, value):
        """Convert a string value to a list value. Used for deserialization and in clean forms."""
        if isinstance(value, (list, tuple)):
            return value

        if not value:
            return None

        if isinstance(value, str):
            try:
                duration, unit = value.split("|")
            except ValueError:
                raise DataError(
                    "Value in database should be of the form '{duration:d}|{unit:s}'"
                )

            return int(duration.strip()), unit.strip()

        return value 
Example #5
Source File: test_migration.py    From caluma with GNU General Public License v3.0 6 votes vote down vote up
def test_migrate_to_form_question_natural_key_reverse(transactional_db):
    executor = MigrationExecutor(connection)
    app = "caluma_form"
    migrate_from = [(app, "0024_auto_20190919_1244")]
    migrate_to = [(app, "0023_auto_20190729_1448")]

    executor.migrate(migrate_from)
    old_apps = executor.loader.project_state(migrate_from).apps

    # Create some old data. Can't use factories here

    Form = old_apps.get_model(app, "Form")
    Question = old_apps.get_model(app, "Question")
    FormQuestion = old_apps.get_model(app, "FormQuestion")

    form_1 = Form.objects.create(slug="form-1")

    question_1 = Question.objects.create(type="text", slug="question-1")
    FormQuestion.objects.create(form=form_1, question=question_1)

    # Migrate backwards.
    executor.loader.build_graph()  # reload.
    with pytest.raises(DataError):
        executor.migrate(migrate_to) 
Example #6
Source File: tasks.py    From cornerwise with MIT License 5 votes vote down vote up
def add_street_view(proposal: Proposal, logger=task_logger):
    api_key = getattr(settings, "GOOGLE_API_KEY")
    secret = getattr(settings, "GOOGLE_STREET_VIEW_SECRET")

    if api_key:
        location = "{0.address}, {0.region_name}".format(proposal)
        url = street_view.street_view_url(location, api_key, secret=secret)
        try:
            # Check that there is not already a Street View image associated
            # with this proposal:
            if not proposal.images\
                           .filter(source="google_street_view")\
                           .exists():
                image = proposal.images.create(
                    url=url,
                    skip_cache=True,
                    source="google_street_view",
                    height=640,
                    width=640,
                    priority=1)
                return image
        except IntegrityError:
            logger.warning("Image with that URL already exists: %s", url)
        except DataError:
            logger.error(
                "Image could not be saved.  Was the URL too long? %s", url)

    else:
        logger.warn("Add a local_settings file with your Google API key "
                    "to add Street View images.") 
Example #7
Source File: models.py    From crash with Mozilla Public License 2.0 5 votes vote down vote up
def _set_signature(self, frame_list):
        text = ""
        json_frame_list = json.loads(frame_list)
        if len(json_frame_list) > 0:
            frame = self._find_frame(json_frame_list)
            function = frame['function']
            if len(function) > 0:
                text = function
            else:
                text = frame['lib_name']

        if len(text) is 0:
            text = "Invalid signature"
            logger.warn("could not create a valid signature for %s" % self.crash_id)

        text = text[:255] if len(text) > 255 else text
        signatures = Signature.objects.filter(signature=text)
        if len(signatures) < 1:
            signature = Signature()
            signature.signature = text
            signature.first_observed = self.upload_time
            signature.last_observed = self.upload_time
        else:
            signature = signatures[0]

        if signature.last_observed < self.upload_time:
            signature.last_observed = self.upload_time
        try:
            signature.save()
        except DataError as e:
            logger.error("error trying to save signature %s" % text)
            logger.error(str(e))
        self.signature = signature 
Example #8
Source File: test_enumfield.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_invalid_value(self):
        with pytest.raises(DataError):
            EnumModel.objects.create(field="elephant") 
Example #9
Source File: test_enumfield.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_empty(self):
        with pytest.raises(DataError):
            EnumModel.objects.create() 
Example #10
Source File: test_size_fields.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_binary_1_max_length(self):
        # Okay
        m = SizeFieldModel(binary1=bytes(1) * (2 ** 8 - 1))
        m.save()

        # Bad - Data too long
        m = SizeFieldModel(binary1=bytes(1) * (2 ** 8))
        with pytest.raises(DataError) as excinfo:
            m.save()
        assert excinfo.value.args[0] == 1406 
Example #11
Source File: test_size_fields.py    From django-mysql with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_tinytext_max_length(self):
        # Okay
        m = SizeFieldModel(text1="a" * (2 ** 8 - 1))
        m.save()

        # Bad - Data too long
        m = SizeFieldModel(text1="a" * (2 ** 8))
        with atomic(), pytest.raises(DataError) as excinfo:
            m.save()
        assert excinfo.value.args[0] == 1406 
Example #12
Source File: test_fields.py    From resolwe with Apache License 2.0 4 votes vote down vote up
def test_predefined_long_slugs(self):
        from .fields_test_app.models import TestModel

        slug_max_length = TestModel._meta.get_field("slug").max_length
        max_slug = "x" * slug_max_length

        # Predefined slug shouldn't be trimmed
        obj = TestModel.objects.create(name="Test object", slug=max_slug)
        self.assertEqual(obj.slug, max_slug)

        with self.assertRaisesRegex(DataError, "value too long"):
            TestModel.objects.create(name="Test object", slug=max_slug + "y") 
Example #13
Source File: test_basic.py    From sentry-python with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def test_sql_psycopg2_placeholders(sentry_init, capture_events):
    sentry_init(
        integrations=[DjangoIntegration()],
        send_default_pii=True,
        _experiments={"record_sql_params": True},
    )
    from django.db import connections

    if "postgres" not in connections:
        pytest.skip("postgres tests disabled")

    import psycopg2.sql

    sql = connections["postgres"].cursor()

    events = capture_events()
    with pytest.raises(DataError):
        names = ["foo", "bar"]
        identifiers = [psycopg2.sql.Identifier(name) for name in names]
        placeholders = [
            psycopg2.sql.Placeholder(var) for var in ["first_var", "second_var"]
        ]
        sql.execute("create table my_test_table (foo text, bar date)")

        query = psycopg2.sql.SQL("insert into my_test_table ({}) values ({})").format(
            psycopg2.sql.SQL(", ").join(identifiers),
            psycopg2.sql.SQL(", ").join(placeholders),
        )
        sql.execute(query, {"first_var": "fizz", "second_var": "not a date"})

    capture_message("HI")

    (event,) = events
    for crumb in event["breadcrumbs"]:
        del crumb["timestamp"]

    assert event["breadcrumbs"][-2:] == [
        {
            "category": "query",
            "data": {"db.paramstyle": "format"},
            "message": "create table my_test_table (foo text, bar date)",
            "type": "default",
        },
        {
            "category": "query",
            "data": {
                "db.params": {"first_var": "fizz", "second_var": "not a date"},
                "db.paramstyle": "format",
            },
            "message": 'insert into my_test_table ("foo", "bar") values (%(first_var)s, '
            "%(second_var)s)",
            "type": "default",
        },
    ] 
Example #14
Source File: add_manga.py    From mangaki with GNU Affero General Public License v3.0 4 votes vote down vote up
def run():
    with open('../data/manga-news/manga.csv') as f:
        next(f)
        artists = {}
        hipsters = Counter()
        for i, line in enumerate(f):
            # print(len(line.split(';;')))
            title, vo_title, writer, mangaka, editor, origin, genre1, genre2, manga_type, synopsis, poster = line.split(';;')
            for artist in [writer, mangaka]:
                if artist in artists:
                    continue
                m = re.match('^([A-ZÔÛÏ\'-]+) (.*)$', writer)
                if m:
                    last_name, first_name = m.groups()
                    last_name = last_name.lower().capitalize()
                if not m:
                    first_name = ''
                    last_name = artist
                if Artist.objects.filter(first_name=first_name, last_name=last_name).count() == 0:
                    a = Artist(first_name=first_name, last_name=last_name)
                    a.save()
                else:
                    a = Artist.objects.get(first_name=first_name, last_name=last_name)
                artists[artist] = a
    with open('../data/manga-news/manga.csv') as f:
        next(f)
        for i, line in enumerate(f):
            title, vo_title, writer, mangaka, editor, origin, genre1, genre2, manga_type, synopsis, poster = line.split(';;')
            try:
                if Manga.objects.filter(title=title, vo_title=vo_title).count() == 0:
                    manga = Manga(title=title, vo_title=vo_title, mangaka=artists[mangaka], writer=artists[writer], editor=editor, origin=origin.lower().replace('hong kong', 'hong-kong').replace('international', 'intl'), manga_type=manga_type.lower(), source='', poster=poster, synopsis=synopsis)
                    manga.save()
                else:
                    manga = Manga.objects.get(title=title, vo_title=vo_title)
                if genre1:
                    manga.genre.add(Genre.objects.get(title=genre1))
                if genre2:
                    manga.genre.add(Genre.objects.get(title=genre2))
            except IntegrityError as err:
                print(line)
                print(writer)
                print(err)
                break
            except DataError as err:
                print(line)
                print(origin)
                print(err)
                break
            except Genre.DoesNotExist as err:
                print(line)
                print('Genres: [%s] [%s]' % (genre1, genre2))
                print(err)
                break