Permalink
Browse files

Added AWSCredential model

Added view to add new AWS credentials, and list already existing credentials for current user
  • Loading branch information...
1 parent 43916c1 commit b93b09652af212d06bb972dd5147d0bcc4fc5819 @zen4ever committed Dec 31, 2010
View
@@ -1,3 +1,4 @@
#!/usr/bin/env python
from route53.models import db
+db.drop_all()
db.create_all()
View
@@ -2,3 +2,5 @@ Flask==0.6
Flask-OAuth==0.9
Flask-Principal==0.2
Flask-SQLAlchemy==0.9.1
+Flask-WTF==0.3.4
+WTForms==0.6.1
View
@@ -0,0 +1,7 @@
+from flaskext import wtf
+from flaskext.wtf import validators
+
+class AWSCredentialForm(wtf.Form):
+ nickname = wtf.TextField('Nickname', validators=[validators.Required()])
+ access_key_id = wtf.TextField('Access Key ID', validators=[validators.Required()])
+ secret_access_key = wtf.TextField('Secret Access Key', validators=[validators.Required()])
View
@@ -7,11 +7,13 @@
# Models
class User(db.Model):
- __tablename__ = 'users'
+ __tablename__ = "users"
+
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(255), unique=True)
oauth_token = db.Column(db.String(255))
oauth_token_secret = db.Column(db.String(255))
+ credentials = db.relation("AWSCredential", backref="user")
def __init__(self, username, oauth_token, oauth_token_secret):
self.username = username
@@ -22,4 +24,20 @@ def is_authenticated(self):
return True
def __repr__(self):
- return '<User %r>' % self.username
+ return "<User %r>" % self.username
+
+
+class AWSCredential(db.Model):
+ __tablename__ = "aws_credentials"
+
+ id = db.Column(db.Integer, primary_key=True)
+ nickname = db.Column(db.String(255))
+ access_key_id = db.Column(db.String(255))
+ secret_access_key = db.Column(db.String(255))
+ user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
+
+ def __init__(self, nickname, access_key_id, secret_access_key, user_id):
+ self.nickname = nickname
+ self.access_key_id = access_key_id
+ self.secret_access_key = secret_access_key
+ self.user_id = user_id
@@ -0,0 +1,10 @@
+{% macro render_field(field) %}
+ <dt>{{ field.label }}</dt>
+ <dd>{{ field(**kwargs)|safe }}
+ {% if field.errors %}
+ <ul class="errors">
+ {% for error in field.errors %}<li>{{ error }}</li>{% endfor %}
+ </ul>
+ {% endif %}
+ </dd>
+{% endmacro %}
@@ -0,0 +1,9 @@
+{% extends "base.html" %}
+
+{% block body %}
+<ul>
+{% for credential in credentials %}
+ <li>{{ credential.nickname }}</li>
+{% endfor %}
+</ul>
+{% endblock %}
@@ -0,0 +1,17 @@
+{% extends "base.html" %}
+
+{% block body %}
+<h2>Add credentials</h2>
+<form action="{{ url_for('new_credentials') }}" method="post" accept-charset="utf-8">
+ {{ form.csrf_token }}
+ {% from "_formhelpers.html" import render_field %}
+ <dl>
+ {{ render_field(form.nickname) }}
+ {{ render_field(form.access_key_id) }}
+ {{ render_field(form.secret_access_key) }}
+ </dl>
+ <p>
+ <button type="submit">Add</button>
+ </p>
+</form>
+{% endblock %}
@@ -1,5 +1,7 @@
{% extends "base.html" %}
{% block body %}
-<p>Homepage</p>
+{% if g.identity.name != 'anon' %}
+<p><a href="{{ url_for('new_credentials') }}">Add credentials</a></p>
+{% endif %}
{% endblock %}
View
@@ -5,8 +5,10 @@
identity_loaded
from route53 import app
+from route53.decorators import login_required
from route53.oauth import twitter
-from route53.models import db, User
+from route53.models import db, User, AWSCredential
+from route53.forms import AWSCredentialForm
@app.route('/')
@@ -56,6 +58,28 @@ def oauth_authorized(resp):
return redirect(next_url)
+@app.route('/credentials/new', methods=['GET', 'POST'])
+@login_required
+def new_credentials():
+ form = AWSCredentialForm()
+ if form.validate_on_submit():
+ credential = AWSCredential(nickname = form.nickname.data,
+ access_key_id = form.access_key_id.data,
+ secret_access_key = form.secret_access_key.data,
+ user_id = g.identity.user.id)
+ db.session.add(credential)
+ db.session.commit()
+ flash('Your new set of AWS credentials has been saved')
+ return redirect(url_for('credentials_list'))
+ return render_template('credentials/new.html', form=form)
+
+@app.route('/credentials/')
+@login_required
+def credentials_list():
+ credentials = g.identity.user.credentials
+ return render_template('credentials/list.html', credentials=credentials)
+
+
@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
user = User.query.filter_by(username=identity.name).first()

0 comments on commit b93b096

Please sign in to comment.