Commit 946dda3099666a1031f975962c72bd8726443736

Authored by hitier
1 parent 4113c4f3

New cli command user_update()

Showing 1 changed file with 116 additions and 87 deletions   Show diff stats
app/commands/commands.py
@@ -13,11 +13,35 @@ from sqlalchemy.orm import Session @@ -13,11 +13,35 @@ from sqlalchemy.orm import Session
13 from sqlalchemy import create_engine 13 from sqlalchemy import create_engine
14 14
15 from app.models import db, Agent, Service, Project, Capacity, Period, Charge 15 from app.models import db, Agent, Service, Project, Capacity, Period, Charge
16 -from app.auth.models import User, _nameToRole 16 +from app.auth.models import User, _nameToRole, _roleToName
17 17
18 from . import bp 18 from . import bp
19 19
20 20
  21 +@bp.cli.command('create_db')
  22 +def create_db():
  23 + """
  24 + Create the database structure. Database should be empty.
  25 +
  26 + configure the proper database uri in the db_config.py file.
  27 + """
  28 + db.create_all()
  29 + admin = User(email='admin@nowhere.org', name='admin', login='admin', role='admin')
  30 + admin.set_password('admin')
  31 + sqlite_uri = db.engine.url.__str__() if 'sqlite' in db.engine.url.__str__() else None
  32 + try:
  33 + db.session.add(admin)
  34 + db.session.commit()
  35 + except IntegrityError:
  36 + current_app.logger.error("User admin already exists, database should be empty at create")
  37 + if sqlite_uri:
  38 + current_app.logger.error("see " + sqlite_uri)
  39 + sys.exit(-1)
  40 +
  41 + if sqlite_uri:
  42 + current_app.logger.info("Created sqlite db: " + sqlite_uri)
  43 +
  44 +
21 @bp.cli.command("feed_from_irap") 45 @bp.cli.command("feed_from_irap")
22 @click.option('--csv-file', '-f', 'csv_file_name', help="the csv file path to feed from") 46 @click.option('--csv-file', '-f', 'csv_file_name', help="the csv file path to feed from")
23 def feed_from_irap(csv_file_name): 47 def feed_from_irap(csv_file_name):
@@ -123,6 +147,59 @@ def feed_from_irap(csv_file_name): @@ -123,6 +147,59 @@ def feed_from_irap(csv_file_name):
123 db.session.commit() 147 db.session.commit()
124 148
125 149
  150 +@bp.cli.command("feed_from_lesia")
  151 +def feed_from_lesia():
  152 + """
  153 + Feed db with agents from a lesia like mysql database.
  154 +
  155 + Remember to configure the proper database uri in the db_config.py file.
  156 + """
  157 + from .lesia_db import lesia_agent, lesia_session, lesia_service, lesia_project, \
  158 + lesia_fonction, lesia_periods, lesia_affectation
  159 +
  160 + agents = lesia_session.query(lesia_agent).all()
  161 + for a in agents:
  162 + n_a = Agent(id=a.IDagent, firstname=a.nom, secondname=a.prenom)
  163 + db.session.add(n_a)
  164 + db.session.commit()
  165 +
  166 + services = lesia_session.query(lesia_service).all()
  167 + for s in services:
  168 + n_s = Service(id=s.id, name=s.nom, abbr=s.abbreviation)
  169 + db.session.add(n_s)
  170 + db.session.commit()
  171 +
  172 + projects = lesia_session.query(lesia_project).all()
  173 + for p in projects:
  174 + n_p = Project(id=p.id, name=p.nom)
  175 + db.session.add(n_p)
  176 + db.session.commit()
  177 +
  178 + fonctions = lesia_session.query(lesia_fonction).all()
  179 + for f in fonctions:
  180 + n_c = Capacity(id=f.id, name=f.nom)
  181 + db.session.add(n_c)
  182 + db.session.commit()
  183 +
  184 + periods = lesia_session.query(lesia_periods)
  185 + for p in periods:
  186 + n_p = Period(name=p.id_semestre)
  187 + db.session.add(n_p)
  188 + db.session.commit()
  189 +
  190 + affectations = lesia_session.query(lesia_affectation)
  191 + for f in affectations:
  192 + p = Period.query.filter(Period.name == f.semestre_id).one()
  193 + n_c = Charge(agent_id=f.agent_id,
  194 + project_id=f.projet_id,
  195 + service_id=f.service_id,
  196 + capacity_id=f.fonction_id,
  197 + period_id=p.id,
  198 + charge_rate=f.charge)
  199 + db.session.add(n_c)
  200 + db.session.commit()
  201 +
  202 +
126 @bp.cli.command("fake_lesia_names") 203 @bp.cli.command("fake_lesia_names")
127 def fake_lesia_names(): 204 def fake_lesia_names():
128 """ 205 """
@@ -186,59 +263,6 @@ def fake_lesia_names(): @@ -186,59 +263,6 @@ def fake_lesia_names():
186 db.session.commit() 263 db.session.commit()
187 264
188 265
189 -@bp.cli.command("feed_from_lesia")  
190 -def feed_from_lesia():  
191 - """  
192 - Feed db with agents from a lesia like mysql database.  
193 -  
194 - Remember to configure the proper database uri in the db_config.py file.  
195 - """  
196 - from .lesia_db import lesia_agent, lesia_session, lesia_service, lesia_project, \  
197 - lesia_fonction, lesia_periods, lesia_affectation  
198 -  
199 - agents = lesia_session.query(lesia_agent).all()  
200 - for a in agents:  
201 - n_a = Agent(id=a.IDagent, firstname=a.nom, secondname=a.prenom)  
202 - db.session.add(n_a)  
203 - db.session.commit()  
204 -  
205 - services = lesia_session.query(lesia_service).all()  
206 - for s in services:  
207 - n_s = Service(id=s.id, name=s.nom, abbr=s.abbreviation)  
208 - db.session.add(n_s)  
209 - db.session.commit()  
210 -  
211 - projects = lesia_session.query(lesia_project).all()  
212 - for p in projects:  
213 - n_p = Project(id=p.id, name=p.nom)  
214 - db.session.add(n_p)  
215 - db.session.commit()  
216 -  
217 - fonctions = lesia_session.query(lesia_fonction).all()  
218 - for f in fonctions:  
219 - n_c = Capacity(id=f.id, name=f.nom)  
220 - db.session.add(n_c)  
221 - db.session.commit()  
222 -  
223 - periods = lesia_session.query(lesia_periods)  
224 - for p in periods:  
225 - n_p = Period(name=p.id_semestre)  
226 - db.session.add(n_p)  
227 - db.session.commit()  
228 -  
229 - affectations = lesia_session.query(lesia_affectation)  
230 - for f in affectations:  
231 - p = Period.query.filter(Period.name == f.semestre_id).one()  
232 - n_c = Charge(agent_id=f.agent_id,  
233 - project_id=f.projet_id,  
234 - service_id=f.service_id,  
235 - capacity_id=f.fonction_id,  
236 - period_id=p.id,  
237 - charge_rate=f.charge)  
238 - db.session.add(n_c)  
239 - db.session.commit()  
240 -  
241 -  
242 @bp.cli.command("feed_periods") 266 @bp.cli.command("feed_periods")
243 @click.option('--begin-year', '-b', 'begin_year', default=2005, help="the start year to begin periods with") 267 @click.option('--begin-year', '-b', 'begin_year', default=2005, help="the start year to begin periods with")
244 @click.option('--end-year', '-e', 'end_year', default=2025, help="the last year to end periods with") 268 @click.option('--end-year', '-e', 'end_year', default=2025, help="the last year to end periods with")
@@ -288,39 +312,6 @@ def feed_random_charges(agent): @@ -288,39 +312,6 @@ def feed_random_charges(agent):
288 db.session.commit() 312 db.session.commit()
289 313
290 314
291 -@bp.cli.command('user_delete')  
292 -@click.argument('user_id')  
293 -def user_delete(user_id):  
294 - """Delete the user by given id (see user_show_all")."""  
295 - user = User.query.get(user_id)  
296 - db.session.delete(user)  
297 - db.session.commit()  
298 -  
299 -  
300 -@bp.cli.command('create_db')  
301 -def create_db():  
302 - """  
303 - Create the database structure. Database should be empty.  
304 -  
305 - configure the proper database uri in the db_config.py file.  
306 - """  
307 - db.create_all()  
308 - admin = User(email='admin@nowhere.org', name='admin', login='admin', role='admin')  
309 - admin.set_password('admin')  
310 - sqlite_uri = db.engine.url.__str__() if 'sqlite' in db.engine.url.__str__() else None  
311 - try:  
312 - db.session.add(admin)  
313 - db.session.commit()  
314 - except IntegrityError:  
315 - current_app.logger.error("User admin already exists, database should be empty at create")  
316 - if sqlite_uri:  
317 - current_app.logger.error("see " + sqlite_uri)  
318 - sys.exit(-1)  
319 -  
320 - if sqlite_uri:  
321 - current_app.logger.info("Created sqlite db: " + sqlite_uri)  
322 -  
323 -  
324 @bp.cli.command('user_add') 315 @bp.cli.command('user_add')
325 @click.argument('email') 316 @click.argument('email')
326 @click.argument('name') 317 @click.argument('name')
@@ -339,6 +330,44 @@ def user_add(email, name, login, password, role): @@ -339,6 +330,44 @@ def user_add(email, name, login, password, role):
339 current_app.logger.info(f"added {name}") 330 current_app.logger.info(f"added {name}")
340 331
341 332
  333 +@bp.cli.command('user_update')
  334 +@click.option('--name', '-n', 'name', default=None, help="the name to set for that user")
  335 +@click.option('--role', '-r', 'role', default=None, help="the role to set for that user")
  336 +@click.option('--email', '-e', 'email', default=None, help="the email to set for that user")
  337 +@click.option('--password', '-p', 'password', default=None, help="the password to set for that user")
  338 +@click.argument('user_id')
  339 +def user_update(user_id, name, role, email, password):
  340 + """Update the user by given id and given parameters."""
  341 + user = User.query.get(user_id)
  342 + if not user:
  343 + current_app.logger.error(f"such user_id doesnt exists {user_id}")
  344 + return
  345 + if name:
  346 + user.name = name
  347 + print(f"User --{user.name}-- name updated to {user.name}")
  348 + if role:
  349 + user.set_role(role)
  350 + print(f"User --{user.name}-- role updated to {_roleToName[user.role]}")
  351 + if email:
  352 + user.email=email
  353 + print(f"User --{user.name}-- email updated to {user.email}")
  354 + if password:
  355 + print(f"User --{user.name}-- password updated")
  356 + user.set_password(password)
  357 + if not ( name or role or email or password):
  358 + print(f"No update for user --{user.name}--")
  359 + db.session.commit()
  360 +
  361 +
  362 +@bp.cli.command('user_delete')
  363 +@click.argument('user_id')
  364 +def user_delete(user_id):
  365 + """Delete the user by given id (see user_show_all")."""
  366 + user = User.query.get(user_id)
  367 + db.session.delete(user)
  368 + db.session.commit()
  369 +
  370 +
342 @bp.cli.command('show_roles') 371 @bp.cli.command('show_roles')
343 def show_roles(): 372 def show_roles():
344 """ List all available roles for a user""" 373 """ List all available roles for a user"""