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 get_phones_from_text(text: str) -> Set[str]: | |
phone_matches_iterator = re.finditer(r'(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}', text) | |
return set((r.group(0) for r in phone_matches_iterator)) | |
def get_emails_from_text(text: str) -> Set[str]: | |
email_matches_iterator = re.finditer(r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+', text) | |
return set((r.group(0) for r in email_matches_iterator)) |
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 FormatDict(dict): | |
def __missing__(self, key): | |
return '{{{}}}'.format(key) | |
def format_string(string, context: dict) -> str: | |
""" | |
Format string without KeyError if key is not present in dict. | |
>>> format_string('Oh, hi {first_name} {last_name}', {'first_name': 'Mark'}) |
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 filter_by_birthdays(qs, from_date, to_date): | |
# Build the list of month/day tuples. | |
dates = [] | |
while from_date < to_date: | |
dates.append((from_date.month, from_date.day)) | |
from_date += timedelta(days=1) | |
# Transform each into queryset keyword args. | |
dates = (dict( |
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 order_by_upcoming_birthdays(qs, from_date=None, birthday_field_name='date_of_birth'): | |
from_date = from_date or now().date() | |
qs = users_qs.annotate( | |
month=Extract(birthday_field_name, 'month'), | |
day=Extract(birthday_field_name, 'day')) | |
whens = [] | |
for i in range(12): | |
whens.append(When(month=(from_date + relativedelta(months=i)).month, then=i)) |
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 string | |
from django.utils.crypto import get_random_string | |
def generate_strong_password(length=12): | |
allowed_chars = string.digits + string.ascii_letters + '+=~|*_-#' | |
while True: | |
password = get_random_string(length=length, allowed_chars=allowed_chars) | |
if (set(password) & set(string.ascii_lowercase) and |
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.models import F, Value | |
from django.db.models.functions import Concat | |
# only one SQL query | |
User.objects.filter(id=user.id).update(description=Concat(F('description'), Value('some note to append'))) |
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.core.exceptions import FieldDoesNotExist | |
from django_extensions.db.fields import json | |
from rest_framework.decorators import list_route, detail_route | |
from reversion.models import Version | |
from common.pagination import SmallPagination, HistoryDiffPagination | |
from history.serializers import HistoryUserSerializer | |
from history.views import get_serialized_history | |
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.contrib.postgres.search import TrigramSimilarity | |
from django.db.models import Case, When, Q, IntegerField | |
def filter_by_trigram_similarity(qs, field_name, search_term, similarity=0.15): | |
if not search_term: | |
return qs | |
similarity_name = field_name + '_similarity' | |
strict_matching_name = field_name + '_strict_matching' |
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 put_forms_errors_to_messages(request, form, only_first=False): | |
for field_name, error_msg in form.errors.items(): | |
if field_name == '__all__': | |
error_msg = error_msg[0] | |
else: | |
field = form.fields[field_name] | |
error_msg = '{}: {}'.format(field.label if field.label else field_name, | |
error_msg[0].lower()) | |
messages.error(request, error_msg) | |
if only_first: |
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
""" | |
Monkey patch for Response class in Django REST Framework. | |
Original response's data will be accessible by key "data" | |
and errors have flat format, so it is easier to handle | |
errors on client side. | |
""" | |
from django.utils import six | |
from rest_framework.response import Response |