Self hosted FLOSS fitness/workout, nutrition and weight tracker
Replace the edit and add methods with generic views
Roland Geider853517bcf9c1
diff --git a/manager/static/js/workout-manager.js b/manager/static/js/workout-manager.jsindex ba526f6b9..1b463c351 100644--- a/manager/static/js/workout-manager.js+++ b/manager/static/js/workout-manager.js@@ -243,7 +243,7 @@ function form_modal_dialog()function scatterplot_modal_dialog(id){- var targetUrl = '/weight/add/' + id+ var targetUrl = '/weight/edit/' + id + '/'$("#ajax-info").load(targetUrl + " .ym-form", function() {// Open the dialogdiff --git a/weight/templates/weight_overview.html b/weight/templates/weight_overview.htmlindex 077a57e26..ad63b5baa 100644--- a/weight/templates/weight_overview.html+++ b/weight/templates/weight_overview.html@@ -90,7 +90,7 @@ $(document).ready(function () {<div id="weight_diagram"></div><p>-<a href="{% url weight.views.add None %}"+<a href="{% url weight-add %}"class="modal-dialog"id="add-weight-entry">{% trans "Add weight entry" %}</a></p>diff --git a/weight/urls.py b/weight/urls.pyindex a4329312d..f8f51f083 100644--- a/weight/urls.py+++ b/weight/urls.py@@ -1,7 +1,19 @@from django.conf.urls import patterns, include, url+from django.contrib.auth.decorators import login_required++from weight.views import WeightAddView+from weight.views import WeightUpdateViewurlpatterns = patterns('weight.views',- url(r'^weight/add/(?P<id>\w*)$', 'add'),++ url(r'^weight/add/$',+ login_required(WeightAddView.as_view()),+ name = 'weight-add'),++ url(r'^weight/(?P<pk>\d+)/edit/$',+ login_required(WeightUpdateView.as_view()),+ name = 'weight-edit'),+url(r'^weight/export_csv/$', 'export_csv'),url(r'^weight/overview/$', 'overview'),url(r'^weight/api/get_weight_data/$', 'get_weight_data'),diff --git a/weight/views.py b/weight/views.pyindex c3ea98b00..5f93254d0 100644--- a/weight/views.py+++ b/weight/views.py@@ -26,13 +26,21 @@ from django.http import HttpResponseRedirectfrom django.forms import ModelFormfrom django.core.context_processors import csrffrom django.core.urlresolvers import reverse+from django.core.urlresolvers import reverse_lazyfrom django.contrib.auth.decorators import login_requiredfrom django.utils.translation import ugettext as _+from django.utils.translation import ugettext_lazyfrom django.db.models import Minfrom django.db.models import Max+from django.views.generic import DeleteView+from django.views.generic import CreateView+from django.views.generic import UpdateView+from weight.models import WeightEntry+from workout_manager.generic_views import YamlFormMixin+logger = logging.getLogger('workout_manager.custom')@@ -41,47 +49,49 @@ class WeightForm(ModelForm):model = WeightEntryexclude=('user',)-@login_required-def add(request, id=None):- """Add a weight entry+class WeightAddView(YamlFormMixin, CreateView):"""-- template_data = {}- template_data.update(csrf(request))- template_data['active_tab'] = 'weight'-- # Load weight-- # If the object is new, we will receice a 'None' (string) as the ID- # from the template, so we check for it (ValueError) and for an actual- # None (TypeError)- try:- int(id)- weight = get_object_or_404(WeightEntry, pk=id, user=request.user)-- except ValueError, TypeError:- weight = WeightEntry()-- template_data['weight'] = weight-- # Process request- if request.method == 'POST':- weight_form = WeightForm(request.POST, instance=weight)+ Generic view to add a new weight entry+ """+ active_tab = 'weight'+ model = WeightEntry+ form_class = WeightForm+ custom_js = '''$(document).ready(function () {+ init_weight_datepicker();+ });'''+ title = ugettext_lazy('Add weight entry')+ form_action = reverse_lazy('weight-add')+ success_url = reverse_lazy('weight.views.overview')+++ def form_valid(self, form):+ """+ Set the owner of the entry here+ """- # If the data is valid, save and redirect- if weight_form.is_valid():- weight = weight_form.save(commit=False)- weight.user = request.user- weight.save()-- return HttpResponseRedirect(reverse('weight.views.overview'))- else:- weight_form = WeightForm(instance=weight)- template_data['weight_form'] = weight_form-- return render_to_response('edit.html',- template_data,- context_instance=RequestContext(request))+ form.instance.user = self.request.user+ return super(WeightAddView, self).form_valid(form)+++class WeightUpdateView(YamlFormMixin, UpdateView):+ """+ Generic view to edit an existing weight entry+ """+ active_tab = 'weight'+ model = WeightEntry+ form_class = WeightForm+ custom_js = '''$(document).ready(function () {+ init_weight_datepicker();+ });'''+ success_url = reverse_lazy('weight.views.overview')+++ def get_context_data(self, **kwargs):+ context = super(WeightUpdateView, self).get_context_data(**kwargs)+ context['form_action'] = reverse('weight-edit', kwargs={'pk': self.object.id})+ context['title'] = _('Edit weight entry for the %s') % self.object.creation_date++ return context@login_requireddef export_csv(request):