Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 22:54 (UTC +03:00)
View GitHub Profile
@LowerDeez
LowerDeez / admin.py
Last active December 6, 2019 12:34
Django. Sinleton admin mixin
class SingleObjectAdminModelMixin(AdminCharCountMixin):
list_display = ('page_name',)
def has_delete_permission(self, request, obj=None):
"""
Forbid object deleting
"""
return True
def page_name(self, obj):
@LowerDeez
LowerDeez / admin.py
Last active December 6, 2019 12:32
Django. JS in admin panel for checking letters count.
class SomeModelAdmin(admin.ModelAdmin):
class Media:
js = ['path/to/script.js']
@LowerDeez
LowerDeez / celery-worker.bat
Last active November 22, 2017 08:53
Django. .bat files to run client and server
@echo off
cmd /k "cd /d %~dp0\env\Scripts\ & activate & cd /d %~dp0\server\ & set "DJANGO_SETTINGS_MODULE=app.settings" & celery -A app worker -l info"
@LowerDeez
LowerDeez / base.babel.js
Created September 28, 2017 09:19
Django. Web-case template. Gulp-task example to minify images
import gulp from 'gulp'
import * as stylus from './tasks/stylus'
import * as templates from './tasks/templates'
import * as pub from './tasks/public'
import * as images from './tasks/images'
gulp.task('images', images.dev)
gulp.task('public', pub.dev)
gulp.task('stylus', stylus.dev)
@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 / 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 / utils.py
Last active January 31, 2018 08:09
Django. Download image from url and create object with this image and FIlerImageField
def estate_gallery_creator(images, base_estate_object):
"""
Creates BaseEstateGallery objects for BaseEstate instance
:param images: list of images urls
:param base_estate_object: BaseEstate instance id
:return: creates BaseEstateGallery object
"""
for index, image_url in enumerate(images, 1):
# Get the filename from the url, used for saving later
file_name = image_url.split('/')[-1]
@LowerDeez
LowerDeez / models.py
Last active March 21, 2018 15:33
Django. Color Field
from django.core.validators import RegexValidator
 
 
class Project(models.Model):
    color = models.CharField(
        max_length=7,
        default="#fff",
        validators=[RegexValidator(
            "(^#[0-9a-fA-F]{3}$)|(^#[0-9a-fA-F]{6}$)")],
        verbose_name=_("color"),
@LowerDeez
LowerDeez / csrf.ajax.js
Created October 19, 2017 21:39
csrf.ajax.js
$(document).ready(function(){
// using jQuery
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {