views:

126

answers:

1

I'm building an small django app in order to manage a store employees roster. The employees are freelancers-like, they have weekly almost-fixed schedules, and may ask for extra ones at any weekday/time.

I'm new to both python and django, and I'm using the django admin.

Everything works fair enough (for me) when I "manually" add a "Turno" (work assignment, I'm not sure, it probably would be "Shift" in English?).

I need some way of adding the weekly fixed Turnos (all at once, and not "manually" one by one) through the django admin, say setting a weekday, the begining and ending times, and a stop date two (or three) months in advance ... How?

Any kind of help will be great, I'm not asking to you people to make out my duty.

Here is my models.py:

from django.db import models
from django.contrib.auth.models import User
import datetime

class Dia(models.Model):
fecha = models.DateField(unique=True)

    class Meta:
        ordering = ['fecha']

    def __unicode__(self):
        return (self.fecha.strftime('%A %d de %b de \'%y'))

class Turno(models.Model):
    dia = models.ForeignKey(Dia)
    perfil_usuario = models.ForeignKey(User, verbose_name="Usuario")
    comienza = models.TimeField()
    finaliza = models.TimeField()
    comentarios = models.TextField(blank=True, null=True)

    def __unicode__(self):
        return self.dia.fecha.strftime('%A %d de %b de \'%y') + ' - ' + self.perfil_usuario.username

And here is my admin.py

from roquen.horarios.models import Turno, Dia
from django.contrib.auth.models import User
from django.contrib import admin
from django import forms

admin.site.register(Dia)

class TurnoAdminForm(forms.ModelForm):
    class Meta:
        model = Turno

    def clean_finaliza(self):
        data = self.cleaned_data['finaliza']
        if data <= self.cleaned_data['comienza']:
            raise forms.ValidationError('La hora de fin debe ser posterior a la de comienzo')
        return data

class TurnoAdmin(admin.ModelAdmin):
    form = TurnoAdminForm
    list_display = ['dia', 'perfil_usuario', 'comienza', 'finaliza']

admin.site.register(Turno, TurnoAdmin)
+1  A: 

You're actually asking a database architecture question. Your current solution has two concepts: workers and shifts such that a 'shift' is an interval of time on a specific date. This model is no longer valid.

To introduce scheduled shifts your problem domain must encapsulate two concepts for the word 'shift' and the distinction between the two is critical to the solution:

  1. A "shift" is an interval of time on a day of the week
  2. A "shift instance" is an interval of time on a specific date

So alter your models accordingly by renaming 'shift' to something which reflects the fact it occurs on a specific date. Then create a new model which reflects a shift more generically: it occurs on a day of the week, not any specific date.

Thence your workers can be associated with certain shifts which occur during the week, but are free to vary their actual shifts. To assign a worker to a specific shift on a specific day create a shift-instance.

You might create a process to periodically (each week) loop over all the shifts and create shift-instances for the coming week.

John Mee
Excellent answer, thank you. Could any of you stackoverflowers point me to something to follow in order to create the process mentioned at the end?
Pablo
You can create a Python script and run it every week via cron. See http://www.cotellese.net/2007/09/27/running-external-scripts-against-django-models/ for more info about using Django with external scripts.
myfreeweb