Commit 02118cc944b163f0fb1c8264c89e89b5c5b1dd9d

Authored by hitier
1 parent b6c0e3c1

Show projects category/labels in table

Showing 2 changed files with 31 additions and 9 deletions   Show diff stats
1 -from app.models import db, Period 1 +from app.models import db, Period, Project, Category
2 2
3 # TODO: make this configurable, and choose another place to use it, 3 # TODO: make this configurable, and choose another place to use it,
4 # in 'routes.py' maybe 4 # in 'routes.py' maybe
@@ -15,7 +15,7 @@ def projects(): @@ -15,7 +15,7 @@ def projects():
15 """ 15 """
16 current_period_id = get_current_period() 16 current_period_id = get_current_period()
17 sql_txt = """ 17 sql_txt = """
18 - select p.id, p.name, IFNULL(sum(tc.charge_rate), 0) as total_charge 18 + select p.id, IFNULL(sum(tc.charge_rate), 0) as total_charge
19 from project as p left join 19 from project as p left join
20 ( select c.project_id, c.charge_rate from charge c where c.period_id = {}) 20 ( select c.project_id, c.charge_rate from charge c where c.period_id = {})
21 tc 21 tc
@@ -23,7 +23,25 @@ def projects(): @@ -23,7 +23,25 @@ def projects():
23 group by p.id 23 group by p.id
24 order by total_charge desc; 24 order by total_charge desc;
25 """.format(current_period_id) 25 """.format(current_period_id)
26 - all_projects = db.session.execute(sql_txt).fetchall() 26 + projects_charges = db.session.execute(sql_txt).fetchall()
  27 + # First row is table titles
  28 + all_projects = [["Id", "Projet"]]
  29 + # Add all categories as last table titles
  30 + for c in Category.query.all():
  31 + all_projects[0].append(c.name)
  32 + all_projects[0].append("Charge")
  33 + # Build the table row by row
  34 + for pc in projects_charges:
  35 + p_id = pc[0]
  36 + p = Project.query.get(int(p_id))
  37 + p_labels = p.labels
  38 + # adding 3 first columns: id, name and total charge
  39 + project_row = [p.id, p.name]
  40 + # then labels, one for each category
  41 + for pl in p_labels:
  42 + project_row.append(pl.label.name)
  43 + project_row.append(pc[1])
  44 + all_projects.append(project_row)
27 return all_projects 45 return all_projects
28 46
29 47
app/main/templates/projects.html
@@ -7,17 +7,21 @@ @@ -7,17 +7,21 @@
7 <table class="table table-hover"> 7 <table class="table table-hover">
8 <thead> 8 <thead>
9 <tr> 9 <tr>
10 - <th scope="col">Projet</th>  
11 - <th scope="col">Charge</th> 10 + {% for c_title in projects[0][1:] %}
  11 + <th scope="col">{{c_title}}</th>
  12 + {% endfor %}
12 </tr> 13 </tr>
13 </thead> 14 </thead>
14 <tbody> 15 <tbody>
15 - {% for project in projects %} 16 + {% for project in projects[1:] %}
16 <tr> 17 <tr>
17 - <td><a href="{{url_for('main.project', project_id=project.id)}}">  
18 - {{ project.name }}</a> 18 + <td><a href="{{url_for('main.project', project_id=project[0])}}">
  19 + {{ project[1] }}</a>
19 </td> 20 </td>
20 - <td>{{ project.total_charge }} % </td> 21 + {% for c in project[2:-1] %}
  22 + <td>{{c}}</td>
  23 + {% endfor %}
  24 + <td>{{ project[-1] }} %</td>
21 </tr> 25 </tr>
22 {% endfor %} 26 {% endfor %}
23 </tbody> 27 </tbody>