Skip to content

Instantly share code, notes, and snippets.

@melizeche
melizeche / prepare-commit-msg
Created July 27, 2022 21:18
Better commit messages
# BRANCH_PREFIX will match everithing before the first underscore so if your branch name
# is ESGCA-9999_my_awesomefeature and your commit message is 'Fixes Changes Rabbits'
# your "final" commit message will be: 'ESGCA-9999: Fixes Changes Rabbits'
COMMIT_MSG_FILE=$1
BRANCH_PREFIX=$(git branch | grep '*' | sed 's/* //' | cut -d _ -f 1)
echo "$BRANCH_PREFIX: $(cat $COMMIT_MSG_FILE)" > "$COMMIT_MSG_FILE"
# Copy this file in the .git/hooks/ directory of your local repo and make it executable `chmod +x prepare-commit-msg`
@melizeche
melizeche / django-filter-sample.py
Created April 20, 2020 00:12 — forked from dkarchmer/django-filter-sample.py
How to use django-filter to add a DRF filter using dates and slugs
class SampleFilter(filters.FilterSet):
start_date = django_filters.DateFilter(name="date", lookup_type='gte')
end_date = django_filters.DateFilter(name="date", lookup_type='lte')
# How to filter by a foreign key that uses slug as a lookup
foo = django_filters.ModelMultipleChoiceFilter(
queryset=MyModel.objects.all(),
to_field_name='slug',
conjoined=True,
)
class Meta:
@melizeche
melizeche / remove_duplicates.py
Last active April 19, 2020 12:55 — forked from victorono/remove_duplicates.py
Django - remove duplicate objects where there is more than one field to compare
from django.db.models import Count, Max
from core.models import HelpRequest
unique_fields = ['phone', 'title']
actives = HelpRequest.objects.filter(active=True)
duplicates = (
actives.values(*unique_fields)
.order_by()
@melizeche
melizeche / cotizacion.py
Last active October 6, 2019 03:06
ejemplo del campus party
# Primero hay que instalar las librerias necesarias
# pip install requests beautifulsoup4
import requests
from bs4 import BeautifulSoup
# Descargamos la pagina del BCP
bcp = requests.get('https://www.bcp.gov.py/webapps/web/cotizacion/monedas',
headers={'user-agent': 'Mozilla/5.0'}, verify=False)
#buscamos todas las etiquetas <td>, elegmos la cuarta, elegimos el texto, reemplazamos el . separador de miles y la coma decimal
cotizacion = float(BeautifulSoup(bcp.text).findAll("td")[3].get_text().replace(".","").replace(",","."))
@melizeche
melizeche / models.py
Last active May 9, 2020 22:31
Django models con relaciones entre tablas
from django.db import models
# Un Autor puede tener varios libros
# Un libro puede tener un autor
# Un Libro puede tener varios Generos
# Un Genero puede pertenecer a varios libros
# null=True, blank=True hace que el campo sea opcional
class Autor(models.Model):
nombre = models.CharField(max_length=120)
@melizeche
melizeche / post-merge
Created April 2, 2019 05:15 — forked from nnja/post-merge
Git Hook: Example post-merge hook that checks for updates to requirements.txt
#!/usr/bin/env python
import sys
import subprocess
diff_requirements = 'git diff ORIG_HEAD HEAD --exit-code -- requirements.txt'
exit_code = subprocess.call(diff_requirements.split())
if exit_code == 1:
print 'The requirements file has changed! Remember to install new dependencies.'
else:
@melizeche
melizeche / upgrade_DO_django_to_python3.sh
Created December 17, 2018 22:46
Upgrade DigitalOcean's "One click apps" Django installation to Python3
apt install -y python3 python3-pip
pip3 install django==1.11.11 netifaces psycopg2-binary gunicorn==19.7.1 gevent
sed -i 's/python/python3.6/g' /usr/bin/gunicorn
sed -i 's/python/python3.6/g' /home/django/django_project/manage.py
/home/django/django_project/manage.py migrate
systemctl restart gunicorn.service
@melizeche
melizeche / get_licenses.py
Created February 9, 2018 21:28
Get licenses of all packages in use
import pkg_resources
import prettytable
def get_pkg_license(pkg):
try:
lines = pkg.get_metadata_lines('METADATA')
except:
lines = pkg.get_metadata_lines('PKG-INFO')
for line in lines:
@melizeche
melizeche / install_pgAdmin4.sh
Last active June 1, 2020 17:45
Easy pgAdmin4 2.0 Installer with python3 for Ubuntu
#!/bin/bash
SP_PATH=$(python3 -m site --user-site)
URL="https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v2.0/pip/pgadmin4-2.0-py2.py3-none-any.whl"
ICON="https://www.postgresql.org/message-id/attachment/1139/pgAdmin.svg"
FILE=$(echo $URL | sed 's/.*\///')
#Install pip3
echo "Installing python3-pip if not installed..."
sudo apt install -y python3-pip
#Get pgAdmin4 Python Wheel
echo "Downloading pgAdmin4..."
@melizeche
melizeche / git_backs.sh
Created November 30, 2017 21:31
Backup/clone several git repositories with all the branches, cronjob prepared
#!/bin/bash
BACKUP_DIR=~/backups/git_backups/
REPOS="git@github.com:melizeche/listaHu.git git@github.com:melizeche/dolarPy.git"
for repo in $REPOS;do
repo_dir=$(echo $repo | sed 's/.*\///')
repo_name=$(echo $repo_dir | sed 's/\.[^.]*$//')
FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y-\%m-\%d`/"
FINAL_REPO_DIR=$FINAL_BACKUP_DIR$repo_name
echo "Backing up "$repo_name in $FINAL_REPO_DIR