Skip to content

Commit

Permalink
🐛 fix: Fixing bug in recording points with multiple users
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquesebastiao committed Feb 5, 2024
1 parent 4b6fd76 commit bea7eb3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
16 changes: 16 additions & 0 deletions timesheet/app/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Timesheet app admin configuration."""

from django.contrib import admin
from django.contrib.auth import get_user_model
from django.db.models import QuerySet

from .models import PointRecord
Expand Down Expand Up @@ -37,3 +38,18 @@ class PointRecordAdmin(admin.ModelAdmin):
def get_pdf(self, request, queryset: QuerySet):
"""Generate a PDF with the selected registers."""
return generate_pdf(queryset)

def get_form(self, request, obj=None, change=False, **kwargs):
"""Return a custom form for the admin."""
form = super().get_form(request, obj, change, **kwargs)
form.base_fields['user'].queryset = get_user_model().objects.filter(
id=request.user.id
)
return form

def get_queryset(self, request):
"""Return a QuerySet of only the logged-in user's point records."""
qs = super().get_queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(user=request.user)
5 changes: 4 additions & 1 deletion timesheet/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PointRecord(models.Model):
"""Model that represents a point record."""

user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
register_date = models.DateField(default=datetime.date.today, unique=True)
register_date = models.DateField(default=datetime.date.today)
clock_in_morning = models.TimeField(null=True, blank=True)
clock_out_morning = models.TimeField(null=True, blank=True)
clock_in_afternoon = models.TimeField(null=True, blank=True)
Expand All @@ -20,5 +20,8 @@ class PointRecord(models.Model):
is_holiday = models.BooleanField(default=False)
is_vacation = models.BooleanField(default=False)

class Meta:
unique_together = ('user', 'register_date')

def __str__(self):
return f'{self.register_date.day}-{self.register_date.month}-{self.register_date.year}'

0 comments on commit bea7eb3

Please sign in to comment.