Skip to content

Instantly share code, notes, and snippets.

View hersonls's full-sized avatar

Herson Leite hersonls

View GitHub Profile
@hersonls
hersonls / mediagenerator_middleware.py
Created February 13, 2013 14:44
Middleware for django-mediagenerator to fix cache with development mode
from mediagenerator.settings import DEV_MEDIA_URL, MEDIA_DEV_MODE
# Only load other dependencies if they're needed
if MEDIA_DEV_MODE:
from mediagenerator.utils import _refresh_dev_names, _backend_mapping
from django.http import HttpResponse, Http404
from django.utils.cache import patch_cache_control
from django.utils.http import http_date
import time
@hersonls
hersonls / placeholder_forms.py
Last active December 11, 2015 20:58
Django: How to add placeholder in Django fields
from django import forms
class PlaceholderForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PlaceholderForm, self).__init__(*args, **kwargs)
for field in self.fields.values():
new_attrs = {
'placeholder': field.label
import os
import sys
INTERP = os.path.join(os.environ['HOME'], '.virtualenvs', 'project', 'bin', 'python')
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
sys.path.insert(1, os.getcwd()) # '[...]./'
sys.path.insert(1, os.path.join(os.getcwd(), 'project')) # '[...]/project/'
@hersonls
hersonls / generate_secret_key.sh
Last active June 2, 2019 22:00
Django SECRET_KEY command line generator
python -c 'from django.utils.crypto import get_random_string; print "\nSECRET_KEY = \"%s\"\n" % get_random_string(50, "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)")'
@hersonls
hersonls / gist:3933060
Created October 22, 2012 18:08
Personal Sublime Text 2 Packages
# Sublime Features
SublimeLinter
ColorPicker
Package Control
# Html
HTMLAttributes
HtmlTidy
# JavaScript
@hersonls
hersonls / signals_example.py
Created September 30, 2012 17:42
Example: Using signals to send email
# I have not tested, only didactic.
#
# signals.py
#
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
def send_notification_mail(sender, instance, raw, using):
context = {}
@hersonls
hersonls / middleware.py
Created September 11, 2012 02:35
Middleware for django-subdomains
class SubdomainURLRoutingMiddleware(SubdomainMiddleware):
def process_request(self, request):
super(SubdomainURLRoutingMiddleware, self).process_request(request)
subdomain = getattr(request, 'subdomain', False)
if subdomain is not False and subdomain not in settings.SUBDOMAIN_BLACKLIST:
# Here is the trick. I will put at the first rule the users url
# set at the settings, this way, the first page will be the
# user page matched with subdomain.
@hersonls
hersonls / gist:3695466
Created September 11, 2012 02:22
Nginx and Apache configuration for sub-domains
# Nginx
server {
# [...]
server_name *.yourdomain.com;
# [...]
}
@hersonls
hersonls / urls.py
Created August 27, 2012 03:01
Django Urls
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
@hersonls
hersonls / settings.py
Last active March 15, 2022 16:11
Django Settings
import os
# Project information
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
PROJECT_NAME = os.path.basename(PROJECT_PATH)
# Helpers
path = lambda *p: os.path.join(PROJECT_PATH, *p)
# Debug