Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 14:21 (UTC +02:00)
View GitHub Profile
@LowerDeez
LowerDeez / after_fetch_queryset_mixin.py
Created September 20, 2021 17:30 — forked from spookylukey/after_fetch_queryset_mixin.py
AfterFetchQuerySetMixin for Django
View after_fetch_queryset_mixin.py
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
LowerDeez / number_str_to_float.py
Created September 14, 2021 07:46 — forked from codingforentrepreneurs/number_str_to_float.py
A simple python function to convert a number string into a float if possible.
View number_str_to_float.py
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:
Float
Boolean -> True
@LowerDeez
LowerDeez / custom_django_checks.py
Created July 23, 2021 14:58 — forked from hakib/custom_django_checks.py
Custom django checks using Django check framework, inspect and ast.
View custom_django_checks.py
"""
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.
View django_change_translation_fallback_language.py
# 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
LowerDeez / views.py
Created June 2, 2021 14:21
Protected file access
View views.py
from django.http import HttpResponse
from django.http import HttpResponseForbidden
from apps.order.models import Certificate, Order
__all__ = (
'files_access',
)
View memory_storage.py
from collections import defaultdict
import weakref
__refs__ = defaultdict(weakref.WeakSet)
def clear_refs(cls):
__refs__[cls].clear()
@LowerDeez
LowerDeez / sha256-hmac.md
Created March 5, 2021 15:03 — forked from jasny/sha256-hmac.md
Hashing examples in different languages
View sha256-hmac.md

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
LowerDeez / middleware.py
Created February 18, 2021 07:09 — forked from bryanchow/middleware.py
Django SSL Redirect Middleware - Django middleware for automatically redirecting to HTTPS for specific URLs
View middleware.py
# https://gist.github.com/1035190
# See also: Snippets 85, 240, 880 at http://www.djangosnippets.org/
#
# Minimally, the following settings are required:
#
# SSL_ENABLED = True
# SSL_URLS = (
# r'^/some/pattern/',
# )
@LowerDeez
LowerDeez / admin.py
Created December 30, 2020 07:48
Django. Custom views for admin 2
View admin.py
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
LowerDeez / attrdict.py
Created December 21, 2020 11:59 — forked from turicas/attrdict.py
Python class with dict-like get/set item
View attrdict.py
#!/usr/bin/env python
# coding: utf-8
class AttrDict(object):
def __init__(self, init=None):
if init is not None:
self.__dict__.update(init)
def __getitem__(self, key):
return self.__dict__[key]