oncall-engine/engine/common/public_primary_keys.py
Joey Orlando d6140cbe8d
Re-enable a few mypy rules + fix existing errors (#2725)
# What this PR does

Related to https://github.com/grafana/oncall/issues/2392

- Re-enable the following `mypy` rules + fix their pre-existing errors:
  - `no-redef`
  - `valid-type`
  - `var-annotated`
- Add stronger return typing to the `GrafanaAPIClient` by use of
generics + add some links to documentation in the method docstrings

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-08-03 09:43:03 +00:00

29 lines
1 KiB
Python

import logging
from django.conf import settings
from django.core.exceptions import FieldError
from django.utils.crypto import get_random_string
logger = logging.getLogger(__name__)
def generate_public_primary_key(prefix: str, length: int = settings.PUBLIC_PRIMARY_KEY_MIN_LENGTH) -> str:
return prefix + get_random_string(length=length, allowed_chars=settings.PUBLIC_PRIMARY_KEY_ALLOWED_CHARS)
def increase_public_primary_key_length(
failure_counter: int, prefix: str, model_name: str, max_attempt_count: int = 5
) -> str:
if failure_counter < max_attempt_count:
logger.warning(
f"Let's try increase a {model_name} "
f"new_public_primary_key length "
f"({failure_counter + 1}/{max_attempt_count}) times"
)
return generate_public_primary_key(
prefix=prefix, length=settings.PUBLIC_PRIMARY_KEY_MIN_LENGTH + failure_counter
)
raise FieldError(
f"A count of {model_name} new_public_primary_key generation " f"attempts is more than {max_attempt_count}!"
)