def add_admin(username, password=None): logger.info("Creating the admin on the database.") generated = False if password == EmptyPassword.GENERATE or password is None: password = generate_random_password() generated = True elif password == EmptyPassword.PROMPT: password = getpass.getpass() admin = Admin(username=username, authentication=hash_password(password), name=username, permission_all=True) try: with SessionGen() as session: session.add(admin) session.commit() except IntegrityError: logger.error("An admin with the given username already exists.") return False if generated: logger.info("Admin with complete access added. " "Login with username %s and password %s", username, password) else: logger.info("Admin with complete access added. " "Login with username %s and supplied password", username) return True
def add_admin(username, password=None, real_name=None): logger.info("Creating the admin on the database.") if password is None: password = generate_random_password() admin = Admin(username=username, authentication=hash_password(password), name=real_name or username, permission_all=True) try: with SessionGen() as session: session.add(admin) session.commit() except IntegrityError: logger.error("An admin with the given username already exists.") return False logger.info("Admin '%s' with complete access added. ", username) return True
def post(self): fallback_page = self.url("admins", "add") try: attrs = _admin_attrs(self) assert attrs.get("authentication") is not None, \ "Empty password not permitted." admin = Admin(**attrs) self.sql_session.add(admin) except Exception as error: self.service.add_notification(make_datetime(), "Invalid field(s)", repr(error)) self.redirect(fallback_page) return if self.try_commit(): self.redirect(self.url("admins")) else: self.redirect(fallback_page)