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_cte.raw import raw_cte_sql | |
from django_cte import CTEQuerySet, With | |
class CustomQuerySet(CTEQuerySet): | |
def filter_by_uuids(self, uuids: Sequence): | |
valid_uuids = [value for value in uuids if is_valid_uuid(value)] | |
placeholders = ",".join(["%s"] * len(valid_uuids)) | |
uuid_table = With( |
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 parse from 'parse' | |
describe('Parser "keysToCamelCase"', () => { | |
it('should return a new object with camel case keys when receiving an object', () => { | |
const object = { first_name_1: 'a name' } | |
const output = parse(object).keysToCamelCase() | |
expect(output).toEqual({ firstName1: 'a name' }) | |
}) |
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
Match host <repo host> exec "[ ! -z $(git config --local user.email) ] && [ $(git config --local user.email) = <work email> ]" | |
IdentityFile ~/.ssh/<work private file> | |
Host <repo host> | |
IdentityFile ~/.ssh/id_rsa |
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 { defaultObject } from './defaultObject'; | |
describe('defaultObject', () => { | |
it('should assign default values based on the provided factory', () => { | |
const output = defaultObject(() => [] as string[]); | |
output.id.push('3'); | |
expect(cleanupProxy(output)).toEqual({ id: ['3'] }); | |
}); |
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 Q | |
def q_xor(*queries: Q) -> Q: | |
output = Q() | |
for i in range(len(queries)): | |
variation = Q() | |
for j, query in enumerate(queries): | |
variation &= ~query if i == j else query | |
output |= variation |
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 Dict, Generator, List, Union | |
from rest_framework import serializers | |
from rest_framework.exceptions import ValidationError | |
from rest_framework.fields import empty | |
class EnhancedModelSerializer(serializers.ModelSerializer): | |
Meta: type |
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 import migrations | |
def update_content_types(apps, schema_editor): | |
db_alias = schema_editor.connection.alias | |
ContentType = apps.get_model('contenttypes.ContentType') | |
ContentType.objects.using(db_alias).filter(app_label__in=['<old_app_1>', '<old_app_2>']).update(app_label='<remaining_app>') | |
class Migration(migrations.Migration): |
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 QuerySet as DjangoQuerySet | |
class QuerySet(DjangoQuerySet): | |
def filter_or_create(self, filters: List[dict], defaults=None, lookup='pk'): | |
""" | |
Fetch existing records and create missing ones in 2 queries | |
@param filters: List of queryset filters as dictionaries | |
@param defaults: Same as QuerySet.get_or_create's "defaults" | |
@param lookup: Field that must be used to check whether the record exists or not (filters must have it) |
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.conf import settings | |
from django.core.management.commands.migrate import Command as MigrateCommand | |
# The value is the key that goes in the `DATABASES` settings. | |
# Ideally set in the Django settings as well, so it can be project-wide accessible | |
SOURCE_DB_LABEL = 'source_db' | |
class Command(MigrateCommand): | |
def handle(self, *args, **options): |
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.conf import settings | |
# The value is the key that goes in the `DATABASES` settings. | |
# Ideally set in the Django settings as well, so it can be project-wide accessible | |
SOURCE_DB_LABEL = 'source_db' | |
class DatabaseRouter: | |
@classmethod | |
def allow_migrate(cls, db_label: str, app_label: str, **hints) -> bool: |
NewerOlder