Skip to content

Instantly share code, notes, and snippets.

@benbacardi
benbacardi / ContentView.swift
Last active February 22, 2023 21:39
Playing with "global router-style" navigation in SwiftUI
//
// ContentView.swift
// AppRouterPlayground
//
// Created by Ben Cardy on 22/02/2023.
//
import SwiftUI
@benbacardi
benbacardi / EqualWidthIcons.swift
Last active February 2, 2023 14:36
SwiftUI Equal Width Icons
//
// EqualWidthIcons.swift
// From https://bencardy.co.uk/2023/02/02/swiftui-equal-width-icons/
//
// Created by Ben Cardy on 02/02/2023.
//
import SwiftUI
struct WhatsNewSection: View {
@benbacardi
benbacardi / README.md
Created February 1, 2019 16:12
SCRIPT-8
@benbacardi
benbacardi / cbvs.md
Last active April 10, 2017 13:10
Django CBV Notes

Stuff I always forget about CBVs

  • self.request is the way to get the request object
  • self.args and self.kwargs are the URL parameters from urls.py (they are also passed to almost every method, but they're not always, and some methods take an args and kwargs that aren't the ones from the URL, so... just use self. everywhere!)
  • dispatch is called before get or post so is a useful place to setup stuff you need everywhere
  • the view object itself is available in the template as view, so templates can access view methods and properties
  • Detail and List views don't just get the template variables object and object_list, they also get <model-name> and <model-name>_list
  • Permission decorators etc are better handled by django-braces
@benbacardi
benbacardi / templatetags.py
Last active August 25, 2024 23:07
Django query_transform templatetag
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def query_transform(context, **kwargs):
'''
Returns the URL-encoded querystring for the current page,
updating the params with the key/value pairs passed to the tag.
@benbacardi
benbacardi / gist:227f924ec1d9bedd242b
Last active August 17, 2024 20:05
Django reverse with a querystring
from django.utils.http import urlencode
def reverse_querystring(view, urlconf=None, args=None, kwargs=None, current_app=None, query_kwargs=None):
'''Custom reverse to handle query strings.
Usage:
reverse('app.views.my_view', kwargs={'pk': 123}, query_kwargs={'search': 'Bob'})
'''
base_url = reverse(view, urlconf=urlconf, args=args, kwargs=kwargs, current_app=current_app)
if query_kwargs:
return '{}?{}'.format(base_url, urlencode(query_kwargs))
@benbacardi
benbacardi / gist:8551eb887739a66fee61
Created January 16, 2015 11:38
Static Colours class
class Colours:
BLACK = '\033[30m'
BLUE = '\033[34m'
GREEN = '\033[32m'
CYAN = '\033[36m'
RED = '\033[31m'
PURPLE = '\033[35m'
YELLOW = '\033[33m'
LIGHT_GREY = '\033[37m'
DARK_GREY = '\033[1;30m'