Skip to content

Instantly share code, notes, and snippets.

@maddrum
maddrum / config.py
Created December 6, 2018 20:20 — forked from jerivas/config.py
One-click Django language select
# settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.i18n',
)
# urls.py (outside i18n_patterns)
(r'^i18n/', include('django.conf.urls.i18n')),
@maddrum
maddrum / README.md
Last active September 25, 2022 19:29
Connect PayPal v2 checkout with Django and handle payment statuses

This GIST will help you implement PayPal v2 API with your Django app and handle payment statuses. I will use: Class-Based FormView and Django template system. To proceed you have to obtain valid PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET /have PayPal business account - production or sandbox/

Steps to complete:

  1. Get PayPal SDK - pip install paypal-checkout-serversdk

  2. Create a FormView with the payment data - amount, currency, description and cliend_id

@maddrum
maddrum / url_parameters
Last active June 7, 2021 12:08
Django template tags to update, remove or append url get parameter
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def query_transform(context, **kwargs):
query = context.request.GET.copy()
for key, value in kwargs.items():
query[key] = value
@maddrum
maddrum / cls_init.py
Last active July 26, 2021 12:00
Super init playground
"""
based on: https://stackoverflow.com/questions/10482953/python-extending-with-using-super-python-3-vs-python-2
"""
class Klass1:
def __init__(self):
print('1')
import random
class MetaSample(type):
def __new__(cls, name, bases, dct):
print(f'BEHOLD, You will get an awesome new class soon!')
x = super().__new__(cls, name, bases, dct)
x.some_var = 100
x.random_var = random.randint(0, 100)
x.called_times = 0
"""getters, setters and property decorators examples
Used to get and set protected variables(starts with _)
using property makes all code call getter and setter functions when rear and write do this class protected variable"""
class AddTesterToString:
def __init__(self, input_string):
self.input_string = input_string
# def getter function - gets _protected variable
def decorator(func):
"""This is a decorator function. It takes some function as a parameter, 'wraps it' in code
and RETURNs wrapped function
This is a way to add functionality without changing base code"""
def wrap(text='this is base function'):
print('this is pre-function')
func(text)
print('this is post func')
import logging
import time
logger = logging.getLogger('MyLogger')
def measure_run_time(func):
def wrapper(*args, **kwargs):
total_start_time = time.time_ns()
@maddrum
maddrum / Django admin Inline models with custom model manager - wrong querysetfix
Last active January 4, 2023 08:54
Django admin Inline models with custom model manager - wrong querysetfix
# bug described here - https://code.djangoproject.com/ticket/33813
----
# Definitions
# models
class ReviewsModelManager(models.Manager):
def get_queryset(self):
qs = super().get_queryset()
qs = qs.filter(published=True)
return qs
@maddrum
maddrum / it-is-pythonic.py
Created October 13, 2022 06:57
Why know it is Python when....
value_list = [
{
'name': 'a',
'value': '1',
},
{
'name': 'a',
'value': '2',
},
{