Skip to content

Instantly share code, notes, and snippets.

from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
class GenericLinkedModel(models.Model):
"""
Abstract base model that allows for an object to be linked (foreign-keyed)
to various models. Useful example: Comments or tags for different types of
objects.
@asafge
asafge / aspect_ratio.html
Created April 9, 2014 09:25
An example of setting a constant aspect ratio for an HTML element.
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
.wrap{
position: relative;
width: 30%; /* Width */
float: left;
margin: 5px;
@asafge
asafge / image_loaded.js
Last active August 29, 2015 14:00
JS - Wait for an image to load by periodically checking it's status. Useful when images are cached, and some browsers don't trigger appropriate events.
var $img = ...,
imagesLoadedCallback = function() {
// Do stuff
}
(function waitForImageLoaded() {
if ($img[0].complete && $img[0].naturalWidth > 0) {
imagesLoadedCallback();
}
else {
@asafge
asafge / pyToJsDateFormats.js
Last active August 29, 2015 14:01
Convert a python/strfdate date/time format to moment.js's format
var pythonToJsFormats = Object.freeze({
'%a': 'ddd',
'%A': 'dddd',
'%w': 'd',
'%d': 'DD',
'%b': 'MMM',
'%B': 'MMMM',
'%m': 'MM',
'%y': 'YY',
'%Y': 'YYYY',
@asafge
asafge / lazy_property.py
Created December 21, 2014 09:11
Python lazy property
class LazyProperty(object):
"""
A descriptor protocol implementation used for wrapping a lambda
function (without parameters) to behave like property access.
Usage, only within a class definition: all_users = LazyProperty(lambda: [])
"""
@staticmethod
def is_lambda(v):
lambda_example = lambda: 0
return isinstance(v, type(lambda_example)) and v.__name__ == lambda_example.__name__
@asafge
asafge / ldap_bind_and_query.py
Last active December 27, 2015 16:19
ldap - bind and query a directory with python-ldap
import ldap
# Initialize and bind
con = ldap.initialize('ldap://10.0.0.23')
dn = "cn=test,dc=example,dc=com"
pw = "Pass1"
con.simple_bind_s(dn, pw)
# Run a query against the directory
@asafge
asafge / fromNative.js
Created November 13, 2013 10:05
AngularJS filter that transforms a "foreign key" represented object to a readable string.
angular.module('filters', []).
filter('fromNative', function() {
/**
* Transform any "foreign key" value to its string representation,
* similar to the way ng-options works for a <select> element,
*
* Example:
* Let's say we have an item object with a userID property (item.userID = 5)
* This ID translates to a 'username' from a 'users' object, that looks
* something like this: users = [ {id: 6, name: 'john'}, ... ].
@asafge
asafge / iptables_python.py
Created November 26, 2013 08:36
IPTables configuration samples in Python
import iptc
# sudo iptables -t filter -F
if __name__=="__main__":
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT")
# Block | ANY | ANY
rule_block_all = iptc.Rule()
rule_block_all.in_interface = "eth+"
rule_block_all.src = "0.0.0.0/0.0.0.0"
@asafge
asafge / index.html
Created March 10, 2020 13:29
Infinite Multilevel Dropdown
<ul class="nav site-nav">
<li><a href=#>Lorem</a></li><!--
--><li><a href=#>Ipsum</a></li><!--
--><li class=flyout>
<a href=#>Dolor</a>
<!-- Flyout -->
<ul class="flyout-content nav stacked">
<li><a href=#>Foo Bar</a></li>
@asafge
asafge / ng-really.js
Created November 12, 2013 13:06
ng-really? An AngularJS directive that creates a confirmation dialog for an action.
/**
* A generic confirmation for risky actions.
* Usage: Add attributes: ng-really-message="Are you sure"? ng-really-click="takeAction()" function
*/
angular.module('app').directive('ngReallyClick', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
var message = attrs.ngReallyMessage;