def register_user(nickname, email, password):
	if not nickname or not email:
		raise AccountWithEmptyField()

	mysql = FreifunkMySQL()
	user_with_nick  = mysql.findone("SELECT id, email FROM users WHERE nickname = %s LIMIT 1",(nickname,))
	user_with_email  = mysql.findone("SELECT id FROM users WHERE email = %s LIMIT 1",(email,),"id")
	pw = generate_password_hash(password)
	if user_with_email:
		mysql.close()
		raise AccountWithEmailExists()
	elif user_with_nick and user_with_nick["email"]:
		mysql.close()
		raise AccountWithNicknameExists()
	else:
		time = mysql.utcnow()
		if user_with_nick:
			mysql.execute("""
				UPDATE users
				SET password = %s, email = %s, created = %s, token = NULL
				WHERE id = %s
				LIMIT 1
			""",(pw,email,time,user_with_nick["id"],))
			mysql.commit()
			mysql.close()
			return user_with_nick["id"]
		else:
			mysql.execute("""
				INSERT INTO users (nickname, password, email, created, token)
				VALUES (%s, %s, %s, %s, NULL)
			""",(nickname,pw,email,time,))
			userid = mysql.cursor().lastrowid
			mysql.commit()
			mysql.close()
			return userid
示例#2
0
import datetime

client = MongoClient(tz_aware=True, connect=False)
db = client.freifunk

users = db.users.find({}, {
    "nickname": 1,
    "password": 1,
    "email": 1,
    "token": 1,
    "created": 1,
    "admin": 1
})

mysql = FreifunkMySQL()
cur = mysql.cursor()
for u in users:
    #print(u)
    cur.execute(
        """
		INSERT INTO users (nickname, password, token, email, created, admin)
		VALUES (%s, %s, %s, %s, %s, %s)
	""", (
            u.get("nickname"),
            u.get("password"),
            u.get("token"),
            u.get("email", ""),
            u.get("created"),
            u.get("admin", 0),
        ))
mysql.commit()