Skip to content

Instantly share code, notes, and snippets.

@23maverick23
23maverick23 / roundDecimal.js
Last active October 12, 2015 00:17
JavaScript: Round decimal
/*
* Simple script to correctly round up 2 decimal numbers.
* Helpful in handling currencies that use 2 decimal place.
* Will ensure 2 decimal places, even when ending in zeros.
* v = value to be rounded.
*/
function roundTwoDecimals (v) {
if (typeof(v) !== 'number') {
return alert('v must be a number!');
} else {
@23maverick23
23maverick23 / roundTwoDecimals.py
Last active September 23, 2016 06:01
Django: Round price to 2 decimals
@property
def update_price(self):
"""
Return a rounded price using the quantity and rate.
This utilizes the Decimal object to get around the strange
rounding behavior in Python with Base 10. The logic converts
both operands to Decimal objects, multiplies them, then
performs a ROUND_UP method using 2 decimal places.
"""
@23maverick23
23maverick23 / formControlLabel.html
Last active October 12, 2015 00:37
Django: Form control label with errors
<div class="control-group{% if form.name.errors %} error{% endif %}">
<label class="control-label" for="name">Name:</label>
<div class="controls">
{{ form.name }}
{% if form.name.errors %}<span class="help-inline">{{ form.name.errors }}</span>{% endif %}
</div>
</div>
@23maverick23
23maverick23 / checkAll.js
Last active October 12, 2015 03:58
JavaScript: Check all checkboxes in table rows
/*
* Check all table rows when header checkbox is checked.
* Also add a class to the table row for highlighting.
*/
// Set initial header checkbox state to false
var checkallEnabled = false;
// When checkbox in table header is clicked...
$("#check_all").click(function() {
@23maverick23
23maverick23 / highlightTableRows.js
Last active October 12, 2015 03:58
JavaScript: Highlight table row when checked
/*
* Highlight table rows when checkbox is checked.
*/
$("tbody>tr").filter(":has(:checkbox:checked)").addClass("success").end().click(function(event) {
$(this).toggleClass("success");
if (event.target.type !== "checkbox") {
$(":checkbox", this).attr("checked", function() {
return !this.checked;
});
@23maverick23
23maverick23 / filtering.py
Created November 17, 2012 21:20 — forked from robgolding/filtering.py
Django: Class-Based View Mixins
class FilterMixin(object):
"""
View mixin which provides filtering for ListView.
"""
filter_url_kwarg = 'filter'
default_filter_param = None
def get_default_filter_param(self):
if self.default_filter_param is None:
raise ImproperlyConfigured(
@23maverick23
23maverick23 / randPassGen.py
Last active June 9, 2019 11:27
Python: Random password generator
#!/usr/bin/env python
import string
import random
def password_generator(size=8, chars=string.ascii_letters + string.digits):
"""
Returns a string of random characters, useful in generating temporary
passwords for automated password resets.
@23maverick23
23maverick23 / bookmarklet.js
Last active May 19, 2021 08:48
JavaScript: Bookmarklet with jQuery
(function(){
// the minimum version of jQuery we want
var v = "1.8.3";
// check prior inclusion and version
if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
var done = false;
var script = document.createElement("script");
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js";
@23maverick23
23maverick23 / bookmarklet.html
Last active October 13, 2015 19:18
HTML5: Bookmarklet hyperlink
<a href="javascript:(function(){if(window.myBookmarklet!==undefined){myBookmarklet();}else{document.body.appendChild(document.createElement('script')).src='http://somewhere.com/script.js?';}})();">Bookmarklet</a>
@23maverick23
23maverick23 / simpleHTMLEncoder.js
Created January 5, 2013 22:18
JavaScript: Simple HTML Encoder
// Encodes a string containing HTML characters
// These include &, >, <, ".
function simpleHTMLEncoder(str) {
return str.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
}