Skip to content

Instantly share code, notes, and snippets.

@ahbanavi
ahbanavi / encryption.php
Last active May 1, 2024 23:18
Encrypt / Decrypt JSON data between Python and PHP using AES 256 GCM
<?php
const PASSPHRASE = ''; // use 'openssl rand -hex 32' to generate key, same with python
function encrypt(array $data): string
{
$data_json_64 = base64_encode(json_encode($data));
$secret_key = hex2bin(PASSPHRASE);
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-gcm'));
$tag = '';
$encrypted_64 = openssl_encrypt($data_json_64, 'aes-256-gcm', $secret_key, 0, $iv, $tag);
@roozbehsam
roozbehsam / datatables.py
Last active January 20, 2021 18:32
Django + MongoDB DataTables (Jquery datatables v1.9 used)
from collections import namedtuple
import pymongo
"""
(Jquery datatables v1.9&below - pymongo v3.11.0 - python v3.8 - mongodb v4.0.6 - django v2.2)
$('#table').dataTable({
"sAjaxSource": "/route_name" // USE AJAX_SOURCE TO SWITCH TO V1.9 AUTOMATICALLY, even if you are using latest version!
});
@mtShaikh
mtShaikh / set-heroku-config-vars-from-env.sh
Created November 12, 2019 06:53 — forked from tibawatanabe/set-heroku-config-vars-from-env.sh
Command line to read .env file and set Heroku config vars
heroku config:set $(cat .env | sed '/^$/d; /#[[:print:]]*$/d')
@maebert
maebert / unbox.sh
Created February 25, 2014 00:42
Setting up a new bac
#!/bin/bash
# This script automatically installs a bunch of command line (Git, wget,
# imagemagick, ...) and GUI (Dropbox, Chrome, Skype, ...) apps on a fresh
# Mac.
#
# It also sets up a Python Scientific Computing environment (matplotlib,
# scipy, iPython Notebook, ...)
#
# Requirement: Have the XCode Command Line Tools installed (download from
@jsummerfield
jsummerfield / gist:1486891
Created December 16, 2011 17:00
Amazon SES SMTP support for Django
"""
Django's SMTP EmailBackend doesn't support an SMTP_SSL connection necessary to interact with Amazon SES's newly announced SMTP server. We need to write a custom EmailBackend overriding the default EMailBackend's open(). Thanks to https://github.com/bancek/django-smtp-ssl for the example.
"""
--- settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'email-smtp.us-east-1.amazonaws.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'username'
@lprsd
lprsd / admin.py
Created June 1, 2011 08:59
django admin default filter values
class YourAdmin(ModelAdmin):
def changelist_view(self, request, extra_context=None):
ref = request.META.get('HTTP_REFERER','')
path = request.META.get('PATH_INFO','')
if not ref.split(path)[-1].startswith('?'):
q = request.GET.copy()
q['usertype'] = 'Publisher'
q['user_status__exact'] = 'activated'
request.GET = q
request.META['QUERY_STRING'] = request.GET.urlencode()