Skip to content

Instantly share code, notes, and snippets.

View particledecay's full-sized avatar
💣
0 17 * * 5 /usr/local/bin/deploy_to_prod.sh

Joey Espinosa particledecay

💣
0 17 * * 5 /usr/local/bin/deploy_to_prod.sh
View GitHub Profile
@particledecay
particledecay / mkvenv
Last active April 8, 2016 12:01
Create a new virtualenv with git cloning and package requirements
#!/bin/bash
# define a help printout
function help {
echo "usage: `basename $0` [-h] [--project-dir dir] [--requirements reqfile] [--url git-url]"
echo " [--virtualenv-dir venv-dir] <project-name>"
echo ""
echo "Create a new virtualenv with git cloning and package requirements."
echo ""
echo "positional arguments:"
@particledecay
particledecay / views.py
Created February 25, 2013 17:29
Simple direct download view using x-sendfile.
def direct_download(request):
"""
Returns a file path via X-Sendfile that the webserver will serve as a regular file (vs a rendered page).
Note: The webserver needs to support this header (i.e., Apache + mod_xsendfile)... Django dev server doesn't.
"""
if request.method == 'GET':
song_slug = request.GET.get('slug')
if song_slug:
song = get_object_or_404(Song, slug=song_slug)
@particledecay
particledecay / models.py
Last active March 1, 2016 21:51
[Django] New Django 1.5 feature for completely custom user models (done by subclassing AbstractBaseUser). Coded by adhering to Django documentation recommendations. Uses django-model-utils 3rd party app, as well as multiple inheritance and string translation. Email is new primary username field.
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin, check_password
from model_utils import Choices
from model_utils.models import TimeStampedModel
class DJManager(BaseUserManager):
def create_user(self, email, first_name, last_name, dj_name, user_type, subscription, password=None):
@particledecay
particledecay / login.html
Created February 14, 2013 20:32
[Django] [HTML] Basic login form (for a custom user model, hence the 'type="email"' validation input), updated for Django 1.5.
{% extends 'base.html' %}
{% block content %}
<div id="login-container" class="container-fluid">
<form action="{% url "users_login" %}" id="login-form" method="post" class="ka-form">
{% csrf_token %}
<p>
{{ form.username.label }}
<input type="email" id="{{ form.username.auto_id }}" name="username">
</p>
@particledecay
particledecay / views.py
Created February 14, 2013 20:18
[Django] Basic ad-hoc search for professionals using Q objects.
def search_publication_professionals(request, firm_slug, template_name='firms/admin/search-publication-professionals.html'):
keywords = request.GET.get('keywords', None)
professionals = []
if keywords:
keywords_q = Q()
for keyword in keywords.split():
keywords_q.add((Q(firm__name__icontains=keyword)), keywords_q.OR)
keywords_q.add((Q(user__first_name__icontains=keyword)), keywords_q.OR)
@particledecay
particledecay / check_reminders.py
Last active March 1, 2016 21:53
[Django] Custom management command to check for all events starting in an arbitrary number of days, and send event reminder emails for them (run from a cron job as follows for 10-day, 3-day, and 1-day checks: 0 2 * * * /virtualenvs/foo/bin/python /projects/foo/foo/manage.py check_reminders 10 3 1)
from datetime import datetime, timedelta
from django.core.management.base import BaseCommand, CommandError
from foo.firms.models import Event
class Command(BaseCommand):
args = '<days days ...>'
help = "Check for any Events that start in a given number of days, and execute send_reminder() on them"
def handle(self, *args, **kwargs):
@particledecay
particledecay / models.py
Created February 14, 2013 19:54
[Django] Simple task class and functions for adding/removing three types of tasks (tasks are for notifying the administrator that there is something to be done, and should be tied to an event and optionally an event registration object).
class Task(models.Model):
REGISTRATION = 0
ATTENDANCE = 1
CLE = 2
TASK_TYPES = (
(REGISTRATION, 'Registration Request'),
(ATTENDANCE, 'Attendance Request'),
(CLE, 'CLE Request'),
@particledecay
particledecay / getval.py
Last active December 13, 2015 18:29
[Django] Custom template filter to retrieve human-readable value from choices tuple or value from dict
from django import template
register = template.Library()
# Utility used like {{ dict|getval:key_name }} to return value even when using variables in dictionary key
@register.filter(name='getval')
def getval(tupdict, key_name):
"""Use this in a template as follows:
To retrieve the value of a dict:
@particledecay
particledecay / create_psql_db.sh
Last active October 23, 2017 20:09
Create PostgreSQL database (with optional PostGIS support) on Ubuntu (assuming you have PostgreSQL 9.1+ installed), typically for use with Django.
#!/bin/bash
DATABASE_NAME=#####
DATABASE_OWNER=#####
USE_POSTGIS=N
if [ ! `which psql` ]; then
echo 'You must have PostgreSQL installed before running this script.'
exit 1
fi