def update_addr(user_id, request): """更换收货地址""" name = request.form.get('name') tel = request.form.get('tel') address_list = request.form.get('address').strip().split(' ') details = request.form.get('details') mydb = ToMongo() value = { 'user_id': user_id, 'name': name, 'tel': tel, 'province': address_list[0], 'city': address_list[1], 'district': address_list[2], 'details': details, } conn = ToConn().to_execute() cur = conn.cursor() address_default = mydb.insert('address', value).inserted_id cur.execute('update users set address_default=%s where id=%s', (str(address_default), user_id)) if address_default: conn.commit() conn.close() else: conn.rollback() conn.close() mydb.close_conn()
def delete_addr(user_id, _id): """删除收货地址""" conn = ToConn().to_execute() cur = conn.cursor() db_conn = ToMongo() cur.execute( 'update users set address_default=null where id=%s and address_default = %s', (user_id, _id)) result = db_conn.delete(col='address', doc={ '_id': ObjectId(_id) }).raw_result if result['ok'] == 1: conn.commit() else: conn.rollback() db_conn.close_conn()