Skip to content

Instantly share code, notes, and snippets.

View toast38coza's full-sized avatar

Christo Crampton toast38coza

View GitHub Profile
@toast38coza
toast38coza / maxmind_location.js
Created February 24, 2011 15:22
Get your location from MaxMinds JavaScript library (requires JQuery)
$.getScript("http://j.maxmind.com/app/geoip.js",function(){
console.log(geoip_country_code());
console.log(geoip_country_name());
console.log(geoip_city());
console.log(geoip_region());
console.log(geoip_region_name());
console.log(geoip_latitude());
console.log(geoip_longitude());
console.log(geoip_postal_code());
});
@toast38coza
toast38coza / gist:865750
Created March 11, 2011 11:11
Add a user to mailchimp with django_mailchimp
"""
requirements:
pip install django_mailchimp
"""
import mailchimp
def add_to_list(self,request, list_id, email):
mailinglist = mailchimp.utils.get_connection().get_list_by_id(list_id)
return mailinglist.subscribe(email,{'EMAIL':email}) # True / False
@toast38coza
toast38coza / send_message_with_bulksms.py
Created April 6, 2011 16:46
Code sample from bulksms.com
//================================================ Python code sample ================================================//
import urllib
# If your firewall blocks access to port 5567, you can fall back to port 80:
# url = "http://bulksms.2way.co.za/eapi/submission/send_sms/2/2.0"
# (See FAQ for more details.)
url = "http://bulksms.2way.co.za:5567/eapi/submission/send_sms/2/2.0"
params = urllib.urlencode({'username' : 'myusername', 'password' : 'xxxxxxxx', 'message' : 'Testing Python', 'msisdn' : 271231231234})
f = urllib.urlopen(url, params)
@toast38coza
toast38coza / s3.py
Created April 6, 2011 19:41
Basic wrapper round boto to create an s3 key from a string
import boto
from boto.s3.key import Key
from django.conf import settings
#settings:
"""
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
DEFAULT_BUCKET
@toast38coza
toast38coza / post_save_content_type_id
Created April 11, 2011 23:14
Get the content_type_id for the changed item on save in the post_save signal
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.contrib.contenttypes.models import ContentType
@receiver(post_save)
def model_saved(sender, instance, signal, *args, **kwargs):
app_label = sender._meta.app_label
model_name = sender._meta.module_name
content_type = ContentType.objects.get(app_label=app_label, model=model_name)
@toast38coza
toast38coza / exclamation.py
Created May 18, 2011 10:25
Jiminy cricket! A fun exclamation filter for django
from django import template
from django.conf import settings
from random import choice
register = template.Library()
#usage: {{explamation}}! A fun exclamation filter for django
@register.filter(name="exclamation")
def exclamation(punctuation):
@toast38coza
toast38coza / instance_based_admin_permissions
Created June 14, 2011 05:23
Django Admin: Limiting access to objects in the admin on an instance level
from django.contrib import admin
from more_with_admin.examples import models
class DocumentAdmin(admin.ModelAdmin):
def queryset(self, request):
qs = super(DocumentAdmin, self).queryset(request)
# If super-user, show all comments
if request.user.is_superuser:
@toast38coza
toast38coza / jquery-dialog-css
Created July 4, 2011 08:30
Simple JQuery dialog over-ride CSS
/*
It makes sense to use the built-in jquery dialog. But (IMO), the styling on them looks like it was done back a systems administrator. Here are a few lines of css to make them look a little less clunky
*/
/**
Main colors:
dark: #545454
light: #ccc
*/
.ui-dialog {border: solid 4px #545454;border-radius:0px;padding:0px;-moz-box-shadow: 3px 3px 4px #545454;-webkit-box-shadow: 3px 3px 4px #545454;box-shadow: 3px 3px 4px #545454;
/* For IE 8 */
@toast38coza
toast38coza / sort_by_dict_key.py
Created October 18, 2011 05:35
Sort a list of dicts by a key value in the dict
# given a list such as:
# list = [{'title':title, 'date': x.created_on}, ...]
# you can sort on a dict key like such
list.sort(key=lambda item:item['date'], reverse=True)
#source: http://stackoverflow.com/questions/652291/sorting-a-list-of-dictionary-values-by-date-in-python
@toast38coza
toast38coza / recursive_file_find.py
Created June 21, 2012 11:22
Recursively find all files that end with a certain extension with subdirectories
# hat-tip: Sven Marnach
# http://stackoverflow.com/questions/5817209/browse-files-and-subfolders-in-python
htmlfiles = [os.path.join(root, name)
for root, dirs, files in os.walk(path)
for name in files
if name.endswith((".html", ".htm"))]