Skip to content

Commit

Permalink
Squashed commit of the following: (closes NYUCCL#48)
Browse files Browse the repository at this point in the history
commit 9e2bd66
Author: Todd Gureckis <todd.gureckis@nyu.edu>
Date:   Tue Dec 10 00:24:49 2013 -0500

    hides the default custom.py from the psiturk import path  (Closes NYUCCL#48)

    kind of hacky, but works for now

commit f10b6cc
Author: Todd Gureckis <todd.gureckis@nyu.edu>
Date:   Tue Dec 10 00:15:41 2013 -0500

    boom!  this works (easier than expected).  here's a nicer example to show how

    - example custom.py script showing how to access various customizable features
    - who want to write /calculate_bonus (?)

commit 81c86ec
Author: Todd Gureckis <todd.gureckis@nyu.edu>
Date:   Mon Dec 9 23:55:39 2013 -0500

    moving authentication out as a importable module for end-users

    - users providing custom routes might want to password protect them (e.g., those providing custom views of data, etc...)
    - this provides a class-based decorator which can do this

commit 5713e0b
Author: Todd Gureckis <todd.gureckis@nyu.edu>
Date:   Mon Dec 9 23:19:39 2013 -0500

    oops, lost these files due to .gitignore

commit 06f7c7c
Author: Todd Gureckis <todd.gureckis@nyu.edu>
Date:   Mon Dec 9 23:15:33 2013 -0500

    very basic start for custom, user-provided routes (Issue NYUCCL#48)

    - changes to psiturk-setup-example
       - now create a folder for the files to live in
       - includes a default 'custom.py' showing how to add custom routes
       - files moved around so that they can be copied easier (all reside in psiturk/example now)
  • Loading branch information
gureckis committed Dec 11, 2013
1 parent 81f7c8b commit 8744e92
Show file tree
Hide file tree
Showing 34 changed files with 155 additions and 78 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
config.txt
participants.db
server.log
static
templates
psiturk-example

*.py[cod]

Expand Down Expand Up @@ -53,8 +52,6 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject
static/dashboard/js/templates/screen_off.r
static/dashboard/js/templates/explain_way.py

/server.log

Expand Down
3 changes: 1 addition & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
recursive-include psiturk/templates_example *
recursive-include psiturk/static_example *
recursive-include psiturk/example *
64 changes: 64 additions & 0 deletions psiturk/example/custom.py.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

# this file imports custom routes into the experiment server

from flask import Blueprint, render_template, request, jsonify, Response, abort
from jinja2 import TemplateNotFound
from functools import wraps
from sqlalchemy import or_

from psiturk.psiturk_config import PsiturkConfig
from psiturk.experiment_errors import ExperimentError
from psiturk.user_utils import PsiTurkAuthorization

# # Database setup
from psiturk.db import db_session, init_db
from psiturk.models import Participant


# load the configuration options
config = PsiturkConfig()
config.load_config()
myauth = PsiTurkAuthorization(config) # if you want to add a password protect route use this

# explore the Blueprint
custom_code = Blueprint('custom_code', __name__, template_folder='templates', static_folder='static')



###########################################################
# serving warm, fresh, & sweet custom, user-provided routes
# add them here
###########################################################

#----------------------------------------------
# example custom route
#----------------------------------------------
@custom_code.route('/my_custom_view')
def my_custom_view():
try:
return render_template('custom.html')
except TemplateNotFound:
abort(404)

#----------------------------------------------
# example using HTTP authentication
#----------------------------------------------
@custom_code.route('/my_password_protected_route')
@myauth.requires_auth
def my_password_protected_route():
try:
return render_template('custom.html')
except TemplateNotFound:
abort(404)

#----------------------------------------------
# example accessing data
#----------------------------------------------
@custom_code.route('/view_data')
@myauth.requires_auth
def list_my_data():
users = Participant.query.all()
try:
return render_template('list.html', participants=users)
except TemplateNotFound:
abort(404)
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions psiturk/example/templates/custom.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>

<html>
<head>
<title>Custom Routes</title>
<link rel=stylesheet href="static/css/style.css" type="text/css" media="screen">
</head>
<body>
<div style="text-align: center;" id="thanks">
<h1>Booyah, this is an example custom route you can add to your project</h1>
</div>
</body>

</html>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions psiturk/example/templates/list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Here are all your users in the database</h1>

{% for person in participants: %}
{{ person.workerid }} &nbsp; {{ person.ipaddress }}<br>

{% endfor %}
File renamed without changes.
File renamed without changes.
44 changes: 11 additions & 33 deletions psiturk/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import datetime
import logging
import urllib2
from functools import wraps
from random import choice
import json
try:
Expand Down Expand Up @@ -48,40 +47,19 @@
app = Flask("Experiment_Server")
init_db()

#----------------------------------------------
# function for authentication
#----------------------------------------------
queryname = config.get('Server Parameters', 'login_username')
querypw = config.get('Server Parameters', 'login_pw')

def wrapper(func, args):
return func(*args)

def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
return username == queryname and password == querypw
###########################################################
# serving warm, fresh, & sweet custom, user-provided routes
###########################################################

def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
try:
sys.path.append(os.getcwd())
from custom import custom_code
except ImportError:
app.logger.info( "Hmm... is seems no custom code (custom.py) assocated with this project.")
pass # do nothing if the
else:
app.register_blueprint(custom_code)

def requires_auth(f):
"""
Decorator to prompt for user name and password. Useful for data dumps, etc.
that you don't want to be public.
"""
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated

#----------------------------------------------
# favicon
Expand Down
29 changes: 17 additions & 12 deletions psiturk/setup_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
from distutils import dir_util
from psiturk_config import PsiturkConfig

static_dir = os.path.join(os.path.dirname(__file__), "static_example")
templates_dir = os.path.join(os.path.dirname(__file__), "templates_example")
example_dir = os.path.join(os.path.dirname(__file__), "example")

static_target = os.path.join(os.curdir, "static")
templates_target = os.path.join(os.curdir, "templates")
example_target = os.path.join(os.curdir, "psiturk-example")

def setup_example():
print "Copying", static_dir, "to", static_target
dir_util.copy_tree(static_dir, static_target)
print "Copying", templates_dir, "to", templates_target
dir_util.copy_tree(templates_dir, templates_target)
print "Creating default configuration file (config.txt)"
config = PsiturkConfig()
config.write_default_config()
if os.path.exists(example_target):
print "Error, `psiturk-example` directory already exists. Please remove it then re-run the command."
else:
print "Creating new folder `psiturk-example` in the current working directory"
os.mkdir(example_target)
print "Copying", example_dir, "to", example_target
dir_util.copy_tree(example_dir, example_target)
# change to target director
os.chdir(example_target)
os.rename('custom.py.txt', 'custom.py')
os.remove("./custom.pyc")
print "Creating default configuration file (config.txt)"
config = PsiturkConfig()
config.write_default_config()

if __name__=="__main__":
setup_example()
setup_example()
4 changes: 0 additions & 4 deletions psiturk/static_example/lib/backbone-min.js

This file was deleted.

6 changes: 0 additions & 6 deletions psiturk/static_example/lib/jquery-min.js

This file was deleted.

11 changes: 0 additions & 11 deletions psiturk/static_example/lib/raphael-min.js

This file was deleted.

Loading

0 comments on commit 8744e92

Please sign in to comment.