Skip to content
This repository
Browse code

Adding "edit" functionality to the example app

  • Loading branch information...
commit 5c4fc5f639f368aef14174cfadfd160f8a5ae217 1 parent 92b4ec4
Charles Leifer authored September 17, 2012
1  example/static/style.css
@@ -14,3 +14,4 @@ h2              { font-size: 1.2em; }
14 14
                   border: 1px solid #AACBE2; }
15 15
 .avatar         { display: block; float: left; margin: 0 10px 0 0; }
16 16
 .message-content { min-height: 80px; }
  17
+.message-edit   { float: right; font-size: 0.6em; }
13  example/templates/edit.html
... ...
@@ -0,0 +1,13 @@
  1
+{% extends "base.html" %}
  2
+
  3
+{% block content_title %}Edit message{% endblock %}
  4
+
  5
+{% block content %}
  6
+  <form action="{{ url_for('edit', message_id=message.id) }}" method=post>
  7
+    <dl>
  8
+      <dt>Message:</dt>
  9
+      <dd><textarea name="content">{{ message.content }}</textarea></dd>
  10
+      <dd><input type="submit" value="Save" /></dd>
  11
+    </dl>
  12
+  </form>
  13
+{% endblock %}
1  example/templates/includes/message.html
... ...
@@ -1,2 +1,3 @@
  1
+{% if user and message.user == user %}<a class="message-edit" href="{{ url_for('edit', message_id=message.id) }}">edit</a>{% endif %}
1 2
 <a class="avatar" href="{{ url_for('user_detail', username=message.user.username) }}"><img src="{{ message.user.gravatar_url() }}" /></a>
2 3
 <p class="message-content">{{ message.content|urlize }}</p>
19  example/views.py
@@ -20,11 +20,11 @@ def homepage():
20 20
 @auth.login_required
21 21
 def private_timeline():
22 22
     user = auth.get_logged_in_user()
23  
-    
  23
+
24 24
     messages = Message.select().where(
25 25
         user__in=user.following()
26 26
     ).order_by(('pub_date', 'desc'))
27  
-    
  27
+
28 28
     return object_list('private_messages.html', messages, 'message_list')
29 29
 
30 30
 @app.route('/public/')
@@ -46,7 +46,7 @@ def join():
46 46
             )
47 47
             user.set_password(request.form['password'])
48 48
             user.save()
49  
-            
  49
+
50 50
             auth.login_user(user)
51 51
             return redirect(url_for('homepage'))
52 52
 
@@ -110,3 +110,16 @@ def create():
110 110
         return redirect(url_for('user_detail', username=user.username))
111 111
 
112 112
     return render_template('create.html')
  113
+
  114
+@app.route('/edit/<int:message_id>/', methods=['GET', 'POST'])
  115
+@auth.login_required
  116
+def edit(message_id):
  117
+    user = auth.get_logged_in_user()
  118
+    message = get_object_or_404(Message, user=user, id=message_id)
  119
+    if request.method == 'POST' and request.form['content']:
  120
+        message.content = request.form['content']
  121
+        message.save()
  122
+        flash('Your changes were saved')
  123
+        return redirect(url_for('user_detail', username=user.username))
  124
+
  125
+    return render_template('edit.html', message=message)

0 notes on commit 5c4fc5f

Please sign in to comment.
Something went wrong with that request. Please try again.