This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import uuid | |
from base64 import urlsafe_b64encode | |
from django.utils.deconstruct import deconstructible | |
from utils.base36 import base36encode | |
@deconstructible |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from markdown import markdown | |
class MarkdownField(serializers.CharField): | |
def to_representation(self, value): | |
return markdown(value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 | |