I'm trying to make an employee clock-in/clock-out app but running into some 
problems. 

Here is my model...

class ClockPunch(models.Model):
    employee = models.ForeignKey(settings.AUTH_USER_MODEL)
    date = models.DateField(auto_now_add=True)
    punched_in = models.BooleanField(default=False)
    punch_in_time = models.DateTimeField(blank=True, null=True)
    punch_in_ip = models.GenericIPAddressField(blank=True, null=True)
    punched_out = models.BooleanField(default=False)
    punch_out_time = models.DateTimeField(blank=True, null=True)
    punch_out_ip = models.GenericIPAddressField(blank=True, null=True)
    hours_for_the_day = models.DecimalField(max_digits=4, decimal_places=2, 
default="0.00", blank=True, null=True)

    class Meta:
        ordering = ['date', ]

    def __str__(self):
        return self.pk


Here are my 2 views (one for punching in and one for punching out...

class ClockPunchInView(LoginRequiredMixin, CreateView):
    context_object_name = "clock_punch"
    fields = ['punch_in_time', 'punched_in',]
    model = ClockPunch
    template_name = 'pages/layout.html'
    success_url = '/'

    def form_valid(self, form):
        object = form.save(commit=False)
        object.employee = self.request.user
        object.punch_in_time = timezone.now()
        object.punched_in = True
        object.save()
        return super(ClockPunchInView, self).form_valid(form)


class ClockPunchOutView(LoginRequiredMixin, CreateView):
    context_object_name = "clock_punch"
    model = ClockPunch
    fields = ['punch_out_time', 'punched_out',]
    #form_class = PunchOutForm
    template_name = 'pages/layout.html'
    success_url = '/'

    def form_valid(self, form):
        object = form.save(commit=False)
        last_entry = 
ClockPunch.objects.filter(employee=self.request.user).order_by("-punch_in_time").first()

        if last_entry.punched_in:
            object.employee = self.request.user
            object.punch_out_time = timezone.now()
            object.punched_out = True
            object.save()
            return super(ClockPunchOutView, self).form_valid(form)
        else:
            return redirect('/')

and my form is just at the top of my navbar...

<form method='POST' action='{% url 'administration:clock_in' %}'>
      {% csrf_token %}
      <input type="submit" style="margin-right:6px;" type="button" 
class="btn btn-success" value="Clock-in"></button>
    </form>
    <form method='POST' action='{% url 'administration:clock_out' %}'>
      {% csrf_token %}
      <input type="submit" style="margin-right:6px;" type="button" 
class="btn btn-danger" value="Clock-out"></button>
    </form>


First of all, is there a better way to go about this? I had problems making 
the ClockPunchOutView an update view where I update the last entry because 
it wanted a pk for the record to update passed to it through the URL, so I 
guess I'll just make every entry a new entry even though entries for 
clocking/punching out will have a null value in all the punch-in/clock-in 
fields and vice versa.

One problem I'm running into is in my ClockPunchOutView form_valid method 
I'm trying to get the last entry so I know whether the user is punched-in 
however it acts as if last_entry.punched_in is not True, even though it is 
True. There must be a problem somewhere in my

last_entry = 
ClockPunch.objects.filter(employee=self.request.user).order_by("-punch_in_time").first()

Thanks for any help you can give

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2da8a91c-804c-40b4-bfc4-a4da911b1070%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to