Ejemplo n.º 1
0
def event_create(request):
    if not may_manage_planning(request.user):
        raise PermissionDenied
    if request.method == 'POST':
        form = EventCreateForm(request.POST)
        if form.is_valid():
            fd = form.cleaned_data
            day = date_to_dt(fd['date'])
            e = Event({
                'name': fd['name'],
                'date': day,
                'kind': fd['template']
            })
            e.save()
            for poolname, periods in templates[fd['template']].items():
                pool = Pool.by_name(poolname)
                for period in periods:
                    begin_date = day + datetime.timedelta(seconds=period[0][0])
                    end_date = day + datetime.timedelta(seconds=period[1][0])
                    v = Vacancy({
                        'name': period[2],
                        'event': _id(e),
                        'begin': (begin_date, period[0][1]),
                        'end': (end_date, period[1][1]),
                        'pool': _id(pool),
                        'assignee': None,
                        'reminder_needed': True,
                    })
                    v.save()
            return HttpResponseRedirect(
                reverse('planning-event-edit', args=(e._id, )))
    else:
        form = EventCreateForm()
    return render(request, 'planning/event_create.html', {'form': form})
Ejemplo n.º 2
0
def event_create(request):
    if not may_manage_planning(request.user):
        raise PermissionDenied
    if request.method == 'POST':
        form = EventCreateForm(request.POST)
        if form.is_valid():
            fd = form.cleaned_data
            day = date_to_dt(fd['date'])
            e = Event({
                'name': fd['name'],
                'date': day,
                'kind': fd['template']})
            e.save()
            for poolname, periods in templates[fd['template']].items():
                pool = Pool.by_name(poolname)
                for period in periods:
                    begin_date = day + datetime.timedelta(seconds=period[0][0])
                    end_date = day + datetime.timedelta(seconds=period[1][0])
                    v = Vacancy({
                        'name': period[2],
                        'event': _id(e),
                        'begin': (begin_date, period[0][1]),
                        'end': (end_date, period[1][1]) ,
                        'pool': _id(pool),
                        'assignee': None,
                        'reminder_needed': True,
                    })
                    v.save()
            return HttpResponseRedirect(reverse('planning-event-edit',
                args=(e._id,)))
    else:
        form = EventCreateForm()
    return render_to_response('planning/event_create.html', {'form': form},
            context_instance=RequestContext(request))
Ejemplo n.º 3
0
def planning_poollist(request):
    if not may_manage_planning(request.user):
        # There's no planning you can change anyway, so what are you doing
        # here?
        raise PermissionDenied
    pools = list(Pool.all())
    return render(request, 'planning/pools.html', {'pools': pools})
Ejemplo n.º 4
0
def planning_poollist(request):
    if not may_manage_planning(request.user):
        # There's no planning you can change anyway, so what are you doing here?
        raise PermissionDenied
    pools = list(Pool.all())
    return render_to_response('planning/pools.html',
            {'pools': pools},
            context_instance=RequestContext(request))
Ejemplo n.º 5
0
def planning_poollist(request):
    if not may_manage_planning(request.user):
        # There's no planning you can change anyway, so what are you doing
        # here?
        raise PermissionDenied
    pools = list(Pool.all())
    return render(request, 'planning/pools.html',
                  {'pools': pools})
Ejemplo n.º 6
0
def event_edit(request, eventid):
    if not may_manage_planning(request.user):
        raise PermissionDenied
    avform = None
    e = Event.by_id(eventid)
    if e is None:
        raise Http404
    if request.method == 'POST':
        if request.POST['action'] == 'remove_event':
            for vacancy in e.vacancies():
                vacancy.delete()
            e.delete()
            return HttpResponseRedirect(reverse('planning-poollist'))
        elif request.POST['action'] == 'remove_vacancy':
            Vacancy.by_id(_id(request.POST['vacancy_id'])).delete()
            return HttpResponseRedirect(reverse('planning-event-edit',
                                                args=(eventid,)))
        elif request.POST['action'] == 'add_vacancy':
            avform = AddVacancyForm(request.POST)
            if avform.is_valid():
                fd = avform.cleaned_data
                (begin_hour, begin_minute) = map(int, fd['begin'].split(':'))
                (end_hour, end_minute) = map(int, fd['end'].split(':'))
                begin_offset = hm2s(begin_hour, begin_minute)
                end_offset = hm2s(end_hour, end_minute)
                begin_date = e.date + datetime.timedelta(seconds=begin_offset)
                end_date = e.date + datetime.timedelta(seconds=end_offset)
                v = Vacancy({
                    'name': fd['name'],
                    'event': _id(e),
                    'begin': (begin_date,
                              fd['begin_is_approximate'] == "True"),
                    'end': (end_date, fd['end_is_approximate'] == "True"),
                    'pool': _id(fd['pool']),
                    'assignee': None,
                    'reminder_needed': True,
                })
                v.save()
                return HttpResponseRedirect(reverse('planning-event-edit',
                                                    args=(eventid,)))
    if avform is None:
        avform = AddVacancyForm()
    pools = dict()
    for p in Pool.all():
        pools[_id(p)] = p
    vacancies = list()
    for v in e.vacancies():
        v.poolname = pools[v.pool_id].name
        v.assignee_text = str(v.assignee.name) if v.assignee else "-"
        v.vid = str(v._id)
        vacancies.append(v)
    vacancies.sort(key=lambda x: str(x.pool_id) + str(x.begin))
    return render(
        request,
        'planning/event_edit.html',
        {'name': e.name, 'kind': e.kind, 'date': e.date.date(),
         'avform': avform, 'vacancies': vacancies},
    )
Ejemplo n.º 7
0
def event_edit(request, eventid):
    if not may_manage_planning(request.user):
        raise PermissionDenied
    avform = None
    e = Event.by_id(eventid)
    if e is None:
        raise Http404
    if request.method == 'POST':
        if request.POST['action'] == 'remove_event':
            for vacancy in e.vacancies():
                vacancy.delete()
            e.delete()
            return HttpResponseRedirect(reverse('planning-poollist'))
        elif request.POST['action'] == 'remove_vacancy':
            Vacancy.by_id(_id(request.POST['vacancy_id'])).delete()
            return HttpResponseRedirect(
                reverse('planning-event-edit', args=(eventid, )))
        elif request.POST['action'] == 'add_vacancy':
            avform = AddVacancyForm(request.POST)
            if avform.is_valid():
                fd = avform.cleaned_data
                (begin_hour, begin_minute) = map(int, fd['begin'].split(':'))
                (end_hour, end_minute) = map(int, fd['end'].split(':'))
                begin_offset = hm2s(begin_hour, begin_minute)
                end_offset = hm2s(end_hour, end_minute)
                begin_date = e.date + datetime.timedelta(seconds=begin_offset)
                end_date = e.date + datetime.timedelta(seconds=end_offset)
                v = Vacancy({
                    'name':
                    fd['name'],
                    'event':
                    _id(e),
                    'begin':
                    (begin_date, fd['begin_is_approximate'] == "True"),
                    'end': (end_date, fd['end_is_approximate'] == "True"),
                    'pool':
                    _id(fd['pool']),
                    'assignee':
                    None,
                    'reminder_needed':
                    True,
                })
                v.save()
                return HttpResponseRedirect(
                    reverse('planning-event-edit', args=(eventid, )))
    if avform is None:
        avform = AddVacancyForm()
    pools = dict()
    for p in Pool.all():
        pools[_id(p)] = p
    vacancies = list()
    for v in e.vacancies():
        v.poolname = pools[v.pool_id].name
        v.assignee_text = str(v.assignee.name) if v.assignee else "-"
        v.vid = str(v._id)
        vacancies.append(v)
    vacancies.sort(key=lambda x: str(x.pool_id) + str(x.begin))
    return render(
        request,
        'planning/event_edit.html',
        {
            'name': e.name,
            'kind': e.kind,
            'date': e.date.date(),
            'avform': avform,
            'vacancies': vacancies
        },
    )
Ejemplo n.º 8
0
def planning_manage(request, poolname):
    if not may_manage_planning(request.user):
        raise PermissionDenied
    pool = Pool.by_name(poolname)
    if pool is None:
        raise Http404
    if not pool.may_manage(request.user):
        raise PermissionDenied
    # TODO reduce number of queries
    events = dict()
    for e in Event.all_in_future():
        eid = _id(e)
        vacancies = list(e.vacancies(pool=pool))
        events[eid] = {
            'vacancies': vacancies,
            'date': e.date.date(),
            'name': e.name,
            'kind': e.kind,
            'id': eid
        }
        posted = False
        events[eid]['vacancies'].sort(key=lambda v: v.begin)
        if request.method == 'POST' and _id(request.POST['eid']) == eid:
            events[eid]['form'] = ManagePlanningForm(
                request.POST, pool=pool, vacancies=events[eid]['vacancies'])
            posted = True
        else:
            events[eid]['form'] = ManagePlanningForm(
                pool=pool, vacancies=events[eid]['vacancies'])
        if posted and events[eid]['form'].is_valid():
            for vacancy in events[eid]['vacancies']:
                worker = request.POST['shift_%s' % vacancy._id]
                if worker == '':
                    vacancy.assignee = None
                    vacancy.reminder_needed = True
                else:
                    if vacancy.assignee_id is None or \
                            _id(vacancy.assignee_id) != _id(worker):
                        delta = datetime.timedelta(days=5)
                        vacancy.reminder_needed = now() + delta < e.date
                        vacancy.assignee_id = _id(worker)
                vacancy.save()
    workers = pool.workers()
    # XXX het is cooler de shift dichtstbijzijnd aan de vacancy te
    # zoeken.  Stel dat iemand over een half-jaar al is ingepland
    # dan is dat niet zo boeiend.  Terwijl hij nu geen enkele
    # bardienst meer zou krijgen
    shifts = pool.last_shifts()
    for eid in events:
        for vacancy in events[eid]['vacancies']:
            vacancy.suggestions = list()
            workers_by_score = dict()
            for worker in workers:
                score = planning_vacancy_worker_score(vacancy, worker)
                if score not in workers_by_score:
                    workers_by_score[score] = list()
                workers_by_score[score].append(worker)
            found_scores = list(workers_by_score.keys())
            found_scores.sort(reverse=True)
            for score in found_scores:
                scorers = workers_by_score[score]
                shuffle(scorers)
                scorers.sort(key=lambda x: shifts[_id(x)]
                             if shifts[_id(x)] else DT_MIN.date())
                for scorer in scorers:
                    vacancy.suggestions.append({
                        'scorer': scorer,
                        'score': score
                    })

    events = list(events.values())
    events.sort(key=lambda e: e['date'])
    return render(request, 'planning/manage.html', {
        'events': events,
        'pool': pool
    })
Ejemplo n.º 9
0
def may_manage_planning(request):
    return {'may_manage_planning': pEs.may_manage_planning(request.user)}
Ejemplo n.º 10
0
def planning_manage(request, poolname):
    if not may_manage_planning(request.user):
        raise PermissionDenied
    pool = Pool.by_name(poolname)
    if pool is None:
        raise Http404
    if not pool.may_manage(request.user):
        raise PermissionDenied
    # TODO reduce number of queries
    events = dict()
    for e in Event.all_in_future():
        eid = _id(e)
        vacancies = list(e.vacancies(pool=pool))
        events[eid] = {'vacancies': vacancies, 'date': e.date.date(),
                'name': e.name, 'kind': e.kind, 'id': eid}
        posted = False
        events[eid]['vacancies'].sort(key=lambda v: v.begin)
        if request.method == 'POST' and _id(request.POST['eid']) == eid:
            events[eid]['form'] = ManagePlanningForm(request.POST, pool=pool,
                    vacancies=events[eid]['vacancies'])
            posted = True
        else:
            events[eid]['form'] = ManagePlanningForm(pool=pool,
                    vacancies=events[eid]['vacancies'])
        if posted and events[eid]['form'].is_valid():
            for vacancy in events[eid]['vacancies']:
                worker = request.POST['shift_%s' % vacancy._id]
                if worker == '':
                    vacancy.assignee = None
                    vacancy.reminder_needed = True
                else:
                    if vacancy.assignee_id == None or \
                            _id(vacancy.assignee_id) != _id(worker):
                        delta = datetime.timedelta(days=5)
                        vacancy.reminder_needed = now() + delta < e.date
                        vacancy.assignee_id = _id(worker)
                vacancy.save()
    workers = pool.workers()
    # XXX het is cooler de shift dichtstbijzijnd aan de vacancy te
    # zoeken.  Stel dat iemand over een half-jaar al is ingepland
    # dan is dat niet zo boeiend.  Terwijl hij nu geen enkele
    # bardienst meer zou krijgen
    shifts = pool.last_shifts()
    for eid in events:
        for vacancy in events[eid]['vacancies']:
            vacancy.suggestions = list()
            workers_by_score = dict()
            for worker in workers:
                score = planning_vacancy_worker_score(vacancy,
                                    worker)
                if score not in workers_by_score:
                    workers_by_score[score] = list()
                workers_by_score[score].append(worker)
            found_scores = list(workers_by_score.keys())
            found_scores.sort(reverse=True)
            for score in found_scores:
                scorers = workers_by_score[score]
                shuffle(scorers)
                scorers.sort(key=lambda x: shifts[_id(x)], cmp=cmp_None)
                for scorer in scorers:
                    vacancy.suggestions.append({'scorer': scorer, 'score': score})

    events = list(events.values())
    events.sort(key=lambda e: e['date'])
    return render_to_response('planning/manage.html',
            {'events': events, 'pool': pool},
           context_instance=RequestContext(request))
Ejemplo n.º 11
0
def may_manage_planning(request):
    return {'may_manage_planning': pEs.may_manage_planning(request.user)}