Skip to content

Instantly share code, notes, and snippets.

View ranelpadon's full-sized avatar

ranelpadon

View GitHub Profile
@ranelpadon
ranelpadon / js_closures_in_loops.html
Last active August 29, 2015 14:12
Alternative implementations using immediately-invoked function expressions (IIFE) to Mozilla's sample codes for JS Closures in loops .
<html>
<head>
</head>
<body>
<!-- Base DOM markup and code logic here: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Closures -->
<p id="help">Helpful notes will appear here</p>
<p>E-mail: <input type="text" id="email" name="email"></p>
<p>Name: <input type="text" id="name" name="name"></p>
<p>Age: <input type="text" id="age" name="age"></p>
@ranelpadon
ranelpadon / twitter_typeahead_with_tripadvisor_data.js
Created January 2, 2015 19:33
Sample integration of Twitter TypeAhead Library with a remote data server (TripAdvisor API).
MAPWIDGET = MAPWIDGET || {};
// Constructor function for TripAdvisor's TypeAhead behavior.
MAPWIDGET.Autocomplete = function(domElement) {
// TypeAhead Source URL per documentation of TripAdvisor.
// See http://api.tripadvisor.com/api/partner/2.0/doc?key=TA_KEY.
// %QUERY is a wildcard needed by the suggestion engine.
var typeAheadUrl = TripAdvisor_API_BASE_URL + '/typeahead/%QUERY?key=' + tripAdvisorKey;
// Create a new suggestion engine dedicated for the specified category.
@ranelpadon
ranelpadon / toolkit.drush.inc.php
Last active August 29, 2015 14:12
Custom Drush Commands Demo. Rename the file as toolkit.drush.inc ($ mv toolkit.drush.inc.php toolkit.drush.inc). Make it executable ($ sudo chmod a+x toolkit.drush.inc). Put this file in ~/.drush folder so that you will have now a ~/.drush/toolkit.drush.inc file.
<?php
/**
* Implements COMMANDFILE_drush_command().
*/
function toolkit_drush_command() {
$items = array();
// Command for Clear Cache All.
// The 'cca' array key is what you type in the terminal, like: drush cca.
@ranelpadon
ranelpadon / drupal_get_node_type.php
Last active August 29, 2015 14:12
Get the node type of the current node using EntityFieldQuery (EFQ) API.
<?php
// Get the node id from the current page's URL.
$nid = arg(1);
// Create a new EFQ object.
$query = new EntityFieldQuery();
// Select the node entity with the target node id.
$query->entityCondition('entity_type', 'node');
@ranelpadon
ranelpadon / drupal_image_with_caption_block.php
Last active August 29, 2015 14:13
Create a Drupal block programmatically. This will create a block with an image and a tagline, demonstrating the use of theme function and render arrays
<?php
// HOW TO USE:
// Create a MY_MODULE.info file. See https://www.drupal.org/node/542202.
// Create a MY_MODULE.module file.
// Put the codes below in MY_MODULE.module file
// THE BIG PICTURE:
// For a block to work in Drupal, you have to do a minimum of 3 things:
// 1. Declare the block in hook_block_info().
@ranelpadon
ranelpadon / issues_with_deleting_objects.py
Last active August 29, 2015 14:19
Python's __del__ has some nasty issues when dealing with objects, list, garbage collection, etc.
# Hypothetical class for saving the Universe.
class Particle:
def __init__(self, name):
self.name = name
print self.name, 'created'
def __del__(self):
print self.name, 'deleted'
# Create new particle objects.
@ranelpadon
ranelpadon / dependent-field.js
Created August 27, 2017 09:54
Update the select list field B based on the currently selected option in select list field A. For example, when you select a particular Music Genre only the related Artist should show. Other common use cases/relationships are Company/Contact Info, Country/City, or Brand/Products.
(function($) {
$(function() {
// Artist's select option values will depend on
// the Genre's selected option.
var $genreSelectWidget = $('#id_genre');
var $artistSelectWidget = $('#id_artist');
var artistSelectWidgetInitial = $artistSelectWidget.val()
// Adjust Artists based on the selected Genre.
function adjustArtistOptions(preSelectedOption) {
@ranelpadon
ranelpadon / django-dependent-field.js
Last active August 28, 2017 04:26
Update the select list field B based on the currently selected option in select list field A. For example, when you select a particular Music Genre, only the related Artists should show. Other common use cases/relationships: Country/Cities, Organization/Members, Company/Contact Persons, Brand/Products, etc.
// Workflow (Genre is the Parent Field, Artist is the Dependent Field):
// 1. user interacts w/ Genre select list widget via JS script.
// 2. JS script fetches the new Artist list in views.py
// 3. JSON response will be used as the new values/options for the Artist select list widget.
(function($) {
$(function() {
// Artist's select option values will depend on
// the Genre's selected option.
var $genreSelectWidget = $('#id_genre');
@ranelpadon
ranelpadon / multiple_replace.py
Last active February 17, 2018 11:52
Find the restricted symbols in the text and substitute with multiple replacement values.
import re
# Restricted symbols taken from https://en.wikipedia.org/wiki/Filename
symbols = ('/', '\\', '?', '%', '*', ':', '|', '"', '<', '>',)
symbols_replacements = {symbol: '_' for symbol in symbols}
space_replacement = {' ': '-'} # Dash instead of underscore
replacements = {}
replacements.update(symbols_replacements)
@ranelpadon
ranelpadon / django-formset--custom-kwarg--formset-save--model-save.py
Last active June 8, 2018 04:47
Custom Django model.save() kwarg to avoid executing costly codes if there are lots of formsets in the page.
# forms.py
class EventSessionFormSet();
...
def save(self, commit=True):
sessions = []
forms = self.forms
total_forms = len(forms)
for i, form in enumerate(forms):