Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Allow in-line editing of exercise comments

Roland Geider612dbadaae78
diff --git a/manager/templates/exercise/view.html b/manager/templates/exercise/view.html
index 6becd9992..9effb479b 100644
--- a/manager/templates/exercise/view.html
+++ b/manager/templates/exercise/view.html
@@ -26,10 +26,23 @@
</form>
<p><em>Form to add comment goes here</em></p>
-
<ul>
+
{% for comment in exercise.exercisecomment_set.all %}
- <li>{{ comment }}</li>
+ <li>
+ {% if comment_edit.id == comment.id %}
+ <form action="/exercise/view/{{ exercise.id }}/edit/comment/{{ comment.id }}" method="post">
+ {% csrf_token %}
+ {{ comment_edit_form.as_p }}
+ <input type="submit" value="Submit" />
+ </form>
+ {% else %}
+ {{ comment.id }}
+ <a href="/exercise/comment/delete/{{ comment.id }}" title="{% trans "Delete" %}">[x]</a>
+ <a href="/exercise/view/{{ exercise.id }}/edit/comment/{{ comment.id }}" title="{% trans "Edit" %}">[e]</a>
+ {{ comment }}
+ {% endif %}
+ </li>
{% empty %}
<li>{% trans "Sorry, no comments for this exercise yet!" %}</li>
{% endfor %}
diff --git a/manager/urls.py b/manager/urls.py
index 382bf1c85..6b740d32c 100644
--- a/manager/urls.py
+++ b/manager/urls.py
@@ -9,6 +9,8 @@ urlpatterns = patterns('manager.views',
url(r'^workout/(?P<id>\d+)/view/$', 'view_workout'),
url(r'^exercise/overview/$', 'exercise_overview'),
url(r'^exercise/view/(?P<id>\d+)$', 'exercise_view'),
+ url(r'^exercise/view/(?P<id>\d+)/edit/comment/(?P<comment_id>\d+)$', 'exercise_view'),
url(r'^exercise/edit/(?P<id>\d+)$', 'exercise_edit'),
+ url(r'^exercise/comment/delete/(?P<id>\d+)$', 'exercisecomment_delete'),
)
\ No newline at end of file
diff --git a/manager/views.py b/manager/views.py
index 19b48ab5e..5b5605143 100644
--- a/manager/views.py
+++ b/manager/views.py
@@ -81,7 +81,19 @@ def add_step_4(request):
return render_to_response('add.html', template_data)
+# ************************
+# Exercise comment functions
+# ************************
+def exercisecomment_delete(request, id):
+ # Load the exercise itself
+ comment = get_object_or_404(ExerciseComment, pk=id)
+ exercise_id = comment.exercise.id
+ comment.delete()
+
+ return HttpResponseRedirect('/exercise/view/%s' % exercise_id)
+
+
# ************************
# Exercise functions
@@ -113,22 +125,24 @@ def exercise_overview(request):
return render_to_response('exercise/overview.html', template_data)
-def exercise_view(request, id):
+def exercise_view(request, id, comment_id=None):
""" Detail view for an exercise
"""
template_data = {}
+ template_data['comment_edit'] = False
# Load the exercise itself
exercise = get_object_or_404(Exercise, pk=id)
template_data['exercise'] = exercise
- # We can create comments from this page, so look for Posts
+ #
+ # We can create and edit comments from this page, so look for Posts
template_data.update(csrf(request))
- if request.method == 'POST':
-
+
+ # Adding a new comment
+ if request.method == 'POST' and not comment_id:
comment_form = ExerciseCommentForm(request.POST)
comment_form.exercise = exercise
- #logger.debug(comment_form)
#logger.debug(comment_form.errors)
new_comment = comment_form.save(commit=False)
new_comment.exercise = exercise
@@ -136,6 +150,22 @@ def exercise_view(request, id):
return HttpResponseRedirect('/exercise/view/%s' % id)
else:
comment_form = ExerciseCommentForm()
+
+ # Editing a comment
+ if comment_id:
+ exercise_comment = get_object_or_404(ExerciseComment, pk=comment_id)
+ template_data['comment_edit'] = exercise_comment
+
+ comment_edit_form = ExerciseCommentForm(instance=exercise_comment)
+ template_data['comment_edit_form'] = comment_edit_form
+
+ if request.method == 'POST':
+ logger.debug(exercise_comment.id)
+ comment_form = ExerciseCommentForm(request.POST, instance=exercise_comment)
+ comment = comment_form.save(commit=False)
+ comment.save()
+ return HttpResponseRedirect('/exercise/view/%s' % id)
+
template_data['comment_form'] = comment_form