Skip to content

Instantly share code, notes, and snippets.

@rvause
rvause / serialize_mixin.py
Last active December 16, 2015 20:49
A generic way to serialize model instances into something you can just json.dumps
from django.db.models.fields import FieldDoesNotExist
class SerializeMixin(object):
def serialize(self, recursive=True):
data = {}
for field in self.__class__._meta.get_all_field_names():
try:
self.__class__._meta.get_field(field)
data[field] = self.serializable_value(field)
@rvause
rvause / profile_completion.py
Last active December 18, 2015 03:59
Wanted a simple way to measure the completion of a profile (that is stored in a UserProfile model related to auth.User) in a Django app
...
def _get_profile_completion(self):
fields = filter(
lambda i: i not in ('user', 'id'),
self.__class__._meta.get_all_field_names()
)
per_field = float(100) / len(fields)
total = 0.0
for field in fields:
val = getattr(self, field)
@rvause
rvause / generic_curry.md
Last active September 7, 2016 16:28
A method to make a curry

A Generic Curry Recipe

It's kinda sour like a sambar and kind of hot like a chilli but here. It's not authentic Indian food at all but it works and it is delicious.

Ingredients

For the paste:

  • Cardamom
  • Fennel seeds
@rvause
rvause / Vagrantfile
Last active December 20, 2015 19:59
just a small Vagrantfile for myself to setup a decent Python environment if I am ever found stuck on Windows
# -*- mode: ruby -*-
# vi: set ft=ruby :
$script = <<SCRIPT
apt-get -y update
apt-get -y install htop vim git-core bzr mercurial sqlite3 build-essential python-dev libjpeg62 libjpeg62-dev zlib1g zlib1g-dev curl python-setuptools postgresql postgresql-client postgresql-server-dev-all libpq-dev
easy_install pip
pip install virtualenv
wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh
useradd -g users -d /home/USERNAME -m -s /bin/bash -p $(echo PASSWORD | openssl passwd -1 -stdin) USERNAME
outlook.com
1033edge.com
11mail.com
123.com
123box.net
123india.com
123mail.cl
123qwe.co.uk
150ml.com
15meg4free.com
EMAIL_BLACKLIST = (
'outlook.com',
'1033edge.com',
'11mail.com',
'123.com',
'123box.net',
'123india.com',
'123mail.cl',
'123qwe.co.uk',
'150ml.com',
@rvause
rvause / admin.py
Created November 20, 2013 12:13
Snippet to add an app's models to Django admin site with no further configuration.
from unipath import Path
from django.contrib import admin
from django.db.models.loading import get_app, get_models
path = Path(__file__)
admin.site.register(get_models(get_app(path.parent.name)))
getCookie = (name) ->
cookieValue = null
if document.cookie && document.cookie != ''
cookies = document.cookie.split ';'
for cookie in cookies
cookie = jQuery.trim cookie
if cookie.substring(0, name.length + 1) == "#{ name }="
@rvause
rvause / go_q.py
Created March 13, 2014 19:26
Compile a complex query in Django
from operator import or_
from django.db.models import Q
from .models import MyModel
query = reduce(or_, [Q(some_field__iexact=v, other_field__icontains=v) for v in values])
MyModel.objects.filter(query)
@rvause
rvause / 2kcxtreme.txt
Created April 26, 2014 18:28
Written on the box: "wspomaga procesy fizjologiczne po spozyciu alkoholu" - "supports the physiological processes after ingestion of alcohol"
polish - english - mg/1
opuncja - prickly pear - 200
kwas bursztynowy - succinic acid - 100
kwas fumarowy - fumaric acid - 37
glutaminian monosodowy - monosodium glutamate - 100
glukoza - glucose - 100
glicyna - glycine - 60
l-cysteina - L-cysteine - 60
zen-szen - ginseng - 80
witaminia c - vitamin C - 27