def set_head_picture(): ''' 图片名存储到数据库 ''' user = g.current_user # 获得图片的七牛云图片 # import ipdb; ipdb.set_trace() head_picture = get_qiniu_token(user.phone_number).json code, key, token = get_qiniu_token(user.phone_number).json.values() # 用户提交的图片 up_head_picture = request.values.get('head_picture') head_picture = 'http://pntn3xhqe.bkt.clouddn.com/{0}'.format(key) user.head_picture = head_picture # 图片上传 localfile = r'{0}'.format(up_head_picture) ret, info = put_file(token, key, localfile) try: db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '未能成功上传'}) redis_store.hset('user:{0}'.format(user.phone_number), 'head_picture', head_picture) return jsonify({'code': 1, 'message': '成功上传'})
def db_add(nick: str, date: str, title: str, text: str) -> None: new_post = Habr(nick, date, title, text) try: db_session.add(new_post) db_session.commit() except: db_session.rollback()
def del_colors(): getid = request.args.get('id') delcolors = db_session.query(ProColor).filter(ProColor.id == getid).first() colorpics = delcolors.colorpic.all() for picurl in colorpics: imgurl = picurl.picurl #获得图片物理地址 imgurl = actros_split(imgurl) delImage(imgurl) #删除物理图片 try: [db_session.delete(n) for n in colorpics] db_session.commit() except Exception as e: print (e) db_session.rollback() return jsonify({"state":"数据库错误"}) try: db_session.delete(delcolors) db_session.commit() except Exception as e: print (e) db_session.rollback() return jsonify({"state":"数据库错误"}) db_session.close() return jsonify({"state":'ok'})
def upbooks(): getid = request.form.getlist('id') manageid = current_user.id if len(getid) == 1: bookid = getid[0] upstatus = MuGb.query.filter_by(bookid=bookid, manageid=manageid).first() if upstatus: upstatus.status = 1 try: db_session.add(upstatus) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({"state": "数据库错误"}) else: manageid = [] for x in getid: manageid.append(current_user.id) for (getid, manageid) in zip(getid, manageid): try: db_session.query(MuGb).\ filter(MuGb.bookid == getid, MuGb.manageid == manageid).\ update({MuGb.status : 1}) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({"state": "数据库错误"}) return jsonify({'state': 'ok'})
def setdf_address(): id = request.get_json().get('id') userid = g.current_user.id addressd = Address.query.filter_by(userid=userid, default=1).first() if addressd: addressd.default = 0 try: db_session.add(addressd) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) setdefault = Address.query.filter_by(id=id).first() setdefault.default = 1 try: db_session.add(setdefault) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) return jsonify({'code': 1, 'message': '设置默认成功'})
def db_add(firm: str, args: list) -> None: new_org = ListOrg(firm, args[0], args[1], args[2], args[3], args[4], args[5], args[6]) try: db_session.add(new_org) db_session.commit() except: db_session.rollback()
def synchronize_players(clan_id): logger.info("synchronize_players(" + clan_id + ")") logger.info("Clan member synchronization triggered for " + str(clan_id)) from model import db_session clan_info = get_clan_info.delay(str(clan_id)).wait() logger.info("Synchronizing " + clan_info['data'][str(clan_id)]['abbreviation']) processed = set() for player_id in clan_info['data'][str(clan_id)]['members']: player = clan_info['data'][str(clan_id)]['members'][player_id] player_data = get_player_info.delay(str(player['account_id'])).wait() p = Player.query.filter_by(wot_id=str(player['account_id'])).first() if not player_data: if p: processed.add(p.id) # skip this guy later when locking players continue # API Error? since = datetime.datetime.fromtimestamp( float(player_data['data'][str(player['account_id'])]['clan']['since'])) if p: # Player exists, update information processed.add(p.id) p.locked = False p.clan = clan_info['data'][str(clan_id)]['abbreviation'] p.role = player['role'] # role might have changed p.member_since = since # might have rejoined else: # New player p = Player(str(player['account_id']), 'https://eu.wargaming.net/id/' + str(player['account_id']) + '-' + player[ 'account_name'] + '/', since, player['account_name'], clan_info['data'][str(clan_id)]['abbreviation'], player['role']) logger.info('Adding player ' + player['account_name']) db_session.add(p) # Lock players which are no longer in the clan for player in Player.query.filter_by(clan=clan_info['data'][str(clan_id)]['abbreviation']): if player.id in processed or player.id is None or player.locked: continue logger.info("Locking player " + player.name) player.locked = True player.lock_date = datetime.datetime.now() db_session.add(player) try: db_session.commit() logger.info("Clan member synchronization successful") except Exception as e: logger.warning("Clan member synchronization failed. Rolling back database transaction:") logger.exception(e) db_session.rollback() finally: db_session.remove() return True
def upst_to_st(orderid, state, text): orderstate = OrderState(orderid=orderid, state=state, text=text, uptime=datetime.datetime.now()) try: db_session.add(orderstate) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({"state": "数据库错误"})
def del_cart(): getid = request.get_json().get('cartid') thiscart = UserCart.query.filter_by(id=getid).first() db_session.delete(thiscart) try: db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 3, 'message': '删除失败'}) return jsonify({'code': 1, 'message': '删除成功'})
def del_address(): id = request.get_json().get('id') delad = Address.query.filter_by(id=id).first() db_session.delete(delad) try: db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) db_session.close() return jsonify({'code': 1, 'message': '删除成功'})
def login(): phone_number = request.get_json().get('phone_number') password = request.get_json().get('password') touchid = request.get_json().get('touchid') user = Users.query.filter_by(phone=phone_number).first() if not user: return jsonify({'code': 0, 'message': '没有此用户'}) if user.password != password: return jsonify({'code': 0, 'message': '密码错误'}) if user.verify != 0: return jsonify({'code': 0, 'message': '账户还没有通过审核'}) if user.lock != 0: return jsonify({'code': 0, 'message': '账户被锁定'}) if user.touchid != touchid or user.touchid != None: user.touchid = touchid try: db_session.add(user) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) m = hashlib.md5() m.update(str(phone_number).encode('utf-8')) m.update(str(password).encode('utf-8')) m.update(str(int(time.time())).encode('utf-8')) token = m.hexdigest() pipeline = current_app.redis.pipeline() pipeline.hmset('user:%s' % user.phone, { 'token': token, 'company': user.company, 'app_online': 1 }) pipeline.set('token:%s' % token, user.phone) pipeline.expire('token:%s' % token, 3600 * 24 * 30) pipeline.execute() return jsonify({ 'code': 1, 'message': '成功登录', 'userid': user.id, 'token': token })
def cancel_order(): orderid = request.get_json().get('id') cancel = Order.query.filter_by(id=orderid).first() if cancel: cancel.state = 99 try: db_session.add(cancel) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 3, 'message': '数据库错误'}) return jsonify({'code': 1, 'message': '订单取消成功'})
def close_touch(): userid = g.current_user.id touchid = None userinfo = Users.query.filter_by(id=userid).first() if userinfo: userinfo.touchid = touchid try: db_session.add(userinfo) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) return jsonify({'code': 1, 'message': '指纹关闭成功'})
def change_password(): userid = g.current_user.id password = request.get_json().get('password') userinfo = Users.query.filter_by(id=userid).first() if userinfo: userinfo.password = password try: db_session.add(userinfo) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) return jsonify({'code': 1, 'message': '修改密码成功'})
def my_info(): getid = request.args.get('getid') adminData = Manage.query.filter_by(id = getid).first() form = MyInfoForm() if adminData: form.getid.data = adminData.id form.username.data = adminData.username form.title.data = adminData.title form.name.data = adminData.name form.phone.data = adminData.phone form.mail.data = adminData.mail form.wechat.data = adminData.wechat if form.validate_on_submit(): getid = request.form.get('getid') password = request.form.get('password') title = request.form.get('title') name = request.form.get('name') phone = request.form.get('phone') mail = request.form.get('mail') wechat = request.form.get('wechat') thuser = Manage.query.filter_by(id = getid).first() thuser.title = title thuser.name = name thuser.phone = phone thuser.mail = mail thuser.wechat = wechat try: db_session.add(thuser) db_session.commit() except Exception as e: print (e) db_session.rollback() return jsonify({"state":"数据库错误"}) db_session.close() flash("修改成功,<span id='time'>3</span>秒后自动跳转管理页。") return redirect('/manage/my_info') return render_template( "admin_info.html", pagename='manage_user', form=form)
def post_userinfo(): userid = g.current_user.id truename = request.get_json().get('truename') mail = request.get_json().get('mail') userinfo = Users.query.filter_by(id=userid).first() if userinfo: userinfo.truename = truename userinfo.mail = mail try: db_session.add(userinfo) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) return jsonify({'code': 1, 'message': '修改资料成功'})
def dellike(): userid = g.current_user.id proid = request.get_json().get('proid') thislike = UserLike.query.filter_by(like_porid=proid, user_id=userid).first() if thislike: db_session.delete(thislike) try: db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 3, 'message': '取消收藏失败'}) return jsonify({'code': 1, 'message': '取消收藏成功'}) return jsonify({'code': 3, 'message': '没有该产品'})
def add_classify(): this = 'add' topid = request.args.get('classid', 0) form = AddclassifyForm() if form.validate_on_submit(): icon = request.form.get('icon') topid = request.form.get('topid') sort = request.form.get('sort') classname = request.form.get('classname') ctype = request.form.get('ctype') display = request.form.get('display') if icon == '' or icon == None: icon = 0 classify = Classify(topid=topid, classname=classname, icon=icon, ctype=ctype, display=display, sort=sort, uptime=datetime.datetime.now()) db_session.query(Classify) if len(classname): try: db_session.add(classify) db_session.commit() # 记录日志 actions = ('%s%s%s' % ('增加分类', ':', classname)) savelog(actions) except Exception as e: print(e) db_session.rollback() flash("数据库错误!") return redirect('/manage/add_classify') flash("添加成功,<span id='time'>3</span>秒后自动跳转管理页。") return redirect('/manage/add_classify') return render_template("edit_classify.html", pagename='manage_classify', this=this, topid=topid, form=form)
def post_book(): userid = g.current_user.id content = request.get_json().get('getnav') # print userid if len(content) > 0: addbook = Guestbook(userid=userid, content=content, addtime=datetime.datetime.now()) try: db_session.add(addbook) db_session.commit() db_session.flush() gbid = addbook.id teamid = g.current_user.teamid manageid = db_session.query(Manage).filter( or_(Manage.teamid == teamid, Manage.group_id == 1)).all() # print len(manageid) if manageid: glbooks = [ MuGb(manageid=manageid[i].id, bookid=gbid, status=0) for i in range(len(manageid)) ] db_session.query(MuGb).first() try: db_session.add_all(glbooks) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({"state": "数据库错误"}) except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) db_session.close() return jsonify({'state': 'ok'})
def up_cart_cont(): getid = request.get_json().get('cartid') getunit = request.get_json().get('unit') # print getid,getunit thiscart = UserCart.query.filter_by(id=getid).first() if thiscart: color_total = getunit * thiscart.price thiscart.unit = getunit thiscart.color_total = color_total db_session.add(thiscart) try: db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 3, 'message': '更新失败'}) return jsonify({'code': 1, 'message': '更新成功'})
def open_touch(): userid = g.current_user.id m = hashlib.md5() m.update(str(request.get_json().get('touchid')).encode('utf-8')) touchid = m.hexdigest() print(touchid) userinfo = Users.query.filter_by(id=userid).first() if userinfo: userinfo.touchid = touchid try: db_session.add(userinfo) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) return jsonify({'code': 1, 'message': '指纹开启成功'})
def register_step_4(): """ 基本资料提交 """ phone_number = request.get_json().get('phone_number') mail = request.get_json().get('mail') company = request.get_json().get('company') truename = request.get_json().get('truename') is_validate = current_app.redis.get('is_validate:%s' % phone_number).decode('utf-8') if is_validate != '1': return jsonify({'code': 0, 'message': '验证码没有通过'}) password = current_app.redis.hget('register:%s' % phone_number, 'password').decode('utf-8') new_user = Users(phone=phone_number, password=password, mail=mail, company=company, truename=truename, lock=0, verify=1, teamid=0, addtime=datetime.datetime.now()) db_session.add(new_user) try: db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '注册失败'}) finally: current_app.redis.delete('is_validate:%s' % phone_number) current_app.redis.delete('register:%s' % phone_number) return jsonify({'code': 1, 'message': '注册成功,请等待审核'})
def ilike(): userid = g.current_user.id proid = request.get_json().get('proid') thislike = UserLike.query.filter_by(like_porid=proid, user_id=userid).first() if thislike: return jsonify({'code': 2, 'message': '已经收藏了'}) user_like = UserLike(user_id=userid, like_porid=proid, add_time=datetime.datetime.now()) db_session.add(user_like) try: db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 3, 'message': '收藏失败'}) return jsonify({'code': 1, 'message': '收藏成功'})
def del_many_cart(): getid = request.get_json().get('cartid') delg = db_session.query(UserCart).filter(UserCart.id.in_((getid))).all() if delg: order_type = delg[0].order_type try: [db_session.delete(n) for n in delg] db_session.commit() db_session.close() except Exception as e: print(e) db_session.rollback() return jsonify({ 'code': 3, 'message': '删除失败', 'order_type': order_type }) return jsonify({ 'code': 1, 'message': '删除成功', 'order_type': order_type })
def edit_Address(): id = request.get_json().get('id') userid = g.current_user.id contacts = request.get_json().get('contacts') phone_number = request.get_json().get('phone_number') address = request.get_json().get('address') default = request.get_json().get('default') if int(default) == 1: addressd = Address.query.filter_by(userid=userid, default=default).first() if addressd: if addressd.id != id: addressd.default = 0 try: db_session.add(addressd) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) editaddress = Address.query.filter_by(id=id).first() editaddress.contacts = contacts editaddress.phone_number = phone_number editaddress.address = address editaddress.default = default try: db_session.add(editaddress) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) db_session.close() return jsonify({'code': 1, 'message': '修改成功'})
def del_onlypro_pic (thpro,thisdata): for pic in thisdata: colorpics = pic.colorpic.all() for picurl in colorpics: imgurl = picurl.picurl #获得图片物理地址 imgurl = actros_split(imgurl) delImage(imgurl) #删除物理图片 try: [db_session.delete(n) for n in colorpics] db_session.commit() except Exception as e: print (e) db_session.rollback() return jsonify({"state":"数据库错误"}) try: db_session.delete(thpro) [db_session.delete(n) for n in thisdata] db_session.commit() except Exception as e: print (e) db_session.rollback() return jsonify({"state":"数据库错误"})
def up_contact(): getid = int(request.args.get('id')) status = int(request.args.get('status')) if status == 0: actioni = '在联系我们中显示用户:' else: actioni = '在联系我们中隐藏用户:' upthis = db_session.query(Manage).filter(Manage.id == getid).first() if upthis: upthis.contact = status db_session.add(upthis) try: db_session.add(upthis) db_session.commit() # 记录日志 actions = ('%s%s' % (actioni, upthis.username)) savelog(actions) except Exception as e: print(e) db_session.rollback() return jsonify({"state": "数据库错误"}) db_session.close() return jsonify({"state": "ok"})
def forget_3(): """ 密码提交 """ phone_number = request.get_json().get('phone_number') password = request.get_json().get('password') password_confirm = request.get_json().get('password_confirm') if len(password) < 6 or len(password) > 30: # 这边可以自己拓展条件 return jsonify({'code': 0, 'message': '密码长度不符合要求'}) if password != password_confirm: return jsonify({'code': 0, 'message': '密码和密码确认不一致'}) is_validate = current_app.redis.get('is_validate:%s' % phone_number).decode('utf-8') if is_validate != '1': return jsonify({'code': 0, 'message': '验证码没有通过'}) userinfo = Users.query.filter_by(phone=phone_number).first() if userinfo: userinfo.password = password try: db_session.add(userinfo) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) finally: current_app.redis.delete('is_validate:%s' % phone_number) current_app.redis.delete('register:%s' % phone_number) return jsonify({'code': 1, 'message': '修改密码成功'})
def add_recommend(): this = 'add' topid = request.args.get('id', 0) form = AddrecommendForm() if form.validate_on_submit(): icon = request.form.get('icon') topid = request.form.get('topid') sort = request.form.get('sort') titles = request.form.get('titles') if icon == '' or icon == None: icon = 0 recommend = Recommend(topid=topid, titles=titles, icon=icon, sort=sort) db_session.query(Recommend) if len(titles): try: db_session.add(recommend) db_session.commit() # 记录日志 actions = ('%s%s%s' % ('增加推荐类型', ':', titles)) savelog(actions) except Exception as e: print(e) db_session.rollback() flash("数据库错误!") return redirect('/manage/add_recommend') flash("添加成功,<span id='time'>3</span>秒后自动跳转管理页。") return redirect('/manage/add_recommend') return render_template("edit_recommend.html", pagename='manage_recommend', this=this, topid=topid, form=form)
def add_address(): userid = g.current_user.id contacts = request.get_json().get('contacts') phone_number = request.get_json().get('phone_number') address = request.get_json().get('address') default = request.get_json().get('default') if int(default) == 1: addressd = Address.query.filter_by(userid=userid, default=default).first() if addressd: addressd.default = 0 try: db_session.add(addressd) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) addressa = Address(userid=userid, contacts=contacts, phone_number=phone_number, address=address, default=default) try: db_session.add(addressa) db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '数据库错误'}) db_session.close() return jsonify({'code': 1, 'message': '添加成功'})
def my_password(getid): form = MyPassForm() if form.validate_on_submit(): m = hashlib.md5() m.update(request.form.get('password')) password = m.hexdigest() mypassword = Manage.query.filter_by(id = getid).first() if mypassword: mypassword.password = password try: db_session.add(mypassword) db_session.commit() except Exception as e: print (e) db_session.rollback() return jsonify({"state":"数据库错误"}) db_session.close() flash("修改成功,<span id='time'>3</span>秒后自动跳转管理页。") return redirect(url_for('manage.my_password', getid = getid)) return render_template( "my_password.html", pagename='manage_user', form=form)