Skip to content

Instantly share code, notes, and snippets.

View maxpoletaev's full-sized avatar

Max Poletaev maxpoletaev

View GitHub Profile
import tempfile
class TempFileManager():
def __init__(self):
self.files = []
def __enter__(self):
return self
#!/bin/bash
# Environment variables:
# REMOTE_USER, REMOTE_HOST — obviously
# Read more about deployment over SSH:
# https://confluence.atlassian.com/bitbucket/access-remote-hosts-via-ssh-847452940.html
set -e # automatic exit on error
from rest_framework.authentication import BaseAuthentication
class SessionAuthenticationWithoutCsrf(BaseAuthentication):
def authenticate(self, request):
request = request._request
user = getattr(request, 'user', None)
if not user or not user.is_active:
return None
import time
from django.core.cache import cache as default_cache
from rest_framework.exceptions import Throttled
class RateLimitThrottle:
"""
The alternative implementation of Rest Framework's throttling feature, that is more driven
but is incompatible with a throttling_classes attribute. The example below gives 10 login
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XML Sitemap</title>
@maxpoletaev
maxpoletaev / rest_router.py
Created June 20, 2017 20:08
The right router for Django Rest Framework
from rest_framework import routers
class RestRouter(routers.SimpleRouter):
routes = [
# list
routers.Route(
url=r'^{prefix}/?$',
mapping={
'get': 'list',
@maxpoletaev
maxpoletaev / flex_test_mixin.py
Created June 20, 2017 20:07
Flex test mixin for Django
import os
from django.conf import settings
from django.http import HttpRequest
import flex
from flex.core import validate_api_call
from flex.exceptions import ValidationError
APISPEC_PATH = os.path.join(settings.BASE_DIR, 'apispec', 'swagger.yml')
flex_schema = flex.load(APISPEC_PATH)
from django_filters import rest_framework as drf_filters
import copy
class NestedFilter:
def __init__(self, filterset, name, prefix=''):
self.filterset = filterset
self.name = name
self.prefix = prefix
import string
def normalize_phone(phone):
if not phone:
return None
phone = ''.join(n for n in phone if n in set(string.digits + '+'))
phone_len = len(phone)
@maxpoletaev
maxpoletaev / current_dir.sh
Created April 13, 2017 12:04
Get current directory in bash
CUR_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)