Skip to content

Instantly share code, notes, and snippets.

View maxpoletaev's full-sized avatar

Max Poletaev maxpoletaev

View GitHub Profile
def base36encode(integer):
chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
sign = '-' if integer < 0 else ''
integer = abs(integer)
result = ''
while integer > 0:
integer, remainder = divmod(integer, 36)
result = chars[remainder] + result
from django.db import connection
from django.utils.deconstruct import deconstructible
def nextval(regclass):
with connection.cursor() as cursor:
cursor.execute(f"select nextval('{regclass}')")
next_id_row = cursor.fetchone()
return next_id_row[0]
import os
import uuid
from base64 import urlsafe_b64encode
from django.utils.deconstruct import deconstructible
from utils.base36 import base36encode
@deconstructible
import os
from email.mime.image import MIMEImage
from django.conf import settings
from django.template.loader import render_to_string
from django.core.mail import get_connection, EmailMultiAlternatives
def make_message(
subject,
from rest_framework import serializers
from markdown import markdown
class MarkdownField(serializers.CharField):
def to_representation(self, value):
return markdown(value)
# Rest Framework
# https://www.django-rest-framework.org/api-guide/settings/
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
'DEFAULT_RENDERER_CLASSES': ('rest_framework.renderers.JSONRenderer',),
'DEFAULT_PARSER_CLASSES': ('rest_framework.parsers.JSONParser',),
'EXCEPTION_HANDLER': 'exams.utils.error_handler.api_exception_handler',
'DEFAULT_AUTHENTICATION_CLASSES': (),
'DEFAULT_METADATA_CLASS': None,
class StaticInitialFormSet(forms.BaseInlineFormSet):
_initial = None
@classmethod
def set_initial(cls, initial):
cls._initial = initial
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self._initial is not None:
"""
Calculates distance using the equirectangular distance approximation.
"""
queryset.extra(select={
'distance_in_km': """
6371 * SQRT(
RADIANS(longitude - %s) * COS(0.5 * RADIANS(latitude + %s))
* RADIANS(longitude - %s) * COS(0.5 * RADIANS(latitude + %s))
+ RADIANS(latitude - %s) * RADIANS(latitude - %s)
from collections import OrderedDict
from rest_framework import serializers
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
class WrappedListSerializer(serializers.ListSerializer):
wrapper_name = 'objects'
def get_resource_name(self):
"""
The module implements PostgreSQL Advisory Locks.
https://www.postgresql.org/docs/10/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
"""
from zlib import crc32
from django.db import connection