Skip to content

Instantly share code, notes, and snippets.

View klebercode's full-sized avatar
🎯
Focusing

Kleber Soares klebercode

🎯
Focusing
View GitHub Profile
@mrts
mrts / raw_id_fields_widget_in_custom_ModelForm.py
Created January 29, 2010 22:34
Use raw_id_fields widget in custom ModelForm
from django.contrib.admin import widgets
from django import forms
from django.contrib import admin
# create wrappers for overriding the queryset
class ToWrapper(object):
def __init__(self, to, manager):
self.to = to
self._default_manager = manager
@ChrisLTD
ChrisLTD / gist:957014
Created May 5, 2011 13:28
Django Nested Regroup Example (Group by category foreign key, then month of start date)
#views.py
def events_index(request, year):
selected_year = Year.objects.get(title=year)
events_list = Event.objects.filter(year = selected_year.id).order_by('category','start_date')
return render_to_response('events_list.html', {"events_list": events_list})
#events_list.html
{% regroup events_list by category.title as events_list_by_category %}
@danielfaust
danielfaust / samsung_remote.py
Created May 30, 2011 04:12
Samsung TV Remote Control Python Script
import time
import socket
import base64
src = '192.168.1.2' # ip of remote
mac = '00-AB-11-11-11-11' # mac of remote
remote = 'python remote' # remote name
dst = '192.168.1.3' # ip of tv
app = 'python' # iphone..iapp.samsung
@armonge
armonge / fields.py
Created July 19, 2011 17:32
Django fields to validate file content type
class ContentTypeRestrictedFileField(models.FileField):
"""
Tomado de http://nemesisdesign.net/blog/coding/django-filefield-content-type-size-validation/
Same as FileField, but you can specify:
* content_types - list containing allowed content_types. Example: ['application/pdf', 'image/jpeg']
* max_upload_size - a number indicating the maximum file size allowed for upload.
2.5MB - 2621440
5MB - 5242880
10MB - 10485760
20MB - 20971520
@bragil
bragil / geolocalizacao.py
Created August 25, 2011 18:16
Dado o endereço, obter localização, latitude e longitude
def dados_do_local(endereco):
"""
Dado o endereco, retorna o endereco processado, a latitude e a longitude do local.
Exemplo:
place, (lat, lng) = dados_do_local(endereco)
"""
from geopy import geocoders
if hasattr(settings, "EASY_MAPS_GOOGLE_KEY") and settings.EASY_MAPS_GOOGLE_KEY:
g = geocoders.Google(settings.EASY_MAPS_GOOGLE_KEY)
else:
@chrillo
chrillo / star.js
Created November 23, 2011 14:07
Draw a star in html canvas
/*
takes the x,y coordinates, the number of spikes, the inner and the outer radius of the spikes
*/
function drawStar(ctx,cx,cy,spikes,r0,r1){
var rot=Math.PI/2*3,x=cx,y=cy,step=Math.PI/spikes
ctx.strokeSyle="#000";
ctx.beginPath();
@robgolding
robgolding / login_required.py
Created July 11, 2012 19:25
Django Class-Based View Mixins: Part 1
class LoginRequiredMixin(object):
"""
View mixin which requires that the user is authenticated.
"""
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(LoginRequiredMixin, self).dispatch(
self, request, *args, **kwargs)
@rtwomey
rtwomey / Heroku DB Migration.mdown
Created July 23, 2012 18:47
How to migrate a DB on heroku to another plan

Recently, I had a staging database on Heroku that was running on the Ronin database (which was originally the lowest-sized DB you could get at Heroku). Since they added two new options, Crane and Kappa, we wanted to take advantage of the cost savings. Here's how you can migrate your Ronin DB to Crane (or any other plan).

The old database was named BROWN while the new one is CRIMSON. You can determine this by running:

heroku pg:info --app myapp-staging
  1. Add Crane database

     heroku addons:add heroku-postgresql:crane --app myapp-staging
    

heroku pg:wait --app myapp-staging

@derek-schaefer
derek-schaefer / replace.py
Created October 4, 2012 17:05
Django template tag for replacing substrings
from django.template import Library, Node, Variable, \
VariableDoesNotExist, TemplateSyntaxError
register = Library()
def get_var(v, context):
try:
return v.resolve(context)
except VariableDoesNotExist:
return v.var
@rturowicz
rturowicz / page.py
Last active December 19, 2018 11:50
django - intermediate admin page
# 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']