Skip to content

Instantly share code, notes, and snippets.

View jourdanrodrigues's full-sized avatar
🏡
Working remotely 😎

Jourdan Rodrigues jourdanrodrigues

🏡
Working remotely 😎
  • Software Engineer
  • Brasil
View GitHub Profile
@jourdanrodrigues
jourdanrodrigues / camelSnakeParser.test.ts
Last active May 28, 2024 13:24
Parse to camel case to snake case and back
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' })
})
@jourdanrodrigues
jourdanrodrigues / ssh_config
Created May 18, 2024 21:46
Use different SSH keys for different repositories based on host and email
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
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'] });
});
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
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
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):
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)
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):
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:
from django.db import models
from django.db.models import QuerySet
# 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 SourceServiceQuerySet(QuerySet):
def as_manager(cls):
return super().as_manager().db_manager(SOURCE_DB_LABEL)