Skip to content

Instantly share code, notes, and snippets.

@spenoir
spenoir / profile_views.py
Created May 23, 2012 11:31
This is a base CBV that utilises django-activity-stream and Towel to enable user profile following. Towel makes it easy to specify one class that handles all the CRUD operations required for profile viewing and editing.
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from towel.modelview import ModelView
from actstream.models import Follow
from models import MyModel
from forms import UserProfileRegistrationForm, UserRoleSearchForm
class BaseProfileView(ModelView):
"""
@spenoir
spenoir / Page
Created August 14, 2012 14:23
A Page model that represents a webpage. It uses django-html-field(edited) to allow html content and allows for multiple images to be assigned to a page. Also allows for slug to be none in the case of homepages
from django.conf import settings
from django.db import models
from html_field.db.models import HTMLField
from html_field import html_cleaner
import re
cleaner = html_cleaner.HTMLCleaner(allow_tags=settings.ALLOWED_TAGS)
class Page(models.Model):
""" A page is the overall page that can have child pages but
@spenoir
spenoir / models.py
Created August 15, 2012 08:06
just a Content model example
class ContentImage(models.Model):
"""
An Image that is specific to Content
"""
content = models.ForeignKey('ContentModel', related_name='images')
# credit = models.CharField(max_length=200, blank=True)
# caption = models.TextField(blank=True)
image = models.ImageField(upload_to=settings.UPLOAD_PATH_CONTENTS_MEDIA)
position = models.CharField(max_length=8, choices=CONTENTS_MEDIA_POSITIONS, blank=True, null=True)
@spenoir
spenoir / pgres disconnect
Created October 4, 2013 12:30
disconnect open connections on a postgres db
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '<DB_NAME>' AND pid <> pg_backend_pid();
@spenoir
spenoir / protractor.saucelabs.config.js
Last active May 27, 2021 11:04
Protractor with Jasmine config for sauce labs
var HtmlReporter = require('protractor-html-screenshot-reporter');
exports.config = {
sauceUser: process.env.SAUCE_USER_NAME,
sauceKey: process.env.SAUCE_ACCESS_KEY,
sauceSeleniumAddress: process.env.SELENIUM_HOST + ':' + process.env.SELENIUM_PORT + '/wd/hub',
specs: [
'app/public/tests/e2e/*.js'
],
@spenoir
spenoir / gist:0edca0ec8bb94c72aefc
Last active September 16, 2015 09:47
Just adding this simple method to your false template Marionette views makes them work the way I expect
// A Marionette Collection or Composite view
...
// prevent the existing view.el from being cleared by rendering
attachHtml: function (collectionView, childView, index) {
return (typeof childView.template === 'boolean' && !childView.template) ? false : Marionette.CollectionView.prototype.attachHtml.apply(this, arguments);
},
...
@spenoir
spenoir / typeahead.spec.js
Last active October 21, 2015 14:23
typeahead test
define(['js/app', 'backbone', 'js/views/components/TypeAheadView', 'js/collections/PlaceNames',
'js/collections/localstorage/UserSearches'
],
function (App, Backbone, TypeAheadView, PlaceNames, UserSearches) {
var typeAheadView;
beforeEach(function() {
App.user = new Backbone.Model({
id: 'test.user@test.test'
});
@spenoir
spenoir / views.py
Created October 28, 2015 12:19
extract from views.py showing a base view example
class BasePageContextMixin(object):
" adds specific page rendering items to context amongst other things "
def get_context_data(self, **kwargs):
context = super(BasePageContextMixin, self).get_context_data(**kwargs)
extra_context = {
'slug': self.kwargs.get('slug'),
# 'basket': self.basket
@spenoir
spenoir / app.py
Created October 28, 2015 12:22
Tests for a simple countries json api written using Flask
import urllib2
from bson import ObjectId
from flask.ext.pymongo import MongoClient
from flask.ext.testing import LiveServerTestCase, TestCase
from nose.tools import nottest
import requests
from prototypes import app, update_es, es, User
conn = MongoClient()
@spenoir
spenoir / app.coffee
Created October 28, 2015 12:25
An example coffeescript app written using the MEAN stack, this is just a quick hack not production code really
express = require("express")
path = require("path")
favicon = require("serve-favicon")
logger = require("morgan")
cookieParser = require("cookie-parser")
bodyParser = require("body-parser")
sassMiddleware = require("node-sass-middleware")
session = require('express-session')
mongoose = require("mongoose")