
from django.views.generic import RedirectView
from django.core.urlresolvers import reverse_lazy


from .models import BodyWeightWorkout

from braces import views


# Create your views here.
class CreateWorkout(
    views.LoginRequiredMixin,
    views.MessageMixin,
    RedirectView
):

    url = reverse_lazy('home')
    login_url = reverse_lazy('login')

    def get(self, request, *args, **kwargs):
        user = request.user
        workout = BodyWeightWorkout(user=user)
        workout.save()
        self.messages.success("New workout created!")

        return super(CreateWorkout, self).get(request, *args, **kwargs)
from datetime import datetime

from django.utils import timezone
from django.shortcuts import redirect
from django.views.generic import View
from django.core.urlresolvers import reverse_lazy

from .models import BodyWeightWorkout

from braces import views


# Create your views here.
class CreateWorkout(
    views.LoginRequiredMixin,
    views.MessageMixin,
    View
):

    url = reverse_lazy('home')
    login_url = reverse_lazy('login')

    def get(self, request, *args, **kwargs):
        now = timezone.now()
        recent_workout = list(BodyWeightWorkout.objects.filter(user=request.user.id).datetimes('created', 'day', order='DESC')[:1])
        difference = (now - recent_workout[0])
        # check to see if they already worked out today
        if difference.days == 0:
            self.messages.success("You already worked out today!")
            return redirect('home')
        else:
            user = request.user
            workout = BodyWeightWorkout(user=user)
            workout.save()
            self.messages.success("New workout created!")
            return redirect('home')

Create Workout now checks to see if you worked out once today already, if so tells user they already worked out on that day. fixed error where redirect would not re-instantiate get request to createview
Create Workout now checks to see if you worked out once today already, if so tells user they already worked out on that day.  fixed error where redirect would not re-instantiate get request to createview

+ from datetime import datetime
  
+ from django.utils import timezone
+ from django.shortcuts import redirect
- from django.views.generic import RedirectView
+ from django.views.generic import View
  from django.core.urlresolvers import reverse_lazy
- 
  
  from .models import BodyWeightWorkout
  
  from braces import views
  
  
  # Create your views here.
  class CreateWorkout(
      views.LoginRequiredMixin,
      views.MessageMixin,
-     RedirectView
+     View
  ):
  
      url = reverse_lazy('home')
      login_url = reverse_lazy('login')
  
      def get(self, request, *args, **kwargs):
+         now = timezone.now()
+         recent_workout = list(BodyWeightWorkout.objects.filter(user=request.user.id).datetimes('created', 'day', order='DESC')[:1])
+         difference = (now - recent_workout[0])
+         # check to see if they already worked out today
+         if difference.days == 0:
+             self.messages.success("You already worked out today!")
+             return redirect('home')
+         else:
-         user = request.user
+             user = request.user
-         workout = BodyWeightWorkout(user=user)
+             workout = BodyWeightWorkout(user=user)
-         workout.save()
+             workout.save()
-         self.messages.success("New workout created!")
+             self.messages.success("New workout created!")
+             return redirect('home')
  
-         return super(CreateWorkout, self).get(request, *args, **kwargs)
Create Workout now checks to see if you worked out once today already, if so tells user they already worked out on that day. fixed error where redirect would not re-instantiate get request to createview
## Code Before:

from django.views.generic import RedirectView
from django.core.urlresolvers import reverse_lazy


from .models import BodyWeightWorkout

from braces import views


# Create your views here.
class CreateWorkout(
    views.LoginRequiredMixin,
    views.MessageMixin,
    RedirectView
):

    url = reverse_lazy('home')
    login_url = reverse_lazy('login')

    def get(self, request, *args, **kwargs):
        user = request.user
        workout = BodyWeightWorkout(user=user)
        workout.save()
        self.messages.success("New workout created!")

        return super(CreateWorkout, self).get(request, *args, **kwargs)
## Instruction:
Create Workout now checks to see if you worked out once today already, if so tells user they already worked out on that day. fixed error where redirect would not re-instantiate get request to createview
## Code After:
from datetime import datetime

from django.utils import timezone
from django.shortcuts import redirect
from django.views.generic import View
from django.core.urlresolvers import reverse_lazy

from .models import BodyWeightWorkout

from braces import views


# Create your views here.
class CreateWorkout(
    views.LoginRequiredMixin,
    views.MessageMixin,
    View
):

    url = reverse_lazy('home')
    login_url = reverse_lazy('login')

    def get(self, request, *args, **kwargs):
        now = timezone.now()
        recent_workout = list(BodyWeightWorkout.objects.filter(user=request.user.id).datetimes('created', 'day', order='DESC')[:1])
        difference = (now - recent_workout[0])
        # check to see if they already worked out today
        if difference.days == 0:
            self.messages.success("You already worked out today!")
            return redirect('home')
        else:
            user = request.user
            workout = BodyWeightWorkout(user=user)
            workout.save()
            self.messages.success("New workout created!")
            return redirect('home')
