Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View aaronlelevier's full-sized avatar

Aaron Lelevier aaronlelevier

View GitHub Profile
@aaronlelevier
aaronlelevier / settings_apps.py
Created March 26, 2014 13:37
A simple way in Django's settings.py to separate out default, 3rd party, and local Apps
# A simple way to break out Apps within the Django settings.py
# file in a logical way. Third party and Local Apps will vary
# based on the project.
DEFAULT_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
@aaronlelevier
aaronlelevier / views.py
Created July 13, 2014 19:19
Handle 404 and 500 errors easy with this snippet in your main Django views.py file. No urls.py configuration needed.
from django.shortcuts import render_to_response
from django.template import RequestContext
def handler404(request):
response = render_to_response('404.html', {},
context_instance=RequestContext(request))
response.status_code = 404
return response
@aaronlelevier
aaronlelevier / access.py
Created July 16, 2014 00:35
django-braces AnonymousRequiredMixin snippet b/c normal import from braces was failing for AnonymousRequiredMixin
import six
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.views import redirect_to_login
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.http import HttpResponseRedirect
from django.utils.encoding import force_text
@aaronlelevier
aaronlelevier / metaclass_password.py
Created July 22, 2014 16:48
metaclass example of looping through classes of password validation in python
class PluginMount(type):
"""
A Meta Class plugin mount location for a list of plugin classes.
By creating this, on all subclasses of PluginMount, python will
instantiate a plugin attribute list and maintain it for the Class
using this Meta Class.
"""
def __init__(cls, name, bases, attrs):
if not hasattr(cls, 'plugins'):
cls.plugins = []
@aaronlelevier
aaronlelevier / length_validator.py
Created July 22, 2014 16:56
zip code length custom django validator
from django.db import models
from django.core.exceptions import ValidationError
def validate_length(value):
if len(value) != 5:
raise ValidationError('%s is not five digits.' % value)
class MyModel(models.Model):
@aaronlelevier
aaronlelevier / mysql_answer.text
Created August 13, 2014 14:08
MySQL Django settings and config
I currently use Postgres and a Mac for my Django Dev environment. However, when I first started I was using a Windows machine with MySQL. When I looked through my notes just now, it was pretty easy to set up. (I didn't use xampp however). What I installed was:
1. MySQL workbench: http://dev.mysql.com/downloads/workbench/
2. MySQL-python connector - I used pip install for this. So if you have "pip" then it's just:
pip install mysql-python
3. And, this is what my database settings looked like in my settings.py:
DATABASES = {
# ---------------------------- None View Helper Functions ------------------------------------
# Place to save image upload file
def handle_uploaded_file(f):
with open(content_file_name(f), 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
# Attempt Imagefield
def content_file_name(instance, filename):
@aaronlelevier
aaronlelevier / las_vegas_weather.py
Last active August 29, 2015 14:05
Get the current weather for Las Vegas using Yahoo!'s Weather API
import requests
import xmltodict
def get_weather(url="http://weather.yahooapis.com/forecastrss?w=12795483&u=f"):
"""
Get the current weather for Las Vegas using Yahoo!'s Weather API
"""
try:
r = requests.get(url)
# parse bytes into a text string
@aaronlelevier
aaronlelevier / django_celery_howto.txt
Created December 23, 2014 15:07
Basic setup and install need to get Celery running in Django with Redis
#pip
pip install redis
pip install celery[redis]
pip install django-celery
# redis server
# download here
http://download.redis.io/redis-stable.tar.gz
# follow install instructions here
http://redis.io/topics/quickstart
@aaronlelevier
aaronlelevier / split_list.py
Created March 30, 2015 22:16
Split a list using python and reformat it in a single column fomat
'''
To convert zipcodes
-------------------
From the output on: http://www.freemaptools.com/find-zip-codes-inside-radius.htm
to a list of strings to use as a filter in SQL.
Change data initially in this format:
1111,2222,3333
To this format: