Skip to content

Commit

Permalink
Partial checkin of working sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
audreyfeldroy committed Mar 20, 2012
0 parents commit 1ca94b7
Show file tree
Hide file tree
Showing 8 changed files with 951 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
*.py[co]

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox

#Translations
*.mo

#Mr Developer
.mr.developer.cfg

# GAE cruft
.Python

# Mac cruft
.DS_Store

# Private settings
private.py
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
==============================
VinylTrove Sample Project
==============================

By @audreyr.

This is a sample Google App Engine (Python 2.7) project that uses the Consumer Notebook API via OAuth2 authentication.

Running it locally
------------------

Prerequisite: Google App Engine SDK, Python 2.7 version

In your terminal::

$ git clone https://github.com/consumernotebook/vinyltrove.git
$ cd vinyltrove

Then open the GAE Launcher, import the project, and click Run. Go to http://127.0.0.1:8080 to see it running.

Live Demo
---------

An instance of this example project is running at [vinyltrove.appspot.com](http://vinyltrove.appspot.com/).
16 changes: 16 additions & 0 deletions vinyltrove/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
application: vinyltrove
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /stylesheets
static_dir: stylesheets

- url: /.*
script: main.app

libraries:
- name: jinja2
version: latest
52 changes: 52 additions & 0 deletions vinyltrove/connect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/bootstrap.min.css">
</head>
<body>
<div class="container">

<div class="row" style="margin-top:20px;">
<div class="span6">
<h1>vinyltrove</h1>
<h2>Manage your vinyl record collection</h2>

<p>Welcome, {{ nickname }}!</p>

<p>Vinyltrove is an app for tracking your vinyl record collection. It's built on top of the Consumer Notebook platform.</p>

<p>
To start using Vinyltrove, click the button:
</p>

<p>
<a class="btn btn-large btn-primary" href="http://127.0.0.1:8000/oauth2/authorize?client_id=832359a86c10a2b60268&response_type=code&redirect_uri=http://127.0.0.1:8080/manage">Connect with Consumer Notebook</a>
</p>

<div class="well">
<h3>
What happens when I connect?
</h3>
<p>
You'll be prompted to create a free Consumer Notebook account if you don't have one yet.
</p>
<p>
In your Consumer Notebook account, a list called "My Vinyl Records" will be created (unless you've already created one).
</p>
</div>
</div>
<div class="span6">
<h2>About this app</h2>
<p>
The code behind Vinyl Record Collection is open-source and runs on Google App Engine. It's a demo of the kind of app that you can build with the Consumer Notebook API.
</p>
<p>
You can download, run, and deploy this yourself: just grab the code from <a href="https://github.com/consumernotebook/cn-gae-sample/">https://github.com/consumernotebook/cn-gae-sample/</a> and follow the instructions in the README file.
</p>
<p>
For more info and code samples, see <a href="http://developers.consumernotebook.com">http://developers.consumernotebook.com</a>.
</p>
</div>
</div>
</body>
</html>
82 changes: 82 additions & 0 deletions vinyltrove/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import jinja2
import json
import os
import urllib
import urllib2
import webapp2
from google.appengine.ext import db
from google.appengine.api import users
import private

jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class Profile(db.Model):
""" A profile corresponds to a GAE user and stores associated metadata for
that user. The key is user.user_id(). """
access_token = db.StringProperty()

class ConnectPage(webapp2.RequestHandler):
""" A demo of using OAuth to connect a user's Consumer Notebook account. """
def get(self):
user = users.get_current_user()
if user:
profile = Profile.get_or_insert(user.user_id())
template_values = {
'nickname': user.nickname()
}
template = jinja_environment.get_template('connect.html')
self.response.out.write(template.render(template_values))
else:
self.redirect(users.create_login_url(self.request.uri))

class ManagePage(webapp2.RequestHandler):
""" Continuation of the OAuth demo. """
def get(self):
user = users.get_current_user()
if user:
# Get the access token (TODO: only get it if needed)
code = self.request.get('code')
url = '{0}/oauth2/access_token/'.format(private.CN_HOST)
params = {
'client_id': private.CLIENT_ID,
'client_secret': private.CLIENT_SECRET,
'grant_type': 'authorization_code',
'redirect_uri': private.REDIRECT_URI,
'code': code
}
try:
f = urllib2.urlopen(url, urllib.urlencode(params))
except urllib2.HTTPError:
f = None
access_token = None
if f:
access_token_response = f.read()
if access_token_response:
access_token_json = json.loads(access_token_response)
access_token = access_token_json[u'access_token']
profile = Profile.get_or_insert(user.user_id())
profile.access_token = access_token
profile.save()

# Use the Lists API
cn_username = 'audreyr'
records_url = '{0}/api/v1/lists/{1}/my-vinyl-records'.format(
private.CN_HOST,
cn_username)

vinyl_records = ['The Doors', 'The Dark Side of the Moon']

template_values = {
'nickname': user.nickname(),
'vinyl_records': vinyl_records
}
template = jinja_environment.get_template('manage.html')
self.response.out.write(template.render(template_values))
else:
self.redirect(users.create_login_url(self.request.uri))

app = webapp2.WSGIApplication([
('/', ConnectPage),
('/manage', ManagePage)],
debug=True)
48 changes: 48 additions & 0 deletions vinyltrove/manage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/bootstrap.min.css">
</head>
<body>
<div class="container">

<div class="row" style="margin-top:20px;">
<h1>vinyltrove</h1>
<h2>Manage your vinyl record collection</h2>

<p>Welcome, {{ nickname }}!</p>

<p><a class="btn" href="/add">Add a vinyl record</a></p>


<h2>Your collection</h2>
<p>
Here are all of your vinyl records:
</p>

<table class="table">
<thead>
<tr>
<th>Album Title</th>
<th>Your Notes</th>
</tr>
</thead>
<tbody>
{% for record in vinyl_records %}
<tr>
<td>{{ record }}</td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>

<h3>About the data</h3>

<p>
The data displayed here is fetched from your Consumer Notebook list called "My Vinyl Records".
</p>
</div>
</div>
</body>
</html>
4 changes: 4 additions & 0 deletions vinyltrove/private.py.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CN_HOST = 'https://consumernotebook.com'
CLIENT_ID = 'your-client-id-here'
CLIENT_SECRET = 'your-client-secret-here'
REDIRECT_URI = 'your-redirect-uri-here'
Loading

0 comments on commit 1ca94b7

Please sign in to comment.