Skip to content

Instantly share code, notes, and snippets.

View DArmstrong87's full-sized avatar
Software Engineer: React, Python, Django

Daniel Armstrong DArmstrong87

Software Engineer: React, Python, Django
View GitHub Profile
@DArmstrong87
DArmstrong87 / run_repos.applescript
Created December 8, 2023 21:10
Shell executable to run an Applescript in iTerm2 which opens repos in separate tabs, pulls latest code and runs a command for each repo
on run argv
local basePath
set basePath to item 1 of argv
tell application "iTerm2"
create window with default profile
end tell
set repos to {"repo-1", "repo-2", "repo-3"}
@DArmstrong87
DArmstrong87 / custom_filter_truncate_with_ellipses.py
Created December 8, 2023 15:23
Custom Django template filter: Confine a string to a max length, replace last three characters with ellipses
from django import template
register = template.Library()
@register.filter(name="truncate_with_ellipses")
def truncate_with_ellipses(value: str, max_length: int) -> str:
"""
Confine a string to a max length, replace last three characters with ellipses
"""
if len(value) <= max_length:
return value
@DArmstrong87
DArmstrong87 / custom_filter_user_in_group.py
Last active December 8, 2023 15:17
Custom django template filter to check if user is in allowed groups
from django import template
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
register = template.Library()
User = get_user_model()
@register.filter(name="user_in_group")
def user_is_in_group(user: User, allowed_groups: str) -> bool:
"""
@DArmstrong87
DArmstrong87 / dynamic_url_params.html
Last active December 6, 2023 17:23
Build JS URL and dynamically insert query params based on selected filters
{### FILTERS ###}
<select id="filter1" name="filter1">
<option value="" selected disabled>Filter 1</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
<select id="filter2" name="filter2">
<option value="" selected disabled>Filter 2</option>
<option value="1">One</option>
<option value="2">Two</option>
@DArmstrong87
DArmstrong87 / migration_with_python.py
Last active March 20, 2024 14:45
Django migrations: Run Python function that takes optional parameter when reversing migration
from django.db import migrations
from myapp.models import MyModel
def convert_mymodel_status(apps, schema_editor, reverse: bool | None = False):
"""
Convert "OLD STATUS" status to "NEW STATUS"
Or reverses these changes
"""
status = "NEW STATUS" if reverse else "OLD STATUS"
updated_status = "OLD STATUS" if reverse else "NEW STATUS"
@DArmstrong87
DArmstrong87 / remove_duplicates.py
Last active December 1, 2023 19:16 — forked from victorono/remove_duplicates.py
Django: Remove duplicate objects where there is more than one field to compare. Keep earliest entry.
from django.db.models import Count, Min
unique_fields = ['field_1', 'field_2']
duplicates = (
MyModel.objects.values(*unique_fields)
.order_by()
.annotate(min_id=Min('id'), count_id=Count('id'))
.filter(count_id__gt=1)
)