Skip to content

Instantly share code, notes, and snippets.

View ranelpadon's full-sized avatar

ranelpadon

View GitHub Profile
@ranelpadon
ranelpadon / sync_migrations.py
Created July 6, 2022 20:07
Prevent "duplicate table/column" errors when running migrations due to a similarly named, previously ran migration
"""
manage.py sync_migrations
"""
import re
from django.core.management.base import BaseCommand
from django.db import (
DEFAULT_DB_ALIAS,
connections,
)
@ranelpadon
ranelpadon / mouse_mover.py
Created September 22, 2022 09:09
Simple script to move the mouse cursor randomly and indefinitely. This will make your status stay "Online" in various apps.
"""
Requires `pip install pyautogui`.
"""
import pyautogui
import random
import time
# Get the screen boundaries.
@ranelpadon
ranelpadon / .zshrc
Last active September 30, 2021 21:14
YouTube Downloader and Trimmer
# Add this to your `~/.bash_profile` or `~/.zshrc` file,
# so that running the Python script in the terminal is more convenient.
# Command: yvt URL START_TIME END_TIME
# Sample: yvt 'https://www.youtube.com/watch?v=RjxKVAOZCQY' 9:41 10:58
yt_video_trimmer() {
python <PATH TO>/yt-video-trimmer.py $1 $2 $3
}
alias yvt=yt_video_trimmer
@ranelpadon
ranelpadon / migrations_syncer.py
Last active April 24, 2021 09:43
Django Migrations Syncer: Prevent "duplicate table/column" errors when running migrations due to a similarly named, previously run migration
# How to use:
# Put this file into a convenient location.
# Then, pipe the script into the Django shell (which will auto-connect to the target db assuming the db credentials are correct):
# $ python <PATH TO>/manage.py shell < <PATH TO>/migrations_syncer.py
# After running the script, check the `django_migrations` table if there are indeed new rows for already run migrations.
from subprocess import (
PIPE,
Popen,
)
@ranelpadon
ranelpadon / karabiner.json
Last active February 23, 2021 05:07
Colemak DHk - Karabiner Elements - Config File
{
"global": {
"check_for_updates_on_startup": true,
"show_in_menu_bar": true,
"show_profile_name_in_menu_bar": false
},
"profiles": [
{
"complex_modifications": {
"parameters": {
@ranelpadon
ranelpadon / settings.json
Created February 23, 2021 05:04
VS Code Settings
{
"window.zoomLevel": -1,
"terminal.integrated.fontFamily": "MesloLGS NF",
"terminal.integrated.cursorBlinking": true,
"terminal.integrated.cursorStyle": "line",
"editor.fontFamily": "Monaco, Menlo, 'Courier New', monospace",
"code-runner.fileDirectoryAsCwd": true,
"terminal.integrated.cwd": ".",
"terminal.integrated.shell.osx": "/usr/local/bin/zsh",
"atomKeymap.promptV3Features": true,
@ranelpadon
ranelpadon / django-formset--custom-kwarg--formset-save--model-save.py
Last active June 8, 2018 04:47
Custom Django model.save() kwarg to avoid executing costly codes if there are lots of formsets in the page.
# forms.py
class EventSessionFormSet();
...
def save(self, commit=True):
sessions = []
forms = self.forms
total_forms = len(forms)
for i, form in enumerate(forms):
@ranelpadon
ranelpadon / multiple_replace.py
Last active February 17, 2018 11:52
Find the restricted symbols in the text and substitute with multiple replacement values.
import re
# Restricted symbols taken from https://en.wikipedia.org/wiki/Filename
symbols = ('/', '\\', '?', '%', '*', ':', '|', '"', '<', '>',)
symbols_replacements = {symbol: '_' for symbol in symbols}
space_replacement = {' ': '-'} # Dash instead of underscore
replacements = {}
replacements.update(symbols_replacements)
@ranelpadon
ranelpadon / django-dependent-field.js
Last active August 28, 2017 04:26
Update the select list field B based on the currently selected option in select list field A. For example, when you select a particular Music Genre, only the related Artists should show. Other common use cases/relationships: Country/Cities, Organization/Members, Company/Contact Persons, Brand/Products, etc.
// Workflow (Genre is the Parent Field, Artist is the Dependent Field):
// 1. user interacts w/ Genre select list widget via JS script.
// 2. JS script fetches the new Artist list in views.py
// 3. JSON response will be used as the new values/options for the Artist select list widget.
(function($) {
$(function() {
// Artist's select option values will depend on
// the Genre's selected option.
var $genreSelectWidget = $('#id_genre');
@ranelpadon
ranelpadon / dependent-field.js
Created August 27, 2017 09:54
Update the select list field B based on the currently selected option in select list field A. For example, when you select a particular Music Genre only the related Artist should show. Other common use cases/relationships are Company/Contact Info, Country/City, or Brand/Products.
(function($) {
$(function() {
// Artist's select option values will depend on
// the Genre's selected option.
var $genreSelectWidget = $('#id_genre');
var $artistSelectWidget = $('#id_artist');
var artistSelectWidgetInitial = $artistSelectWidget.val()
// Adjust Artists based on the selected Genre.
function adjustArtistOptions(preSelectedOption) {