Skip to content

Instantly share code, notes, and snippets.

View eezis's full-sized avatar

Ernest Ezis eezis

  • Boulder, CO
View GitHub Profile
@eezis
eezis / Django EC2 Ubuntu
Last active August 12, 2019 15:58
Django on Amazon EC2 using Ubuntu
ALSO SEE THIS: http://eddychan.com/post/18484749431/minimum-viable-ops-deploying-your-first-django-app-to
AND THIS: http://bitnami.com/stack/django
These are my notes on how to quickly setup a python, virtualenv (use virtualenv-burrito FTW), and django.
Setup an EC-2 instance:
=======================
Use the quick launch wizard:
@eezis
eezis / django question 1
Created November 12, 2012 21:09
Django Question: best way to approach highly conditional page output
I have this framed out and working but I wanted to post this question to see how more experienced Django developers would approach this problem. So to use a simple domain
that everyone can understand, I will frame this as a simple reporting page on a web site where an investor wants to view different classes of assets (stocks, bonds, cds,
etc). I only show three instrument types but like any project it could grow to many more over the project life cycle.
Given the following models . . .
class Investor(models.Model):
first = models.CharField()
blah = models.CharField()
blan = models.CharField()
@eezis
eezis / sublime find blank lines
Created November 24, 2012 15:57
Sublime Text: find the beginning of blank lines (and optionally replace)
find: ^\n <- select the far left button that toggles regex [.*]
replace: <p>\n
tags: Sublime Text 2, find blank lines, replace blank lines
@eezis
eezis / clone a virtualenv
Created December 6, 2012 23:13
Copy Clone a virtualenv
##Clone or Copy a virtualenv##
The virtualenvwrapper documentation declares that its feature set includes "Wrappers for managing your virtual environments (create, delete, copy)." But I could not find the copy command anywhere. After fruitless for the command I figred that well designed software usually has a predictable answer:
$cpvirtualenv Env1 Env2
So that's the ticket. Where Env1 is your existing venv and Env2 is the new one you wish to start. So I riff off of Patrick Altman's handy [setup instructions] for new Django projects to create a local stub (where I have swapped in the dev release of Django 1.5) and add utilities like South. Starting new projects is now as simple as . . .
@eezis
eezis / fkonabstract
Last active December 16, 2015 19:09
No FK on abstract base class
This is a contrived example that illustrates my problem. I want to subclass a base class (PersonBase)
into several different descendant classes. Each person type can have multiple emails and phone numbers
The email and phone class should have a 1..n relationship to all the different people subclasses.
I thought could structure this as follows, but the FK cannot be created on the abstract base class. What
is the best practice for solving this situation . . . instantiate that base model instead of making it
abstract? Or try to implement GenericForeignKeys, or do they introduce more problems than they solve?
Here's the problem in pseudocode . . .
@eezis
eezis / Django EC2 Bad Request (400)
Last active June 28, 2023 16:23
Resolving Bad Request (400) when using Django on an EC2 instance DEBUG
This gist pertains to a Bad Request(400) error when trying to access an EC2 instance running django . . .
I set up an EC2 server, installed django, started runserver and tested. The result was
Bad Request (400)
When using Django 1.5 and higher, if you specify DEBUG=False in the settings.py file, you must also specify ALLOWED_HOSTS
or you will recieve a "Bad Request (400)" error message when attempting to retrieve a page.
On an Amazone EC2 instance, if you are ***testing*** with the public DNS, then you should include the public DNS as one of the allowed hosts.
@eezis
eezis / update postgres last value.py
Last active October 7, 2017 03:20
A code snippet to update the "last_value" of postgres's sequence generator when it is out of sync with the actual values in your tables.
"""
This utility addresses the InegrityError that occurs when you try to add a new record to
(probably a recently ported) postgres database while using Django, here's more . . .
duplicate key value violates unique constraint "<app>_<table>_pkey"
DETAIL: Key (id)=(2) already exists.
The problem here is that the Postgres sequence generators are out of sync with your data.
Here's a good overview: http://www.vlent.nl/weblog/2011/05/06/integrityerror-duplicate-key-value-violates-unique-constraint/
@eezis
eezis / redirectmiddleware.py
Created December 7, 2013 17:48
A Django Middleware Redirection class that provides a "last chance" to grab a developing 404 response and redirect it to a new URL.
"""
This piece of middleware grabs onto the tail end of process_response hook
and allows for redirects to be applied using regex matches.
Use Case: A legacy website with many old urls will have accumulated SEO mojo based
on external links. When the site is converted to a new fromat (say, drops the cruft like '.html')
or goes from abbreviated forms /art/id/t/234234 to /article/2014/january/technology/get-a-better-job
the old urls are at risk of breakage and could lose SEO mojo unless preserved
Best preservation comes from redirecting old content to new, which is easily accomplished
@eezis
eezis / apostrophe-diff-than-quot
Created March 20, 2014 15:10
Distinction between ' and " usage in Elixir?
What are the "rules" or guidelines for using ' vs " in Elixir?
iex> l1 =['test']
['test']
iex> l2 = ['test']
['test']
iex> l1 == l2
true
iex> l3 = ["test"]
["test"]
@eezis
eezis / plug streaming
Created March 23, 2014 19:09
Simple Streaming with Elixir Plug.
defmodule MyPlug do
import Plug.Connection
def init(options) do
# initialize options
options
end
def call(conn, _opts) do