Skip to content

Instantly share code, notes, and snippets.

View archatas's full-sized avatar

Aidas Bendoraitis archatas

View GitHub Profile
@archatas
archatas / processing_with_progress.py
Last active February 6, 2023 04:17
Progress bar useful for django management commands and scripts that require user interaction
from tqdm import tqdm
from myapp.models import MyModel
progress_bar = tqdm(desc="Processing", total=MyModel.objects.count())
for obj in MyModel.objects.all():
obj.process() # do something time consuming
progress_bar.update(1)
progress_bar.close()
@archatas
archatas / test.py
Created July 6, 2017 14:14
Disable migrations for tests. Run tests with: python manage.py test --settings=settings.test
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
from ._base import *
# ...
class DisabledMigrations(object):
def __contains__(self, item):
return True
def __getitem__(self, item):
@archatas
archatas / example.html
Last active July 11, 2017 18:42
Avoid listing HTML attribute values within translatable string in Django template
{% blocktrans with username=request.user.username link=request.user.get_url_path css_class="btn btn-link" %}
Hello <a href="{{ link }}" class="{{ css_class }}">{{ username }}</a>!
{% endblocktrans %}
@archatas
archatas / emojis-for-git-commits.md
Last active August 15, 2020 06:09
Emojis for Git Commits

✨ - New feature

👾 - Bugfix

🎨 - UI and style

🔨 - Code refactoring, dependency upgrade, improving code quality

🚧 - Work in progress

@archatas
archatas / .htaccess
Created July 26, 2017 23:19
Allow access to a website only to specific user agents or visitors with authentication credentials
SetEnvIf User-Agent ^VipAgent1 vip_agent
SetEnvIf User-Agent ^VipAgent2 vip_agent
Order Allow,Deny
Allow from env=vip_agent
AuthType Basic
AuthName "Protected Login"
AuthUserFile /path/to/htpasswd
Require valid-user
@archatas
archatas / emojis_to_html_entities.py
Last active January 10, 2018 15:10
Converts 4-byte emojis and other Unicode symbols to HTML entities, so that they could be saved to MySQL with UTF-8 encoding.
# -*- coding: utf-8 -*-
def emojis_to_html_entities(html):
import re
def replace_with_html_entity(match):
# from something like "u'\U0001f60e'" get only "0001f60"
codepoint = repr(match.group(0))[4:-1]
return u'&#x{};'.format(codepoint)
@archatas
archatas / article_headline.html
Last active January 28, 2018 03:08
In Django models, keep model-related constants at the model level instead of the module level.
{% load i18n %}
<h1>
{{ article.title }}
{% if article.publishing_status == article.PUBLISHING_STATUS_DRAFT %}({% trans "draft" %}){% endif %}
</h1>
@archatas
archatas / cleanup_exif.py
Created May 16, 2018 21:10
Remove EXIF information from an image
# -*- coding: utf-8 -*-
from PIL import Image
def cleanup_exif(input_file_path, output_file_path):
"""
Remove EXIF information from an image
"""
image_file = open(input_file_path)
@archatas
archatas / models.py
Created May 23, 2018 13:56
Custom Django Managers and QuerySets
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
class MyModelQuerySet(models.QuerySet):
def published(self):
return self.filter(is_published=True)
@archatas
archatas / console.py
Created June 12, 2018 16:40
Allows to print text in console using colors and other special effects
# -*- coding: UTF-8 -*-
"""
ANSI escape codes
http://en.wikipedia.org/wiki/ANSI_escape_code
"""
EFFECTS = {
'reset': "\x1b[0m",