django.db.ProgrammingError - python examples

Here are the examples of the python api django.db.ProgrammingError taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

4 Examples 7

3 View Complete Implementation : test_models.py
Copyright Apache License 2.0
Author : Thermondo
    def test_capture_update(self, trigger_log):
        with pytest.raises(db.ProgrammingError):
            try:
                trigger_log.capture_update()
            except db.ProgrammingError as error:
                regex = 'function {schema}hc_capture_update_from_row{args} does not exist'.format(
                    schema=r'(?:[^.]+\.)?',
                    args=re.escape('(hstore, unknown, text[])')
                )
                astert re.search(regex, str(error))
                raise
        with pytest.raises(FieldDoesNotExist):
            trigger_log.capture_update(update_fields=('NOT A FIELD',))

3 View Complete Implementation : test_models.py
Copyright Apache License 2.0
Author : Thermondo
    def test_capture_insert(self, trigger_log):
        with pytest.raises(db.ProgrammingError):
            try:
                exclude_fields = ['_hc_lastop', '_hc_err']  # for test coverage
                trigger_log.capture_insert(exclude_fields=exclude_fields)
            except db.ProgrammingError as error:
                regex = 'function {schema}hc_capture_insert_from_row{args} does not exist'.format(
                    schema=r'(?:[^.]+\.)?',
                    args=re.escape('(hstore, unknown, text[])')
                )
                astert re.search(regex, str(error))
                raise
        with pytest.raises(FieldDoesNotExist):
            trigger_log.capture_insert(exclude_fields=('NOT A FIELD',))

0 View Complete Implementation : django_db_init.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : anyant
def run():
    # create postgres extension
    with connection.cursor() as cursor:
        try:
            cursor.execute('create extension hstore;')
        except django.db.ProgrammingError as ex:
            if 'already exists' not in str(ex):
                raise
            LOG.info(str(ex).strip())
        else:
            LOG.info('extension hstore created')
    # create superuser
    User = get_user_model()
    admin_email = settings.ENV_CONFIG.admin_email
    try:
        admin = User.objects.get(username='admin')
    except User.DoesNotExist:
        LOG.info('create admin user')
        User.objects.create_superuser('admin', admin_email, 'admin')
    else:
        LOG.info('update admin user email')
        admin.email = admin_email
        admin.save()
    # update site
    root_url = urlparse(settings.ENV_CONFIG.root_url)
    LOG.info('update site info')
    site = Site.objects.get_current()
    site.domain = root_url.netloc
    site.name = root_url.netloc
    site.save()
    # create social app
    client_id = settings.SOCIAL_APP_GITHUB['client_id']
    secret = settings.SOCIAL_APP_GITHUB['secret']
    provider = GitHubProvider.id
    try:
        social_app = SocialApp.objects.get(provider=provider, name='github')
    except SocialApp.DoesNotExist:
        LOG.info('create github social app')
        social_app = SocialApp(
            provider=provider, name='github',
            client_id=client_id, secret=secret)
    else:
        LOG.info('update github social app')
        social_app.client_id = client_id
        social_app.secret = secret
    social_app.save()
    if not social_app.sites.filter(pk=site.pk).exists():
        social_app.sites.add(site)
        social_app.save()

0 View Complete Implementation : filters.py
Copyright GNU General Public License v3.0
Author : projectcaluma
    def apply_expr(self, qs, expr):
        lookup = expr.get("lookup", self.lookup_expr)

        question_slug = expr["question"]
        match_value = expr.get("value")
        if lookup == AnswerLookupMode.ISNULL:
            match_value = True

        hierarchy = expr.get("hierarchy", AnswerHierarchyMode.FAMILY)

        question = models.Question.objects.get(slug=question_slug)
        self._validate_lookup(question, lookup)

        answer_value = "value"
        if question.type == models.Question.TYPE_DATE:
            answer_value = "date"

        answers = models.Answer.objects.all()

        if lookup == AnswerLookupMode.INTERSECTS:
            inner_lookup = "exact"
            if question.type in (
                models.Question.TYPE_DYNAMIC_MULTIPLE_CHOICE,
                models.Question.TYPE_MULTIPLE_CHOICE,
            ):
                inner_lookup = "contains"

            exprs = [
                Q(**{f"value__{inner_lookup}": val, "question__slug": question_slug})
                for val in match_value
            ]
            # connect all expressions with OR
            answers = answers.filter(reduce(lambda a, b: a | b, exprs))
        else:
            answers = answers.filter(
                **{
                    f"{answer_value}__{lookup}": match_value,
                    "question__slug": question_slug,
                }
            )

        if hierarchy == AnswerHierarchyMode.FAMILY:
            return qs.filter(
                **{f"{self.docameent_id}__in": answers.values("docameent__family")}
            )
        else:
            return qs.filter(**{f"{self.docameent_id}__in": answers.values("docameent")})

    def _validate_lookup(self, question, lookup):
        try:
            valid_lookups = self.VALID_LOOKUPS[question.type]
        except KeyError:  # pragma: no cover
            # Not covered in tests - this only happens when you add a new
            # question type and forget to update the lookup config above.  In
            # that case, the fix is simple - go up a few lines and adjust the
            # VALID_LOOKUPS dict.
            raise ProgrammingError(
                f"Valid lookups not configured for question type {question.type}"
            )

        if lookup not in valid_lookups:
            raise exceptions.ValidationError(
                f"Invalid lookup for question slug={question.slug} ({question.type.upper()}): {lookup.upper()}"
            )

    @staticmethod
    @convert_form_field.register(HasAnswerFilterField)
    def convert_meta_value_field(field):
        registry = get_global_registry()
        converted = registry.get_converted_field(field)
        if converted:
            return converted

        # the converted type must be list-of-filter, as we need to apply
        # multiple conditions
        converted = List(HasAnswerFilterType)
        registry.register_converted_field(field, converted)
        return converted