Skip to content

Instantly share code, notes, and snippets.

View martin056's full-sized avatar

Martin Angelov martin056

View GitHub Profile
@martin056
martin056 / form_soup_to_dict.py
Created June 4, 2018 07:57
Transforms form data with initial values to a dictionary that can be used for POST request. Handles Django admin forms.
def form_soup_to_dict(form_soup, add_save=False):
"""
Transforms form data with initial values to a dictionary that can be used for POST request.
* `add_save` - In Django Admin forms POST data expects `_save` to make the request.
"""
data = {}
for input_field in form_soup.find_all('input'):
# We have some custom buttons added to the Django Admin
@martin056
martin056 / poc_prepagination.py
Created June 7, 2019 13:47
Main Idea - DECOUPLE serialization & pagination
class LimitOffsetPrepagination(LimitOffsetPagination):
def __init__(self, request):
self.request = request
def paginate_queryset(self, queryset, postpagination=None):
self.results = super().paginate_queryset(queryset=queryset, request=self.request)
if postpagination:
self.results = postpagination(self.results)
@martin056
martin056 / multple_return_values_mock.py
Last active August 13, 2019 17:06
This is an example of a way to have a unittest mock that returns different values each time it's called
# in example.py
def inner_func(x=None):
if x is None:
return 1
return x + 1
def big_func():
@martin056
martin056 / WithDialogActions.tsx
Last active April 15, 2020 11:33
HOC that uses Redux connect and React-router withRouter
import React from 'react';
import _ from 'lodash';
import { withRouter, RouteComponentProps } from 'react-router-dom';
import { connect } from 'react-redux';
import { Diff } from 'utility-types';
import { AppState } from 'globalReducer';
import {
openDialog,
closeDialog,