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, )
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, )
def user_can_modify_timeentry(user, date_): """ Checks if user can modify timeentry, rule: User can modify timeentry for current month or previous month provided it is first day of month. """ if "admin" in user.groups: return True date = copy.copy(date_) if isinstance(date, datetime.datetime): date = date.date() today = datetime.date.today() diff = today - date if diff.days <= 0: # date in future or today return True if date.year == today.year and date.month == today.month: # the same month return True if today.day == 1: prev_today = previous_month(today) if date.year == prev_today.year and date.month == prev_today.month: # first of month, employee can change/add timeentry for previous month return True return False
def user_can_modify_timeentry(user, date_): """ Checks if user can modify timeentry, rule: User can modify timeentry for current month or previous month provided it is first day of month. """ if 'admin' in user.groups: return True date = copy.copy(date_) if isinstance(date, datetime.datetime): date = date.date() today = datetime.date.today() diff = today - date if diff.days <= 0: # date in future or today return True if date.year == today.year and date.month == today.month: # the same month return True if today.day <= 3: prev_today = previous_month(today) if date.year == prev_today.year and date.month == prev_today.month: # third day of month, employee can change/add timeentry for previous month return True return False
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, )
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, )
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, )
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, )