Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 03:14 (UTC +03:00)
View GitHub Profile
@LowerDeez
LowerDeez / order_conf.html
Created October 8, 2017 16:57
Django. Send mail with custom html template and context
<!DOCTYPE html>
<html>
<body>
<br>
<h2>Thank you for your order {{ order.full_name }}</h2>
<h3>Your Order ID: {{ order.short_uuid }}</h3>
<h3>Shipping Details:</h3>
@LowerDeez
LowerDeez / base.html
Last active March 2, 2024 08:32
Django. Simple Cart app for basic shop.
<div class="cart">
{% with total_items=cart|length %}
{% if cart|length > 0 %}
Ваша корзина:
<a href="{% url "cart:CartDetail" %}">
{{ total_items }} тов. {{ cart.get_total_price }} руб.
</a>
{% else %}
Корзина пустая
{% endif %}
@LowerDeez
LowerDeez / home.html
Created May 28, 2017 21:29
Django Pagination (with search query)
{% for article in articles %}
{% include 'articles/partial/partial_article.html' %}
{% empty %}
<h1>Nothing to show</h1>
{% endfor %}
<!-- Pagination -->
{% if articles.has_other_pages %}
<div class="text-center">
<ul class="pagination pagination-lg text-center">
@LowerDeez
LowerDeez / custom_django_checks.py
Created July 23, 2021 14:58 — forked from hakib/custom_django_checks.py
Custom django checks using Django check framework, inspect and ast.
"""
Custom django checks.
H001: Field has no verbose name.
H002: Verbose name should use gettext.
H003: Words in verbose name must be all upper case or all lower case.
H004: Help text should use gettext.
H005: Model must define class Meta.
H006: Model has no verbose name.
H007: Model has no verbose name plural.
@LowerDeez
LowerDeez / admin.py
Last active February 17, 2023 05:15
Django. Admin Tabular Inline initial data
from django.utils.functional import curry
class DetailsInline(admin.TabularInline):
model = Details
# formset = DetailsFormset
extra = 3
def get_formset(self, request, obj=None, **kwargs):
initial = []
if request.method == "GET":
@LowerDeez
LowerDeez / admin.py
Created October 6, 2017 10:40
Django. Admin Tabular Inline. Get parent object
class ProductVariantAdminInline(admin.TabularInline):
extra = 0
model = ProductVariant
def get_parent_object_from_request(self, request):
"""
Returns the parent object from the request or None.
Note that this only works for Inlines, because the `parent_model`
is not available in the regular admin.ModelAdmin as an attribute.
@LowerDeez
LowerDeez / after_fetch_queryset_mixin.py
Created September 20, 2021 17:30 — forked from spookylukey/after_fetch_queryset_mixin.py
AfterFetchQuerySetMixin for Django
class AfterFetchQuerySetMixin:
"""
QuerySet mixin to enable functions to run immediately
after records have been fetched from the DB.
"""
# This is most useful for registering 'prefetch_related' like operations
# or complex aggregations that need to be run after fetching, but while
# still allowing chaining of other QuerySet methods.
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@LowerDeez
LowerDeez / number_str_to_float.py
Created September 14, 2021 07:46 — forked from codingforentrepreneurs/number_str_to_float.py
A simple python function to convert a number string into a float if possible.
from fractions import Fraction
def number_str_to_float(amount_str:str) -> (any, bool):
"""
Take in an amount string to return float (if possible).
Valid string returns:
Float
Boolean -> True
# This is a hack so that French translation falls back to English translation,
# not German translation (which is the default locale and the original
# strings).
from django.utils.translation import trans_real
class MyDjangoTranslation(trans_real.DjangoTranslation):
def _add_fallback(self, localedirs=None):
if self._DjangoTranslation__language[:2] in {'de', 'en'}:
return super()._add_fallback(localedirs)
@LowerDeez
LowerDeez / views.py
Created June 2, 2021 14:21
Protected file access
from django.http import HttpResponse
from django.http import HttpResponseForbidden
from apps.order.models import Certificate, Order
__all__ = (
'files_access',
)