Skip to content

Instantly share code, notes, and snippets.

View arthuralvim's full-sized avatar

Arthur Alvim arthuralvim

View GitHub Profile
@arthuralvim
arthuralvim / group_tags.py
Created August 21, 2014 04:33
A template tag to check if an user belongs to a specific group.
from django import template
from django.contrib.auth.models import Group
register = template.Library()
@register.filter(name='has_group')
def has_group(user, group_name):
group = Group.objects.get(name=group_name)
return True if group in user.groups.all() else False
@arthuralvim
arthuralvim / email_send_check.py
Created August 28, 2014 09:24
Fast check if your SMTP settings are ok.
# Enter python manage.py shell
from django.core.mail import EmailMessage
email = EmailMessage('Subject', 'Message', to=['example@example.com'])
email.send()
# It should return 1 if everything is ok.
@arthuralvim
arthuralvim / serializers.py
Created September 7, 2014 22:10
[Django Rest Framework] Serializer for a PrimaryKeyRelatedField where we use a unique key instead of the primary key.
class ModelA(models.Model):
attr1 = models.Charfield()
attr2 = models.Charfield(unique=True)
class ModelB(models.Model):
foreign = models.ForeignKey(ModelA)
class OtherFieldUniqueSerializer(serializers.PrimaryKeyRelatedField):
@arthuralvim
arthuralvim / permissions.py
Created September 10, 2014 14:27
Django Rest Framework - Only ajax requests are permitted.
from rest_framework.permissions import BasePermission
class IsAjaxPermission(BasePermission):
"""
Check is request is ajax.
"""
def has_object_permission(self, request, view, obj):
return request.is_ajax()
@arthuralvim
arthuralvim / kill_spiders.sh
Created September 24, 2014 17:28
Scrapy - Kill all spiders
ps aux | grep "scrapy crawl" | grep -v grep | awk "{ print \$2 }" | xargs kill -9
@arthuralvim
arthuralvim / pre-commit
Created February 4, 2015 02:18
Git pre-commit hook to check if there's any debugger breakpoint inside your python files.
#!/bin/bash
pdb_check=$(git grep pdb -- '*.py')
if [ ${#pdb_check} -gt 0 ]
then
echo "COMMIT REJECTED: commit contains code with break points. Please remove before commiting."
echo $pdb_check
exit 1
else
echo "Code contains no break points"
@arthuralvim
arthuralvim / enc_example.py
Last active August 29, 2015 14:15
Encryption example.
# -*- coding: utf-8 -*-
from Crypto.Cipher import XOR
import base64
import functools
import hashlib
def func_encrypt(key, plaintext):
cipher = XOR.new(key)
@arthuralvim
arthuralvim / testing_postgres_conn.py
Created February 24, 2015 17:21
Testing Postgres Connection
#!/usr/bin/python
import psycopg2
import sys
host = ''
dbname = ''
user = ''
password = ''
port = 5432
@arthuralvim
arthuralvim / s3cmd_sync.sh
Last active September 1, 2015 20:44 — forked from niraj-shah/s3cmd_sync.sh
Downloading files from S3.
# Installation
wget -O- -q http://s3tools.org/repo/deb-all/stable/s3tools.key | sudo apt-key add -
sudo wget -O/etc/apt/sources.list.d/s3tools.list http://s3tools.org/repo/deb-all/stable/s3tools.list
sudo apt-get update && sudo apt-get install s3cmd
# Configuration
s3cmd --configure
@arthuralvim
arthuralvim / goc-2010.py
Created March 14, 2015 14:45
Google Developer Day 2010: Na pacata vila campestre de Ponteironuloville, todos os telefones têm 6 dígitos. A companhia telefônica estabelece as seguintes regras sobre os números: Não pode haver dois dígitos consecutivos idênticos, porque isso é chato; A soma dos dígitos tem que ser par, porque isso é legal; O último dígito não pode ser igual ao…
# -*- coding: utf-8 -*-
from itertools import tee
class ListaTelefones(object):
def __init__(self, lista_de_telefones):
self.telefones = lista_de_telefones.split()
self.telefones_validos = []