def _activityHeader(**kwargs): from models.account import Account from models.project import Project, Component, Membership, Label from helpers.account import AccountHelper from helpers.project import ProjectHelper header = {} if kwargs.has_key('start_date'): header['start_date'] = kwargs['start_date'] else: header['start_date'] = request.values.get('start_date', g._constant()['DATE']['MONTH']) #start_date = datetime.datetime.strptime(start_datestring, '%Y-%m-%d') #start_ts = start_date.strftime('%s') if kwargs.has_key('end_date'): header['end_date'] = kwargs['end_date'] else: header['end_date'] = request.values.get('end_date', g._constant()['DATE']['TODAY']) #end_date = datetime.datetime.strptime(end_datestring, '%Y-%m-%d') #end_ts = end_date.strftime('%s') header['employees'] = [] if kwargs.has_key('employees'): header['employees'] = kwargs['employees'] elif app.access('profile', action='administer'): header['employees'] = AccountHelper.listAccounts() elif app.access('profile', action='list'): header['employees'] = AccountHelper.listActiveAccounts() elif app.access('profile', action='read', account=g.account): header['employees'] = [g.account] else: header['employees'] = [] header['skip_projects'] = ProjectHelper.listVacationProjects() header['projects'] = [] if kwargs.has_key('projects'): header['projects'] = kwargs['projects'] elif app.access('project', action='administer'): header['projects'] = ProjectHelper.listAllComponents() elif app.access('project', action='list'): header['projects'] = ProjectHelper.listAllActiveComponents() elif app.access('membership', account=g.account): header['projects'] = ProjectHelper.listAllComponentsForMember(account=g.account) else: header['projects'] = [] return header
def activity_statistics(account_id=None): from models.account import Account from models.report import Report from helpers.report import ReportHelper account = Account.query.filter_by(id=account_id).first() if not account: return Response(json.dumps({'status':404, 'description':'Not found', 'errors':['not found']}), mimetype='application/json') elif not app.access('activity', account=account): return Response(json.dumps({'status':403, 'description':'Not authorized', 'errors':['not authorized']}), mimetype='application/json') header = _activityHeader(employees=[account]) filters = {} #filters['project'] = header['projects'] # to allow the users ses all their reports filters['project'] = None filters['employee'] = [account.id for account in header['employees']] filters['start_date'] = header['start_date'] filters['end_date'] = header['end_date'] dataReports = [] if app.access('report', action='administer'): dataReports = ReportHelper.listReports( start_date=filters['start_date'], end_date=filters['end_date'], accounts=filters['employee'], components=filters['project'] ) elif app.access('report', action='list'): dataReports = ReportHelper.listActiveReports( start_date=filters['start_date'], end_date=filters['end_date'], accounts=filters['employee'], components=filters['project'] ) else: return Response(json.dumps({'status':403, 'description':'Not authorized', 'errors':['not authorized']}), mimetype='application/json') statistics = { 'hrs_day': 0.0, 'hrs_week': 0.0, 'hrs_month': 0.0, 'tasks_day': 0, 'tasks_week': 0, 'tasks_month': 0, 'projects_day': {}, 'projects_week': {}, 'projects_month': {} } for report in dataReports: if report.due_date == g._constant()['DATE']['TODAY']: statistics['hrs_day'] += report.duration statistics['tasks_day'] += 1 if not statistics['projects_day'].has_key(report.path): statistics['projects_day'][report.path] = 0.0 statistics['projects_day'][report.path] += report.duration if report.due_date >= g._constant()['DATE']['WEEK']: statistics['hrs_week'] += report.duration statistics['tasks_week'] += 1 if not statistics['projects_week'].has_key(report.path): statistics['projects_week'][report.path] = 0.0 statistics['projects_week'][report.path] += report.duration if report.due_date >= g._constant()['DATE']['MONTH']: statistics['hrs_month'] += report.duration statistics['tasks_month'] += 1 if not statistics['projects_month'].has_key(report.path): statistics['projects_month'][report.path] = 0.0 statistics['projects_month'][report.path] += report.duration html = render_template('report/dashboard.html', statistics=statistics, filters=filters) return Response(json.dumps({'status':200, 'description':'OK', 'data':statistics, 'html':html, 'filters':filters, 'errors':[]}), mimetype='application/json')