Skip to content

Instantly share code, notes, and snippets.

View archatas's full-sized avatar

Aidas Bendoraitis archatas

View GitHub Profile
@archatas
archatas / specification.md
Created July 11, 2018 18:26
Idea for the {% include_by_ajax %} template tag in Django

Idea for the {% include_by_ajax %} template tag in Django

The Problem

Home pages of websites usually show data aggregated from different sections. To render a homepage might take some time if the relations and filters are complex or if there are a lot of images.

The Solution

I have an idea how to speed up the initial page load by delaying the rendering of some parts of the page (for example, those which are bellow the fold). This could be done by a new third-party app with a template tag {% include_by_ajax template_name %}. This template tag would work similarly like {% include template_name %}, but instead of the content in the page, it would render placeholders, and then by Ajax call it would access the same page again, load it with all content rendered, and would dynamically fill in the missing content to the main page.

@archatas
archatas / views.py
Last active November 28, 2018 20:10
Adding custom Django CMS toolbar menu
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.shortcuts import render
from .models import MyObj
@archatas
archatas / do_something.py
Last active March 30, 2022 13:56
Django Management Command Boilerplate
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = ""
SILENT, NORMAL, VERBOSE = 0, 1, 2
def handle(self, *args, **options):
self.verbosity = options.get("verbosity", self.NORMAL)
self.prepare()
@archatas
archatas / provision.yml
Last active May 30, 2020 19:52
Ansible rule to install the micro editor
- name: Install micro editor
shell: curl https://getmic.ro | bash > /dev/null 2>&1
args:
chdir: /usr/local/bin
warn: no
creates: /usr/local/bin/micro
@archatas
archatas / views.py
Created June 8, 2020 12:55
Django File Download
def goodie_download(request, goodie_id):
instance = get_object_or_404(Goodie, pk=goodie_id)
if not instance.document:
raise Http404("File does not exist")
stream = instance.document.open("rb")
response = FileResponse(stream, content_type="application/octet-stream")
response["Content-Disposition"] = f"attachment; filename={instance.get_nice_filename()}"
return response
@archatas
archatas / simple_encryption.py
Last active November 16, 2022 21:39
Simple functions to encrypt and decrypt unicode strings in Django using pycryptodomex library
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
import codecs
from django.conf import settings
from django.utils.encoding import force_text, force_bytes
def encrypt_string(plain):
@archatas
archatas / backup_blog_1st_things_1st_com_db.j2
Created January 24, 2022 16:05
Ansible template for the bash script that creates database backups of the Wordpress database every day of the week
#!/usr/bin/env bash
CURRENT_DIR=$(dirname "$0")
WEEK_DATE=$(LC_ALL=en_US.UTF-8 date +"%w-%A")
BACKUPS_DIR="{{ project_root }}/db_backups/blog.1st-things-1st.com"
CREDENTIALS_PATH=${BACKUPS_DIR}/.sqlpwd
BACKUP_PATH_LATEST=${BACKUPS_DIR}/latest.sql
BACKUP_PATH_DAILY=${BACKUPS_DIR}/${WEEK_DATE}.sql
DATABASE={{ mysql_blog_1st_things_1st_com_dbname }}
USER={{ mysql_blog_1st_things_1st_com_username }}
PASSWORD={{ mysql_blog_1st_things_1st_com_password }}
@archatas
archatas / latest_dependencies.py
Created March 22, 2022 19:44
This script lists latest migration dependencies of selected Django apps. Useful for data migrations that combine data from different apps.
"""
This script lists latest migration dependencies of selected apps.
Usage for selected apps:
(venv)$ python manage.py showmigrations | python latest_dependencies.py app1 app2 app3
Usage for all apps:
(venv)$ python manage.py showmigrations | python latest_dependencies.py
@archatas
archatas / urls.py
Created April 16, 2022 19:40
oauth2_provider_adjustments
from django.urls import re_path
from oauth2_provider import views as oauth2_provider_views
from . import views
app_name = "oauth2_provider_adjustments"
urlpatterns = [
# Base
@archatas
archatas / import_blog_posts.py
Created April 23, 2022 10:50
A boilerplate code for Django management commands
from django.core.management.base import BaseCommand
class Command(BaseCommand):
SILENT, NORMAL, VERBOSE, VERY_VERBOSE = 0, 1, 2, 3
help = "Imports blog posts from an RSS feed"
def handle(self, *args, **options):
self.verbosity = int(options.get("verbosity"))