Skip to content

Instantly share code, notes, and snippets.

@mpyrev
mpyrev / admin_tools.py
Created October 24, 2018 07:57
Replaces text representation of object with link to corresponding admin change page
import typing
from functools import wraps
from django.urls import reverse
from django.utils.html import format_html
def display_admin_link(admin_method: typing.Callable=None, *, display_function: typing.Callable=str):
"""
Returns link to corresponding admin change page of object
@mpyrev
mpyrev / monkey_patch.py
Last active October 4, 2018 09:27
Monkey patch Django 1.8 migration executor for better performance. Can cause issues, be careful.
# coding: utf-8
from __future__ import absolute_import
from django import VERSION
from django.apps.registry import apps as global_apps
from django.db import router, migrations
from django.db.migrations import executor, migration
from django.db.migrations.loader import MigrationLoader
from django.db.migrations.recorder import MigrationRecorder
from django.db.migrations.state import ProjectState
@mpyrev
mpyrev / gist:39e9b0a94a6d82e810d645efdbdb5253
Created April 24, 2018 10:02
Let's Encrypt certbot on system without nginx authenticator support
certbot --authenticator standalone --pre-hook "service nginx stop" --post-hook "service nginx start" --installer nginx
@mpyrev
mpyrev / rest_with_markdown.py
Created November 16, 2017 09:51
get_view_description function with markdown support
import markdown
from django.utils.encoding import smart_text
from django.utils.safestring import mark_safe
from rest_framework.utils import formatting
__all__ = ['get_view_description']
if markdown.version <= '2.2':
@mpyrev
mpyrev / mixins.py
Last active July 6, 2017 14:15
Django REST Framework mixin making use of default values when value is None
# coding: utf-8
from __future__ import unicode_literals
from collections import OrderedDict
from rest_framework.fields import SkipField
from rest_framework.relations import PKOnlyObject
class DefaultIfNoneMixin(object):