Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar

Oleg Kleshchunov LowerDeez

  • Kyiv, Ukraine
  • 14:21 (UTC +02:00)
View GitHub Profile
LowerDeez /
Created September 20, 2021 17:30 — forked from spookylukey/
AfterFetchQuerySetMixin for Django
class AfterFetchQuerySetMixin:
QuerySet mixin to enable functions to run immediately
after records have been fetched from the DB.
# This is most useful for registering 'prefetch_related' like operations
# or complex aggregations that need to be run after fetching, but while
# still allowing chaining of other QuerySet methods.
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
LowerDeez /
Created September 14, 2021 07:46 — forked from codingforentrepreneurs/
A simple python function to convert a number string into a float if possible.
from fractions import Fraction
def number_str_to_float(amount_str:str) -> (any, bool):
Take in an amount string to return float (if possible).
Valid string returns:
Boolean -> True
LowerDeez /
Created July 23, 2021 14:58 — forked from hakib/
Custom django checks using Django check framework, inspect and ast.
Custom django checks.
H001: Field has no verbose name.
H002: Verbose name should use gettext.
H003: Words in verbose name must be all upper case or all lower case.
H004: Help text should use gettext.
H005: Model must define class Meta.
H006: Model has no verbose name.
H007: Model has no verbose name plural.
# This is a hack so that French translation falls back to English translation,
# not German translation (which is the default locale and the original
# strings).
from django.utils.translation import trans_real
class MyDjangoTranslation(trans_real.DjangoTranslation):
def _add_fallback(self, localedirs=None):
if self._DjangoTranslation__language[:2] in {'de', 'en'}:
return super()._add_fallback(localedirs)
LowerDeez /
Created June 2, 2021 14:21
Protected file access
from django.http import HttpResponse
from django.http import HttpResponseForbidden
from apps.order.models import Certificate, Order
__all__ = (
from collections import defaultdict
import weakref
__refs__ = defaultdict(weakref.WeakSet)
def clear_refs(cls):
LowerDeez /
Created March 5, 2021 15:03 — forked from jasny/
Hashing examples in different languages

Example inputs:

Variable Value
key the shared secret key here
message the message to hash here

Reference outputs for example inputs above:

| Type | Hash |

LowerDeez /
Created February 18, 2021 07:09 — forked from bryanchow/
Django SSL Redirect Middleware - Django middleware for automatically redirecting to HTTPS for specific URLs
# See also: Snippets 85, 240, 880 at
# Minimally, the following settings are required:
# SSL_URLS = (
# r'^/some/pattern/',
# )
LowerDeez /
Created December 30, 2020 07:48
Django. Custom views for admin 2
from django import forms
from django.db.models import FilteredRelation, Q
from django.urls import path
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.http import HttpResponseRedirect
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.http import urlencode
from django.utils.translation import ugettext_lazy as _
LowerDeez /
Created December 21, 2020 11:59 — forked from turicas/
Python class with dict-like get/set item
#!/usr/bin/env python
# coding: utf-8
class AttrDict(object):
def __init__(self, init=None):
if init is not None:
def __getitem__(self, key):
return self.__dict__[key]