Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 00:02 (UTC +03:00)
View GitHub Profile
@LowerDeez
LowerDeez / admin.py
Last active December 30, 2020 07:43
Django. Custom views for admin.
@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
list_display = [
'name',
'status',
'add_product_action',
'add_mattress_action'
]
list_display_links = ['name', 'status']
readonly_fields = [
@LowerDeez
LowerDeez / decorators.py
Created October 19, 2018 06:44
Django. Example how to use a class as dacorator
from functools import wraps
from django.http.response import HttpResponse
from django.utils.decorators import available_attrs
from rest_framework_extensions.settings import extensions_api_settings
from django.utils import six
@LowerDeez
LowerDeez / script.py
Created October 1, 2018 09:06
Django. Remove GET parameters with specific prefix from querystring
qs = request.META.get('QUERY_STRING', '')
querystring = qs.split('&')
filtered_querystring = filter(lambda p: not p.split('=')[0].startswith('utm_'), querystring)
qs = '&'.join(filtered_querystring)
@LowerDeez
LowerDeez / render.py
Created August 23, 2018 14:16
Django. Create PDF with xhtml2pdf
from io import BytesIO
import os
from django.conf import settings
from django.contrib.staticfiles import finders
from django.http import HttpResponse
from django.template.loader import render_to_string
from django.utils import dateformat
from django.utils.timezone import now
@LowerDeez
LowerDeez / model_serach.py
Created May 9, 2018 12:13
Django. Search by given model, fields and query
import re
import operator
from django.apps import apps
from django.conf import settings
from django.db.models import Q
TERMS = re.compile(r'"([^"]+)"|(\S+)').findall
NORM_SPACE = re.compile(r'\s{2,}').sub
@LowerDeez
LowerDeez / settings.py
Created April 7, 2018 17:57
Django. Get List of Current Users
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
@LowerDeez
LowerDeez / handlers.py
Created March 22, 2018 07:40
Django. Signal to check when user login
from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver
from django.contrib.gis.geoip2 import GeoIP2
from django.conf import settings
import geoip2
from apps.mailer.tasks import send_template_email
from .models import LoginStatistics
@LowerDeez
LowerDeez / form.pug
Created March 21, 2018 15:32
Django. How to render form in Pug
include /../mixins/index.pug
form.py-3(method="post", enctype="multipart/form-data")
+tag csrf_token
+if('form.non_field_errors()')
.alert.alert-danger: +exp form.non_field_errors().as_text()
+for('hidden_field in form.hidden_fields()')
+exp hidden_field
@LowerDeez
LowerDeez / enroll_reminder.py
Created March 6, 2018 15:55
Django. Custom management command.
import datetime
from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.mail import send_mass_mail
from django.contrib.auth.models import User
from django.db.models import Count
class Command(BaseCommand):
help = 'Sends an e-mail reminder to users registered more \
@LowerDeez
LowerDeez / middleware.py
Created March 6, 2018 15:43
Django. Custom middleware for objects subdomains
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, redirect
from .models import Course
class SubdomainCourseMiddleware(object):
"""
Provides subdomains for courses
"""
def process_request(self, request):