Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 05:21 (UTC +03:00)
View GitHub Profile
@LowerDeez
LowerDeez / forms.py
Created February 26, 2018 14:36
Django. How add to form extra fields
class MyForm(forms.Form):
...
def __init__(self, *args, **kwargs):
extra_fields = kwargs.pop('extra', 0)
super().__init__(*args, **kwargs)
if extra_fields and int(extra_fields) > 0:
for index in range(int(extra_fields)):
# generate extra fields in the number specified via extra_fields
@LowerDeez
LowerDeez / script.py
Created February 26, 2018 14:33
Django. Float Choices
blank_choice = ((None, '---------'),)
STAR_CHOICES = blank_choice + tuple((float(i*0.5), str(float(i*0.5))) for i in range(1, 11))
@LowerDeez
LowerDeez / tests.py
Last active December 6, 2019 12:38
Django. How to test Celery task.
@override_settings(
EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend',
CELERY_ALWAYS_EAGER=True)
def test_valid_create(self):
response = client.post(
self.create_view_name,
data=json.dumps(self.valid_payload),
content_type='application/json'
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
@LowerDeez
LowerDeez / script.py
Last active March 21, 2018 13:51
Django. Get count of objects filtered by choices.
kwargs['count'] = {
v['status']: v['status__count']
for v in
self.get_queryset().values('status').annotate(Count('status')).order_by('status')
}
# status = model choice field
@LowerDeez
LowerDeez / template.pug
Created January 19, 2018 15:26
Django. Render ModelChoice field as quanryset in pug with adding custom data-attrs for option tag
select.ordering__select(data-msg=trans('Заполните поле'), required)
+for('item in form.fields.field_name.queryset')
option(value=exp('item.pk'), data-price=exp('item.price'))
+exp item.title
.errors
+exp form.delivery_way.errors.as_text()
@LowerDeez
LowerDeez / script.py
Created December 21, 2017 09:53
Extract GPS & Exif Data from Images using Python
from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS
class ImageMetaData(object):
'''
Extract the exif data from any image. Data includes GPS coordinates,
Focal Length, Manufacture, and more.
'''
exif_data = None
@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:
@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 / 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 / __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: