async def update_device_name(args_dict): """ Функция принимает имя устройства и обновляет файл и данные в редис """ name = args_dict["name"] with open("device.name", "w") as f: f.write(name) redis_client.set("device_name", name)
def _update_device_name(): try: with open("device.name", "r") as f: device_name = f.read() redis_client.set("device_name", device_name) except FileNotFoundError: with open("device.name", "w") as f: device_name = platform.node() f.write(device_name) redis_client.set("device_name", device_name)
def set_access_token(access_token, ex=None): """ 将 access_token 存入 redis 中 :param access_token: 微信返回的 access_token :param ex: 过期时间,微信返回的 expires_in,单位为秒,过期时间减60秒保证及时刷新 """ redis_client.set(RedisConfig.Key.access_token, access_token, ex=ex - 60 if ex else ex)
def set_jsapi_ticket(ticket, ex=None): """ 将 jsapi ticket 存入 redis 中 :param ticket: 微信返回的 ticket :param ex: 过期时间,微信返回的 expires_in,单位为秒,过期时间减60秒保证及时刷新 """ redis_client.set(RedisConfig.Key.jsapi_ticket, ticket, ex=ex - 60 if ex else ex)
def new_client_method(client_instance, *args, **kwargs): redis_key = _make_key(key_format, client_method, args, kwargs) cached = redis_client.get(redis_key) if cached: return json.loads(cached.decode('utf-8')) api_response = client_method(client_instance, *args, **kwargs) redis_client.set( redis_key, json.dumps(api_response), ex=TTL, ) return api_response
def sign_in(): """Log the user into the system.""" # Attempt to process the form form = forms.FormSignIn() if form.validate_on_submit(): # The login was correct if login.confirm(form.email.data, form.password.data): # Generate a unique login token for this session signin_token = token_hex(16) session["signin_token"] = signin_token # Sign the user in, remembering their login if requested user = AuthUser(form.email.data, signin_token) login_user(user, remember=form.remember_me.data) # Make a record of the user session database.user_record_login_time(form.email.data) # Record the user session and set it to expire # at the default expire time redis_key = redis_utils.make_key(redis_utils.RedisKeys.UserSession, user.username, signin_token, "active") redis_client.setex(redis_key, redis_utils.KEY_EXPIRE_TIME, "true") # Load the user info and store it in redis and store the data # as individual keys in redis. This is not an ideal format, but it # is what must be done until redis-py > 3.4.1 is released, which # modifies the hset() signature to add a mapping= param to # set the entire dictionary at once. See GH #66. user_data = database.user_load_full_data(form.email.data) for k, v in user_data.items(): redis_client.set( redis_utils.make_key(redis_utils.RedisKeys.UserData, user.username, signin_token, k), v, ) # Have the user select their current campus return redirect(url_for("root.campus_select")) # If the login info was not valid, let the user know flash("The username or password was incorrectly entered.", "error") return redirect(url_for("root.index")) # The form was not filled out flash("You must sign in to continue. 😉", "error") return redirect(url_for("root.index"))
def create_job(self, job_id, service_id, scheduled_for=None): data = {"id": job_id} if scheduled_for: data.update({'scheduled_for': scheduled_for}) data = _attach_current_user(data) job = self.post(url='/service/{}/job'.format(service_id), data=data) redis_client.set( 'has_jobs-{}'.format(service_id), b'true', ex=cache.TTL, ) return job
def create_job(self, job_id, service_id, scheduled_for=None): data = {"id": job_id} if scheduled_for: data.update({"scheduled_for": scheduled_for}) data = _attach_current_user(data) job = self.post(url="/service/{}/job".format(service_id), data=data) redis_client.set( "has_jobs-{}".format(service_id), b"true", ex=cache.TTL, ) stats = self.__convert_statistics(job["data"]) job["data"]["notifications_sent"] = stats["delivered"] + stats["failed"] job["data"]["notifications_delivered"] = stats["delivered"] job["data"]["notifications_failed"] = stats["failed"] job["data"]["notifications_requested"] = stats["requested"] return job
def _update_mac(interface): mac_address = netifaces.ifaddresses(interface)[ netifaces.AF_LINK][0]['addr'] redis_client.set("mac_address", mac_address)
def _update_ip(interface): ip_address = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr'] redis_client.set("ip_address", ip_address)
def register_last_email_login_datetime(self, user_id): redis_client.set( self._last_email_login_key_name(user_id), datetime.utcnow().isoformat(), ex=int(timedelta(days=30).total_seconds()), )