Beispiel #1
0
 def create_user(user):
     logging.info('[SERVICE]: Creating user')
     email = user.get('email', None)
     password = user.get('password', None)
     password = ''.join(random.choices(string.ascii_uppercase + string.digits, k=20)) if password is None else password
     role = user.get('role', 'USER')
     name = user.get('name', 'notset')
     country = user.get('country', None)
     institution = user.get('institution', None)
     if role not in ROLES:
         role = 'USER'
     if email is None or password is None:
         raise Exception
     current_user = User.query.filter_by(email=user.get('email')).first()
     if current_user:
         raise UserDuplicated(message='User with email '+email+' already exists')
     user = User(email=email, password=password, role=role, name=name, country=country, institution=institution)
     try:
         logging.info('[DB]: ADD')
         db.session.add(user)
         db.session.commit()
         try:
             email = EmailService.send_html_email(
                 recipients=[user.email],
                 html='<p>User: '******'</p><p>Password: '******'</p>',
                 subject='[GEF] User created'
             )
         except EmailError as error:
             raise error
     except Exception as error:
         raise error
     return user
 def recover_password(user_id):
     logging.info('[SERVICE]: Recovering password' + user_id)
     logging.info('[DB]: QUERY')
     user = UserService.get_user(user_id=user_id)
     if not user:
         raise UserNotFound(message='User with id ' + user_id +
                            ' does not exist')
     password = ''.join(
         random.choices(string.ascii_uppercase + string.digits, k=20))
     user.password = user.set_password(password=password)
     try:
         logging.info('[DB]: ADD')
         db.session.add(user)
         db.session.commit()
         try:
             email = EmailService.send_html_email(
                 recipients=[user.email],
                 html='<p>User: '******'</p><p>Password: '******'</p>',
                 subject='[trends.earth] Recover password')
         except EmailError as error:
             raise error
     except Exception as error:
         raise error
     return user
 def update_execution(execution, execution_id):
     logging.info('[SERVICE]: Updating execution')
     status = execution.get('status', None)
     progress = execution.get('progress', None)
     results = execution.get('results', None)
     if status is None and progress is None and results is None:
         raise Exception
     execution = ExecutionService.get_execution(execution_id=execution_id)
     if not execution:
         raise ExecutionNotFound(message='Execution with id '+execution_id+' does not exist')
     if status is not None:
         execution.status = status
         if status == 'FINISHED' or status == 'FAILED':
             execution.end_date = datetime.datetime.utcnow()
             execution.progress = 100
             user = UserService.get_user(str(execution.user_id))
             script = ScriptService.get_script(str(execution.script_id))
             email = EmailService.send_html_email(
                 recipients=[user.email],
                 html=EXECUTION_FINISHED_MAIL_CONTENT.format(status, execution.params.get('task_name'), script.name, str(execution.id), execution.start_date, execution.end_date, status),
                 subject='[MISLAND] Execution finished'
             )
     if progress is not None:
         execution.progress = progress
     if results is not None:
         execution.results = results
     try:
         logging.info('[DB]: ADD')
         db.session.add(execution)
         db.session.commit()
     except Exception as error:
         raise error
     return execution