Skip to content

Instantly share code, notes, and snippets.

View kezabelle's full-sized avatar

Keryn Knight kezabelle

View GitHub Profile
@kezabelle
kezabelle / monkeypatch.py
Last active March 6, 2019 16:02
Make Django templates error super loudly, maybe?
from django.template import TemplateSyntaxError
from django.template.base import Variable, VariableDoesNotExist
old_resolve = Variable._resolve_lookup
def x(self, context):
try:
return old_resolve(self, context)
except VariableDoesNotExist as e:
part = e.params[0]
if part not in ("SETTINGS_MODULE",):
@kezabelle
kezabelle / models.py
Last active February 26, 2019 14:00
For Vincent in Slack
class MyQuerySet(QuerySet):
def is_trial(self):
return self.filter(is_trial=True)
def isnt_trial(self):
return self.filter(is_trial=False)
class MyModel(Model):
trial = BooleanField(default=False)
@kezabelle
kezabelle / error_as_expected.py
Last active January 10, 2019 08:49
bleh, typing is pain.
from typing import Tuple, Dict, List
class M(object):
def __init__(self, x: int):
self.id = x
Models = List[M]
@kezabelle
kezabelle / watch_obj_for_attr_changes.py
Created March 2, 2018 14:00
Extremely hacky, just about seems to work for logging changes ... can't figure out the way to go with accesses though
class MutationObserver(wrapt.ObjectProxy):
def __init__(self, *args, **kwargs):
super(MutationObserver, self).__init__(*args, **kwargs)
self.__dict__['_self_changed'] = []
self.__dict__['_self_used'] = []
def __setattr__(self, key, value):
@kezabelle
kezabelle / 0001_initial.py
Last active March 1, 2018 11:59
When `makemigrations` happens, `B` won't include the `a` FK so subsequent data migrations won't be able to query by `a`
# Generated by Django 2.1.dev20180228190652 on 2018-03-01 11:08
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
@kezabelle
kezabelle / views.py
Last active February 2, 2018 09:02
something like this maybe. for xref on #django
class Goaway(View):
def dispatch(self, request, *args, **kwargs):
try:
resp = super(Goaway, self).dispatch(request, *args, **kwargs)
except Http404 as e:
return HttpResponseRedirect("/")
def get_object(self, *args, **kwargs):
obj = super(Goaway, self).get_object(*args, **kwargs)
if obj.account_id != self.request.user.account_id:
@kezabelle
kezabelle / um.py
Created May 30, 2017 12:34
For MrGrj in #django IRC
q = self.request.GET.get('q')
items = ('frq_id', 'line_cls', 'technology_id', 'type_id', 'line_capacity_id', 'el_intf_type_a', 'location_a_id', 'location_z_id', 'comments', 'opened_at', 'opened_by', 'finalised', 'attach_id', 'location_a_id_old', 'offer_deadline', 'location_z_id_old', 'el_intf_type_z', 'cancelled', 'qinq', 'flow_type_id')
itemdicts = ({'{}__contains'.format(item): x} for item in items)
itemqs = (Q(**itemd) for itemd in itemdicts)
itemq = reduce(operator.or_, itemqs)
@kezabelle
kezabelle / base_components.py
Last active May 17, 2017 15:07
an idea for pattern atlas?
class BaseComponent(object):
def get_name(self):
try:
return force_text(name)
except AttributeError:
return self.__class__.__name__
def get_dependencies(self):
try:
return list(self.dependencies)
>>> from django.utils import timezone
>>> import pytz
>>> now = timezone.now()
>>> x = pytz.timezone('Europe/London')
>>> now2 = now.replace(tzinfo=x)
>>> now3 = now2.astimezone(pytz.utc)
>>> now4 = now3.astimezone(x)
(datetime.datetime(2017, 5, 9, 12, 58, 13, 596984, tzinfo=<UTC>),
datetime.datetime(2017, 5, 9, 12, 58, 13, 596984, tzinfo=<DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD>),
@kezabelle
kezabelle / lol.py
Created May 8, 2017 13:35
Finding use of exclude on a modelform ...
from django.forms.models import ModelFormOptions
def lol(self, k, v):
if k == "exclude" and v:
print(v)
return setattr(self, k, v)
ModelFormOptions.__setattr__ = lol.__get__(ModelFormOptions)