def get(self): check_role = self.request.GET.get('role') if not check_role or check_role == 'None': pivot_q = self.session.query(User.id, User.name, User.start_work, User.stop_work, User.roles)\ .filter(User.is_not_client())\ .filter(User.is_active==True)\ .order_by(User.name) else: pivot_q = self.session.query(User.id, User.name, User.start_work, User.stop_work, User.roles)\ .filter(User.is_not_client())\ .filter(User.is_active==True)\ .filter(User.roles.op('&&')('{%s}'%(check_role)))\ .order_by(User.name) users = {} for s in pivot_q: if s.start_work: users.setdefault(s.start_work.year, [0]*24)[s.start_work.month-1] += 1 if s.stop_work: users.setdefault(s.stop_work.year, [0]*24)[s.stop_work.month-1+12] += 1 role = User.ROLES return dict( start=users, roles=role, check=check_role, quarters_sum=self._quarters_sum, link_with_year=self._create_link_with_year, link_with_month=self._create_link_with_month, link_with_quarter=self._create_link_with_quarter, )
def get(self): check_role = self.request.GET.get('role') if not check_role or check_role == 'None': pivot_q = self.session.query(User.id, User.name, User.start_work, User.stop_work, User.roles)\ .filter(User.is_not_client())\ .filter(User.is_active==True)\ .order_by(User.name) else: pivot_q = self.session.query(User.id, User.name, User.start_work, User.stop_work, User.roles)\ .filter(User.is_not_client())\ .filter(User.is_active==True)\ .filter(User.roles.op('&&')('{%s}'%(check_role)))\ .order_by(User.name) users = {} for s in pivot_q: if s.start_work: users.setdefault(s.start_work.year, [0] * 24)[s.start_work.month - 1] += 1 if s.stop_work: users.setdefault(s.stop_work.year, [0] * 24)[s.stop_work.month - 1 + 12] += 1 role = User.ROLES return dict( start=users, roles=role, check=check_role, quarters_sum=self._quarters_sum, link_with_year=self._create_link_with_year, link_with_month=self._create_link_with_month, link_with_quarter=self._create_link_with_quarter, )
def __iter__(self): if self.empty: yield '', u'-- None --' query = DBSession.query(User.id, User.name).filter(User.is_not_client()).filter(User.is_active==True).order_by(User.name) for id, name in query: yield str(id), name if self.inactive: yield '', ' '*8 query = DBSession.query(User.id, User.name).filter(User.is_not_client()).filter(User.is_active==False).order_by(User.name) for id, name in query: yield str(id), name
def get(self): check_role = self.request.GET.get('role') get_year = self.request.GET.get('year') get_month = self.request.GET.get('month') get_stop_work = self.request.GET.get('stop') get_quaters = self.request.GET.get('q') is_get = True if check_role or get_month or get_stop_work or get_year else False users=[] if get_year: year = int(get_year) if get_month: month = int(get_month) day_of_week, days_in_month = calendar.monthrange(year, month) start_date = datetime.date(year, month, 1) stop_date = datetime.date(year, month, days_in_month) elif get_quaters: quater = int(get_quaters) day_of_week, days_in_month = calendar.monthrange(year, quater*3) start_date = datetime.date(year, ((quater-1)*3)+1, 1) stop_date = datetime.date(year, quater*3, days_in_month) else: start_date = datetime.date(year, 1, 1) stop_date = datetime.date(year, 12, 31) if (not check_role or check_role == 'None') and not get_stop_work: users = User.query.filter(User.is_not_client())\ .filter(User.start_work >= start_date)\ .filter(User.start_work <= stop_date)\ .order_by(User.name).all() elif (not check_role or check_role == 'None') and get_stop_work: users = User.query.filter(User.is_not_client())\ .filter(User.stop_work >= start_date)\ .filter(User.stop_work <= stop_date)\ .order_by(User.name).all() elif check_role and get_stop_work: users = User.query.filter(User.is_not_client())\ .filter(User.stop_work >= start_date)\ .filter(User.stop_work <= stop_date)\ .filter(User.roles.op('&&')('{%s}'%(check_role)))\ .order_by(User.name).all() elif check_role and not get_stop_work: users = User.query.filter(User.is_not_client())\ .filter(User.start_work >= start_date)\ .filter(User.start_work <= stop_date)\ .filter(User.roles.op('&&')('{%s}'%(check_role)))\ .order_by(User.name).all() return dict( users=users, is_get=is_get )
def get(self): check_role = self.request.GET.get('role') get_year = self.request.GET.get('year') get_month = self.request.GET.get('month') get_stop_work = self.request.GET.get('stop') get_quaters = self.request.GET.get('q') is_get = True if check_role or get_month or get_stop_work or get_year else False users = [] if get_year: year = int(get_year) if get_month: month = int(get_month) day_of_week, days_in_month = calendar.monthrange(year, month) start_date = datetime.date(year, month, 1) stop_date = datetime.date(year, month, days_in_month) elif get_quaters: quater = int(get_quaters) day_of_week, days_in_month = calendar.monthrange( year, quater * 3) start_date = datetime.date(year, ((quater - 1) * 3) + 1, 1) stop_date = datetime.date(year, quater * 3, days_in_month) else: start_date = datetime.date(year, 1, 1) stop_date = datetime.date(year, 12, 31) if (not check_role or check_role == 'None') and not get_stop_work: users = User.query.filter(User.is_not_client())\ .filter(User.start_work >= start_date)\ .filter(User.start_work <= stop_date)\ .order_by(User.name).all() elif (not check_role or check_role == 'None') and get_stop_work: users = User.query.filter(User.is_not_client())\ .filter(User.stop_work >= start_date)\ .filter(User.stop_work <= stop_date)\ .order_by(User.name).all() elif check_role and get_stop_work: users = User.query.filter(User.is_not_client())\ .filter(User.stop_work >= start_date)\ .filter(User.stop_work <= stop_date)\ .filter(User.roles.op('&&')('{%s}'%(check_role)))\ .order_by(User.name).all() elif check_role and not get_stop_work: users = User.query.filter(User.is_not_client())\ .filter(User.start_work >= start_date)\ .filter(User.start_work <= stop_date)\ .filter(User.roles.op('&&')('{%s}'%(check_role)))\ .order_by(User.name).all() return dict(users=users, is_get=is_get)
def get(self): year = self.request.GET.get('year', datetime.date.today().year) users = User.query.filter(User.is_not_client()).filter(User.is_active==True).filter(User.freelancer==False).all() used_entries = self.session.query('user_id', 'days').from_statement(""" SELECT t.user_id, sum(t.time)/8 as days FROM time_entry t WHERE deleted = false AND t.project_id = 86 AND date_part('year', t.date) = :year GROUP BY user_id """).params(year=year).all() used = defaultdict(lambda: 0) for u in used_entries: used[u[0]] = u[1] applications = m.Absence.get_for_year(year) return dict( users=users, mandated=Leave.get_for_year(year), used=used, applications=applications, year=int(year), serialize_url=h.serialize_url, str=str, )
def __iter__(self): if self.empty: yield '', u'-- None --' query = DBSession.query(User.id, User.name).filter(User.is_not_client()).filter( User.is_active == True).order_by(User.name) for id, name in query: yield str(id), name if self.inactive: yield '', ' ' * 8 query = DBSession.query(User.id, User.name).filter( User.is_not_client()).filter(User.is_active == False).order_by( User.name) for id, name in query: yield str(id), name
def get(self): year = self.request.GET.get('year', datetime.date.today().year) users = User.query.filter(User.is_not_client()).filter( User.is_active == True).filter(User.freelancer == False).all() used_entries = self.session.query('user_id', 'days').from_statement(""" SELECT t.user_id, sum(t.time)/8 as days FROM time_entry t WHERE deleted = false AND t.project_id = 86 AND date_part('year', t.date) = :year GROUP BY user_id """).params(year=year).all() used = defaultdict(lambda: 0) for u in used_entries: used[u[0]] = u[1] applications = m.Absence.get_for_year(year) return dict( users=users, mandated=Leave.get_for_year(year), used=used, applications=applications, year=int(year), serialize_url=h.serialize_url, str=str, )
def _annually_report(self, year): year_start = datetime.date(year, 1, 1) year_end = datetime.date(year, 12, 31) excuses_error = None config_obj = ApplicationConfig.get_current_config() query = DBSession.query('uid', 'date', 'presence').from_statement(""" SELECT p.user_id as "uid", date_trunc('day', p.ts) as "date", MIN(p.ts) as "presence" FROM presence_entry p WHERE p.ts >= :year_start AND p.ts <= :year_end GROUP BY p.user_id, date_trunc('day', p.ts) """).params(year_start=year_start, year_end=year_end) data = query.all() users = User.query.filter(User.is_active==True)\ .filter(User.is_not_client())\ .filter(User.is_not_freelancer())\ .order_by(User.name).all() _excuses = excuses.presence() data = self._group_by_user_monthly(data, _excuses) stats = self._prepare_statistics(data) return dict( data=data, users=users, stats=stats, today=datetime.datetime.today(), year_start=year_start, deltazero=deltazero, late_limit=config_obj.monthly_late_limit, excuses_error=excuses_error, )
def _get_not_full_time_employees(self): users = User.query\ .filter(User.is_active==True)\ .filter(User.is_not_client())\ .filter(User.start_full_time_work==None)\ .order_by(User.name) users = users.all() return users
def _get_not_full_time_employees(self): users = User.query\ .filter(User.is_active==True)\ .filter(User.is_not_client())\ .filter(User.start_full_time_work==None)\ .order_by(User.name) users = users.all() return users
def get(self): users = ( User.query.filter(User.is_active == True) .filter(User.is_not_client()) .filter(User.freelancer == False) .order_by(User.name) ) return [{"id": u.id, "name": u.name, "img": u.avatar_url} for u in users]
def __iter__(self): if self.first_empty: yield '', '' * 8 query = DBSession.query(User.id, User.name)\ .filter(User.is_not_client())\ .filter(User.is_active==True)\ .order_by(User.name) for client_id, client_name in query: yield str(client_id), client_name if self.inactive: yield '', ' ' * 8 query = DBSession.query(User.id, User.name)\ .filter(User.is_not_client())\ .filter(User.is_active==False)\ .order_by(User.name) for id, name in query: yield str(id), name
def __iter__(self): if self.first_empty: yield '', ''*8 query = DBSession.query(User.id, User.name)\ .filter(User.is_not_client())\ .filter(User.is_active==True)\ .order_by(User.name) for client_id, client_name in query: yield str(client_id), client_name if self.inactive: yield '', ' '*8 query = DBSession.query(User.id, User.name)\ .filter(User.is_not_client())\ .filter(User.is_active==False)\ .order_by(User.name) for id, name in query: yield str(id), name
def get(self): res = User.query.filter(User.is_active == True).filter(User.is_not_client()).order_by(User.name).all() users = [user for user in res if not user.freelancer] freelancers = [user for user in res if user.freelancer] clients = [] if self.request.has_perm("admin"): clients = User.query.filter(User.is_active == True).filter(User.is_client()).order_by(User.name).all() return dict(users=users, freelancers=freelancers, clients=clients)
def get(self): users = User.query.filter(User.is_active==True)\ .filter(User.is_not_client())\ .filter(User.freelancer==False)\ .order_by(User.name).all() freelancers = User.query.filter(User.is_active==True)\ .filter(User.is_not_client())\ .filter(User.freelancer==True)\ .order_by(User.name).all() clients = [] if self.request.has_perm('admin'): clients = User.query.filter(User.is_active==True)\ .filter(User.is_client())\ .order_by(User.name).all() return dict( users=users, freelancers=freelancers, clients=clients, )
def get(self): users = User.query.filter(User.is_active==True)\ .filter(User.is_not_client())\ .filter(User.freelancer==False)\ .order_by(User.name).all() freelancers = User.query.filter(User.is_active==True)\ .filter(User.is_not_client())\ .filter(User.freelancer==True)\ .order_by(User.name).all() clients = [] if self.request.has_perm('admin'): clients = User.query.filter(User.is_active==True)\ .filter(User.is_client())\ .order_by(User.name).all() return dict( users=users, freelancers=freelancers, clients=clients, )
def get(self): full = self.request.GET.get('full') == '1' inactive = self.request.GET.get('inactive') == '1' users = User.query.order_by(User.name) if not (self.request.has_perm('admin') and inactive): users = users.filter(User.is_active == True) if full: return dict(users=[u.to_dict(full=True) for u in users], ) else: users = users.filter(User.is_not_client())\ .filter(User.freelancer==False) return dict(users=[u.to_dict() for u in users], )
def get(self): year = self.request.GET.get('year', datetime.date.today().year) users = User.query.filter(User.is_not_client()).filter(User.is_active==True).filter(User.freelancer==False).all() used = Leave.get_used_for_year(year) applications = m.Absence.get_for_year(year) return dict( users=users, mandated=Leave.get_for_year(year), used=used, applications=applications, year=int(year), serialize_url=h.serialize_url, str=str, )
def get(self): year = self.request.GET.get('year', datetime.date.today().year) users = User.query.filter(User.is_not_client()).filter( User.is_active == True).filter(User.freelancer == False).all() used = Leave.get_used_for_year(year) applications = m.Absence.get_for_year(year) return dict( users=users, mandated=Leave.get_for_year(year), used=used, applications=applications, year=int(year), serialize_url=h.serialize_url, str=str, )
def get(self): full = self.request.GET.get('full') == '1' inactive = self.request.GET.get('inactive') == '1' users = User.query.order_by(User.name) if not (self.request.has_perm('can_see_inactive_users') and inactive): users = users.filter(User.is_active==True) if full: return dict( users=[u.to_dict(full=True) for u in users], ) else: users = users.filter(User.is_not_client())\ .filter(User.is_not_freelancer()) return dict( users=[u.to_dict() for u in users], )
def _annually_report(self, year): year_start = datetime.date(year, 1, 1) year_end = datetime.date(year, 12, 31) excuses_error = None config_obj = ApplicationConfig.get_current_config() entries = DBSession.query('user_id', 'date').from_statement(""" SELECT date_trunc('day', s.date) as date, s.user_id as user_id FROM time_entry s WHERE DATE(s.modified_ts) > s.date AND s.date >= :year_start AND s.date <= :year_end GROUP BY date_trunc('day', s.date), s.user_id """).params(year_start=year_start, year_end=year_end) users = User.query.filter(User.is_active==True)\ .filter(User.is_not_client())\ .order_by(User.is_freelancer(), User.name) entries_grouped = {} _excuses = excuses.wrongtime() for user_id, date in entries: month = date.month - 1 entry = entries_grouped.setdefault(user_id, [0] * 12) if date.strftime('%Y-%m-%d') not in _excuses.get(user_id, {}).keys(): entry[month] += 1 stats = {} for user_id, entry in entries_grouped.iteritems(): stats_entry = stats.setdefault(user_id, [0]) stats_entry[0] = sum(entry) return dict( entries=entries_grouped, stats=stats, users=users, year_start=year_start, limit=config_obj.monthly_incorrect_time_record_limit, excuses_error=excuses_error, )
def _annually_report(self, year): year_start = datetime.date(year, 1, 1) year_end = datetime.date(year, 12, 31) excuses_error = None config_obj = ApplicationConfig.get_current_config() entries = DBSession.query('user_id', 'date').from_statement(""" SELECT date_trunc('day', s.date) as date, s.user_id as user_id FROM time_entry s WHERE DATE(s.modified_ts) > s.date AND s.date >= :year_start AND s.date <= :year_end GROUP BY date_trunc('day', s.date), s.user_id """).params(year_start=year_start, year_end=year_end) users = User.query.filter(User.is_active==True)\ .filter(User.is_not_client())\ .order_by(User.is_freelancer(), User.name) entries_grouped = {} _excuses = excuses.wrongtime() for user_id, date in entries: month = date.month - 1 entry = entries_grouped.setdefault(user_id, [0]*12) if date.strftime('%Y-%m-%d') not in _excuses.get(user_id, {}).keys(): entry[month] += 1 stats = {} for user_id, entry in entries_grouped.iteritems(): stats_entry = stats.setdefault(user_id, [0]) stats_entry[0] = sum(entry) return dict( entries=entries_grouped, stats=stats, users=users, year_start=year_start, limit=config_obj.monthly_incorrect_time_record_limit, excuses_error=excuses_error, )
def _annually_report(self, year): year_start = datetime.date(year, 1, 1) year_end = datetime.date(year, 12, 31) excuses_error = None config_obj = ApplicationConfig.get_current_config() query = self.session.query('uid', 'date', 'presence').from_statement(""" SELECT p.user_id as "uid", date_trunc('day', p.ts) as "date", MIN(p.ts) as "presence" FROM presence_entry p WHERE p.ts >= :year_start AND p.ts <= :year_end GROUP BY p.user_id, date_trunc('day', p.ts) """).params(year_start=year_start, year_end=year_end) data = query.all() users = User.query.filter(User.is_active==True)\ .filter(User.is_not_client())\ .filter(User.freelancer==False)\ .order_by(User.name).all() _excuses = excuses.presence() data = self._group_by_user_monthly(data, _excuses) stats = self._prepare_statistics(data) return dict( data=data, users=users, stats=stats, today=datetime.datetime.today(), year_start=year_start, deltazero=deltazero, late_limit=config_obj.monthly_late_limit, excuses_error=excuses_error, )
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): # Requested (or current) year year = self.request.GET.get('year') year = int(year) if year else datetime.date.today().year # Year start day and end day start = datetime.date(year, 1, 1) end = datetime.date(year, 12, 31) start_day = dict( day=start.day, dow=start.weekday(), ) day_count, date_range, months = self.necessary_data(start, end) holidays = Holiday.query \ .filter(Holiday.date >= start) \ .all() # Users users = [] user_groups = [] for name, (fullname, shortcut) in self.request.user.get_locations(): usersq = User.query.filter(User.is_not_client()) \ .filter(User.is_active==True) \ .filter(User.location==name) \ .order_by(User.is_freelancer(), User.name).all() users.extend(usersq) user_groups.append((fullname, len(usersq))) # Leaves leave_mandated = Leave.get_for_year(start.year) leave_used = Leave.get_used_for_year(start.year) # Transform users to dictionary, group by city and order by leave days users = [ dict( id=str(u.id), name=u.name, leave_mandated=leave_mandated[u.id][0], leave_used=leave_used[u.id], location=u.location, ) for u in users ] users = sorted( users, key=lambda u: u['location'], ) user_groups = sorted( user_groups, key=lambda u: u, ) absences, absences_months = self.get_absences(start, end, users) lates = self.get_lates(start, end) absences_sum = ( reduce(lambda s, u: s + u['leave_used'], users, 0), reduce(lambda s, u: s + u['leave_mandated'], users, 0), ) data = { 'users': users, 'userGroups': user_groups, 'year': start.year, 'startDay': start_day, 'dayCount': day_count, 'months': months, 'absences': absences, 'absencesSum': absences_sum, 'absencesMonths': absences_months, 'lates': lates, 'holidays': [h.date.strftime('%Y-%m-%d') for h in holidays], } return dict( data=json.dumps(data, ensure_ascii=False), year=start.year, v=self, )
def get(self): # Requested (or current) year year = self.request.GET.get('year') year = int(year) if year else datetime.date.today().year # Year start day and end day start = datetime.date(year, 1, 1) end = datetime.date(year, 12, 31) start_day = dict( day=start.day, dow=start.weekday(), ) day_count, date_range, months = self.necessary_data(start, end) holidays = Holiday.query \ .filter(Holiday.date >= start) \ .all() # Users 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() 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() user_groups = [ (u'Poznań', len(users_p)), (u'Wrocław', len(users_w)), ] reverse = False if self.request.user.location == 'wroclaw': user_groups.insert(0, user_groups.pop()) reverse = True users_p.extend(users_w) # Leaves leave_mandated = Leave.get_for_year(start.year) leave_used = Leave.get_used_for_year(start.year) # Transform users to dictionary, group by city and order by leave days users = [ dict( id=str(u.id), name=u.name, leave_mandated=leave_mandated[u.id][0], leave_used=leave_used[u.id], location=u.location, ) for u in users_p ] employees, students = [], [] for user in users: if user['leave_mandated'] > 0: employees.append(user) else: students.append(user) users = employees + students # groupby location users = sorted(users, key=lambda u: u['location'], reverse=reverse) absences, absences_months = self.get_absences(start, end, users) lates = self.get_lates(start, end) absences_sum = ( reduce(lambda s, u: s + u['leave_used'], users, 0), reduce(lambda s, u: s + u['leave_mandated'], users, 0), ) data = { 'users': users, 'userGroups': user_groups, 'year': start.year, 'startDay': start_day, 'dayCount': day_count, 'months': months, 'absences': absences, 'absencesSum': absences_sum, 'absencesMonths': absences_months, 'lates': lates, 'holidays': [h.date.strftime('%Y-%m-%d') for h in holidays], } return dict( data=json.dumps(data, ensure_ascii=False), year=start.year, v=self, )
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, )
def get(self): # Requested (or current) year year = self.request.GET.get('year') year = int(year) if year else datetime.date.today().year # Year start day and end day start = datetime.date(year, 1, 1) end = datetime.date(year, 12, 31) start_day = dict( day=start.day, dow=start.weekday(), ) day_count, date_range, months = self.necessary_data(start, end) holidays = Holiday.query \ .filter(Holiday.date >= start) \ .all() # Users 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() 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() user_groups = [ (u'Poznań', len(users_p)), (u'Wrocław', len(users_w)), ] reverse = False if self.request.user.location == 'wroclaw': user_groups.insert(0, user_groups.pop()) reverse = True users_p.extend(users_w) # Leaves leave_mandated = Leave.get_for_year(start.year) leave_used = Leave.get_used_for_year(start.year) # Transform users to dictionary, group by city and order by leave days users = [dict( id=str(u.id), name=u.name, leave_mandated=leave_mandated[u.id][0], leave_used=leave_used[u.id], location=u.location, ) for u in users_p] employees, students = [], [] for user in users: if user['leave_mandated'] > 0: employees.append(user) else: students.append(user) users = employees + students # groupby location users = sorted( users, key=lambda u: u['location'], reverse=reverse ) absences, absences_months = self.get_absences(start, end, users) lates = self.get_lates(start, end) absences_sum = ( reduce(lambda s, u: s + u['leave_used'], users, 0), reduce(lambda s, u: s + u['leave_mandated'], users, 0), ) data = { 'users': users, 'userGroups': user_groups, 'year': start.year, 'startDay': start_day, 'dayCount': day_count, 'months': months, 'absences': absences, 'absencesSum': absences_sum, 'absencesMonths': absences_months, 'lates': lates, 'holidays': [h.date.strftime('%Y-%m-%d') for h in holidays], } return dict( data=json.dumps(data, ensure_ascii=False), year=start.year, v=self, )
def get(self): # Requested (or current) year year = self.request.GET.get('year') year = int(year) if year else datetime.date.today().year # Year start day and end day start = datetime.date(year, 1, 1) end = datetime.date(year, 12, 31) start_day = dict( day=start.day, dow=start.weekday(), ) day_count, date_range, months = self.necessary_data(start, end) holidays = Holiday.query \ .filter(Holiday.date >= start) \ .all() # Users users = [] user_groups = [] for name, (fullname, shortcut) in self.request.user.get_locations(): usersq = User.query.filter(User.is_not_client()) \ .filter(User.is_active==True) \ .filter(User.location==name) \ .order_by(User.is_freelancer(), User.name).all() users.extend(usersq) user_groups.append((fullname, len(usersq))) # Leaves leave_mandated = Leave.get_for_year(start.year) leave_used = Leave.get_used_for_year(start.year) # Transform users to dictionary, group by city and order by leave days users = [dict( id=str(u.id), name=u.name, leave_mandated=leave_mandated[u.id][0], leave_used=leave_used[u.id], location=u.location, ) for u in users] users = sorted( users, key=lambda u: u['location'], ) user_groups = sorted( user_groups, key=lambda u: u, ) absences, absences_months = self.get_absences(start, end, users) lates = self.get_lates(start, end) absences_sum = ( reduce(lambda s, u: s + u['leave_used'], users, 0), reduce(lambda s, u: s + u['leave_mandated'], users, 0), ) data = { 'users': users, 'userGroups': user_groups, 'year': start.year, 'startDay': start_day, 'dayCount': day_count, 'months': months, 'absences': absences, 'absencesSum': absences_sum, 'absencesMonths': absences_months, 'lates': lates, 'holidays': [h.date.strftime('%Y-%m-%d') for h in holidays], } return dict( data=json.dumps(data, ensure_ascii=False), year=start.year, v=self, )