예제 #1
0
def start_work(user_id, los_id, work_date, wort_start_time):
    qs = WorkTime.objects.filter(list_of_staff__user=user_id, list_of_staff_id=los_id, from_date=work_date)
    if qs.count() == 0:
        wt = WorkTime(
                from_date = work_date,
                from_time = wort_start_time,
                to_date = None,
                to_time = None,
                list_of_staff_id = los_id,
                reason_id = REASON_HOUR_WORK_ID,
        )
        wt.save()
        return wt.pk
    else:
        return qs[0].pk
예제 #2
0
def index(request):
    user = request.user
    context = {"user": user}
    contracts = user.contract_set.all()
    now = datetime.now()
    month = now.month
    year = now.year
    context['year'] = year
    context['month'] = month
    if request.method == 'GET':
        if request.GET.get('month') and request.GET.get('year'):
            if int(request.GET['month']) > 12 or int(request.GET['month']) < 1:
                raise ValidationError("Invalid month.")
            if int(request.GET['year']) > year + 2 or int(request.GET['year']) < year - 2:
                raise ValidationError("Invalid year.")
            month = int(request.GET['month'])
            year = int(request.GET['year'])
            context['year'] = year
            context['month'] = month
    if request.method == 'POST':
        if request.POST.get('month') and request.POST.get('year'):
            if int(request.POST['month']) > 12 or int(request.POST['month']) < 1:
                raise ValidationError("Invalid month.")
            if int(request.POST['year']) > year + 2 or int(request.POST['year']) < year - 2:
                raise ValidationError("Invalid year.")
            month = int(request.POST['month'])
            year = int(request.POST['year'])
            context['year'] = year
            context['month'] = month
        if request.POST.get("contract_id"):
            try:
                contract = Contract.objects.get(id=request.POST["contract_id"])
                wt = WorkTime()
                wt.activity = request.POST['activity']
                wt.pause = request.POST['pause']
                if not wt.pause:
                    wt.pause = 0
                start = request.POST['date'] + " " + request.POST['start']
                startpattern = "%Y-%m-%d %H:%M"
                if re.match(r'^([0-9]{2}|[0-9])$', request.POST['start']):
                    startpattern = "%Y-%m-%d %H"
                start = datetime.strptime(start, startpattern)
                endpattern = "%Y-%m-%d %H:%M"
                if re.match(r'^([0-9]{2}|[0-9])$', request.POST['end']):
                    endpattern = "%Y-%m-%d %H"
                end = request.POST['date'] + " " + request.POST['end']
                end = datetime.strptime(end, endpattern)
                year = start.year
                month = start.month
                wLog = WorkLog.objects.get(contract=contract, month=month, year=year)
                wt.work_log = wLog
                wt.end = end
                wt.begin = start
                wt.clean_fields(year, month)
                wt.save()
            except ObjectDoesNotExist as v:
                context['error'] = [v.message]
            except ValueError as v:
                context['error'] = [v.message]
            except ValidationError as v:
                context['error'] = v.messages
            context['post'] = 'y'
            context['posted_contract'] = int(request.POST['contract_id'])
            context['postdata'] = request.POST
    month = context['month']
    year = context['year']
    ctracs = []
    for c in contracts:
        if c.contract_begin.year > year or \
                        c.contract_end.year < year or \
                (c.contract_begin.year == year and c.contract_begin.month > month) or \
                (c.contract_end.year == year and c.contract_end.month < month):
            continue

        workL = getWorkLog(c, month, year)
        workSum = workL.calcHours()
        c.cw = workL
        c.cSum = workSum
        c.partVac = int(round(workL.contract.vacation / 12.0))
        if c.hours * 1.5 - workSum <= c.hours * 1.5 - c.hours:
            c.critSum = True
        ctracs.append(c)
    context['contracts'] = ctracs
    years = []
    for i in range(-2, 3):
        years.append(datetime.now().year + i)
    context['years'] = years
    return render(request, 'hiwi_portal.html', context)
예제 #3
0
def wd_manage_apply(request, month, year, contract):
    c = Contract.objects.get(id=int(contract), user=request.user)
    month = int(month)
    year = int(year)
    firstDayOfMonth = datetime(year, month, 1, 0, 0, 1, 0).weekday()
    daysInMonth = monthrange(year, month)
    workL = WorkLog.objects.get(contract=c, month=month, year=year)
    # First try apply all anual activities
    anuals = c.fixedworkdustactivity_set.all()
    for a in anuals:
        if a.week_day > firstDayOfMonth:
            anualStep = 1 + a.week_day - firstDayOfMonth
        elif a.week_day == firstDayOfMonth:
            anualStep = 1
        else:
            anualStep = 1 + 7 - firstDayOfMonth + a.week_day
        while anualStep <= daysInMonth[1] and workL.calcHours() + a.avg_length <= c.hours:
            wt = WorkTime()
            wt.work_log = workL
            if a.avg_length >= 6:
                wt.pause = 1
            else:
                wt.pause = 0
            wt.begin = datetime(year, month, anualStep, a.start.hour, a.start.minute, 0, 0)
            beginstamp = (wt.begin - datetime(1970, 1, 1)).total_seconds()
            wt.end = datetime.fromtimestamp(beginstamp +
                                            a.avg_length * 60.0*60.0 + wt.pause * 60.0*60.0)
            # wt.end = wt.begin.replace(hour=int(wt.begin.hour + math.floor(a.avg_length) + wt.pause))
            # wt.end = wt.end.replace(minute=int(round((a.avg_length - math.floor(a.avg_length)) * 60)))
            wt.activity = a.description
            wt.clean_fields(year, month)
            wt.save()
            anualStep += 7
    # Then fill with "other" activities
    filler = FillerWorkDustActivity.objects.all()
    largestFreeSlot = 0
    smallestFiller = filler.aggregate(Min('avg_length'))['avg_length__min']

    while not smallestFiller == None and largestFreeSlot >= smallestFiller:
        pass
    return redirect("/?month=" + str(month) + "&year=" + str(year) + "#" + str(c.id))
예제 #4
0
def index(request):
    user = request.user
    context = {"user": user}
    contracts = user.contract_set.all()
    now = datetime.now()
    month = now.month
    year = now.year
    context['year'] = year
    context['month'] = month
    if request.method == 'GET':
        if request.GET.get('month') and request.GET.get('year'):
            if int(request.GET['month']) > 12 or int(request.GET['month']) < 1:
                raise ValidationError("Invalid month.")
            if int(request.GET['year']) > year + 2 or int(
                    request.GET['year']) < year - 2:
                raise ValidationError("Invalid year.")
            month = int(request.GET['month'])
            year = int(request.GET['year'])
            context['year'] = year
            context['month'] = month
    if request.method == 'POST':
        if request.POST.get('month') and request.POST.get('year'):
            if int(request.POST['month']) > 12 or int(
                    request.POST['month']) < 1:
                raise ValidationError("Invalid month.")
            if int(request.POST['year']) > year + 2 or int(
                    request.POST['year']) < year - 2:
                raise ValidationError("Invalid year.")
            month = int(request.POST['month'])
            year = int(request.POST['year'])
            context['year'] = year
            context['month'] = month
        if request.POST.get("contract_id"):
            try:
                contract = Contract.objects.get(id=request.POST["contract_id"])
                wt = WorkTime()
                wt.activity = request.POST['activity']
                wt.pause = request.POST['pause']
                if not wt.pause:
                    wt.pause = 0
                start = request.POST['date'] + " " + request.POST['start']
                startpattern = "%Y-%m-%d %H:%M"
                if re.match(r'^([0-9]{2}|[0-9])$', request.POST['start']):
                    startpattern = "%Y-%m-%d %H"
                start = datetime.strptime(start, startpattern)
                endpattern = "%Y-%m-%d %H:%M"
                if re.match(r'^([0-9]{2}|[0-9])$', request.POST['end']):
                    endpattern = "%Y-%m-%d %H"
                end = request.POST['date'] + " " + request.POST['end']
                end = datetime.strptime(end, endpattern)
                year = start.year
                month = start.month
                wLog = WorkLog.objects.get(contract=contract,
                                           month=month,
                                           year=year)
                wt.work_log = wLog
                wt.end = end
                wt.begin = start
                wt.clean_fields(year, month)
                wt.save()
            except ObjectDoesNotExist as v:
                context['error'] = [v.message]
            except ValueError as v:
                context['error'] = [v.message]
            except ValidationError as v:
                context['error'] = v.messages
            context['post'] = 'y'
            context['posted_contract'] = int(request.POST['contract_id'])
            context['postdata'] = request.POST
    month = context['month']
    year = context['year']
    ctracs = []
    for c in contracts:
        if c.contract_begin.year > year or \
                        c.contract_end.year < year or \
                (c.contract_begin.year == year and c.contract_begin.month > month) or \
                (c.contract_end.year == year and c.contract_end.month < month):
            continue

        workL = getWorkLog(c, month, year)
        workSum = workL.calcHours()
        c.cw = workL
        c.cSum = workSum
        c.percent = (workSum / c.hours) * 100.0
        c.partVac = int(round(workL.contract.vacation / 12.0))
        if c.hours * 1.5 - workSum <= c.hours * 1.5 - c.hours:
            c.critSum = True
        ctracs.append(c)
    context['contracts'] = ctracs
    years = []
    for i in range(-2, 3):
        years.append(datetime.now().year + i)
    context['years'] = years
    return render(request, 'hiwi_portal.html', context)
예제 #5
0
def wd_manage_apply(request, month, year, contract):
    c = Contract.objects.get(id=int(contract), user=request.user)
    month = int(month)
    year = int(year)
    firstDayOfMonth = datetime(year, month, 1, 0, 0, 1, 0).weekday()
    daysInMonth = monthrange(year, month)
    workL = WorkLog.objects.get(contract=c, month=month, year=year)
    # First try apply all anual activities
    anuals = c.fixedworkdustactivity_set.all()
    for a in anuals:
        if a.week_day > firstDayOfMonth:
            anualStep = 1 + a.week_day - firstDayOfMonth
        elif a.week_day == firstDayOfMonth:
            anualStep = 1
        else:
            anualStep = 1 + 7 - firstDayOfMonth + a.week_day
        while anualStep <= daysInMonth[1] and workL.calcHours(
        ) + a.avg_length <= c.hours:
            wt = WorkTime()
            wt.work_log = workL
            if a.avg_length >= 6:
                wt.pause = 1
            else:
                wt.pause = 0
            wt.begin = datetime(year, month, anualStep, a.start.hour,
                                a.start.minute, 0, 0)
            beginstamp = (wt.begin - datetime(1970, 1, 1)).total_seconds()
            wt.end = datetime.fromtimestamp(beginstamp +
                                            a.avg_length * 60.0 * 60.0 +
                                            wt.pause * 60.0 * 60.0)
            # wt.end = wt.begin.replace(hour=int(wt.begin.hour + math.floor(a.avg_length) + wt.pause))
            # wt.end = wt.end.replace(minute=int(round((a.avg_length - math.floor(a.avg_length)) * 60)))
            wt.activity = a.description
            wt.clean_fields(year, month)
            wt.save()
            anualStep += 7
    # Then fill with "other" activities
    filler = FillerWorkDustActivity.objects.all()
    largestFreeSlot = 0
    smallestFiller = filler.aggregate(Min('avg_length'))['avg_length__min']

    while not smallestFiller == None and largestFreeSlot >= smallestFiller:
        pass
    return redirect("/?month=" + str(month) + "&year=" + str(year) + "#" +
                    str(c.id))