def nginxadd(): if request.method == 'POST': domain=request.form['domain'].strip() ipsstr=request.form['ips'].strip() ips=ipsstr.split(',') #校验IP ipflag=0 from app.utils.verify import verify_ip for ip in ips: if verify_ip(ip) is False: ipflag=ipflag+1 flash(u'IP校验失败,请检查') #校验域名 from app.utils.verify import verify_domain if verify_domain(domain) is False: flash(u'域名校验失败,请检查') #域名是否已经存在 domainexists=redis_store.hexists('nginxip',domain) if domainexists is True: flash(u'域名已经存在,不能新增添加,只能编辑') #域名和ip都校验通过后则进行保存到redis动作 if verify_domain(domain) == True and ipflag == 0 and domainexists == False: redis_store.hset('nginxip',domain,ipsstr) return redirect(url_for('nginx')) return render_template('nginx/add.html')
def request_chat(message): user_id = str(request.cookies.get('user_id')) friend_id = str(message['friend_id']) room_id = '-'.join([user_id, friend_id]) redis_store.hset('active_chat', user_id, friend_id) join_room(room_id) received = Messages.query.filter_by(sender=friend_id, receiver=user_id).all() sent = Messages.query.filter_by(sender=user_id, receiver=message['friend_id']).all() all_messages = received + sent all_messages.sort(key=lambda x: x.id, reverse=False) emit('chat', { 'chat': json.dumps([{ 'message': message.body, 'sender': message.sender } for message in all_messages]) }, broadcast=False)
def nginxedit(domain): if request.method == 'POST': ipsstr=request.form['ips'].strip() ips=ipsstr.split(',') #编辑时只允许编辑IP #校验IP from app.utils.verify import verify_ip ipflag=0 for ip in ips: if verify_ip(ip) is False: ipflag=ipflag+1 flash(u'IP校验失败,请检查') #ip都校验通过后则进行保存到redis动作 if ipflag == 0: redis_store.hset('nginxip',domain,ipsstr) return redirect(url_for('nginx')) #如果不是post则是get,并做如下动作 k=domain v=redis_store.hgetall('nginxip')[k] r = {'k':k,'v':v} return render_template('nginx/edit.html',r=r)
def get_proxy(): len = redis_store.hlen(KEY) if len < LIMIT: rsp = requests.get(TAI_YANG_PROXY.format(num=NUM)) data = rsp.json() if 0 == data["code"]: proxies = data["data"] for proxy in proxies: ip_port = "{ip}:{port}".format(ip=proxy["ip"], port=proxy["port"]) redis_store.hset(KEY, ip_port, 1)
def profile(): form = ProfileForm() if form.validate_on_submit(): # добавляем данные текущего пользователя redis_store.hset(current_user.id, 'username', form.username.data) redis_store.hset(current_user.id, 'role', form.role.data) flash('Данные сохранены!') return redirect(url_for('auth.profile')) elif request.method == 'GET': form.username.data = current_user.username form.role.data = current_user.role return render_template('auth/profile.html', form=form)
def register(): # if current_user.is_authenticated: # return redirect(url_for('main.index')) form = RegistrationForm() if form.validate_on_submit(): # # добавляем текущий email в базу emails для проверки # redis_store.sadd('emails', form.email.data) # добавляем данные текущего пользователя id = 0 if redis_store.get('counter') == None else int( redis_store.get('counter')) redis_store.hset(id, 'username', form.username.data) #redis_store.hset(id, 'email', form.email.data) redis_store.hset(id, 'password_hash', generate_password_hash(form.password.data)) redis_store.hset(id, 'operating_mode', 'test') redis_store.hset(id, 'role', 'user') # добавляем пользователя в список пользователей сайта redis_store.hset('site_users', id, id) flash( Markup('<strong>' + form.username.data + '</strong> успешно зарегистрирован!')) redis_store.set('counter', id + 1) return redirect(url_for('auth.login')) return render_template('auth/register.html', form=form)
def showcart(): uid = request.cookies.get('sessionuid') cart = {} if uid is not None: cart = redis_store.hgetall('cart:' + uid) newcart = {} total = 0 # print type(cart.items()) for x in cart: product = Product.query.filter_by(id=x).first() print "TOTAL" print total # if product is not None: # ensure that admin has not deleted the product while it was in someones cart a = {} oos = {} deleted = {} product = checkMinimumPrice(product) print product.name print '--------SQ---' print product.quantity print '--------PQ---' print int(cart[x]) if product.quantity > 0: #see if product is in stock on the backend a['stockquantity'] = product.quantity a['quantity'] = int(cart[x]) a['importantmessage'] = '' if a['stockquantity'] < a['quantity']: #insufficient stock, auto update stock on redis and cart page a['quantity'] = a['stockquantity'] a['importantmessage'] = "Quantity changed from " + str( cart[x]) + "->" + str(a['stockquantity']) redis_store.hset('cart:' + uid, x, a['stockquantity']) print a['importantmessage'] a['name'] = product.name a['id'] = product.id a['maxprice'] = product.price a['status'] = 'available' #Dealing with coupon if product.minpricecoupon is not 0: a['minprice'] = product.minpricecoupon.afterprice if product.minpricecoupon.upto > 0: a['couponused'] = product.minpricecoupon.name + " upto Rs." + product.minpricecoupon.upto else: a['couponused'] = product.minpricecoupon.name else: a['minprice'] = product.price a['couponused'] = '' a['total'] = a['quantity'] * a['minprice'] total += a['total'] newcart[x] = a else: # if product is not in stock oos['quantity'] = int(cart[x]) oos['name'] = product.name oos['id'] = product.id oos['maxprice'] = product.price oos['couponused'] = '' oos['minprice'] = product.price if product.quantity == -1: #Deleted product oos['status'] = 'deleted' oos['quantity'] = 0 else: #Out of Stock oos['status'] = 'oos' newcart[x] = oos # else: #if admin has deleted the product, auto-delete it from cart # rare case, that it has been removed from database # redis_store.hset('cart:' + uid,x,-1) return render_template("cart.html", title="Your Cart", cart=newcart, total=total)
def wrapper(*args, **kwargs): args_data = [data for data in args] #args_data = ['origin','faculty','page','force_reload'] ''' 若是获取LIST,参数为 origin,faculty,page,force_reload 若是获取DETAIL,参数为 url,request_type ''' if storage_type == 'list': # 缓存新闻列表 name = args_data[0] # origin if name in ['xy', 'jw']: # 将xy,jw与xb区分,方便进行缓存 key = args_data[2] # page else: key = "%s_%s" % (args_data[1], args_data[2]) # faculty_page data = redis_store.hget(name, key) if args_data[3]: data = func(*args, **kwargs) redis_store.hset(name, key, str(data)) redis_store.expire(name, 2592000) # 缓存过期时间为30天 elif data: data = ast.literal_eval(bytes.decode(data)) redis_store.expire(name, 2592000) return data else: return {} ''' else: data = func(*args, **kwargs) redis_store.hset(name, key, str(data)) redis_store.expire(name, 2592000) # 缓存过期时间为30天 ''' elif storage_type == 'detail': # 缓存新闻详细 url = args[0] list_length = len(args) # url result_1 = re.search('http://(.*?)/', url) # 用于判断是是否是教务处新闻详细 if result_1.group(1) == 'www.gdust.cn': result_2 = re.search('(\d{8})\/(\d{1,4})', url) kw = result_2.group(1) + result_2.group(2) else: kw = re.search('\&id\=(\d{1,3})', url).group(1) data = redis_store.get(kw) if args[1]: data = func(*args, **kwargs) redis_store.set(kw, str(data)) redis_store.expire(kw, 2592000) elif data: data = ast.literal_eval(bytes.decode(data)) redis_store.expire(kw, 2592000) return data else: return {} ''' else: data = func(*args, **kwargs) redis_store.set(kw, str(data)) redis_store.expire(kw, 2592000) ''' return data