Skip to content

Instantly share code, notes, and snippets.

// save the newAstro to the database
newAstro.save(function(err){
if (err) {
console.error("Error on saving new astronaut");
console.error(err); // log out to Terminal all errors
// rebuild the templateData page
var templateData = {
page_title : 'Enlist a new astronaut',
errors : err.errors, // include the error msg objects keys = fieldnames
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// validation function
var nameValidation = function(val) {
console.log("inside name validation");
console.log(val);
if (val.length >= 5) {
return true;
@johnschimmel
johnschimmel / evan_notes.js
Created March 4, 2013 14:40
Added some comments inline. Main idea, you probably don't need to do "var note = new Note()" looks like you can result.notes.push(noteData) to add a new note to a document. See Mongoose docs on Sub Docs - specficially Adding Sub Docs http://mongoosejs.com/docs/subdocs.html
exports.note = function(req, res) {
// get the hidden value: video.id
var videoId = req.body.videoId;
// Query for video with the matching videoId
Video.findOne({videoId:videoId}, function(err, result) {
console.log('videoId: ' + videoId);
// Prepare new note for video with the form data
var noteData = {
@johnschimmel
johnschimmel / web.js
Last active December 13, 2015 19:08
Simple NodeJS HTTP web server
// Create a HTTP server on port 8000
// Send plain text headers and 'Hello World' to each client
var http = require('http');
var port = process.env.PORT || 8000;
var counter = 0;
http.createServer(function (req, res) {
@johnschimmel
johnschimmel / tcp_echo.js
Last active December 13, 2015 19:08
Simple TCP Echo server, * Basic 'connect', 'data' and 'end' events * Client message is echoed back
/*
--------------------------------
USAGE
Start Server, navigate to code directory in Terminal:
> node tcp_echo.js
In another new Terminal window, connect to TCP Echo server
> telnet localhost 5000
@johnschimmel
johnschimmel / image_to_blobstore.py
Created February 6, 2013 23:05
AppEngine upload, combine images and save to blob store. Python PIL and PIL paste function to give uploaded image a background
# -*- coding: utf-8 -*-
"""
A real simple app for using webapp2 with auth and session.
It just covers the basics. Creating a user, login, logout
and a decorator for protecting certain handlers.
Routes are setup in routes.py and added in main.py
"""
@johnschimmel
johnschimmel / resize_w_background.py
Last active December 12, 2015 05:48
Python PIL example of taking a source image, resizing and adding a background. Will create 2 new image files, FILENAME-resized.jpg and FILENAME-background.jpg
"""
Run this script from terminal, be sure to have one image in the same directory as the script. Update the sourceFilename variable to the filename of the image.
I ran this with http://cdn.theatlanticwire.com/img/upload/2012/10/03/AP226995126585/large.jpg saved as 'running-teddy.jpg'
"""
import os
import Image
import ImageOps
@johnschimmel
johnschimmel / jsonify_route.py
Created November 6, 2012 16:12
Sample Flask route to create JSON from Dictionary
from flask import jsonify
@app.route('/data/ideas')
def data_ideas():
# query for the ideas - return oldest first, limit 10
ideas = models.Idea.objects().order_by('+timestamp').limit(10)
if ideas:
@johnschimmel
johnschimmel / app.py
Last active September 6, 2018 00:27
Get remote JSON - sample flask route
import os
from flask import Flask, request, render_template # Retrieve Flask, our framework
import requests
app = Flask(__name__) # create our flask app
# this is our main page
@app.route("/")
def index():
@johnschimmel
johnschimmel / models.py
Created October 29, 2012 23:26
updated course units field
# -*- coding: utf-8 -*-
from mongoengine import *
from flask.ext.mongoengine.wtf import model_form
from datetime import datetime
class Comment(EmbeddedDocument):
name = StringField(required=False)
comment = StringField()