This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.db.models import CharField | |
| from django.db.models import Func | |
| from django.db.models import Value | |
| from django.db.models.functions import Cast | |
| class JSONExtractAndCast(Cast): | |
| def __init__(self, expression, *paths, output_field): | |
| # borrowed from django_mysql.models.functions.JSONExtract | |
| exprs = [expression] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| import random | |
| def create_random_datetime_between_dates( | |
| start: datetime.datetime, | |
| end: datetime.datetime, | |
| ) -> datetime.datetime: | |
| random_timestamp = random.uniform(start.timestamp(), end.timestamp()) | |
| return datetime.datetime.fromtimestamp(random_timestamp) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import annotations | |
| import itertools | |
| import collections | |
| from typing import TypeVar | |
| from typing import Iterable | |
| from typing import Iterator | |
| T = TypeVar("T") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import List | |
| from django.core.exceptions import FieldError | |
| from django.db.models import Count | |
| from django.db.models import Q | |
| from django.db.models import QuerySet | |
| def get_duplicate_instances(queryset: QuerySet, fields: List[str]) -> List[QuerySet]: | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import annotations | |
| from typing import Any | |
| from typing import NamedTuple | |
| from django.contrib.contenttypes.models import ContentType | |
| from django.db import models | |
| class GenericModelInstance(NamedTuple): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [tool.ruff] | |
| target-version = "py39" | |
| line-length = 100 | |
| src = ["app", "tests"] | |
| select = [ | |
| "E", # pycodestyle | |
| "F", # Pyflakes | |
| "TID251", # https://docs.astral.sh/ruff/rules/banned-api/ | |
| "TID252", # https://docs.astral.sh/ruff/rules/relative-imports/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import annotations | |
| from rest_framework import serializers | |
| class FormattedSerializerMethodField(serializers.SerializerMethodField): | |
| """ | |
| Like SerializerMethodField, but allowing you to apply an optional | |
| formatter serializer to the field's output. | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from rest_framework import serializers | |
| from rest_framework.fields import get_attribute | |
| class ChildSourceListSerializer(serializers.ListSerializer): | |
| """List serializer that respects child's `source` field""" | |
| def get_attribute(self, instance): | |
| attribute = super().get_attribute(instance) | |
| return [get_attribute(item, self.child.source_attrs) for item in attribute] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import logging | |
| from dataclasses import dataclass | |
| from typing import Union, List | |
| logger = logging.getLogger(__name__) | |
| class Validations: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import functools | |
| from typing import Any | |
| from operator import getitem | |
| def get_from_path(data: dict, *path, default: Any = None) -> Any: | |
| """ | |
| Safe get data given a path returning a default value | |
| >>> mydict = {"first": {"second": [3, 4]}} |
NewerOlder