Skip to content

Instantly share code, notes, and snippets.

View AndrewJHart's full-sized avatar
:atom:
Building react apps & micro-services

Andrew Hart AndrewJHart

:atom:
Building react apps & micro-services
View GitHub Profile
@AndrewJHart
AndrewJHart / app.js file
Created July 16, 2013 17:42 — forked from saniko/app.js file
Sample Backbone + Marionette + require-js AMD javascript app
define([
'jquery',
'underscore',
'backbone',
'marionette',
'handlebars',
'text!templates/app_view.html',
'modules/mainMenuView/mainMenuView',
var oldSync = Backbone.sync;
Backbone.sync = function(method, model, options){
options.beforeSend = function(xhr){
xhr.setRequestHeader('X-CSRFToken', CSRF_TOKEN);
};
return oldSync(method, model, options);
};
@import 'jeet'
edit()
section
center()
background red
article
col(2/4, offset: 1/4)
background orange
from django.db import models
import uuid
class UUIDField(models.CharField):
"""
A field which stores a UUID value in hex format. This may also have
the Boolean attribute 'auto' which will set the value on initial save to a
new UUID value (calculated using the UUID1 method). Note that while all
UUIDs are expected to be unique we enforce this with a DB constraint.
def save_m2m(self, bundle):
"""
Handles the saving of related M2M data.
Due to the way Django works, the M2M data must be handled after the
main instance, which is why this isn't a part of the main ``save`` bits.
Currently slightly inefficient in that it will clear out the whole
relation and recreate the related data as needed.
"""
@AndrewJHart
AndrewJHart / Backbone.sync_csrftoken.js
Created November 14, 2013 15:47 — forked from gcollazo/Backbone.sync_csrftoken.js
Set backbone to automatically set the CSRF header token for django for all ajax requests
var oldSync = Backbone.sync;
Backbone.sync = function(method, model, options){
options.beforeSend = function(xhr){
xhr.setRequestHeader('X-CSRFToken', CSRF_TOKEN);
};
return oldSync(method, model, options);
};
@AndrewJHart
AndrewJHart / resources.py
Last active December 28, 2015 09:29
Override django-tastypie hydrate_FIELD to remove/delete JSON field(s) (related field in this instance) for tastypie resource on PUT request only before its converted to object and saved.
def hydrate_device(self, bundle):
"""
override hydrate method for incoming bundle hydrate is done on a per field basis (really cool)
so this allows to override only the one (related field) and to strip the JSON data from the PUT
request thus allowing PUT request to go through successfully. NOTE: Only for PUT requests - we need
this data for POST or else the related field (device) wont be created
Issue stems from the relation - DeviceSettings has one-to-one to Device, DeviceSettings stores meta
data and other stuff about the device w/o interferring with the original device model and allowing
separation of concerns to make the Device and Service models work as standalone app for apns/gcm. Thus,
unique foreign key constraint on the Device model w/ APNService FK & due to one way relationship
@AndrewJHart
AndrewJHart / logging_settings.py
Last active January 4, 2016 15:29 — forked from palewire/settings.py
Semi-advanced and useful logging config for django. Makes for good base to replace default Django config; great for app layout skeleton
# replaces default django logging config
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
'null': {
@AndrewJHart
AndrewJHart / tastypie_ApiKey_Resource.py
Last active August 29, 2015 13:56 — forked from mkubenka/api.py
Inspiration for the ApiKey resource implementation; thanks to original author martinsandstrom.
from tastypie.exceptions import NotFound
from tastypie.resources import ModelResource
from tastypie.authentication import BasicAuthentication, ApiKeyAuthentication
from tastypie.models import ApiKey, create_api_key
from django.contrib.auth.models import User
# listen for post_save signal on User model & trigger a function to generate the API key
models.signals.post_save.connect(create_api_key, sender=User)
# callable that takes allowed methods for production but returns POST & GET verbs if testing w/ localhost or debug
var FadeTransitionRegion = Backbone.Marionette.Region.extend({
show: function(view){
this.ensureEl();
view.render();
this.close(function() {
if (this.currentView && this.currentView !== view) { return; }
this.currentView = view;