Skip to content

Instantly share code, notes, and snippets.

View niccolomineo's full-sized avatar

Niccolò Mineo niccolomineo

View GitHub Profile
@niccolomineo
niccolomineo / trunc_decimal.py
Last active October 22, 2024 18:45
Truncate decimal to position with none of the unwanted roundings
def trunc_decimal(number: Decimal | float, position: int):
"""
Truncate decimal to position with none of the unwanted roundings.
:param position: decimal position to truncate the decimal to
:return: decimal truncated to position
"""
coefficient = "1" + "0" * position
return floor(number * int(coefficient)) / int(coefficient)
@niccolomineo
niccolomineo / models.py
Last active August 19, 2024 09:12
Automatically register multiple Wagtail tag models
"""Wagtail models."""
from django.db.models import CASCADE, Model
from modelcluster.contrib.taggit import ClusterTaggableManager
from taggit.models import TaggedItemBase
from wagtail.models import Page, ParentalKey
TAG_FIELDS = [
("Tag Category 1", "tag_category_1"),
@niccolomineo
niccolomineo / Dockerfile
Created July 28, 2024 10:27
PostgreSQL 16 on Debian bookworm 12.6
FROM python:3.11-slim-bookworm
RUN curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
RUN sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
postgresql-client-16
@niccolomineo
niccolomineo / webpack.config.js
Last active July 25, 2024 08:42
A Webpack configuration for obfuscating JavaScript in an Electron.js app
const Path = require('path'),
NodeExternals = require('webpack-node-externals'),
Copy = require('copy-webpack-plugin'),
Obfuscator = require('webpack-obfuscator')
module.exports = {
context: __dirname,
mode: 'production',
target: 'electron-main',
entry: {
@niccolomineo
niccolomineo / pyproject.toml
Last active August 9, 2023 10:15
A `pyproject.toml` configuration for a Django / Python 3.11 installable package
[build-system]
build-backend = "setuptools.build_meta"
requires = [
"setuptools",
"wheel",
]
[project]
name = "Django Project"
version = "0.0.1"
@niccolomineo
niccolomineo / text_utils.py
Last active August 27, 2024 11:51
A utiliy to strip the Byte Order Mark from UTF-8 text
"""Text utils."""
def strip_bom(value):
"""Strip Byte Order Mark from UTF-8 text."""
return value.encode("utf-8").decode("utf-8-sig")
@niccolomineo
niccolomineo / storages.py
Last active August 3, 2024 13:19
A file system storage with file overwriting capabilities
"""Django storages."""
from django.core.files.storage import FileSystemStorage
class FileSystemOverwriteStorage(FileSystemStorage):
"""A file system storage with file overwriting capabilities."""
def get_available_name(self, name, max_length=None):
"""Return the available name."""
@niccolomineo
niccolomineo / filters.py
Last active August 27, 2024 11:53
Django ArrayField inheritable list filter
"""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 lookups."""
@niccolomineo
niccolomineo / mixins.py
Last active August 27, 2024 11:51
A mixin handling read-only fields per group, admin model and form type in Django
class FieldPermissionsMixin:
"""
Define a mixin handling read-only fields per group, admin model and form type.
!!!THIS IS JUST A DRAFT, 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 September 10, 2021 16:29
(Django admin inline) PIL thumbnail generation w/ smart cropping
# 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