def get_my_ip(uid, priv): gc = GlobalConfig() _url = "http://whatismyip.akamai.com/" if gc.get("my_ip_address") == "": req = Request(url=_url) resp = urlopen(req) ip_addr = resp.read().decode() # store ip address into cache gc.set("my_ip_address", ip_addr) return rtn.success(ip_addr) else: return rtn.success(gc.get("my_ip_address"))
def migrate_superadmin(): ''' This function aims to migrate superadmin's account data (including username, email , password hash) from temporal SQLite database to main database. Why exists? At the beginning, database setting has not been configured yet. Thus it's impossible to store superadmin's account data to user's database directly. How it works? read superadmin's account data from GlobalConfig database (in which the data is stored when step 1 is done.) and run init_database() to ensure SQLAlchemy API is available. Next, just use the API to insert data and delete the original one since there's no reason to keep it then. :return: ''' if app.config.get("SQLALCHEMY_DATABASE_URI") == None: # ensure main database is initialized and SQLAlchemy available. init_database() # read data from GlobalConfig database gc = GlobalConfig() _username = gc.get("temp_superadmin_username") _email = gc.get("temp_superadmin_email") _hash = gc.get("temp_superadmin_hash") #for superadmin, privilege = 1 try: super_admin_user = Users(username=_username, privilege=PRIVILEGES.ROOT_USER, email=_email, hash=_hash) try: super_admin_user.insert_byhash() except: traceback.print_exc() # if everything works correctly <including the inserting operation above>, # it is time to delete account data gc.set("temp_superadmin_username", "") gc.set("temp_superadmin_email", "") gc.set("temp_superadmin_hash", "") # for empty value, just emit it return True except: g_logger.error(traceback.format_exc()) return False