Skip to content

Instantly share code, notes, and snippets.

View archatas's full-sized avatar

Aidas Bendoraitis archatas

View GitHub Profile
@archatas
archatas / requests_fix.py
Created August 31, 2016 11:06
Fixing SSL certificate issues in requests library
# In case of SSL errors in Python requests library:
# (myenv)$ pip install certifi
# Then add the following to your Django settings:
import os, certifi
os.environ['REQUESTS_CA_BUNDLE'] = certifi.where()
@archatas
archatas / add_css_dynamically.js
Last active August 31, 2016 11:07
Add CSS rules dynamically
/**
* Add CSS rules dynamically
* Example:
* dyn_css_rule("#menu li:hover", "color: red")
*/
window.dyn_css_rule = function(sSelector, sCssText) {
try {
var aSS = document.styleSheets;
var i;
for (i=aSS.length-1; i>=0; i--) {
@archatas
archatas / admin.py
Last active December 14, 2016 01:11
Django administration inlines for inlines
# -*- coding: UTF-8 -*-
from django.contrib import admin
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.utils.text import force_text
from .models import Painter, Picture, Review
def get_picture_preview(obj):
if obj.pk: # if object has already been saved and has a primary key, show picture preview
@archatas
archatas / backup_db.sh
Last active February 6, 2017 21:07
Bash script to save database backups for every day of the week in files like 1-Monday.sql, 2-Tuesday.sql, etc. This should be executed daily as a cron job.
#! /usr/bin/env bash
SECONDS=0
PROJECT_PATH=/home/example
CRON_LOG_FILE=${PROJECT_PATH}/logs/backup_db.log
BACKUP_PATH=${PROJECT_PATH}/db_backups/$(LC_ALL=en_US.UTF-8 date +"%w-%A").sql
USER=dbusername
DATABASE=dbname
PASS=dbpassword
EXCLUDED_TABLES=(
@archatas
archatas / cleanup.sh
Created February 6, 2017 21:15
Django management commands scheduled and with logging
#!/usr/bin/env bash
SECONDS=0
PROJECT_PATH=/home/myproject
CRON_LOG_FILE=${PROJECT_PATH}/logs/cleanup.log
echo "Cleaning up the database" > ${CRON_LOG_FILE}
date >> ${CRON_LOG_FILE}
cd ${PROJECT_PATH}
source bin/activate
@archatas
archatas / base.py
Last active April 6, 2017 23:06
Random unique string ID mixin for Django models
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
import shortuuid
from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
RANDOM_UNIQUE_ID_LENGTH = getattr(settings, "RANDOM_UNIQUE_ID_LENGTH", 12)
@archatas
archatas / postgres-cheatsheet.md
Created April 27, 2017 15:46 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

If run with -E flag, it will describe the underlaying queries of the \ commands (cool for learning!).

Most \d commands support additional param of __schema__.name__ and accept wildcards like *.*

@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 / .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