Commit 6daf1ab9bfd8686fb2c4a48d4ec6e6556a2f9fe5

Registered users get a link to profile page

  Change API and templates so that registered users in sweet store get a link
to their (empty) profile page.
swtr.py
(35 / 9)
  
4747 return False
4848 return True
4949
50def getUsers():
51 db = g.connection[app.config['DATABASE']]
52 coll = db['sweet_users']
53 users = []
54 for i in coll.find():
55 users.append(i['user'])
56 return users
57
5058@app.before_request
5159def init_db():
5260 g.connection = Connection(app.config['DB_HOST'], app.config['DB_PORT'])
180180@app.route('/serveUser')
181181def serveUser():
182182 if "logged_in" in session:
183 print session["logged_in"]
183 #print session["logged_in"]
184184 session['key'] = conf.SECRET_KEY
185185 return render_template('user.html')
186186 else:
187187 return render_template('login.html', error=None)
188188
189@app.route('/user', methods=['POST', "GET"])
190def user():
189@app.route('/user/', methods=['POST', 'GET'])
190@app.route('/user/<user_id>', methods=['GET'])
191def user(user_id='all'):
191192 if request.method == 'POST':
192193 response = make_response()
193194 db = g.connection[app.config['DATABASE']]
194195 collection = db['sweet_users']
195 collection.insert({'user':request.form["user"],"key":request.form["key"]})
196
197 # check if user already exists
198 if request.form['user'] in getUsers():
199 #print 'user already exists!'
200 flash('User already exists!')
201 return redirect(url_for('serveUser'))
202
203 # else insert new user
204 collection.insert({'user': request.form['user'],
205 'key': request.form['key']})
206 response.status_code = 200
207 response.data = 'User added.'
196208 return response
209
197210 elif request.method == 'GET':
198211 db = g.connection[app.config['DATABASE']]
199212 collection = db['sweet_users']
200213 users = []
201 for user in collection.find():
202 users.append(user['user'])
214 if user_id == 'all':
215 users = getUsers()
216 else:
217 user = collection.find_one({'user': user_id})
218 if user:
219 users.append(user['user'])
220 else:
221 abort(404)
203222 return render_template("users.html", users=users)
204223
205224
247247 for row in res:
248248 d = row
249249 d['id'] = str(row['_id'])
250 # d['text'] = row['text']
251 # d["title"] = row["title"]
252 # d["user"] = row["user"]
250 if d['who'] in getUsers():
251 d['registered'] = True
253252 entries.append(d)
254253 return entries
255254
  
55 {% for entry in entries %}
66 {% if entry.who %}
77 <li>
8 @<b>{{ entry.who }}</b>
8 {% if entry.registered %}
9 <b>
10 <a href="/user/{{ entry.who }}">@{{ entry.who }}</a>
11 </b>
12 {% else %}
13 <b>@{{ entry.who }}</b>
14 {% endif %}
15
916 <b>#{{ entry.what }}</b> /{{ entry.where }} {{ entry.how|safe }}
1017 {% if entry.created|len > 0 %}
1118 <small>created: {{entry.created }} UTC</small>
  
3434{% if session.logged_in %}
3535<div class="form-signin">
3636 <h2 class="form-signin-heading">Please create a user and assign a key</h2>
37 <input type=text id="user" name=user class="input-block-level" placeholder="Name">
38 <input id="password" class="input-block-level" type=password size=30 name=title placeholder="Password">
39 <button class="btn btn-large btn-primary" type=submit onclick="encryptAndSend();">Submit</button>
37 <form method="POST" action="/user/">
38 <input type="text" id="user" name="user" class="input-block-level" placeholder="Name">
39 <input id="password" class="input-block-level" type="password" size="30" name="key" placeholder="Password">
40 <input class="btn btn-large btn-primary" type="submit" value="Submit">
41 </form>
4042</div>
4143{% endif %}
4244
  
44 {% for user in users %}
55 <p>{{ user }}</p>
66 {% endfor %}
7{% elif users|len == 1 %}
8 <p> {{ users[0] }} </p>
9{% else %}
10 <p> You have to login as admin to view this page. </p>
711{% endif %}
812{% endblock %}