Skip to content

Instantly share code, notes, and snippets.

@gcetusic
gcetusic / historymixin.py
Last active August 29, 2015 14:03
History mixin for django models that want to track history of certain fields
class HistoryMixin(object):
_diff_fields = set()
_ignore_fields = set()
_save_model = None
_model_fk = None
def save(self, *args, **kwargs):
should_save = False
saving_fields = {}
@gcetusic
gcetusic / gist:26122700b1fcd680eb31
Last active August 29, 2015 14:08
Assigning model to choice tupple in modelchoice fields
class AssignModelChoiceIterator(forms.models.ModelChoiceIterator):
def choice(self, obj):
return (
self.field.prepare_value(obj),
self.field.label_from_instance(obj), obj)
class AssignModelChoiceField(forms.ModelChoiceField):
def _get_choices(self):
if hasattr(self, '_choices'):
@gcetusic
gcetusic / Djangorange.html
Last active August 29, 2015 14:14
Django template - for i in range...
{% with items|length as item_count %}
{% with ''|center:item_count as range %}
{% for _ in range %}
<li class="hscroll_list__item">
{% render_next 'simple_card_type_SPsA' %}
</li>
{% endfor %}
{% endwith %}
{% endwith %}
@gcetusic
gcetusic / add_formset_form.js
Last active August 29, 2015 14:16
Dynamically create a new form for a Django formset
function cloneMore(selector, type) {
var newElement = $(selector).clone(true);
var total = $('#id_' + type + '-TOTAL_FORMS').val();
newElement.find(':input').each(function() {
var name = $(this).attr('name').replace(
'-' + (total - 1) + '-', '-' + total + '-');
var id = 'id_' + name;
$(this).attr({'name': name, 'id': id}).val('').removeAttr('checked');
});
newElement.find('label').each(function() {
@gcetusic
gcetusic / removeparam.js
Created March 17, 2015 13:41
Remove parameter from url
var rtn = sourceURL.split("?")[0],
param,
params_arr = [],
queryString = (sourceURL.indexOf("?") !== -1) ? sourceURL.split("?")[1] : "";
if (queryString !== "") {
params_arr = queryString.split("&");
for (var i = params_arr.length - 1; i >= 0; i -= 1) {
param = params_arr[i].split("=")[0];
if (param === key) {
params_arr.splice(i, 1);
@gcetusic
gcetusic / paramonclick.js
Created March 17, 2015 13:42
Add parameter on link click
window.addEventListener("click", function(e) {
var href = e.target.getAttribute("href");
if (href) {
href += (href.match(/\?/) ? '&' : '?');
location.href = href + "_st_userhash=" + docCookies.getItem('analytics-user');
e.preventDefault();
}
});
@gcetusic
gcetusic / calling
Created April 13, 2015 12:30
printing calling function and line
import inspect
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
print calframe[1][2], calframe[1][1]
@gcetusic
gcetusic / divider.py
Created April 16, 2015 11:12
Given an integer, divides that integer equally based on the divisor without losing the number because of the way Python deals with integer divisions
def get_key_borders(self, count):
borders = []
cpu_count = CPU_COUNT
while cpu_count:
count = count - (count / cpu_count)
borders.append(count)
cpu_count -= 1
borders.sort()
return borders
@gcetusic
gcetusic / gist:60130ec8237458606fc5
Created September 23, 2015 12:14
Get count of entries with distinct uuid field when dataset too big to use .distinct()
db['collection_name'].aggregate({$group: { _id: "$uuid"}}, {$group: { _id: 1, count: {$sum: 1 }}})
@gcetusic
gcetusic / app__init__.py
Created November 12, 2015 13:57
Override Django project global settings on application load
def inject_app_defaults(application):
"""Inject an application's default settings"""
try:
__import__('%s.settings' % application)
import sys
# Import our defaults, project defaults, and project settings
_app_settings = sys.modules['%s.settings' % application]
_def_settings = sys.modules['django.conf.global_settings']
_settings = sys.modules['django.conf'].settings