Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Edit and add exercise categories with generic views, fix some bugs

Roland Geider63e0ee5773b6
diff --git a/exercises/templates/overview.html b/exercises/templates/overview.html
index bfbd4478d..60aa8e5ed 100644
--- a/exercises/templates/overview.html
+++ b/exercises/templates/overview.html
@@ -133,7 +133,7 @@ $(document).ready(function() {
</tbody>
</table>
-<!-- <a href="{% url exercises.views.exercise_category_edit category.id %}">[e]</a> -->
+<!-- <a href="{% url exercisecategory-edit category.id %}">[e]</a> -->
</div>
{% endfor %}
</div>
@@ -160,7 +160,7 @@ $(document).ready(function() {
</a>
</p>
<p>
- <a href="{% url exercises.views.exercise_category_edit None %}"
+ <a href="{% url exercisecategory-add %}"
class="modal-dialog">
<img src="{% static 'images/icons/folder-new.svg' %}"
width="32"
diff --git a/exercises/tests/categories.py b/exercises/tests/categories.py
index 1c7a91567..8252723c0 100644
--- a/exercises/tests/categories.py
+++ b/exercises/tests/categories.py
@@ -81,8 +81,8 @@ class ExerciseCategoryTestCase(WorkoutManagerTestCase):
category = ExerciseCategory.objects.get(pk = 3)
old_name = category.name
- response = self.client.post(reverse('exercises.views.exercise_category_edit',
- kwargs={'id': 3}),
+ response = self.client.post(reverse('exercisecategory-edit',
+ kwargs={'pk': 3}),
{'name': 'A different name'})
# There is a redirect
@@ -104,8 +104,8 @@ class ExerciseCategoryTestCase(WorkoutManagerTestCase):
# No name
if not fail:
- response = self.client.post(reverse('exercises.views.exercise_category_edit',
- kwargs={'id': 3}),
+ response = self.client.post(reverse('exercisecategory-edit',
+ kwargs={'pk': 3}),
{'name': ''})
self.assertTrue(response.context['category_form'].errors['name'])
diff --git a/exercises/urls.py b/exercises/urls.py
index fc7700bc1..f96bcf313 100644
--- a/exercises/urls.py
+++ b/exercises/urls.py
@@ -3,6 +3,8 @@ from django.conf.urls import patterns, include, url
from exercises.views import ExerciseUpdateView
from exercises.views import ExerciseAddView
from exercises.views import ExerciseDeleteView
+from exercises.views import ExerciseCategoryAddView
+from exercises.views import ExerciseCategoryUpdateView
urlpatterns = patterns('exercises.views',
url(r'^exercise/overview/$', 'exercise_overview'),
@@ -20,7 +22,9 @@ urlpatterns = patterns('exercises.views',
url(r'^exercise/comment/(?P<id>\d+)/delete/$', 'exercisecomment_delete'),
# Categories
- url(r'^exercise/category/(?P<id>\d*)/edit/$', 'exercise_category_edit'),
+ url(r'^exercise/category/(?P<pk>\d+)/edit/$', ExerciseCategoryUpdateView.as_view(), name='exercisecategory-edit'),
+ url(r'^exercise/category/add/$', ExerciseCategoryAddView.as_view(), name='exercisecategory-add'),
+ #url(r'^exercise/category/(?P<id>\d*)/edit/$', 'exercise_category_edit'),
url(r'^exercise/category/(?P<id>\d*)/delete/$', 'exercise_category_delete'),
)
diff --git a/exercises/views.py b/exercises/views.py
index 8d8414663..9e58e7754 100644
--- a/exercises/views.py
+++ b/exercises/views.py
@@ -182,6 +182,7 @@ class YamlFormMixin(ModelFormMixin):
select_lists = []
static_files = []
custom_js = ''
+ form_action = ''
def get_context_data(self, **kwargs):
'''
@@ -212,6 +213,13 @@ class YamlFormMixin(ModelFormMixin):
# Custom JS code on form (autocompleter, editor, etc.)
context['custom_js'] = self.custom_js
+ # When viewing the page on it's own, this is not necessary, but when
+ # opening it on a modal dialog, we need to make sure the information
+ # reaches the correct controller
+ #logger.debug(reverse('exercises.views.exercise_overview'))
+ #logger.debug(self.object.get_absolute_url())
+ #context['form_action'] = 'self.form_action'
+ #logger.debug(context)
return context
class ExercisesUpdateView(YamlFormMixin, UpdateView):
@@ -238,7 +246,7 @@ class ExercisesDeleteView(YamlFormMixin, DeleteView):
def dispatch(self, *args, **kwargs):
return super(ExercisesDeleteView, self).dispatch(*args, **kwargs)
-
+
class ExerciseUpdateView(ExercisesUpdateView):
model = Exercise
@@ -252,7 +260,27 @@ class ExerciseUpdateView(ExercisesUpdateView):
'js/workout-manager.js']
custom_js = 'init_tinymce();'
+
+ def get_form_class(self):
+ # Define the exercise form here because only at this point during the request
+ # have we access to the currently used language. In other places Django defaults
+ # to 'en-us'.
+ class ExerciseForm(ModelForm):
+ language = load_language()
+ category = ModelChoiceField(queryset=ExerciseCategory.objects.filter(language = language.id))
+ class Meta:
+ model = Exercise
+
+ return ExerciseForm
+
+ def get_context_data(self, **kwargs):
+ context = super(ExerciseUpdateView, self).get_context_data(**kwargs)
+ context['form_action'] = reverse('exercise-edit', kwargs={'pk': self.object.id})
+
+ return context
+
+
class ExerciseAddView(ExercisesCreateView):
model = Exercise
@@ -266,11 +294,53 @@ class ExerciseAddView(ExercisesCreateView):
'js/workout-manager.js']
custom_js = 'init_tinymce();'
+
+ def get_form_class(self):
+
+ # Define the exercise form here because only at this point during the request
+ # have we access to the currently used language. In other places Django defaults
+ # to 'en-us'.
+ class ExerciseForm(ModelForm):
+ language = load_language()
+ category = ModelChoiceField(queryset=ExerciseCategory.objects.filter(language = language.id))
+ class Meta:
+ model = Exercise
+
+ return ExerciseForm
+
+
+ def get_context_data(self, **kwargs):
+ context = super(ExerciseAddView, self).get_context_data(**kwargs)
+ context['form_action'] = reverse('exercise-add')
+
+ return context
+
+
class ExerciseDeleteView(ExercisesDeleteView):
model = Exercise
+class ExerciseCategoryAddView(ExercisesCreateView):
+ model = ExerciseCategory
+ form_fields = ['name']
+
+ def get_context_data(self, **kwargs):
+ context = super(ExerciseCategoryAddView, self).get_context_data(**kwargs)
+ context['form_action'] = reverse('exercisecategory-add')
+
+ return context
+
+class ExerciseCategoryUpdateView(ExercisesUpdateView):
+ model = ExerciseCategory
+ form_fields = ['name']
+
+ def get_context_data(self, **kwargs):
+ context = super(ExerciseCategoryUpdateView, self).get_context_data(**kwargs)
+ context['form_action'] = reverse('exercisecategory-edit', kwargs={'pk': self.object.id})
+
+ return context
+
@permission_required('exercises.delete_exercise')
def exercise_delete(request, id):
# Load the exercise
@@ -279,42 +349,6 @@ def exercise_delete(request, id):
return HttpResponseRedirect(reverse('exercises.views.exercise_overview'))
-@permission_required('exercises.change_exercisecategory')
-def exercise_category_edit(request, id):
- template_data = {}
- template_data.update(csrf(request))
- template_data['active_tab'] = 'exercises'
-
- if not id:
- category = ExerciseCategory()
- else:
- category = get_object_or_404(ExerciseCategory, pk=id)
- template_data['category'] = category
-
- if request.method == 'POST':
- category_form = ExerciseCategoryForm(request.POST, instance=category)
-
- # If the data is valid, save and redirect
- if category_form.is_valid():
-
- # Load the language
- language = load_language()
-
- # Save the category
- category = category_form.save(commit=False)
- category.language = language
- category.save()
-
- return HttpResponseRedirect(reverse('exercises.views.exercise_overview'))
- else:
- category_form = ExerciseCategoryForm(instance=category)
-
- template_data['category_form'] = category_form
-
- return render_to_response('edit_category.html',
- template_data,
- context_instance=RequestContext(request))
-
@permission_required('exercises.delete_exercisecategory')
def exercise_category_delete(request, id):
# Load the category