Skip to content

Instantly share code, notes, and snippets.

@andylolz
andylolz / fetch_all.py
Created October 1, 2012 15:56
Useful useful useful
def fetch_all(model_or_query):
'''Fetch all the entities of the given model, or satisfying the given query.
'''
if isinstance(model_or_query, type) and issubclass(model_or_query, db.Model):
query = model_or_query.all(keys_only=False)
elif isinstance(model_or_query, db.Query):
query = model_or_query
else:
raise Exception("model_or_query is neither a model nor a query")
@andylolz
andylolz / gist:3860781
Created October 9, 2012 19:11
Block out a load of IDs
from google.appengine.ext import db
import sys
import inspect
def fetch_all(model_or_query):
'''Fetch all the entities of the given model, or satisfying the given query.
'''
if isinstance(model_or_query, type) and issubclass(model_or_query, db.Model):
query = model_or_query.all(keys_only=False)
elif isinstance(model_or_query, db.Query):
@andylolz
andylolz / gist:3861722
Created October 9, 2012 22:03
Hammer the datastore
def hammer_the_datastore():
demo = models.School.all().filter('url_slug', 'demonstration').fetch(1)[0]
donations = []
print("Generating donations . . .")
for x in range(0, 500):
donations.append(models.Donation(
school=demo,
num_cells=10,
preferred_cell_index=-1,
donor_first_name="Andy",
@andylolz
andylolz / gist:3885906
Created October 13, 2012 19:49
Laravel question

The problem

So this isn’t my actual scenario, but I think it illustrates my problem quite well. Imagine an online school report system. I have Teachers, Students, and Reports. Teachers and Students are both Users. The relationship between Students and Reports is one-to-one (a student just has a single report card), but between Teachers and Reports is one-to-many (a teacher has a whole classroom-worth of reports.) The Report model has both a student_id and a teacher_id, both of which are foreign keys to the User table.

I’d like to represent these relationships in my models, so for a given User I can call:

User::find(5)->reports;
@andylolz
andylolz / Artisan patch
Created October 14, 2012 16:14
Make key:generate work with the --env switch
diff --git a/laravel/cli/tasks/key.php b/laravel/cli/tasks/key.php
index 0c08a84..b7b5d00 100644
--- a/laravel/cli/tasks/key.php
+++ b/laravel/cli/tasks/key.php
@@ -19,7 +19,14 @@ class Key extends Task {
*/
public function __construct()
{
- $this->path = path('app').'config/application'.EXT;
+ if($environment = get_cli_option('env'))
@andylolz
andylolz / Student.php
Created October 14, 2012 23:36
Fun extending the User model. This is my solution to the problem I posted here: http://gist.github.com/3885906 Thanks to Kindari on laravel IRC
<?php
class Student extends User {
public function reports() {
// I know I said the relationship here is one-to-one..!
// But I don't mind hacking this too much.
return $this->has_many('Report', 'student_id');
}
}
@andylolz
andylolz / _typesetter.php
Created November 28, 2012 23:44
Typeset some content for a textbox; truncate it to fit.
<?php
/**
* Preliminary work on the typesetter.
*
* $input_str - the input
* $width - the width of the text area in characters
* $height - the height of the text area in characters
* $overflow_str - the string appended to the text area, in the event of an overflow
*
* returns the typeset contents of the text area
@andylolz
andylolz / datagrab_assets_hack.diff
Created March 5, 2013 10:12
Hack to get DataGrab (roughly) working with Assets 2, and Assets 2 inside a Matrix field.
diff --git a/assets/third_party_code/ajw_datagrab/fieldtypes/datagrab_assets.php b/assets/third_party_code/ajw_datagrab/fieldtypes/datagrab_assets.php
index ea3f8ec..4f22dc2 100644
--- a/assets/third_party_code/ajw_datagrab/fieldtypes/datagrab_assets.php
+++ b/assets/third_party_code/ajw_datagrab/fieldtypes/datagrab_assets.php
@@ -24,8 +24,21 @@ class Datagrab_assets extends Datagrab_fieldtype {
while( $subitem = $DG->datatype->get_sub_item(
$item, $DG->settings["cf"][ $field ], $DG->settings, $field ) ) {
- $data[ "field_id_" . $field_id ][] = $subitem;
-
@andylolz
andylolz / jekyll_to_wp.py
Last active December 19, 2015 00:58
convert a directory of jekyll posts to wordpress posts
# -*- coding: utf-8 -*-
import os
import markdown
import yaml
from pywordpress import Wordpress
import xmlrpclib
class jekyll_to_wp():
@andylolz
andylolz / compare_pb_gov_uk.py
Created July 6, 2013 00:19
For all public bodies listed on gov.uk, find the best match currently on publicbodies.org
import csv
import urllib2
from bs4 import BeautifulSoup, UnicodeDammit
import Levenshtein
class compare_pb_gov_uk():
def __init__(self):