Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 02:12 (UTC +03:00)
View GitHub Profile
@LowerDeez
LowerDeez / admin.py
Created October 23, 2017 11:09
Django. Likes app
from django.contrib import admin
from .models import Like
admin.site.register(Like)
@LowerDeez
LowerDeez / admin.py
Last active December 6, 2019 12:26
Django. Update queryset for Foreign Keys in Admin TabularInline
class VariantImageAdminInline(admin.TabularInline):
model = VariantImage
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
"""enable ordering drop-down alphabetically"""
if db_field.name == 'image':
resolved = resolve(request.path_info)
if resolved.args:
parent_object = self.parent_model.objects.filter(pk=resolved.args[0]).first()
@LowerDeez
LowerDeez / models.py
Created October 29, 2017 18:17
Django. Custom storage to overwrite files. And few file uploads examples
# saves images to folder with class name if it is generic relation gallery
def image_upload_to(instance, filename):
return "{}//{}".format(slugify(instance.content_object.__class__.__name__), filename)
# the same but for abstarct class
def image_upload_to(instance, filename):
return "{}//{}".format(slugify(instance.__class__.__name__), filename)
# saves image with user full name (for profile image)
def image_upload_to(instance, filename):
@LowerDeez
LowerDeez / forms.py
Last active November 1, 2017 13:17
Django. Render ModelMultipleChoiceField in pug
from django import forms
from django.utils.translation import ugettext_lazy as _
from apps.country.models.country import Country
from apps.country.models.resort import Resort
from apps.hotel import HotelStars
from apps.hotel.models import HotelType
class HotelFilterForm(forms.Form):
country = forms.ModelMultipleChoiceField(
@LowerDeez
LowerDeez / js_load_more.js
Last active November 1, 2017 13:16
Django. Ajax pagination
$(document).ready(function() {
// pagination with appending objects (load more pagination)
$(".infinite-more-link").click(function(e) {
var link = $(this);
var page = link.data('page')
$.ajax({
method: 'GET',
url: $(this).data('url'),
data: {
'page': page
@LowerDeez
LowerDeez / script.js
Last active November 3, 2017 13:23
Django. Render modelchoicefield in pug with data attributes
$(document).ready(function() {
$('.about__change-select').on('change', function(){
var option = $('option:selected', this).data('price');
$('.date_price').text(Math.floor(option).toString().replace(/\B(?=(\d{3})+(?!\d))/g, " "))
})
})
@LowerDeez
LowerDeez / __init__.py
Last active November 21, 2017 08:53
Django. Custom templates
from __future__ import unicode_literals
import logging
from django.utils.translation import ugettext_lazy as _
logger = logging.getLogger(__name__)
class TemplatesChoices:
@LowerDeez
LowerDeez / template.pug
Created November 21, 2017 11:23
Django. Simple logout view
a.header-top__login(href=exp("url('accounts:logout')")+"?next=" + exp("df.urlencode(request.path)"))
@LowerDeez
LowerDeez / create.html
Created December 1, 2017 14:23
Django. Nested Inlines
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
</head>
<body>
@LowerDeez
LowerDeez / another_script.py
Last active March 21, 2018 13:55
Django. How to add watermark
""" Function for applying watermarks to images.
Original found here:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879
"""
try:
import Image
import ImageEnhance
except ImportError:
try: