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
| 1. create new app in dashboard.heroku.com | |
| 2. create Procfile | |
| echo "web: gunicorn <app-name>.wsgi --log-file -1" > Procfile | |
| 3. add settings file for heroku | |
| from .<base-settings-file> import * | |
| import dj_database_url | |
| ALLOWED_HOSTS = ['<name-app>.herokuapp.com', ] |
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
| """ | |
| This example shows how to run Django management command from some of your Django apps. | |
| Stdout and stderr are redirected to Django's StreamingHttpResponse, so | |
| you will see command execution result in realtime. | |
| Example can be easily adopted to run all Django's management commands or | |
| any bash command. | |
| """ | |
| import importlib |
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 |
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
| 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
| 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.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
| 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
| 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
| 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( |
OlderNewer