def add_user(first_name, last_name, username, password, method, is_hashed, email, timezone, preferred_languages, overwrite=False): logger.info("Creating the user in the database.") pwd_generated = False if password is None: assert not is_hashed password = generate_random_password() pwd_generated = True if is_hashed: stored_password = build_password(password, method) else: stored_password = hash_password(password, method) if preferred_languages is None or preferred_languages == "": preferred_languages = "[]" else: preferred_languages = \ "[" + ",".join("\"" + lang + "\"" for lang in preferred_languages.split(",")) + "]" user = User(first_name=first_name, last_name=last_name, username=username, password=stored_password, email=email, timezone=timezone, preferred_languages=preferred_languages) with SessionGen() as session: if overwrite: existing_user = session.query(User) \ .filter(User.username == username).first() if existing_user is not None: user = existing_user user.first_name = first_name user.last_name = last_name user.username = username if not pwd_generated: user.password = stored_password else: pwd_generated = False user.email = email or user.email user.timezone = timezone or user.timezone user.preferred_languages = preferred_languages or \ user.preferred_languages try: session.add(user) session.commit() except IntegrityError: logger.error("A user with the given username already exists.") return False logger.info("User added%s. " "Use AddParticipation to add this user to a contest." % (" with password %s" % password if pwd_generated else "")) return True
def _makeuser(self, username): """ Return a User object for the specified user which can be saved to the database. username (unicode): the name of the user to generate return (User,Participation): database object for the user """ user = self.users[username] # The user should never actually use this password, because we set # different passwords for each participation. udb = User(username=user.username, first_name=user.firstname, last_name=user.lastname, password=build_password(user.password)) udb.timezone = user.timezone udb.preferred_languages = user.primary_statements return udb
def _makeuser(self, username): """ Return a User object for the specified user which can be saved to the database. username (unicode): the name of the user to generate return (User,Participation): database object for the user """ user = self.users[username] # The user should never actually use this password, because we set # different passwords for each participation. udb = User(username=user.username, first_name=user.firstname, last_name=user.lastname, password=build_password(user.password)) udb.timezone = user.timezone udb.preferred_languages = user.primary_statements return udb