Beispiel #1
0
    def get(self):
        user_id = self.request.GET.get('user_id')
        month_start, month_end = self._get_month()
        user = User.query.filter(User.id == user_id).one()

        query = DBSession.query('date', 'incorrect_count').from_statement("""
            SELECT  date, COUNT(date) as incorrect_count
            FROM time_entry s
            WHERE DATE(s.modified_ts) > s.date AND
                  s.user_id = :user_id AND
                  s.date >= :month_start AND
                  s.date <= :month_end
            GROUP BY date
        """).params(month_start=month_start,
                    month_end=month_end,
                    user_id=user.id)
        data = query.all()

        data = self._group_by_user_monthly(data, user.id)

        holidays = Holiday.all()

        return dict(
            data=data,
            user=user,
            is_holiday=lambda date: Holiday.is_holiday(date, holidays=holidays
                                                       ),
            month_start=month_start,
            month_end=month_end,
            next_month=next_month(month_start),
            prev_month=previous_month(month_start),
            datetime=datetime,
        )
Beispiel #2
0
    def get(self):
        user_id = self.request.GET.get('user_id')
        month_start, month_end = self._get_month()
        user = User.query.filter(User.id==user_id).one()

        query = DBSession.query('date', 'incorrect_count').from_statement("""
            SELECT  date, COUNT(date) as incorrect_count
            FROM time_entry s
            WHERE DATE(s.modified_ts) > s.date AND
                  s.user_id = :user_id AND
                  s.date >= :month_start AND
                  s.date <= :month_end
            GROUP BY date
        """).params(month_start=month_start, month_end=month_end, user_id=user.id)
        data = query.all()

        data = self._group_by_user_monthly(data, user.id)

        holidays = Holiday.all()

        return dict(
            data=data,
            user=user,
            is_holiday=lambda date: Holiday.is_holiday(date, holidays=holidays),

            month_start=month_start, month_end=month_end,
            next_month=next_month(month_start),
            prev_month=previous_month(month_start),
            datetime=datetime,
        )
Beispiel #3
0
    def get(self):
        user_id = self.request.GET.get('user_id')
        month_start, month_end = self._get_month()
        user = User.query.filter(User.id == user_id).one()

        query = DBSession.query('date', 'presence', 'leave').from_statement("""
        SELECT date_trunc('day', p.ts) as "date",
               MIN(p.ts) as "presence",
               MAX(p.ts) as "leave"
        FROM presence_entry p
        WHERE p.ts >= :month_start AND
              date_trunc('day', p.ts) <= :month_end AND
              p.user_id = :user_id
        GROUP BY date_trunc('day', p.ts)
        """).params(month_start=month_start,
                    month_end=month_end,
                    user_id=user_id)
        data = query.all()

        holidays = Holiday.all()

        data = self._group_by_user_monthly(data, user.id)

        return dict(
            data=data,
            user=user,
            is_holiday=lambda date: Holiday.is_holiday(date, holidays=holidays
                                                       ),
            month_start=month_start,
            month_end=month_end,
            next_month=next_month(month_start),
            prev_month=previous_month(month_start),
            deltazero=deltazero,
            datetime=datetime,
        )
Beispiel #4
0
    def get(self):
        user_id = self.request.GET.get('user_id')
        month_start, month_end = self._get_month()
        user = User.query.filter(User.id==user_id).one()

        query = self.session.query('date', 'presence', 'leave').from_statement("""
        SELECT date_trunc('day', p.ts) as "date",
               MIN(p.ts) as "presence",
               MAX(p.ts) as "leave"
        FROM presence_entry p
        WHERE p.ts >= :month_start AND
              date_trunc('day', p.ts) <= :month_end AND
              p.user_id = :user_id
        GROUP BY date_trunc('day', p.ts)
        """).params(month_start=month_start, month_end=month_end, user_id=user_id)
        data = query.all()

        holidays = Holiday.all()

        data = self._group_by_user_monthly(data, user.id)

        return dict(
            data=data,
            user=user,
            is_holiday=lambda date: Holiday.is_holiday(date, holidays=holidays),

            month_start=month_start, month_end=month_end,
            next_month=next_month(month_start),
            prev_month=previous_month(month_start),
            deltazero=deltazero,
            datetime=datetime,
        )
Beispiel #5
0
def last_day_of_month(date):
    return first_day_of_month(h.next_month(date)) - relativedelta(days=1)
Beispiel #6
0
 def first_day_of_next_month(date):
     next_month = h.next_month(date)
     return datetime.date(next_month.year, next_month.month, 1)
Beispiel #7
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,
        )
Beispiel #8
0
def last_day_of_month(date):
    return first_day_of_month(h.next_month(date)) - relativedelta(days=1)
Beispiel #9
0
 def first_day_of_next_month(date):
     next_month = h.next_month(date)
     return datetime.date(next_month.year, next_month.month, 1)
Beispiel #10
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,
        )