Skip to content

Instantly share code, notes, and snippets.

View sahid's full-sized avatar

Sahid Orentino Ferdjaoui sahid

View GitHub Profile
<form method='POST' action='http://upload.example.com' enctype='multipart/form-data' id='form'>
<input type='file' />
<input type='submit' />
</form>
<script>
var io = new goog.net.IframeIo;
goog.events.listen (io, goog.net.EventType.COMPLETE,
function (event) {
alert(event.target.getResponseText ());
class User (db.model):
name = db.StringProperty ()
prop = db.IntergerPeroperty ()
class Presence (db.model):
user = db.ReferenceProperty (User)
last = db.DateTimeProperty ()
# Keys users onlines
presences = Presence.all ().filter ('last >', date_considered_connected)
## Models.py
class Message (MarkDeleted):
uuid = db.StringProperty (required=True)
owner = db.ReferenceProperty (Actor, collection_name="message_owner", required=True)
target = db.ReferenceProperty (Actor, collection_name="message_target", required=True)
entry = db.ReferenceProperty (Entry, required=True)
reply = db.SelfReferenceProperty ()
label = db.StringProperty (choices=MESSAGE_LABEL, required=True)
@sahid
sahid / gist:545550
Created August 23, 2010 14:02
GAE Query Counter
def query_counter (q, cursor=None, limit=500):
if cursor:
q.with_cursor (cursor)
count = q.count (limit=limit)
if count == limit:
return count + query_counter (q, q.cursor (), limit=limit)
return count
def prefetch_refprops(entities, *props):
fields = [(entity, prop) for entity in entities for prop in props]
ref_keys = [prop.get_value_for_datastore(x) for x, prop in fields]
# Because if ref_keys has a None value,
# db.get raise a BadArgumentError exception.
for (i, v) in enumerate (ref_keys):
if v is None:
del ref_keys[i]
del fields[i]
@sahid
sahid / gist:653233
Created October 29, 2010 09:45
A Django Template tag to create or reuse all node of the template (with memcache)
import logging
from google.appengine.api import memcache
from django.conf import settings
from django import template
class HTMLListNode (template.Node):
""" Create or reuse all node of the template.
@sahid
sahid / gist:715591
Created November 25, 2010 16:22
serialize and deserialze model with GAE
# Largely inspired by: http://blog.notdot.net/2009/9/Efficient-model-memcaching
def serialize(models):
if isinstance(models, db.Model):
return db.model_to_protobuf(models).Encode()
elif isinstance(models, list):
return [serialize(x) for x in models]
elif isinstance(models, dict):
return dict([(k, serialize(x)) for k, x in models.items()])
@sahid
sahid / gist:748691
Created December 20, 2010 17:39
Async insert on Google App Engine
import os
import logging
from google.appengine.ext import db
from google.appengine.api import datastore
from google.appengine.datastore import datastore_rpc
def async_put(entities, **kwargs):
config = datastore.CreateConfig(**kwargs)
entities, _ = datastore.NormalizeAndTypeCheck(entities, db.Model)
@sahid
sahid / gist:778179
Created January 13, 2011 16:58
a safe datastore model for google appengine
import logging
import time
from google.appengine.runtime import apiproxy_errors
from google.appengine.ext import db
DATASTORE_NB_RETRY=5
DATASTORE_TIME_RETRY=.15
DATASTORE_DEFAULT_DEADLINE=5
DATASTORE_DEFAULT_CFG=db.create_config(
class CapabilityDatastore (object):
def process_request (self, request):
datastore_write = CapabilitySet('datastore_v3', capabilities=['write'])
if not datastore_write.is_enabled():
return views.common_capability(request)