Skip to content

Instantly share code, notes, and snippets.

@goldhand
goldhand / Django + Ajax dynamic forms .py
Last active September 29, 2023 06:32
Django form with Ajax. A simple Task model that can be updated using a CBV with an AJAX mixin. The view sends post data with ajax then updates the view with a callback to a DetailView with a json mixin.There is an abstract CBV, AjaxableResponseMixin, based on the example form django docs, that is subclassed in the TaskUpdateView CBV. TaskUpdateV…
#models.py
class Task(models.Model):
title = models.CharField(max_length=255)
description = models.TextField()
def __unicode__(self):
return self.title
@goldhand
goldhand / bluetooth setup ubuntu 12.04
Created June 27, 2013 03:40
Setup Logitech K810 Keyboard to pair with ubuntu 12.04
1. Set keyboard discoverable
2. Get the mac address of the bluetooth device:
$ hcitool scan
3. Create new device:
$ sudo bluez-simple-agent hci0 [bluetooth device mac address = XX:XX:XX:XX:XX:XX]
which will hopefully return somthing like:
DisplayPasskey (/org/bluez/537/hci0/..., [PIN = 123456])
else:
$ sudo bluez-simple-agent hci0 XX:XX:XX:XX:XX:XX repair
start over
@goldhand
goldhand / views.py
Created July 19, 2013 03:02
TaskUpdateView responds with bound form data as a json object
import json
from crispy_forms.utils import render_crispy_form
from django.contrib.auth.models import User
from django.shortcuts import render_to_response, render, get_object_or_404
from django.http import HttpResponseRedirect
from django.views import generic
from django.core.urlresolvers import reverse_lazy
from django.http import HttpResponse
from django.forms.models import inlineformset_factory
@goldhand
goldhand / task_update_json.html
Created July 19, 2013 05:43
Updates any Task model without reloading using ajax
#views.py
class TaskUpdateView(generic.UpdateView):
model = Task
form_class = TaskForm
@json_view
def dispatch(self, *args, **kwargs):
return super(TaskUpdateView, self).dispatch(*args, **kwargs)
@goldhand
goldhand / subcat.js
Created August 10, 2013 05:55
for the sub categories
/**
* User: goldhand
* Date: 8/9/13
* Time: 10:52 PM
*/
var tcl = $('#task-category-list').find('ul').each(function(i) { console.log($(this).parent().attr('id')); console.log($(this).sortable('toArray'));});
@goldhand
goldhand / createdevdb.py
Created August 10, 2013 06:10
creates a dev db with argv[1] users, each with argv[2] projects that use the same argv[3] categories, each with argv[4] tasks
import sys, os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cpm.settings")
from django.contrib.auth.models import User
from projects.models import Project
from tasks.models import Task, TaskCategory
from changes.models import ChangeOrder
from updates.models import Update
@goldhand
goldhand / recursiveness
Created August 15, 2013 08:29
Things I learned dealing with JSON trees in django
# Things I learned dealing with recursive JSON objects
This is a summary of my findings while dealing with a django model, TaskCategory.
## Two ways for manipulating a set of trees
I found two different ways for manipulating objects with an uncertain amount
of ascendants / descendants. The first involves manipulating an object within
a tree and the second involves taking apart the tree, manipulating the branches
and building a new tree
from django.db import models
class Slugged(models.Model):
title = models.CharField(max_length=500)
slug = models.CharField(max_length=2000, blank=True, null=True)
class Meta:
abstract = True
@goldhand
goldhand / cpm_tags.py
Created August 30, 2013 23:58
thumbnail template tag
__author__ = 'wpl'
import os
from urllib import quote, unquote
from django.core.files import File
from django.core.files.storage import default_storage
from django.conf import settings
from django import template
try:
@goldhand
goldhand / get_dict_from_list
Created September 10, 2013 19:43
Pass a list of dict conditions to find a particular dict in a recursive set of dicts
def check_conditions(conditions={}, D={}):
for key in conditions:
if D[key] == conditions[key]:
continue
else:
return False
return True
def get_dict_from_list(DL=[], conditions=[], i=0, children_key='children'):
"""