Skip to content

Instantly share code, notes, and snippets.

@hello-josh
hello-josh / entities.py
Last active August 29, 2015 14:26
Using flask_wtf and wtforms_appengine to generate forms from an ndb.Model subclass with an ndb.KeyProperty
from google.appengine.ext import ndb
class AuditMixin(object):
"""Audit fields for datastore objects"""
created_at = ndb.DateTimeProperty(auto_now_add=True)
created_user = ndb.StringProperty(indexed=False)
modified_at = ndb.DateTimeProperty(auto_now=True)
modified_user = ndb.StringProperty(indexed=False)
@hello-josh
hello-josh / omg_timezone.py
Last active February 28, 2018 07:50
Get a user's timezone on app engine based off of the Google Maps Time Zone API
import time
import json
from pytz.gae import pytz
from google.appengine.api import urlfetch
#: Preset object for America/New_York
EasternTz = pytz.timezone('America/New_York')
def user_timezone_from_latlong(lat_long=None):
from google.appengine.ext import ndb
from my_models import User
id_ = '12345'
user = User.get_by_id(id_)
print user.Username
print user.PhoneNumber
user = ndb.Key(User, id_).get() # same as User.get_by_id
@hello-josh
hello-josh / lazy.py
Created July 14, 2015 19:49
Lazy Iterator to populate WTForms choices
class LMSInstanceIterator(object):
"""Lazy iterator to populate ClientEditForm.lms_instance when rendered"""
def __iter__(self):
return iter([(instance.key.id(), instance.name)
for instance in lmsinstances.query()])
class ClientEditForm(BaseForm):
lms_instance = fields.SelectField('LMS Instance', choices=LMSInstanceIterator())
{
"name":"Guido",
"addresses.type": ["home", "work"],
"addresses.street": [null, "Spear St"],
"addresses.city": ["Amsterdam", "SF"],
}
@hello-josh
hello-josh / the_best.py
Created July 14, 2015 16:01
Probably the best code I've seen in July
messages = lib.messaging.get_all_messages(opaqueID)
message_count = 0
for message in messages:
message_count = message_count + 1
metrics['message_count'] = mesage_count
@hello-josh
hello-josh / How to delete entities.md
Created May 12, 2015 18:18
Google App Engine - Deleting entities from your local dev_appserver.py

Delete entities from your local google app engine datastore

First you need to shut down any app that is using the datastore. The app holds the file in memory while running and flushes it to disk when it shuts down. This means any writes you do while the app is running will be undone when you shut down the app.

Next, find the datastore file and connect to it using sqlite.

user@host:path$ sqlite3 my-app.sqlite3
I've got no users
To hold me down
To make me fret, or make me frown
I had users
But now I'm free
There are no users on me
Hi-ho the me-ri-o
That's the only way to be
I want the world to know
@hello-josh
hello-josh / lib.php.diff
Created March 17, 2015 13:29
Diff to enable deleting of non-empty subpages based on the 2013052400 version of mod_subpage
diff --git a/mod/subpage/lib.php b/mod/subpage/lib.php
index 2050835..07ed4fa 100644
--- a/mod/subpage/lib.php
+++ b/mod/subpage/lib.php
@@ -80,10 +80,17 @@ function subpage_delete_instance($id) {
// if deleting a subpage activity from course/mod.php page (not delete the whole course)
if ($PAGE->pagetype == 'course-mod') {
$subpagecmid = required_param('delete', PARAM_INT);
- // Check if all the sections in this subpage is empty
- $subpageinstance = mod_subpage::get_from_cmid($subpagecmid);
@hello-josh
hello-josh / fixtures.py
Last active August 29, 2015 14:16
GAE Datastore Fixtures idea
import datetime
from nosegae.fixture import NdbFixture
import models
class MyGreetings(NdbFixture):
"""Holds all data fixtures to be managed by the library
Each inner class's attributes are used as `kwargs`
in `model`'s constructor like similar to