Skip to content

Instantly share code, notes, and snippets.


CHANN channprj

View GitHub Profile
niuware /
Created Oct 18, 2019
How to stream a CSV file from a large QuerySet using Django's StreamingHttpResponse

Stream a CSV file from a QuerySet using StreamingHttpResponse

This is a sample on how to stream the results of a large QuerySet into a CSV file using Django StreamingHttpResponse class.

  1. Add the CSVStream class in your project, for example a file:
import csv
from django.http import StreamingHttpResponse
mayneyao / notion2blog.js
Last active Jul 27, 2021 > Personal Blog | custom domain + disqus comment
View notion2blog.js
const MY_DOMAIN = ""
const START_PAGE = ""
const DISQUS_SHORTNAME = "agodrich"
addEventListener('fetch', event => {
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
lizthegrey / attributes.rb
Last active Jun 8, 2021
Hardening SSH with 2fa
View attributes.rb
default['sshd']['sshd_config']['AuthenticationMethods'] = 'publickey,keyboard-interactive:pam'
default['sshd']['sshd_config']['ChallengeResponseAuthentication'] = 'yes'
default['sshd']['sshd_config']['PasswordAuthentication'] = 'no'
tkafka / listAllEventListeners.js
Last active May 22, 2021 — forked from dmnsgn/listAllEventListeners.js
List all event listeners in a document
View listAllEventListeners.js
console.table((function listAllEventListeners() {
const allElements ='*'));
allElements.push(document); // we also want document events
const types = [];
for (let ev in window) {
if (/^on/.test(ev)) types[types.length] = ev;
let elements = [];
for (let i = 0; i < allElements.length; i++) {
johngrib /
Created Dec 31, 2018
Show github contribution graph on terminal
#! /usr/local/bin/bash
curl -s$MYGITHUB/ > $RAW
TITLE=`cat /tmp/johngrib-github | pcregrep -M '[0-9,]+ contributions\s*\n\s*in the last year'`
canokay /
Last active Jul 29, 2021
Django Custom View Decorators
from django.http import HttpResponseRedirect
from django.core.exceptions import PermissionDenied
from django.urls import reverse
from django.shortcuts import render
from django.shortcuts import redirect
def role_required(allowed_roles=[]):
def decorator(view_func):
def wrap(request, *args, **kwargs):
omarryhan / Sanic-Gino-Alembic migrations Setup
Last active May 30, 2020
Sanic/Gino/Alembic migrations
View Sanic-Gino-Alembic migrations Setup
# Do the first 6 steps only once.
1. pip install --user alembic
2. bash: ``cd {{my_project}} && alembic init alembic``
3. bash: ``text_editor {{my_project}}/alembic.ini``
4. Change: "sqlalchemy.url = postgres://{{username}}:{{password}}@{{address}}/{{db_name}}"
5. bash: ``text_editor {{my_project}}/alembic/``
6. Now, import your metadata/db object from your app.:
# {{my_project}}/{{my_project_dir}}/
# Darkify Slack on Mac OS or Linux.
# curl | sh
if [ "`uname -s`" = "Darwin" ]; then
nirizr /
Last active Jun 11, 2021
sqlalchemy upsert supporting delayed ORM insertion and duplicate removal (inside a single query)
def upsert(session, model, rows):
table = model.__table__
stmt = postgresql.insert(table)
primary_keys = [ for key in inspect(table).primary_key]
update_dict = { c for c in stmt.excluded if not c.primary_key}
if not update_dict:
raise ValueError("insert_or_update resulted in an empty update_dict")
stmt = stmt.on_conflict_do_update(index_elements=primary_keys,
slowkow /
Created Jul 19, 2018
Remove all traces of emoji from a text file.
#!/usr/bin/env python
Remove emoji from a text file and print it to stdout.
python input.txt > output.txt