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