Skip to content

Instantly share code, notes, and snippets.

@bgrace
bgrace / gist:021f55a9f160685e2433
Last active August 29, 2015 14:09
Printable on NSManaged Object doesn't get invoked.
// See notes in first comment
if let moc = AppDelegate.instance.managedObjectContext {
// fetching NSManagedObject with fancy generic typesafe helper method
if let allGrowers = moc.fetch(Grower.self, predicate: nil, sortDescriptors: nil, returnsObjectsAsFaults: true) {
switch allGrowers {
case .Value(let b):
let allGrowers = b.contents
@bgrace
bgrace / Wagtail dynamic templates
Created November 12, 2014 20:11
Override Wagtail CMS templates based on the URL of the incoming request. Search by specific URL, then look for templates which override by model type.
class PathOverrideable(object):
def get_template(self, request, mode='', **kwargs):
try:
return self._path_overrideable_template
except AttributeError:
if not self.url:
return get_template(self.template)
@bgrace
bgrace / gist:a5216b4059007924a75c
Last active February 16, 2018 18:35
Mixed navigation menus for Wagtail CMS
# Notes:
# Python 3 assumed, but shouldn't be hard to backport to Python 2
# Cobbled together from two slightly different implementations, sorry for any inconsistencies.
# This creates two models, NavigationMenu, and NavigationMenuItem. A site can have many NavigationMenus, which are referenced from
# the template by location. E.g., 'footer', 'left_nav'. You can also create single-item lists for special purpose links/buttons,
# such a privacy_policy or homepage_cta. This helps to reduce hard coding in templates.
# NavigationMenuItem mixes in some different link types (Page, Document, URL) and presents them in a consistent way. It provides
# the ability to override the title of the referenced object.
var dict = Dictionary<String,Bool>()
dict["a"] = true
dict["c"] = false
func matchOneOrTheOtherWithOptionals(a: Bool?, b: Bool?) -> String {
switch (a, b) {
case (.Some(true), let b) where b == .None || !b!:
return "a was true, but b was None or false"
case (let a, .Some(true)) where a == .None || a == .Some(false):