Commit 95880b0db99419e1e001241e8e2297c610dac779

Authored by hitier
1 parent 55d58635

New charges_for_projects() REST/CSV exposed

ce commit là je le change.
app/db_mgr.py
... ... @@ -202,6 +202,28 @@ def charges_by_project_stacked(project_id, category="service"):
202 202 return all_charges
203 203  
204 204  
  205 +def charges_for_projects_stacked():
  206 + sql_req = """
  207 + select p.name, sum(charge_rate) as tot_charg
  208 + from charge
  209 + join project p on p.id = charge.project_id
  210 + where period_id = {}
  211 + group by project_id
  212 + """
  213 + projects_req = "select name from project order by id"
  214 + projects_names = [name for (name,) in db.session.execute(projects_req)]
  215 + headers = ['period'] + projects_names
  216 + all_charges = [headers]
  217 + for (period_id, period_name) in db.session.execute("select id, name from period order by id"):
  218 + project_charges = [period_name]
  219 + charges_for_projects_req = sql_req.format(period_id)
  220 + for( project_name, project_charge) in db.session.execute(charges_for_projects_req):
  221 + project_charge = str(round(project_charge / charge_unit, 2)) if project_charge else '0'
  222 + project_charges.append(project_charge)
  223 + all_charges.append(project_charges)
  224 + return all_charges
  225 +
  226 +
205 227 def charges_by_agent_stacked(agent_id):
206 228 """
207 229 Build the list of charges for all projects of one agent, period by period
... ...
app/main/routes.py
... ... @@ -284,3 +284,16 @@ def charge_agent_csv(agent_id):
284 284 resp = make_response("\n".join(csv_table))
285 285 resp.headers['Content-Type'] = 'text/plain;charset=utf8'
286 286 return resp
  287 +
  288 +
  289 +@bp.route('/charge/projects')
  290 +@role_required('project')
  291 +def projects_stats_csv():
  292 + csv_table = []
  293 + for line in db_mgr.charges_for_projects_stacked():
  294 + line = [cell.replace(",", "-") for cell in line]
  295 + line_string = ",".join(line)
  296 + csv_table.append(line_string)
  297 + resp = make_response("\n".join(csv_table))
  298 + resp.headers['Content-Type'] = 'text/plain;charset=utf8'
  299 + return resp
... ...
tests/backend_tests.py
... ... @@ -75,6 +75,12 @@ class DbMgrTestCase(BaseTestCase):
75 75 # Waiting for 17 periods + headers line
76 76 self.assertEqual(18, len(stacked_charges))
77 77  
  78 + def test_charges_for_projects_stacked(self):
  79 + stacked_charges = db_mgr.charges_for_projects_stacked()
  80 + # Waiting for 17 periods + headers line
  81 + self.assertEqual(18, len(stacked_charges))
  82 + self.assertEqual(102, len(stacked_charges[0]))
  83 +
78 84 def test_category_labels(self):
79 85 category_labels = db_mgr.category_labels()
80 86 categories = [_cl['name'] for _cl in category_labels]
... ...