Skip to content

Instantly share code, notes, and snippets.

@marceloleiva
marceloleiva / .env-example
Created Apr 3, 2020 — forked from gjbagrowski/.env-example
Django-environ example
View .env-example
# project-repo/app/settings/.env-example
#
# DJANGO
#
SITE_HOST=localhost:8000
USE_SSL=False
ALLOWED_HOSTS=localhost,127.0.0.1
SECRET_KEY=asdasd
DEBUG=true # never on production, will cause settings including api keys to leak
DJANGO_LOG_LEVEL=DEBUG
@marceloleiva
marceloleiva / forms.py
Created Jul 7, 2019 — forked from eerien/forms.py
Comma Separated Values Form Field for Django. There are CommaSeparatedCharField, CommaSeparatedIntegerField.
View forms.py
from django import forms
from django.core import validators
from django.core.exceptions import ValidationError
class MinLengthValidator(validators.MinLengthValidator):
message = 'Ensure this value has at least %(limit_value)d elements (it has %(show_value)d).'
class MaxLengthValidator(validators.MaxLengthValidator):
message = 'Ensure this value has at most %(limit_value)d elements (it has %(show_value)d).'
@marceloleiva
marceloleiva / page.py
Created Jul 21, 2018 — forked from rturowicz/page.py
django - intermediate admin page
View page.py
# admin.py: admin action definition
def make_copy(self, request, queryset):
form = None
if 'apply' in request.POST:
form = CopyPageForm(request.POST)
if form.is_valid():
issue = form.cleaned_data['issue']
@marceloleiva
marceloleiva / django_model_graph.sh
Created Apr 3, 2018 — forked from perrygeo/django_model_graph.sh
Generate UML diagram of django app models
View django_model_graph.sh
apt-get install python-pygraphviz
pip install django-extensions
# add 'django_extensions' to INSTALLED_APPS in settings.py
python manage.py graph_models trees -o test.png
@marceloleiva
marceloleiva / letsencrypt_2017.md
Created Jun 6, 2017 — forked from cecilemuller/letsencrypt_2020.md
How to setup Let's Encrypt for Nginx on Ubuntu 16.04 (including IPv6, HTTP/2 and A+ SLL rating)
View letsencrypt_2017.md

How to setup Let's Encrypt for Nginx on Ubuntu 16.04 (including IPv6, HTTP/2 and A+ SLL rating)

There are two main modes to run the Let's Encrypt client (called Certbot):

  • Standalone: replaces the webserver to respond to ACME challenges
  • Webroot: needs your webserver to serve challenges from a known folder.

Webroot is better because it doesn't need to replace Nginx (to bind to port 80).

In the following, we're setting up mydomain.com. HTML is served from /var/www/mydomain, and challenges are served from /var/www/letsencrypt.

@marceloleiva
marceloleiva / views.py
Created Apr 27, 2017 — forked from MrYoda/views.py
Python 3, Django 1.9+: Excel file creation and send on-the-fly with XlsxWriter & BytesIO
View views.py
import xlsxwriter
from io import BytesIO
from django.http import StreamingHttpResponse
from django.views.generic import View
def get_foo_table_data():
"""
Some table data
"""
@marceloleiva
marceloleiva / Buscar texto con re.py
Created Oct 17, 2016
Buscar texto con re created by marceloleiva - https://repl.it/Dy6x/1
View Buscar texto con re.py
import re
texto = 'Supermercado (1,10,5)'
busqueda_coordenada = re.search(r'(.+)(\(.{5,}\))', texto)
print(busqueda_coordenada.groups())
if busqueda_coordenada:
if len(busqueda_coordenada.groups()) >= 2:
texto = busqueda_coordenada.group(1).strip()
coordenada = re.sub('[()]', '', busqueda_coordenada.group(2)).split(',')
View determinar_encoding.py
import chardet
def determinar_encoding(input_file='despachos.csv'):
file = open(input_file, 'rb').read()
encoding = chardet.detect(file)
if encoding:
return encoding['encoding']
return None
View django_reversion.md
pip install django-reversion

Add reversion to INSTALLED_APPS

INSTALLED_APPS = [
...
...
...
View jquery.validate.rut.js
function validaRut(campo){
if ( campo.length == 0 ){ return false; }
if ( campo.length < 8 ){ return false; }
campo = campo.replace('-','')
campo = campo.replace(/\./g,'')
var suma = 0;
var caracteres = "1234567890kK";
var contador = 0;