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')
def activity_data(): from models.account import Account from models.project import Project from models.report import Report from helpers.report import ReportHelper import time if not app.access('activity', account=g.account): return Response(json.dumps({'status':403, 'description':'Not authorized', 'errors':['not authorized']}), mimetype='application/json') header = _activityHeader() filters = {} filters['project'] = request.values.getlist('filter[project]') or [project.id for project in header['projects']] # uncomment this line if you need to make sure that the employees would see all reports, whether they are members or not #filters['project'] = request.values.getlist('filter[project]') or None filters['employee'] = request.values.getlist('filter[employee]') or [account.id for account in header['employees']] filters['start_date'] = header['start_date'] filters['end_date'] = header['end_date'] header['reports'] = [] if app.access('report', action='administer'): header['reports'] = ReportHelper.listReports( start_date=filters['start_date'], end_date=filters['end_date'], accounts=filters['employee'], components=filters['project'] ) elif app.access('report', action='list'): header['reports'] = ReportHelper.listActiveReports( start_date=filters['start_date'], end_date=filters['end_date'], accounts=filters['employee'], components=filters['project'] ) else: header['reports'] = [] for reportIndex in range(len(header['reports'])): reportDict = { 'id': header['reports'][reportIndex].id, 'offline': header['reports'][reportIndex].offline, 'due_date': header['reports'][reportIndex].due_date, 'duration': header['reports'][reportIndex].duration, 'summary': header['reports'][reportIndex].summary, 'project_id': header['reports'][reportIndex].project_id, 'component_id': header['reports'][reportIndex].component_id, 'account_id': header['reports'][reportIndex].account_id, 'reporter_id': header['reports'][reportIndex].reporter_id, 'created': header['reports'][reportIndex].created, 'modified': header['reports'][reportIndex].modified, 'status': header['reports'][reportIndex].status, 'deleted': (header['reports'][reportIndex].status & Report.STATUS_DELETED > 0) } reportDict['labels'] = { 'date': header['reports'][reportIndex].due_date, 'path': header['reports'][reportIndex].path, 'account': header['reports'][reportIndex].account.__str__(), 'project': header['reports'][reportIndex].project.__str__(), 'component': header['reports'][reportIndex].component.__str__() } reportDict['links'] = { 'edit': url_for('report_edit', account_id=urllib.quote_plus(str(header['reports'][reportIndex].account_id)), report_id=urllib.quote_plus(str(header['reports'][reportIndex].id))), 'delete': url_for('report_delete', account_id=urllib.quote_plus(str(header['reports'][reportIndex].account_id)), report_id=urllib.quote_plus(str(header['reports'][reportIndex].id))), 'account': url_for('profile_view', account_id=urllib.quote_plus(str(header['reports'][reportIndex].account_id))), 'project': url_for('project_view', project_id=urllib.quote_plus(str(header['reports'][reportIndex].project_id))), 'component': url_for('project_view', project_id=urllib.quote_plus(str(header['reports'][reportIndex].project_id))) } header['reports'][reportIndex] = reportDict header['employees'] = [account.alias for account in header['employees']] header['projects'] = [component.path for component in header['projects']] header['skip_projects'] = [component.path for component in header['skip_projects']] return Response(json.dumps({'status':200, 'description':'OK', 'data':header, 'filters':filters, 'errors':[]}), mimetype='application/json')