Skip to content

Instantly share code, notes, and snippets.

@mikesname
mikesname / horrible.py
Created February 20, 2012 21:11
SQLAlchemy, dynamic table creation
def init_i18n_class(cls):
"""Create, via metadata reflection, an I18N class object
for i18n-enabled tables."""
tablename = cls.__tablename__ + "_i18n"
if Base.metadata.tables.get(tablename) is not None:
classname = cls.__name__ + "I18N"
i18nt = Table(tablename, Base.metadata,
Column("id", ForeignKey("%s.id" % cls.__tablename__), primary_key=True),
Column("culture", String(25), primary_key=True),
ForeignKeyConstraint(["id"], ["%s.id" % cls.__tablename__]),
@mikesname
mikesname / gist:3635588
Created September 5, 2012 11:57
Create DocumentaryUnit with Repository and User params...
// Create a doc unit with a repository and a user...
// Glosses over lots of potential errors!
public void createDocumentaryUnit(Long repositoryId, Long userId,
Map<String, Object> data) throws ValidationError,
DeserializationError, PermissionDenied {
// Load the repository this collection will be part of...
Agent repo = graph.getVertex(repositoryId, Agent.class);
@mikesname
mikesname / MyProxyController.scala
Created December 4, 2012 19:20
Basic web proxy with Play 2.0 - example only and not for actual use...
/**
app/controllers/MyProxyController.scala
*/
package controllers
// Basic proxying web service...
// Standard play libs - all the Scala stuff
// is in the play.api package so it doesn't
// conflict with the Java equivalents.
@mikesname
mikesname / gist:4383736
Last active December 10, 2015 04:48
Blueprints nested transaction handling, contrived example.
import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.neo4j.Neo4jGraph;
public class BlueprintsTest {
private final String[] testData = {
"foo",
"bar",
@mikesname
mikesname / gist:4383741
Last active December 10, 2015 04:48
Neo4j transaction handling, contrived example...
import org.neo4j.graphdb.Transaction;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.neo4j.Neo4jGraph;
public class Neo4jTest {
private final String[] testData = {
"foo",
@mikesname
mikesname / gist:4594442
Last active December 11, 2015 11:29
Attempt to bodge together a Play 2.0 CLI management command... the file was bunged in the test package in order to have the running() and FakeApplication() utils available. It was executed with something like: play "run-main test.XMLtoGeoff"
package test
import play.api.test.Helpers.running
import play.api.test.FakeApplication
import models.Repository
import scala.io.Source
import controllers.Collections.processSource
import importers.USHMM
@mikesname
mikesname / gist:5237809
Last active December 15, 2021 23:10
Example Play JSON Enum reading/writing
package enumtest
import play.api.libs.json._
object EnumUtils {
def enumReads[E <: Enumeration](enum: E): Reads[E#Value] = new Reads[E#Value] {
def reads(json: JsValue): JsResult[E#Value] = json match {
case JsString(s) => {
@mikesname
mikesname / GraphGist-SimpleRBAC.adoc
Last active March 22, 2022 15:40
Very simplistic way of doing role-based access control (RBAC) with Neo4j.

This is a very simple approach to doing role-based access control with Neo4j. It is optimistic, in the sense that all items are assumed to be world-readable unless they have specific constraints. Item visibility can be constrained to either individual users or all users who belong to a role. Roles are also hierarchical, so can inherit privileges from other roles.

First, lets create our basic example data:

@mikesname
mikesname / gist:8383440
Last active January 3, 2016 00:29
Basic Play optional authenticated action.
import play.api.mvc._
case class Context(email: Option[String])
class AuthenticatedRequest[A](val context: Context, request: Request[A]) extends WrappedRequest[A](request)
object MaybeAuthenticated extends ActionBuilder[AuthenticatedRequest] {
def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[SimpleResult]) = {
request.session.get("email").map { email =>
block(new AuthenticatedRequest(Context(Some(email)), request))
@mikesname
mikesname / gist:9027468
Last active August 29, 2015 13:56
Stackoverflow: How to replace all the values with the same key in a JSON tree
// Reply to: http://stackoverflow.com/questions/21801071/how-to-replace-all-the-values-with-the-same-key-in-a-json-tree/21804784#comment32996624_21804784
/**
* If the object matches an $oid, return the value and the new key
* Otherwise, return the original value
*/
def idOrValue(key: String, js: JsValue): (String, JsValue) = js \ "$oid" match {
case JsUndefined() => key -> js
case v => key -> v
}