Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 07:31 (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
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.
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.
"""
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
LowerDeez / sha256-hmac.md
Created March 5, 2021 15:03 — forked from jasny/sha256-hmac.md
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
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
# 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 / attrdict.py
Created December 21, 2020 11:59 — forked from turicas/attrdict.py
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:
self.__dict__.update(init)
def __getitem__(self, key):
return self.__dict__[key]
@LowerDeez
LowerDeez / convert_key.sh
Last active December 28, 2019 07:30 — forked from innocuo/convert_key.sh
Convert PuTTY .ppk key to OpenSSH in Ubuntu
sudo apt-get install putty-tools
# Place your keys in some directory, e.g. your home folder. Now convert the PPK keys to SSH keypairs:cache search
# To generate the private key:
puttygen your_private_key.ppk -O private-openssh -o your_new_key
chmod 600 your_new_key
# Move these keys to ~/.ssh and make sure the permissions are set to private for your private key:
mkdir -p ~/.ssh
@LowerDeez
LowerDeez / create_sha256_signature.py
Last active October 3, 2019 08:39 — forked from gauravvjn/create_sha256_signature.py
Create HMAC SHA256 signature/encryption/encode
"""
https://gist.github.com/Azadehkhojandi/50eaae4cf20b21faef186f2c8ee97873
"""
import hmac
import hashlib
import binascii
# 1
@LowerDeez
LowerDeez / django_absolute_sum.py
Created April 24, 2019 09:02 — forked from iandmyhand/django_absolute_sum.py
Django ORM function to sum absolute values.
from django.db.models import Sum
class AbsoluteSum(Sum):
name = 'AbsoluteSum'
template = '%(function)s(%(absolute)s(%(expressions)s))'
def __init__(self, expression, **extra):
super(AbsoluteSum, self).__init__(
expression, absolute='ABS ', output_field=IntegerField(), **extra)