Skip to content

Instantly share code, notes, and snippets.

View berinhard's full-sized avatar
👾
let's play that

berin berinhard

👾
let's play that
View GitHub Profile
@berinhard
berinhard / django_tests.py
Created February 20, 2013 21:51
A proof of concept taking just the code organization into account.
#coding:utf-8
from django.test import Client
from django.conf import settings
from django.db.models.query import QuerySet
from django.contrib.auth.models import User
from django.test import TestCase as DjangoTestCase
from django.core.urlresolvers import reverse, NoReverseMatch
class StatusCodeAssertsMixin(object):
"""
@berinhard
berinhard / list_field.py
Last active December 16, 2015 04:08
SeparatedValuesField for Django
class SeparatedValuesField(models.TextField):
"""
Usage:
class Foo(models.Model):
bar = SeparatedValuesField()
_list = [1, '2', date.today()]
obj = Foo.objects.create(bar=_list)
assert obj.bar == _list
"""
@berinhard
berinhard / converter.sh
Last active December 18, 2015 05:19
Bash lines to update Django's url templatetag for 1.5 version required format.
for x in $(ack-grep '"{% url' -l); do sed -i "s/\"{% url \([^ ]\+\)/\"{% url '\1'/g" $x; done;
for x in $(ack-grep "'{% url" -l); do sed -i "s/'{% url \([^ ]\+\)/'{% url \"\1\"/g" $x; done;
for x in $(ack-grep "^{% url" -l); do sed -i "s/'^{% url \([^ ]\+\)/'{% url \"\1\"/g" $x; done;

Upgrade Django from 1.1 to 1.5.1. This highlights some of the issues that one may encounter when updating Django. It's not possible to cover every case, but this should provide a good general starting point.

Change to DATABASES variable in settings.py.

Django now supports multiple databases and changes are needed to how the database connections are defined.

  • Changed in Django 1.2
  • Change Required by Django 1.4
  • Source:
class Class1():
pass
class Class2():
pass
class Class3(object):
pass
a = Class1()
@berinhard
berinhard / pyvideos.py
Last active January 20, 2016 13:51
Pyvideos.org Safe Box
# coding:utf-8
import requests
from collections import Iterator
def api_data_fetcher(url):
response = requests.get(url)
if not response.ok:
raise Exception(u'Request error: {}'.format(response.content))
return response.json()
@berinhard
berinhard / pop_up_admin_file_widget.py
Created June 14, 2016 16:40
This gist show a solution that I've figured out to open all documents links on model object detail page of Django's admin on a new tab.
class NewTabAdminFileWidget(AdminFileWidget):
"""
This widget is an extensions from AdminFileWidget which is used with all
ImageFields and FileFields at admin forms. This widget follows the api defined
by django.forms.widgets.ClearableFileInput widget.
"""
def __init__(self, *args, **kwargs):
super(NewTabAdminFileWidget, self).__init__(*args, **kwargs)
template_with_initial_new_tab = (
@berinhard
berinhard / free_reader.py
Last active February 28, 2018 17:32
Um leitor gratuito para jornais
import requests
import sys
import textwrap
from lxml.html import document_fromstring
from optparse import OptionParser
from urllib.parse import urlparse
class FolhaSpParser(object):
@berinhard
berinhard / molhao_do_berin.md
Last active July 29, 2017 23:59
Molhão do Berin

Open source food: Molhão do Berin

Esta receita é para fazer um molho de tomate natural. O rendimento é de 8 a 10 potes de vidro.

Ingredientes

  • 15 kg de tomate italiano
  • 6 cebolas (aproximadamente 600g)
  • 2 cabeças de alho
  • Orégano
@berinhard
berinhard / videotogif.sh
Last active September 14, 2018 20:36
Video to Gif with ffmpeg
#!/bin/sh
#### to create the video:
# ffmpeg -r 50 -i img%03d.jpg -c:v libx264 -vf fps=24 -pix_fmt yuv420p -vf scale=800:-1 out.mp4
palette="/tmp/palette.png"
filters="fps=15,scale=800:-1:flags=lanczos"
ffmpeg -v warning -i $1 -vf "$filters,palettegen" -y $palette