This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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",): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Tuple, Dict, List | |
class M(object): | |
def __init__(self, x: int): | |
self.id = x | |
Models = List[M] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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>), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |