Commit 536cb7017b99ba1f44c56fab144c0f133321ecab

Authored by hitier
1 parent d568e561

Fixed some code smells

Showing 2 changed files with 31 additions and 22 deletions   Show diff stats
app/db_mgr.py
... ... @@ -8,6 +8,19 @@ from app.models import db, Period, Project, Category
8 8 charge_unit = 100
9 9  
10 10  
  11 +def get_periods():
  12 + return db.session.execute("select id, name from period order by id")
  13 +
  14 +
  15 +def get_current_period():
  16 + """
  17 + :return: the id of the period of current day
  18 + """
  19 + # TODO: request on dates as soon as periods are dated
  20 + p = Period.query.filter((Period.name == '2021') | (Period.name == '2021_S1')).one()
  21 + return p.id
  22 +
  23 +
11 24 def category_labels():
12 25 """
13 26 Build a list of dicts of the categorized labels for form display:
... ... @@ -192,7 +205,7 @@ def charges_by_project_stacked(project_id, category="service"):
192 205 categories = [c for (c,) in db.session.execute(categories_req)]
193 206 headers = ['period'] + categories
194 207 all_charges = [headers]
195   - for (period_id, period_name) in db.session.execute("select id, name from period order by id"):
  208 + for (period_id, period_name) in get_periods():
196 209 # build the request from the string previously set
197 210 charge_by_categorie_req = sql_req.format(project_id, period_id)
198 211 # build the charges line for the current period
... ... @@ -242,7 +255,7 @@ def charges_by_labels_stacked(category_id):
242 255 labels_names = [name for (name,) in db.session.execute(labels_req)]
243 256 headers = ['period'] + labels_names
244 257 all_charges = [headers]
245   - for (period_id, period_name) in db.session.execute("select id, name from period order by id"):
  258 + for (period_id, period_name) in get_periods():
246 259 project_charges = [period_name]
247 260 charges_for_projects_req = sql_req.format(period_id, category_id)
248 261 for (project_name, project_charge) in db.session.execute(charges_for_projects_req):
... ... @@ -280,7 +293,7 @@ def charges_for_projects_stacked():
280 293 projects_names = [name for (name,) in db.session.execute(projects_req)]
281 294 headers = ['period'] + projects_names
282 295 all_charges = [headers]
283   - for (period_id, period_name) in db.session.execute("select id, name from period order by id"):
  296 + for (period_id, period_name) in get_periods():
284 297 project_charges = [period_name]
285 298 charges_for_projects_req = sql_req.format(period_id)
286 299 for (project_name, project_charge) in db.session.execute(charges_for_projects_req):
... ... @@ -301,7 +314,7 @@ def charges_by_agent_stacked(agent_id):
301 314 categories = [c for (c,) in db.session.execute(categories_req)]
302 315 headers = ['period'] + categories
303 316 all_charges = [headers]
304   - for (period_id, period_name) in db.session.execute("select id, name from period order by id"):
  317 + for (period_id, period_name) in get_periods():
305 318 charge_by_project_req = """
306 319 select sum(c.charge_rate)
307 320 from project p
... ... @@ -366,12 +379,3 @@ def charges_by_agent(agent_id):
366 379 else:
367 380 all_charges.append([p, 0])
368 381 return all_charges
369   -
370   -
371   -def get_current_period():
372   - """
373   - :return: the id of the period of current day
374   - """
375   - # TODO: request on dates as soon as periods are dated
376   - p = Period.query.filter((Period.name == '2021') | (Period.name == '2021_S1')).one()
377   - return p.id
... ...
app/main/routes.py
... ... @@ -164,6 +164,11 @@ def agent(agent_id):
164 164  
165 165  
166 166 # - - - - - - - - - - - - - - - - - - - - FORMS - - - - - - - - - - - - - - - - - - - - #
  167 +
  168 +updated_message = "mis à jour"
  169 +added_message = "ajouté"
  170 +
  171 +
167 172 @bp.route('/project/<project_id>/delete', methods=('POST', 'GET'))
168 173 @role_required('admin')
169 174 def project_delete(project_id=None):
... ... @@ -185,8 +190,8 @@ def agent_delete(agent_id=None):
185 190 @bp.route('/category/<category_id>/delete', methods=('POST', 'GET'))
186 191 @role_required('admin')
187 192 def category_delete(category_id=None):
188   - this_category = Category.query.get(int(category_id))
189 193 flash("Suppression de la Catégorie pas encore implémentée", 'warning')
  194 + # this_category = Category.query.get(int(category_id))
190 195 # flash(f"Category {this_category.name} effacé")
191 196 return redirect(url_for('main.categories'))
192 197  
... ... @@ -194,8 +199,8 @@ def category_delete(category_id=None):
194 199 @bp.route('/label/<label_id>/delete', methods=('POST', 'GET'))
195 200 @role_required('admin')
196 201 def label_delete(label_id=None):
197   - this_label = Label.query.get(int(label_id))
198 202 flash("Suppression du Label pas encore implémentée", 'warning')
  203 + # this_label = Label.query.get(int(label_id))
199 204 # flash(f"Label {this_label.name} effacé")
200 205 return redirect(url_for('main.labels'))
201 206  
... ... @@ -220,11 +225,11 @@ def label_edit(label_id=None):
220 225 if label_id:
221 226 # then update existing
222 227 this_label = Label.query.get(int(label_id))
223   - done_string = "mis à jour."
  228 + done_string = updated_message+"."
224 229 else:
225 230 # or create from scratch
226 231 this_label = Label()
227   - done_string = "ajouté."
  232 + done_string = added_message+"."
228 233 # fill orm with form and write to db
229 234 this_label.from_request(request)
230 235 db.session.add(this_label)
... ... @@ -254,7 +259,7 @@ def category_edit(category_id=None):
254 259 if category_id:
255 260 # then update existing
256 261 this_category = Category.query.get(int(category_id))
257   - done_string = "mise à jour."
  262 + done_string = updated_message+"."
258 263 else:
259 264 # or create from scratch
260 265 this_category = Category()
... ... @@ -289,7 +294,7 @@ def project_edit(project_id=None):
289 294 if project_id:
290 295 # then update existing
291 296 this_project = Project.query.get(int(project_id))
292   - done_string = "mis à jour."
  297 + done_string = updated_message+"."
293 298 else:
294 299 # check name doesnt exist yet
295 300 existing_project = Project.query.filter(Project.name == request.form.get('name')).one_or_none()
... ... @@ -298,7 +303,7 @@ def project_edit(project_id=None):
298 303 return redirect(url_for('main.project_edit'))
299 304 # or create from scratch
300 305 this_project = Project()
301   - done_string = "ajouté."
  306 + done_string = added_message+"."
302 307 # fill orm with form and write to db
303 308 this_project.from_request(request)
304 309 db.session.add(this_project)
... ... @@ -340,11 +345,11 @@ def agent_edit(agent_id=None):
340 345 if agent_id:
341 346 # then update existing
342 347 this_agent = Agent.query.get(int(agent_id))
343   - done_string = "mis à jour."
  348 + done_string = updated_message+"."
344 349 else:
345 350 # or create from scratch
346 351 this_agent = Agent()
347   - done_string = "ajouté."
  352 + done_string = added_message+"."
348 353 # fill orm with form and write to db
349 354 this_agent.from_request(request)
350 355 db.session.add(this_agent)
... ...