Skip to content

Instantly share code, notes, and snippets.

View kottenator's full-sized avatar

Rostyslav Bryzgunov kottenator

  • Facebook
  • Seattle, WA
View GitHub Profile
@kottenator
kottenator / temperature.min.py
Created November 11, 2016 04:21
Simple temperature calculator (°C / °F)
c = lambda f: (f - 32) * 5 / 9.; f = lambda c: c * 9 / 5. + 32; print('\n'.join(u'{2:>6} °C ← °F {0:>4} °C → °F {1:6}'.format(i * 10, f(i * 10), round(c(i * 10), 1)) for i in range(-10, 11)))
@kottenator
kottenator / history.js
Created October 26, 2016 18:56
Document title & History API
var button = document.createElement('button');
button.textContent = 'Push';
document.body.appendChild(button);
var x, title;
if (!history.state) {
x = 0;
title = document.title;
// Without this we can loose the initial page's title on browser navigation
@kottenator
kottenator / compressor_filters.py
Last active July 6, 2016 20:41
django-compressor - abstract static URL
"""
Django 'base' app - compressor filters.
"""
from compressor.filters.css_default import CssAbsoluteFilter
class CssRelativeFilter(CssAbsoluteFilter):
"""
``compressor.filters.css_default.CssAbsoluteFilter`` converts relative URLs to images, fonts, etc
in CSS code to absolute URLs like '/static/base/images/logo.svg'.
@kottenator
kottenator / simple-pagination.js
Created July 13, 2015 20:44
Simple pagination algorithm
// Implementation in ES6
function pagination(c, m) {
var current = c,
last = m,
delta = 2,
left = current - delta,
right = current + delta + 1,
range = [],
rangeWithDots = [],
l;
@kottenator
kottenator / djrf-error-parser.js
Last active April 15, 2022 13:49
JS error parser for django-rest-framework
/**
* Specific error parser for django-rest-framework errors structure
* Tested on django-rest-framework v2.3.13
* Live example at http://jsbin.com/wiqumo
*
* Error structure examples:
*
* // The most simple case
* { detail: "Authentication is required" }
*
@kottenator
kottenator / multiple_select_field.py
Last active June 24, 2020 01:52
Django's multiple-choice model field with static choices
"""
Field with multiple *static* choices (not via m2m)
Value is stored in DB as comma-separated values
Default widget is forms.CheckboxSelectMultiple
Python value: list of values
Original Django snippet: https://djangosnippets.org/snippets/1200/
It's 6 years old and doesn't work with latest Django
Also it implements 'max_choices' functionality - I have removed it for simplicity
@kottenator
kottenator / select2-flat.css
Created April 23, 2014 10:49
Select2 styles for Flat UI
/*
* Select2 v3.4.6 styles customization for Flat UI
*/
/*----------------------------------------------- Main select element ------------------------------------------------*/
.select2-container .select2-choice {
height: 41px; /* Jobsy form controls have 37px total height */
border: 2px solid #bdc3c7;
border-radius: 6px;
outline: none;
font: 15px/38px "Lato", Liberation Sans, Arial, sans-serif;
@kottenator
kottenator / ajax_setup.js
Last active August 30, 2016 11:23
Setup jQuery AJAX & show errors
/**
* Setup jQuery AJAX
*
* Requires noty.js and 2 its layouts: noty/layouts/top.js, noty/layouts/topRight.js
*/
$.ajaxSetup({
error: function(xhr, status, error) {
// we ignore aborted XHR or when user press F5 while XHR is in progress
if (status == 'abort' || xhr.readyState == 0)
return;
@kottenator
kottenator / noty_flat_theme.css
Last active August 29, 2015 13:59
Noty theme in CSS (not in JS as it recommends by default)
/**
* Noty 2.2.2 Theme: Flat UI colors
* @author kottenator
*/
.flat .noty_bar {
font: 16px/1.3 Arial, Liberation Sans, sans-serif;
text-align: center;
padding: 12px 15px 13px;
position: relative;
border: 2px solid #eee;
@kottenator
kottenator / js_observer.js
Last active August 12, 2019 15:17
JavaScript Simple Observer
/**
* Simple Observer pattern mixin (custom event dispatching & listening)
* Doesn't requires any 3rd-party JS libs. It's pure JS
*
* Usage:
*
* var MyObject = {...}; // your custom object
* Observer.apply(MyObject); // you also can apply Observer to a prototype
* MyObject.on('inited', function() { console.log("I'm alive!"); });
* MyObject.trigger('inited');