Beispiel #1
0
 def for_week(self, year, week_number):
     dates = week_dates(year, week_number)
     return self.filter(when__in=dates).order_by('when', 'span')
Beispiel #2
0
def call_duty_week(request, year=None, week=None):
    user = request.user
    if year is None or week is None:
        year, week = year_and_week()
        plan = planning.BoardWeek.current_week()
    else:
        year = int(year)
        week = int(week)
        plan = planning.BoardWeek(year, week)

    oncall_ids = [[str(oc.id) for oc in sh] for sh in plan.oncall()]
    dom_ids = plan.dom_ids()
    real_ids = dict(zip(dom_ids, plan.shift_ids()))
    oncall = dict(zip(dom_ids, oncall_ids))

    avails = plan.available()
    uids = [str(u.id) for u in avails]

    pics = []
    for pic in [u.get_profile().picture for u in avails]:
        if pic:
            pics.append("%s%s" % (settings.MEDIA_URL, pic))
        else:
            pics.append(False)
    id_pics = dict(zip(uids, pics))

    names = [u.get_full_name() for u in avails]

    initials = all_initials(avails)
    id_initials = dict(zip(uids, initials))

    disp_names = ["%s (%s)" % (name, inits) for name, inits in zip(names, initials)]
    disp_names = [htmlents(dn) for dn in disp_names]
    disp_names = [" ".join(dn.split()) for dn in disp_names]
    id_disp_names = dict(zip(uids, disp_names))

    drag_ids = ['drag-%s' % i for i in initials]
    drags = []
    for drag_id, disp_name, pic in zip(drag_ids, disp_names, pics):
        if pic:
            drags.append('<span id="%s"><img src="%s" title="%s"/></span>' % (drag_id, pic, disp_name))
        else:
            drags.append('<span id="%s">%s</span>' % (drag_id, disp_name))
    id_drags = dict(zip(uids, drags))

    if request.method == 'POST' and request.is_ajax():
        initial_users = dict(zip(initials, avails))
        dom_id_shifts = dict(zip(dom_ids, plan.shifts))
        for dom_id, shift in zip(dom_ids, plan.shifts):
            old_users = User.objects.filter(oncallduty__shift=shift).distinct()
            new_users = []
            if request.POST.has_key(dom_id):
                new_users = [initial_users[x] for x 
                        in request.POST[dom_id].split('|')]
            for old_user in old_users:
                if not old_user in new_users:
                    shift.oncallduty_set.filter(user=old_user).delete()
            for new_user in new_users:
                if not new_user in old_users:
                    o, created = baljan.models.OnCallDuty.objects.get_or_create(
                        shift=shift,
                        user=new_user
                    )
                    assert created
        messages.add_message(request, messages.SUCCESS, 
                _("Your changes were saved."))
        return HttpResponse(simplejson.dumps({'OK':True}))

    adjacent = adjacent_weeks(week_dates(year, week)[0])
    tpl = {}
    tpl['week'] = week
    tpl['year'] = year
    tpl['prev_y'] = adjacent[0][0]
    tpl['prev_w'] = adjacent[0][1]
    tpl['next_y'] = adjacent[1][0]
    tpl['next_w'] = adjacent[1][1]
    tpl['real_ids'] = simplejson.dumps(real_ids)
    tpl['oncall'] = simplejson.dumps(oncall)
    tpl['drags'] = simplejson.dumps(id_drags)
    tpl['initials'] = simplejson.dumps(id_initials)
    tpl['pictures'] = simplejson.dumps(id_pics)
    tpl['uids'] = simplejson.dumps(uids)
    return render_to_response('baljan/call_duty_week.html', tpl, 
            context_instance=RequestContext(request))