Skip to content

Instantly share code, notes, and snippets.

View fmalina's full-sized avatar

F. Malina fmalina

View GitHub Profile
@fmalina
fmalina / sql_debug.html
Last active May 13, 2020 12:53
Django SQL debug template with SQL code highlighting and line breaks for legibility.
{% if debug %}
<div class="sql_debug">
<div class="showhide">
{{ sql_queries|length }} quer{{ sql_queries|pluralize:"y,ies" }}
{% ifnotequal sql_queries|length 0 %}
(<a href="#" onclick="var s=document.getElementById('queries').style;s.display=s.display=='none'?'':'none';this.innerHTML=this.innerHTML=='show'?'hide':'show';">show</a>)
{% endifnotequal %}
</div>
<div id="queries" style="display:none">
{% for query in sql_queries %}
@fmalina
fmalina / date_extract.py
Last active September 28, 2021 15:42
Dates extractor with tests to extract dates from HTML and text (regex based parser)
""" Extract dates from HTML
test using: python date_extract.py -v
"""
import calendar
import re
from datetime import date
from django.template.defaultfilters import striptags
""" Create acronyms for names following rules
test using: python acronym_maker.py -v
"""
def acronym_maker(s):
"""
Make up 2 and 3 letter acronyms for given string
WIP: The following tests are not passing
2-letter short, 3-letter short, full name
@fmalina
fmalina / gislibs_sourcebuild.sh
Last active September 29, 2021 18:55
Setup of GIS libs for geodjango from source for older OS X
# Setup of GIS libs from source for *NIX (older OS X)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Sometimes package manager can't do
# e.g. Homebrew is broken for older OS X even High Sierra, so this is handy
#
# Warning: brew uninstalling your older but working brew packages on older OS X
# can get you in a situation where you won't be able to brew install them again!!
#
# https://docs.djangoproject.com/en/3.2/ref/contrib/gis/install/geolibs/
@fmalina
fmalina / adaptive.py
Created October 1, 2021 15:08
Mobile phone detection middleware for Django
"""Mobile phone detection middleware for Django"""
from django.utils.cache import patch_vary_headers
class MobileMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def is_mobile(self, ua):
ua = ua.lower()
@fmalina
fmalina / paging.py
Last active October 3, 2021 19:11
Pagination for Django reusable at view level (simple, generic)
"""
Generic pagination reusable at view level.
Paging middleware needs installing in the settings:
MIDDLEWARE = (
... almost at the end...
'paging.paging_middleware',
Use in a view like so:
@fmalina
fmalina / amazon_product_image_by_asin.py
Last active December 30, 2021 19:36
Download a product image from Amazon by ASIN code with no Amazon affiliate account
"""
Install dependencies using:
pip3 install Pillow, requests, lxml, cssselect
Edit your product ASIN IDs
Enter API KEY from scraperapi.com
"""
from io import BytesIO
import requests
from PIL import Image, UnidentifiedImageError