Пример #1
0
 def _get_month_days(self, start, end):
     """
     calculetes worked days (from begining of month to today, from today to end of month)
     """
     today = datetime.date.today()
     days_worked = h.get_working_days(start, h.previous_day(today))
     days_left = h.get_working_days(today, end)
     return days_worked, days_left
Пример #2
0
 def _get_month_days(self, start, end):
     """
     calculetes worked days (from begining of month to today, from today to end of month)
     """
     today = datetime.date.today()
     days_worked = h.get_working_days(start, h.previous_day(today))
     days_left = h.get_working_days(today, end)
     return days_worked, days_left
Пример #3
0
    def dispatch(self):
        form = AbsenceCreateForm(self.request.POST, request=self.request)
        days, mandated, used, left = 0, 0, 0, 0
        if form.popup_date_start.data:
            mandated, used, left = user_leave(self.request,
                                              form.popup_date_start.data.year)
            if form.popup_date_end.data:
                days = h.get_working_days(form.popup_date_start.data,
                                          form.popup_date_end.data)
                left -= days
        if self.request.method == 'POST' and form.validate():
            date_start = form.popup_date_start.data
            date_end = form.popup_date_end.data
            type = form.popup_type.data
            remarks = form.popup_remarks.data
            user_id = form.popup_user_id.data
            absence = Absence(
                user_id=user_id,
                date_start=date_start,
                date_end=date_end,
                days=days,
                type=type,
                remarks=remarks,
            )
            DBSession.add(absence)
            return Response(self._('Done') + RELOAD_PAGE)

        return dict(form=form,
                    days=days,
                    mandated=mandated,
                    used=used,
                    left=left)
Пример #4
0
    def get_info(self):
        entries, sum_worked_hours, sum_bugs_worked_hours = self.get_worked_hours()
        points_achieved = self.board.points_achieved
        points = self.board.points
        total_hours = sum_worked_hours
        total_bugs_hours = sum_bugs_worked_hours

        users = []
        if self.sprint.team_id:
            users = DBSession.query(User)\
                        .filter(User.id.in_(self.sprint.team.users))\
                        .filter(User.is_active==True)\
                        .order_by(User.name).all()
        result = dict(
            start=self.sprint.start.strftime('%Y-%m-%d'),
            end=self.sprint.end.strftime('%Y-%m-%d'),
            days_remaining=h.get_working_days(datetime.date.today(), self.sprint.end),
            total_bugs = len(self.board.bugs),
            users=users,
        )
        self.sprint.commited_points = points
        self.sprint.achieved_points = points_achieved
        self.sprint.worked_hours = total_hours
        self.sprint.bugs_worked_hours = total_bugs_hours
        return result
Пример #5
0
    def get_info(self):
        entries, sum_worked_hours, sum_bugs_worked_hours = self.get_worked_hours()
        points_achieved = self.board.points_achieved
        points = self.board.points
        total_hours = sum_worked_hours
        total_bugs_hours = sum_bugs_worked_hours

        users = []
        if self.sprint.team_id:
            users = DBSession.query(User)\
                        .filter(User.id.in_(self.sprint.team.users))\
                        .filter(User.is_active==True)\
                        .order_by(User.name).all()
        result = dict(
            start=self.sprint.start.strftime('%Y-%m-%d'),
            end=self.sprint.end.strftime('%Y-%m-%d'),
            days_remaining=h.get_working_days(datetime.date.today(), self.sprint.end),
            total_bugs = len(self.board.bugs),
            users=users,
        )
        self.sprint.commited_points = points
        self.sprint.achieved_points = points_achieved
        self.sprint.worked_hours = total_hours
        self.sprint.bugs_worked_hours = total_bugs_hours
        return result
Пример #6
0
    def get(self):
        date_start = self.request.GET.get('date_start')
        date_end = self.request.GET.get('date_end')

        if date_start:
            date_start = datetime.datetime.strptime(date_start,
                                                    '%d/%m/%Y').date()
            mandated, used, left = user_leave(self.request, date_start.year)
            days = 0

            if date_end:
                type = self.request.GET.get('type')
                date_end = datetime.datetime.strptime(date_end,
                                                      '%d/%m/%Y').date()

                days = h.get_working_days(date_start, date_end)
                if days is None:
                    days = 0
                if type in ('planowany', 'zadanie'):
                    left -= days

            return dict(days=days, left=left, mandated=mandated)

        self.request.response.status = 400
        return dict(date_start='This field is required.')
Пример #7
0
 def validate_popup_type(self, field):
     if self.popup_date_start.data and self.popup_date_end.data and field.data not in (u'inne', u'okolicznosciowe', u'l4'):
         mandated, used, left = user_leave(self.request, self.popup_date_start.data.year)
         days = h.get_working_days(self.popup_date_start.data, self.popup_date_end.data)
         left -= days
         if left < 0:
             raise ValidationError(_(u'There is no leave to use, please choose Absence or conntact with Assistant'))
Пример #8
0
    def dispatch(self):
        form = AbsenceCreateForm(self.request.POST, request=self.request)
        days, mandated, used, left = 0, 0, 0, 0
        if form.popup_date_start.data:
            mandated, used, left = user_leave(self.request, form.popup_date_start.data.year)
            if form.popup_date_end.data:
                days = h.get_working_days(form.popup_date_start.data, form.popup_date_end.data)
                left -= days
        if self.request.method == 'POST' and form.validate():
            date_start = form.popup_date_start.data
            date_end = form.popup_date_end.data
            type = form.popup_type.data
            remarks = form.popup_remarks.data
            user_id = form.popup_user_id.data
            absence = Absence(
                user_id=user_id,
                date_start=date_start,
                date_end=date_end,
                days=days,
                type=type,
                remarks=remarks,
            )
            self.session.add(absence)
            return Response(self._('Done') + RELOAD_PAGE)

        return dict(
            form=form,
            days=days,
            mandated=mandated,
            used=used,
            left=left
        )
Пример #9
0
    def get(self):
        date_start = self.request.GET.get('date_start')
        date_end = self.request.GET.get('date_end')

        if date_start:
            date_start = datetime.datetime.strptime(date_start, '%d/%m/%Y').date()
            mandated, used, left = user_leave(self.request, date_start.year)
            days = 0

            if date_end:
                type = self.request.GET.get('type')
                date_end = datetime.datetime.strptime(date_end, '%d/%m/%Y').date()

                days = h.get_working_days(date_start, date_end)
                if days is None:
                    days = 0
                if type in ('planowany', 'zadanie'):
                    left -= days

            return dict(
                days=days,
                left=left,
                mandated=mandated
            )

        self.request.response.status = 400
        return dict(
            date_start='This field is required.'
        )
Пример #10
0
    def get(self):
        date_start = self.request.GET.get('date_start')
        date_end = self.request.GET.get('date_end')
        type = self.request.GET.get('type')
        days = 0
        if date_start:
            date_start = datetime.datetime.strptime(date_start, '%d/%m/%Y').date()
            mandated, used, left = user_leave(self.request, date_start.year)
            if date_end:
                date_end = datetime.datetime.strptime(date_end, '%d/%m/%Y').date()
                days = h.get_working_days(date_start, date_end)
                if days is None:
                    days = 0
                if type in ('planowany', 'zadanie'):
                    left -= days

            return dict(
                status='ok',
                days=days,
                left=left,
                mandated=mandated,
            )
        return dict(
            status='nok',
        )
Пример #11
0
 def validate_popup_type(self, field):
     if field.data == u'l4' and not self.request.user.employment_contract:
         raise ValidationError(_(u"Only user on employment contract can submit L4 absence."))
     if self.popup_date_start.data and self.popup_date_end.data and field.data not in (u'inne', u'okolicznosciowe', u'l4'):
         mandated, used, left = user_leave(self.request, self.popup_date_start.data.year)
         days = h.get_working_days(self.popup_date_start.data, self.popup_date_end.data)
         left -= days
         if left < 0:
             raise ValidationError(_(u'There is no leave to use, please choose Absence or conntact with Assistant'))
Пример #12
0
 def validate_popup_type(self, field):
     if self.popup_date_start.data and self.popup_date_end.data and field.data not in (
             u'inne', u'okolicznosciowe', u'l4'):
         mandated, used, left = user_leave(self.request,
                                           self.popup_date_start.data.year)
         days = h.get_working_days(self.popup_date_start.data,
                                   self.popup_date_end.data)
         left -= days
         if left < 0:
             raise ValidationError(
                 _(u'There is no leave to use, please choose Absence or conntact with Assistant'
                   ))
Пример #13
0
    def get_info(self):
        entries, sum_worked_hours = self.get_worked_hours()
        points_achieved = self.get_points_achieved()
        points = self.get_points()
        total_hours = sum_worked_hours

        result = dict(
            start=self.sprint.start.strftime('%Y-%m-%d'),
            end=self.sprint.end.strftime('%Y-%m-%d'),
            days_remaining=h.get_working_days(datetime.date.today(), self.sprint.end),
            total_bugs = len(self.bugs),
        )
        self.sprint.commited_points = points
        self.sprint.achieved_points = points_achieved
        self.sprint.worked_hours = total_hours
        return result
Пример #14
0
    def get_info(self):
        entries, sum_worked_hours = self.get_worked_hours()
        points_achieved = self.get_points_achieved()
        points = self.get_points()
        total_hours = sum_worked_hours

        result = dict(
            start=self.sprint.start.strftime('%Y-%m-%d'),
            end=self.sprint.end.strftime('%Y-%m-%d'),
            days_remaining=h.get_working_days(datetime.date.today(), self.sprint.end),
            total_bugs = len(self.bugs),
        )
        self.sprint.commited_points = points
        self.sprint.achieved_points = points_achieved
        self.sprint.worked_hours = total_hours
        return result
Пример #15
0
    def _get_expected(self, first_day_of_work):
        def first_day_of_next_month(date):
            next_month = h.next_month(date)
            return datetime.date(next_month.year, next_month.month, 1)

        months = []
        day = first_day_of_work
        while day < self.end:
            month_expected = h.get_working_days(day, idate.last_day_of_month(day))
            months.append(month_expected * 8)
            day = first_day_of_next_month(day)

        empty_months = len(self.months) - len(months)
        months = [0] * empty_months + months

        quarters = sum(months[0:3]), sum(months[3:])

        return quarters, months
Пример #16
0
    def _get_expected(self, first_day_of_work):
        def first_day_of_next_month(date):
            next_month = h.next_month(date)
            return datetime.date(next_month.year, next_month.month, 1)

        months = []
        day = first_day_of_work
        while day < self.end:
            month_expected = h.get_working_days(day, idate.last_day_of_month(day))
            months.append(month_expected * 8)
            day = first_day_of_next_month(day)

        empty_months = len(self.months) - len(months)
        months = [0] * empty_months + months

        quarters = sum(months[0:3]), sum(months[3:])

        return quarters, months
Пример #17
0
    def post(self):
        absence = self.request.json.get('absence')
        form = AbsentApplicationForm(MultiDict(**absence),
                                     request=self.request)
        if form.validate():
            response = {
                u'request': False,
                u'hours': False,
                u'calendar_entry': False,
            }

            memcache.clear()

            date_start = form.popup_date_start.data
            date_end = form.popup_date_end.data
            days = 0
            if date_start and date_end:
                days = h.get_working_days(date_start, date_end)
            type = form.popup_type.data
            remarks = form.popup_remarks.data
            absence = Absence(
                user_id=self.request.user.id,
                date_start=date_start,
                date_end=date_end,
                days=days,
                type=type,
                remarks=remarks,
            )

            DBSession.add(absence)
            memcache.delete(MEMCACHED_NOTIFY_KEY % date_start)

            if absence.type != 'inne':
                holidays = Holiday.all()
                date = date_start
                oneday = datetime.timedelta(days=1)
                description = self._(u'Auto Leave: ${type} - ${remarks}',
                                     type=dict(ABSENCE_TYPES)[type],
                                     remarks=remarks)
                project_id = L4_PROJECT_ID if type == u'l4' else LEAVE_PROJECT_ID

                while date <= date_end:
                    if not Holiday.is_holiday(date, holidays=holidays):
                        timeentry = TimeEntry(
                            user_id=self.request.user.id,
                            date=date,
                            time=8,
                            description=description,
                            project_id=project_id,
                        )
                        DBSession.add(timeentry)
                    date += oneday

                self._send_email(
                    absence.type,
                    date_start.strftime('%Y-%m-%d'),
                    date_end.strftime('%Y-%m-%d'),
                    days,
                    remarks,
                )

                response[u'request'] = True
                response[u'hours'] = True

            calendar = cal.Calendar(self.request.user)
            event = cal.Event(date_start, date_end + cal.oneday,
                              u'NIEOB-[%s]' % self.request.user.name, remarks)
            event_id = calendar.addEvent(event)

            response[u'calendar_entry'] = bool(event_id)

            return response

        self.request.response.status = 400
        return form.errors
Пример #18
0
    def get(self):
        month_start, month_end = self._get_month()

        entries = self.session.query('user_id', 'date', 'time', 'late_count').from_statement("""
        SELECT t.user_id as "user_id", t.date as "date", (
            SELECT COALESCE(SUM(h.time), 0.0) FROM
            time_entry h
            WHERE h.user_id = t.user_id
            AND h.date = t.date
            AND h.deleted = FALSE
        ) as "time", (
            SELECT COUNT(*)
            FROM time_entry s
            WHERE s.user_id = t.user_id
            AND s.date = t.date
            AND DATE(s.modified_ts) > s.date
        ) as "late_count"
        FROM time_entry t
        WHERE t.date >= :month_start
          AND t.date <= :month_end
        GROUP BY t.user_id, t.date;
        """).params(month_start=month_start, month_end=month_end)
        if not self.request.has_perm('view'):
            users = [self.request.user] # TODO do we need to constrain entries also?
            locations= {
                self.request.user.location: ('', 1)
            }
        else:
            users_w = User.query.filter(User.is_not_client()) \
                                .filter(User.is_active==True) \
                                .filter(User.location=='wroclaw') \
                                .order_by(User.freelancer, User.name) \
                                .all()
            users_p = User.query.filter(User.is_not_client()) \
                                .filter(User.is_active==True) \
                                .filter(User.location=='poznan') \
                                .order_by(User.freelancer, User.name) \
                                .all()
            locations = {
                'wroclaw': [u'Wrocław', len(users_w)],
                'poznan': [u'Poznań', len(users_p)],
            }
            locations[self.request.user.location][1] -= 1
            if self.request.user.location == 'wroclaw':
                users = users_w
                users.extend(users_p)
            else:
                users = users_p
                users.extend(users_w)

        today = datetime.date.today()
        grouped = defaultdict(lambda: defaultdict(lambda: 0.0))
        late = defaultdict(lambda: defaultdict(lambda: False))
        sums = defaultdict(lambda: 0.0)
        daily_sums = defaultdict(lambda: 0.0)
        for user_id, date, time, late_count in entries:
            grouped[user_id][date] = time
            if date <= today:
                sums[user_id] += time
                daily_sums[date] += time
            late[user_id][date] = late_count > 0

        holidays = Holiday.all()
        count_of_required_month_hours = {}
        count_of_required_hours_to_today = {}

        for user in users:
            sftw = user.start_full_time_work or datetime.date(1970, 1, 1)

            if sftw > month_end:
                start_work = datetime.date(today.year+10, 1, 1)
            elif sftw < month_start:
                start_work = month_start
            else:
                start_work = sftw

            count_of_required_month_hours[user.id] = h.get_working_days(start_work, month_end) * 8
            count_of_required_hours_to_today[user.id] = h.get_working_days(start_work, today if today < month_end else month_end) * 8

        # move current user to the front of the list
        current_user_index = None
        for i, user in enumerate(users):
            if user.id == self.request.user.id:
                current_user_index = i

        users.insert(0, users.pop(current_user_index))

        return dict(
            entries=grouped, users=users, sums=sums, late=late, excuses=excuses.wrongtime(),
            daily_sums=daily_sums, monthly_sum=sum(daily_sums.values()),
            dates=__builtin__.list(dates_between(month_start, month_end)),
            is_holiday=lambda date: Holiday.is_holiday(date, holidays=holidays),
            month_start=month_start,
            prev_date=previous_month(month_start),
            next_date=next_month(month_start),
            today=today,
            count_of_required_month_hours=count_of_required_month_hours,
            count_of_required_hours_to_today=count_of_required_hours_to_today,
            locations=locations,
        )
Пример #19
0
    def dispatch(self):
        form = AbsentApplicationForm(self.request.POST, request=self.request)
        days, mandated, used, left = 0, 0, 0, 0
        if form.popup_date_start.data:
            mandated, used, left = user_leave(self.request, form.popup_date_start.data.year)
            if form.popup_date_end.data:
                days = h.get_working_days(form.popup_date_start.data, form.popup_date_end.data)
                left -= days
        if self.request.method == 'POST' and form.validate():
            memcache.clear()
            response = u''
            date_start = form.popup_date_start.data
            date_end = form.popup_date_end.data
            type = form.popup_type.data
            remarks = form.popup_remarks.data
            absence = Absence(
                user_id=self.request.user.id,
                date_start=date_start,
                date_end=date_end,
                days=days,
                type=type,
                remarks=remarks,
            )
            memcache.delete(MEMCACHED_NOTIFY_KEY % date_start)
            self.session.add(absence)

            if absence.type != 'inne':
                # let's add timeentries for this leave
                holidays = Holiday.all()
                date = date_start
                oneday = datetime.timedelta(days=1)
                description = self._(u'Auto Leave: ${type} - ${remarks}',
                    type=dict(ABSENCE_TYPES)[type],
                    remarks=remarks
                )
                project_id = L4_PROJECT_ID if type == u'l4' else LEAVE_PROJECT_ID

                while date <= date_end:
                    if not Holiday.is_holiday(date, holidays=holidays):
                        timeentry = TimeEntry(
                            user_id=self.request.user.id,
                            date=date,
                            time=8,
                            description=description,
                            project_id=project_id,
                        )
                        self.session.add(timeentry)
                    date += oneday

                ## let's send email
                deferred = self._send_mail(
                    absence.type,
                    date_start.strftime('%Y-%m-%d'),
                    date_end.strftime('%Y-%m-%d'),
                    days,
                    remarks,
                )
                response += self._(u'Request added<br>Hours added<br>')

            ## and add event to calendar:
            calendar = cal.Calendar(self.request.user)
            event = cal.Event(
                date_start,
                date_end+cal.oneday,
                u'NIEOB-[%s]' % self.request.user.name,
                remarks,
            )
            event_id = calendar.addEvent(event)

            if event_id:
                response += self._(u'Calendar entry has been added')
            else:
                response += u'Calendar entry has <b class="red">NOT</b> beed added'

            return Response(response)

        return dict(
            form=form,
            days=days,
            mandated=mandated,
            used=used,
            left=left
        )
Пример #20
0
    def get(self):
        month_start, month_end = self._get_month()

        entries = (
            DBSession.query("user_id", "date", "time", "late_count")
            .from_statement(
                """
        SELECT t.user_id as "user_id", t.date as "date", (
            SELECT COALESCE(SUM(h.time), 0.0) FROM
            time_entry h
            WHERE h.user_id = t.user_id
            AND h.date = t.date
            AND h.deleted = FALSE
        ) as "time", (
            SELECT COUNT(*)
            FROM time_entry s
            WHERE s.user_id = t.user_id
            AND s.date = t.date
            AND DATE(s.modified_ts) > s.date
        ) as "late_count"
        FROM time_entry t
        WHERE t.date >= :month_start
          AND t.date <= :month_end
        GROUP BY t.user_id, t.date;
        """
            )
            .params(month_start=month_start, month_end=month_end)
        )

        if not self.request.has_perm("can_see_users_times"):
            users = [self.request.user]  # TODO do we need to constrain entries also?
            locations = {self.request.user.location: ("", 1)}
        else:
            location_names = self.request.user.get_locations()

            users = {}
            for name, (fullname, shortcut) in location_names:
                usersq = (
                    User.query.filter(User.is_not_client())
                    .filter(User.is_active == True)
                    .filter(User.location == name)
                    .order_by(User.is_freelancer(), User.name)
                )
                users[name] = usersq.all()

            locations = {name: [User.LOCATIONS[name][0], len(users)] for name, users in users.iteritems()}

            locations[self.request.user.location][1] -= 1

            ordered_users = []
            for name, (fullname, shortcut) in location_names:
                ordered_users.extend(users[name])

            users = ordered_users

        # move current user to the front of the list
        current_user_index = None
        for i, user in enumerate(users):
            if user.id == self.request.user.id:
                current_user_index = i

        users.insert(0, users.pop(current_user_index))

        today = datetime.date.today()
        grouped = defaultdict(lambda: defaultdict(lambda: 0.0))
        late = defaultdict(lambda: defaultdict(lambda: False))
        sums = defaultdict(lambda: 0.0)
        daily_sums = defaultdict(lambda: 0.0)
        for user_id, date, time, late_count in entries:
            grouped[user_id][date] = time
            if date <= today:
                sums[user_id] += time
                daily_sums[date] += time
            late[user_id][date] = late_count > 0

        holidays = Holiday.all()
        count_of_required_month_hours = {}
        count_of_required_hours_to_today = {}

        for user in users:
            sftw = user.start_full_time_work
            if sftw:
                if sftw > month_end:
                    start_work = datetime.date(today.year + 10, 1, 1)
                elif sftw < month_start:
                    start_work = month_start
                else:
                    start_work = sftw

                month_hours = h.get_working_days(start_work, month_end) * 8

                today_ = today if today < month_end else month_end
                hours_to_today = h.get_working_days(start_work, today_) * 8

                count_of_required_month_hours[user.id] = month_hours
                count_of_required_hours_to_today[user.id] = hours_to_today
            else:
                count_of_required_month_hours[user.id] = 0
                count_of_required_hours_to_today[user.id] = 0

        return dict(
            entries=grouped,
            users=users,
            sums=sums,
            late=late,
            excuses=excuses.wrongtime(),
            daily_sums=daily_sums,
            monthly_sum=sum(daily_sums.values()),
            dates=__builtin__.list(dates_between(month_start, month_end)),
            is_holiday=lambda date: Holiday.is_holiday(date, holidays=holidays),
            month_start=month_start,
            prev_date=previous_month(month_start),
            next_date=next_month(month_start),
            today=today,
            count_of_required_month_hours=count_of_required_month_hours,
            count_of_required_hours_to_today=count_of_required_hours_to_today,
            locations=locations,
        )
Пример #21
0
    def post(self):
        absence = self.request.json.get('absence')
        form = AbsentApplicationForm(MultiDict(**absence), request=self.request)
        if form.validate():
            response = {
                u'request': False,
                u'hours': False,
                u'calendar_entry': False,
            }

            memcache.clear()

            date_start = form.popup_date_start.data
            date_end = form.popup_date_end.data
            days = 0
            if date_start and date_end:
                days = h.get_working_days(date_start, date_end)
            type = form.popup_type.data
            remarks = form.popup_remarks.data
            absence = Absence(
                user_id=self.request.user.id,
                date_start=date_start,
                date_end=date_end,
                days=days,
                type=type,
                remarks=remarks,
            )

            DBSession.add(absence)
            memcache.delete(MEMCACHED_NOTIFY_KEY % date_start)

            if absence.type != 'inne':
                holidays = Holiday.all()
                date = date_start
                oneday = datetime.timedelta(days=1)
                description = self._(
                    u'Auto Leave: ${type} - ${remarks}',
                    type=dict(ABSENCE_TYPES)[type],
                    remarks=remarks
                )
                project_id = L4_PROJECT_ID if type == u'l4' else LEAVE_PROJECT_ID

                while date <= date_end:
                    if not Holiday.is_holiday(date, holidays=holidays):
                        timeentry = TimeEntry(
                            user_id=self.request.user.id,
                            date=date,
                            time=8,
                            description=description,
                            project_id=project_id,
                        )
                        DBSession.add(timeentry)
                    date += oneday

                self._send_email(
                    absence.type,
                    date_start.strftime('%Y-%m-%d'),
                    date_end.strftime('%Y-%m-%d'),
                    days,
                    remarks,
                )

                response[u'request'] = True
                response[u'hours'] = True

            calendar = cal.Calendar(self.request.user)
            event = cal.Event(
                date_start,
                date_end + cal.oneday,
                u'NIEOB-[%s]' % self.request.user.name,
                remarks
            )
            event_id = calendar.addEvent(event)

            response[u'calendar_entry'] = bool(event_id)

            return response

        self.request.response.status = 400
        return form.errors