Skip to content

Instantly share code, notes, and snippets.

# Roles Dictionary Values Example (1,2,4,8,16,32...) Up to 32 values
roles = {"addUsers": 1, "updatePermissions": 2, "uploadAttachments": 4, "createPosts": 8}
# Check if permission 9 has updatePermissions
# bitand will return 0 if False and a number if True
>>> 9&roles['updatePermissions']
0
# Check if permission 9 has addUsers
@johngag
johngag / gist:5341955
Created April 9, 2013 00:44
Example BitWise Dictionary
roles = {"addUsers": 1, "updatePermissions": 2, "uploadAttachments": 4, "createPosts": 8}
@johngag
johngag / gist:4209311
Created December 4, 2012 22:04
jQuery starts-with and ends-with
$.extend($.expr[":"], {
"starts-with": function(elem, i, data, set) {
var text = $.trim($(elem).text()),
term = data[3];
return text.indexOf(term) === 0;
},
"ends-with": function(elem, i, data, set) {
var text = $.trim($(elem).text()),
term = data[3];
return text.lastIndexOf(term) === text.length - term.length;
@johngag
johngag / gist:4192058
Created December 3, 2012 01:32
Python/Django Example requirements file
beautifulsoup4==4.1.3
Django==1.4.2
Fabric==1.4.3
argparse==1.2.1
django-appconf==0.5
django-compressor==1.2
psycopg2==2.4.5
pycrypto==2.6
ssh==1.8.0
wsgiref==0.1.2
@johngag
johngag / gist:4192053
Created December 3, 2012 01:30
Fabric Python/Django requirements
def update_requirements():
requirements = os.path.join(env.home, 'requirements')
with prefix('source %s/bin/activate' % env.python_path):
with cd(requirements):
cmd=['pip install']
cmd += ['--requirement %s' % os.path.join(requirements, 'apps.txt')]
print(cmd)
run(' '.join(cmd))
@johngag
johngag / gist:4184578
Created December 1, 2012 20:00
Django - Access ManyToMany objects Models
class Category(models.Model):
name = models.CharField(max_length=200)
def __unicode__(self):
return self.name
class Post(models.Model):
title = models.TextField(max_length=200)
content = HTMLField()
excerpt = HTMLField()
above_fold = models.BooleanField(default=False)
@johngag
johngag / gist:4184550
Created December 1, 2012 19:54
Django - Access ManyToMany objects in view Template
{% for post in latest_blog_posts %}
<div class="row">
<div>
<h2 class="post-title"><a href="/blog/post/{{ post.id }}/">{{ post.title }}</a></h2>
<h5 class="post-date">{{ post.pub_date }} - {% for cat in post.category.all %}{{ cat.name }}{% if not forloop.last %},{% endif %}{% endfor %}</h5>
<p class="lead">{% autoescape off %}{{ post.content|safe }}{% endautoescape %}</p>
</div>
</div>
<hr>
{% endfor %}
class HomeController < ApplicationController
def index @posts = RSS::Parser.parse(open('http://johngag.tumblr.com/rss').read, false).items
end
end
<%= link_to post.title, post.link, :target => "_parent" %>
<%= render :partial => "home/post", :collection => @posts.first(5) %>
@johngag
johngag / gist:4033967
Created November 7, 2012 19:52
Javascript continents and countries associated
var world = {"Africa":["Algeria","Angola","Benin","Botswana","Burkina","Burundi","Cameroon","Cape Verde","Central African Republic","Chad","Comoros","Congo","Djibouti","Egypt","Equatorial Guinea","Eritrea","Ethiopia","Gabon","Gambia","Ghana","Guinea","Guinea-Bissau","Ivory Coast","Kenya","Lesotho","Liberia","Libya","Madagascar","Malawi","Mali","Mauritania","Mauritius","Morocco","Mozambique","Namibia","Niger","Nigeria","Rwanda","Sao Tome and Principe","Senegal","Seychelles","Sierra Leone","Somalia","South Africa","South Sudan","Sudan","Swaziland","Tanzania","Togo","Tunisia","Uganda","Zambia","Zimbabwe"],
"Asia":["Afghanistan","Bahrain","Bangladesh","Bhutan","Brunei","Burma","Cambodia","China","East Timor","India","Indonesia","Iran","Iraq","Israel","Japan","Jordan","Kazakhstan","North Korea","South Korea","Kuwait","Kyrgyzstan","Laos","Lebanon","Malaysia","Maldives","Mongolia","Nepal","Oman","Pakistan","Philippines","Qatar","Russian Federation","Saudi Arabia","Singapore","Sri Lanka","Syria","Tajikistan","Thailan
@johngag
johngag / gist:3907775
Created October 17, 2012 20:00
tpl loader
tpl = {
templates: {},
loadTemplates: function (names, callback) {
var that = this;
var loadTemplate = function (index) {
var name = names[index];