Skip to content

Instantly share code, notes, and snippets.

View augustomen's full-sized avatar

Augusto Men augustomen

  • Canada
View GitHub Profile
@augustomen
augustomen / postgresql_charfield.py
Created March 28, 2023 20:21
Django 4.1 - CharField that doesn't require max_length (for databases like PostgreSQL)
from django.db import models
class CharField(models.CharField):
"""A subclass of django.db.models.CharField that doesn't require max_length."""
def _check_max_length_attribute(self, **kwargs):
"""Allow max_length=None (the default value) without errors."""
if self.max_length is None:
return []
@augustomen
augustomen / nomigration.md
Last active March 28, 2023 20:19
Django - Prevent migration of non-database attributes

Prevent migration of non-database attributes

By default, when creating migrations, Django will compare all the attributes of all models and all fields and compare them to the latest migrated state. This means that changes to attributes like verbose_name, choices, help_text, and validators cause new migrations to be created - even though they have no effect on the underlying database.

This is the way Django was designed to behave, but if during development you have constant changes to those attributes, and would like to prevent them from

@augustomen
augustomen / flask_mailman_ses.py
Created October 11, 2022 18:45
Flask-Mailman backend to send emails via AWS SES
"""Flask-Mailman backend to send emails via AWS SES."""
import boto3
from botocore.exceptions import ClientError
from flask_mailman.backends.base import BaseEmailBackend
class EmailBackend(BaseEmailBackend):
"""Implements sending email via SES through boto3.
Usage (in settings):
@augustomen
augustomen / cdn_storage.py
Created June 20, 2022 15:50
Django - Render static links using CDN from GitHub
from urllib.parse import urljoin
from django.conf import settings
from django.contrib.staticfiles.storage import StaticFilesStorage
from django.utils.encoding import filepath_to_uri
class CustomStaticFilesStorage(StaticFilesStorage):
"""Allows setting a different STATIC_URL for each static file prefix."""
@augustomen
augustomen / capturelog.py
Created October 24, 2019 15:44
Logger capturing utility
class CaptureLog:
"""Inside the context of this object, all log from the given logger is redirected to a temporary file.
Within the context, the file can be read. After the context exits, the file is deleted.
:param logger: A Logger instance or a string which is the path to the logger.
:param level: Minimum level to capture logs.
:param log_format: Format string used to capture logs from this logger.
Example of usage:
@augustomen
augustomen / fieldset.html
Last active September 24, 2019 13:29
Django Admin Bootstrapped customized fieldset.html that maintains alignment of labels and fields in two-column lines
{% load bootstrapped_goodies_tags %}
<fieldset class="_module _aligned" id="fieldset-{% if stacked_prefix %}{{ stacked_prefix }}-{% endif %}{{ forloop.counter }}" style="background:transparent">
{% if fieldset.name %}
<legend>
{% if 'collapse' in fieldset.classes %}
<a data-toggle="collapse" data-target="#fieldset-{% if stacked_prefix %}{{ stacked_prefix }}-{% endif %}{{ forloop.counter }} .fields">
{{ fieldset.name }}
&nbsp;<span class="btn btn-xs btn-default"><span class="glyphicon glyphglyphicon glyphicon-resize-full"></i></span>
</a>
{% else %}
@augustomen
augustomen / build.gradle
Created June 13, 2018 14:30
Obtain Android versionCode from git commit count
def loadVersionCode() {
def code = 1
new ByteArrayOutputStream().withStream { os ->
exec {
commandLine 'git', 'rev-list', 'HEAD', '--count'
standardOutput = os
}
code = os.toString().toInteger()
}
println "Version Code: $code"
# -*- coding: utf-8 -*-
import re
import sys
NEWLINE = '\r\n'
TIME_STR = re.compile(r'\d{2}\:\d{2}\:\d{2}[\,\.]\d{0,3}\s*--\>\s*\d{2}\:\d{2}\:\d{2}[\,\.]\d{0,3}')
TAGS_PATTERN = re.compile(r'\<[^\>]*\>')
@augustomen
augustomen / xmltosrt.py
Created February 29, 2016 02:43
Converts a Youtube closed captions xml to srt
#!/usr/bin/python
import re
import sys
from datetime import datetime, timedelta
pattern = re.compile(r'<text start="([\d\.]+)" dur="([\d\.]+)">(.*?)</text>')
with open(sys.argv[1]) as fin:
content = fin.read().decode('utf-8')
dini = datetime(2010, 1, 1)
#! /usr/bin/env python
import sys
def main():
sys.stdout.write("SET sql_mode='NO_BACKSLASH_ESCAPES';\n")
lines = sys.stdin.read().splitlines()
for line in lines:
sys.stdout.write(process_line(line))
sys.stdout.write('\n')