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.
| | | | 47 | return False | 47 | return False |
---|
48 | return True | 48 | return True |
---|
49 | | 49 | |
---|
| | 50 | def 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 | |
---|
50 | @app.before_request | 58 | @app.before_request |
---|
51 | def init_db(): | 59 | def init_db(): |
---|
52 | g.connection = Connection(app.config['DB_HOST'], app.config['DB_PORT']) | 60 | g.connection = Connection(app.config['DB_HOST'], app.config['DB_PORT']) |
---|
… | | … | |
---|
180 | @app.route('/serveUser') | 180 | @app.route('/serveUser') |
---|
181 | def serveUser(): | 181 | def serveUser(): |
---|
182 | if "logged_in" in session: | 182 | if "logged_in" in session: |
---|
183 | print session["logged_in"] | | print session["logged_in"] |
---|
| | 183 | #print session["logged_in"] | 184 | session['key'] = conf.SECRET_KEY | 184 | session['key'] = conf.SECRET_KEY |
---|
185 | return render_template('user.html') | 185 | return render_template('user.html') |
---|
186 | else: | 186 | else: |
---|
187 | return render_template('login.html', error=None) | 187 | return render_template('login.html', error=None) |
---|
188 | | 188 | |
---|
189 | @app.route('/user', methods=['POST', "GET"]) | | @app.route('/user', methods=['POST', "GET"]) |
---|
190 | def user(): | | def user(): |
---|
| | 189 | @app.route('/user/', methods=['POST', 'GET']) | | | 190 | @app.route('/user/<user_id>', methods=['GET']) |
---|
| | 191 | def user(user_id='all'): |
---|
191 | if request.method == 'POST': | 192 | if request.method == 'POST': |
---|
192 | response = make_response() | 193 | response = make_response() |
---|
193 | db = g.connection[app.config['DATABASE']] | 194 | db = g.connection[app.config['DATABASE']] |
---|
194 | collection = db['sweet_users'] | 195 | collection = db['sweet_users'] |
---|
195 | collection.insert({'user':request.form["user"],"key":request.form["key"]}) | | 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.' |
---|
196 | return response | 208 | return response |
---|
| | 209 | |
---|
197 | elif request.method == 'GET': | 210 | elif request.method == 'GET': |
---|
198 | db = g.connection[app.config['DATABASE']] | 211 | db = g.connection[app.config['DATABASE']] |
---|
199 | collection = db['sweet_users'] | 212 | collection = db['sweet_users'] |
---|
200 | users = [] | 213 | users = [] |
---|
201 | for user in collection.find(): | | for user in collection.find(): |
---|
202 | users.append(user['user']) | | 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) |
---|
203 | return render_template("users.html", users=users) | 222 | return render_template("users.html", users=users) |
---|
204 | | 223 | |
---|
205 | | 224 | |
---|
… | | … | |
---|
247 | for row in res: | 247 | for row in res: |
---|
248 | d = row | 248 | d = row |
---|
249 | d['id'] = str(row['_id']) | 249 | d['id'] = str(row['_id']) |
---|
250 | # d['text'] = row['text'] | | # d['text'] = row['text'] |
---|
251 | # d["title"] = row["title"] | | # d["title"] = row["title"] |
---|
252 | # d["user"] = row["user"] | | # d["user"] = row["user"] |
---|
| | 250 | if d['who'] in getUsers(): | | | 251 | d['registered'] = True |
---|
253 | entries.append(d) | 252 | entries.append(d) |
---|
254 | return entries | 253 | return entries |
---|
255 | | 254 | |
---|
| | | | 5 | {% for entry in entries %} | 5 | {% for entry in entries %} |
---|
6 | {% if entry.who %} | 6 | {% if entry.who %} |
---|
7 | <li> | 7 | <li> |
---|
8 | @<b>{{ entry.who }}</b> | | @<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 | |
---|
9 | <b>#{{ entry.what }}</b> /{{ entry.where }} {{ entry.how|safe }} | 16 | <b>#{{ entry.what }}</b> /{{ entry.where }} {{ entry.how|safe }} |
---|
10 | {% if entry.created|len > 0 %} | 17 | {% if entry.created|len > 0 %} |
---|
11 | <small>created: {{entry.created }} UTC</small> | 18 | <small>created: {{entry.created }} UTC</small> |
---|
| | | | 34 | {% if session.logged_in %} | 34 | {% if session.logged_in %} |
---|
35 | <div class="form-signin"> | 35 | <div class="form-signin"> |
---|
36 | <h2 class="form-signin-heading">Please create a user and assign a key</h2> | 36 | <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"> | | <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"> | | <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> | | <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> |
---|
40 | </div> | 42 | </div> |
---|
41 | {% endif %} | 43 | {% endif %} |
---|
42 | | 44 | |
---|
| | | | 4 | {% for user in users %} | 4 | {% for user in users %} |
---|
5 | <p>{{ user }}</p> | 5 | <p>{{ user }}</p> |
---|
6 | {% endfor %} | 6 | {% 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> |
---|
7 | {% endif %} | 11 | {% endif %} |
---|
8 | {% endblock %} | 12 | {% endblock %} |
---|