Skip to content

Instantly share code, notes, and snippets.

Avatar

Niccolò Mineo niccolomineo

View GitHub Profile
@niccolomineo
niccolomineo / filters.py
Last active Sep 10, 2021
Django ArrayField inheritable list filter
View filters.py
"""Define Django filters."""
from django.contrib.admin import SimpleListFilter
class ArrayFieldListFilter(SimpleListFilter):
"""An admin list filter for ArrayFields."""
def lookups(self, request, model_admin):
"""Return the filtered queryset."""
@niccolomineo
niccolomineo / settings.py
Last active Feb 17, 2021
Settings env init
View settings.py
"""
Initialise settings.
This implementation requires class-based definition of each environment
in an individual file (e.g. `production.py` / `class Production`).
This very init code should go in an `__init__.py` file in the same folder as the environment files.
"""
@niccolomineo
niccolomineo / mixins.py
Last active Sep 10, 2020
A mixin handling read-only fields per group, admin model and form type in Django
View mixins.py
class FieldPermissionsMixin:
"""
Define a mixin handling read-only fields per group, admin model and form type.
!!!THIS IS JUST A STUB, AWAITING COMPLETION!!!
Read-only fields can be specified in a setting exemplified below.
For permission names, codenames without the model name are considered well-formed.
GROUPS = {
@niccolomineo
niccolomineo / admin.py
Last active Sep 10, 2021
(Django admin inline) PIL thumbnail generation w/ smart cropping
View admin.py
# Requirements:
# - a model with `file`and `thumbnail` fields.
# - the smartcrop module for Python https://github.com/smartcrop/smartcrop.py
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
"""Set MyModel Admin."""
formset = MyModelFormset
@niccolomineo
niccolomineo / response_mock.py
Last active Feb 17, 2021
Django response mock object
View response_mock.py
"""Define Django Response Mock."""
import json
from django.conf import settings
from django.utils.translation import gettext_lazy as _
class ResponseMock:
"""Define response mock."""
@niccolomineo
niccolomineo / google-spreadsheet-select-rightmost-sheet.gs
Created Mar 2, 2020
Select rightmost sheet in a Google Spreadsheet
View google-spreadsheet-select-rightmost-sheet.gs
function selectRightmostSheet()
{
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
var sheets = spreadsheet.getSheets()
spreadsheet.setActiveSheet(sheets[sheets.length-1])
}
selectRightmostSheet()
View google-drive-api-jwt-service-account
import os
import time
import mimetypes
import jwt
import google.auth.crypt
import google.auth.jwt
import requests
credentials_file = json.load(open(
os.path.dirname(os.path.realpath(__file__)) +
@niccolomineo
niccolomineo / toggle-bluetooth.applescript
Last active Jan 19, 2019
Toggles Bluetooth on Mac Os. Requires blueutil utility.
View toggle-bluetooth.applescript
on run {input, parameters}
tell application "System Events"
set current_status to (do shell script "eval $(/usr/libexec/path_helper -s); blueutil | grep 'Power:' | cut -c8-")
if current_status = "0" then
do shell script "eval $(/usr/libexec/path_helper -s); blueutil -p 1"
beep
delay 0.25
@niccolomineo
niccolomineo / get-ns-rgb-components-at-coords.swift
Last active Aug 7, 2018
Get NS RGB components at specific coordinates based on main display id
View get-ns-rgb-components-at-coords.swift
func getNSComponentsAt(x: Int, y: Int) -> NSColor {
let screenID = NSScreen.main!.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as! UInt32
let image: CGImage = CGDisplayCreateImage(CGDirectDisplayID(screenID), rect: CGRect(x: CGFloat(x), y: CGFloat(y), width: 1, height: 1))!
let bitmap = NSBitmapImageRep(cgImage: image)
let color = bitmap.colorAt(x: 0, y: 0)
self.redComponent = (color?.redComponent)!
self.greenComponent = (color?.greenComponent)!
self.blueComponent = (color?.blueComponent)!
View join-filter-angularjs.js
// Filter: removes all non alphanumerical characters from input
myApp.filter('join', function() {
return function(input) {
input = input || '';
var output = input.replace(/\W/g, '');
return output;
};
});