Skip to content

Instantly share code, notes, and snippets.

View cwurld's full-sized avatar

Chuck Martin cwurld

View GitHub Profile
@cwurld
cwurld / fix_chosen.js
Created December 4, 2014 23:22
For Fixing Chosen Selects Created by Django Dynamic Formsets
/**
* Created by Chuck Martin on 12/4/14.
*
* When django-dynamic-formset creates a new formset and that formset used the "Chosen" widget
* (http://harvesthq.github.io/chosen/), the newly created Chosen widget does not work right.
*
* This code solves that problem by replacing the select (along with the chosen code) that was cloned with a new
* select created from scratch.
*
* This function returns a function for creating a new select that can be associated with Chosen.
@cwurld
cwurld / convert_template.py
Last active August 29, 2015 14:11
Converts a Django page template into a page template for a Django project template
from __future__ import absolute_import
__author__ = 'Chuck Martin'
import os
import Tkinter
import tkFileDialog
def convert_template(file_path=None, out_dir=None):
@cwurld
cwurld / ccm_python-flckr.py
Created April 29, 2013 18:18
How to POST a photo to Flickr using Python The Flickr api docs are OK, but lacking in detail about how to post a photo once you complete the oauth process. Matt Kelsey has a nice blog post about basic oauth process with Flickr: http://mkelsey.com/2011/07/03/Flickr-oAuth-Python-Example/. Read his blog first. I should mention that I tried using Py…
import time
import oauth2 as oauth
import requests # From: http://docs.python-requests.org/en/latest/
# oauth_token and oauth_token_secret are the tokens produced at the end of the Flickr oauth process
token = oauth.Token(oauth_token, oauth_token_secret)
photo_url = 'http://api.flickr.com/services/upload'
@cwurld
cwurld / selenium_for_chosen.py
Last active January 1, 2016 17:16
Python code for handling the jquery Chosen plugin when testing using selenium.
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
class Chosen(object):
"""
For selenium testing of the jquery Chosen widget (v1.1.0 https://harvesthq.github.io/chosen/).
@cwurld
cwurld / django_select2_mixin.py
Last active April 21, 2016 14:30
django and select2
'''
I have a model for customer intake. One of the fields is for tracking how the customer heard about us. I do
not want an endless proliferation of values, so I would like to use a multi-select. The problem is I also want the
user to add new choices as needed. It turns out this is possible with the Select2 widget:
https://select2.github.io/examples.html#tokenizer - check out this link to see how to setup your javascript.
It turns out this is a little tricky to implement. Below is a mixin you can use with a form to handle the field.
Also, if you are using bootstrap, you will want: https://fk.github.io/select2-bootstrap-css/
@cwurld
cwurld / gist:7db1a642c32ff548c184
Last active January 16, 2017 23:51
django selenium cheatsheet
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.support.select import Select
# DOES NOT WORK WITH selenium 2.5 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@cwurld
cwurld / gist:bbd138c953b416644ed66901d1e9196b
Last active July 18, 2017 10:07
Example of Python Dedupe Gazetteer
# Gazetteer ---------------------------------------------------------------------------------------------------------
def load_deduped_output_for_gazetteer(filename):
"""
Parse data from dedupe into:
1. Canonical dataset (no dupes)
2. messy data - all the dupes
3. markPairs.
:return:
@cwurld
cwurld / container_with_items.py
Last active October 19, 2017 18:57
Django Container with Items - (e.g. inlineformset_factory)
from django.forms.models import inlineformset_factory
from django.shortcuts import render
def container_with_items(request, container_id):
ItemFormSet = inlineformset_factory(
models.Container, models.Item, form=forms.InlineItemForm, fk_name='container', extra=1
)
# Use this to layout the Item forms
@cwurld
cwurld / xlsxwriter_cheatsheet.py
Last active November 6, 2019 06:47
xlsxwriter Cheat Sheet
# xlsxwriter Cheat Sheet
#
# xlsxwriter is an awesome package for writing Excel spreadsheets. It's got excellent documentation. And it's got
# way more functionality than I need. This cheat sheet contains the basic functionality that I use all the time.
#
# While it is possible to put formulas in the spreadsheet, I have had some problems with spreadsheets as email attachments
# when the spreadsheet has formulas and the email client is on mobile.
import xlsxwriter
@cwurld
cwurld / select2_for_selenium.py
Created July 7, 2020 17:07
Code to manipulate a Select2 widget from Selenium as if it were a normal Select
from collections import namedtuple
# Javascript scripts -----------------------------------------------------------------------------------------------
SELECT_BY_VALUE = \
'''
$(arguments[0]).val(arguments[1]);
$(arguments[0]).trigger('change');
'''