Beispiel #1
0
def add_visits(user_id):
    """访问量加1函数"""
    db_conn = ToMongo()
    dawn_timestamp = get_dawn_timestamp()
    last_date = db_conn.get_col('visits').aggregate([{
        '$group': {
            '_id': '$_id',
            'day': {
                '$last': '$date'
            }
        }
    }])
    date_list = list(last_date)
    # 不为空
    if date_list:
        # 同一天加入
        if date_list[0]['day'] == get_dawn_timestamp():
            db_conn.update('visits', {'_id': date_list[0]['_id']},
                           {'$addToSet': {
                               'users_id': user_id
                           }})
        else:
            # 不是同一天,插入新的文档
            db_conn.insert('visits', {
                'date': dawn_timestamp,
                'users_id': [user_id]
            })
    else:
        # 为空也插入
        db_conn.insert('visits', {
            'date': dawn_timestamp,
            'users_id': [user_id]
        })
Beispiel #2
0
def get_user_addr_info(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')
    _id = request.form.get('_id')
    db_conn = ToMongo()
    result = db_conn.insert(
        'address', {
            'name': name,
            'tel': tel,
            'province': address_list[0],
            'city': address_list[1],
            'district': address_list[2],
            'details': details,
            'user_id': user_id
        })
    if result.inserted_id:
        conn = ToConn()
        to_exec = conn.to_execute()
        cur = to_exec.cursor()
        r = cur.execute('update users set address_default=%s where id=%s',
                        (str(result.inserted_id), user_id))
        if r:
            to_exec.commit()
            to_exec.close()
        else:
            to_exec.rollback()
            to_exec.close()
        conn.to_close()
    db_conn.close_conn()
    return r
Beispiel #3
0
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()
Beispiel #4
0
def evaluate_model(user_id, user_name, request, book_ids):
    """用户评论模型"""
    rlt = {}
    order_no = request.get('order_no')
    star = int(request.get('star'))
    context = request.get('context')
    anonymous = request.get('anonymous')
    if not star:
        rlt['error'] = '评分不能为空'
        return rlt
    img_path = ''
    create_time = get_now()
    if anonymous:
        user_name = user_name[0] + '**'
    else:
        user_name = user_name[0] + '*' + user_name[-1]
    id_list = []
    mydb = ToMongo()
    for book_id in book_ids:
        value = {'$addToSet':
                     {'comment':
                          {'order_no': order_no,
                           'star': star,
                           'context': context,
                           'user_id': user_id,
                           'user_name': user_name,
                           'img_path': img_path,
                           'create_time': create_time
                           }
                      }
                 }
        result = mydb.update('evaluate', {'_id': ObjectId(book_id)}, value)
        if result.modified_count:
            id_list.append(result.modified_count)
        else:
            # 无评论时,直接插入
            rel = mydb.insert('evaluate',
                              {'_id': ObjectId(book_id),
                               'comment':
                                   [
                                       {'order_no': order_no,
                                        'star': star,
                                        'context': context,
                                        'user_id': user_id,
                                        'user_name': user_name,
                                        'img_path': img_path,
                                        'create_time': create_time
                                        }
                                   ]
                               })
            id_list.append(rel.inserted_id)
        # print(id_list)
    if len(id_list) != len(book_ids):
        # print(id_list)
        rlt['error'] = '评论失败,请重试!'
    mydb.close_conn()
    return rlt
Beispiel #5
0
def admin_register(email, password, auth_list):
    user = {
        'email': email,
        'password': generate_password_hash(password),
        'is_effective': 1,
        'last_signIn_time': 0,
        'create_time': get_now(),
        'sign_count': 0,
        'auth': auth_list
    }
    my_db = ToMongo()
    result = my_db.insert('admin', user)
    return result.inserted_id
Beispiel #6
0
def evaluate_model(user_id, user_name, request):
    """用户评论模型"""
    rlt = {}
    order_no = request.form.get('order_no')
    if int(get_order_status(order_no)) == 4:
        rlt['error'] = '已评论!'
        return rlt
    star = int(request.form.get('star'))
    context = request.form.get('context')
    anonymous = request.form.get('anonymous')
    img = request.files.get('img')
    if not star:
        rlt['error'] = '评分不能为空'
        return rlt
    if img:
        img_path = save_img(order_no, img)
    else:
        img_path = ''
    create_time = get_now()
    book_ids = get_book_id(order_no)
    if anonymous:
        user_name = user_name[0] + '**'
    elif user_name is None:
        user_name = '***'
    else:
        user_name = user_name[0] + '*' + user_name[-1]
    id_list = []
    mydb = ToMongo()
    for book_id in book_ids:
        evaluates = mydb.get_col('evaluate').find_one(
            {'_id': ObjectId(book_id)})
        if evaluates:
            value = {
                '$addToSet': {
                    'comment': {
                        'order_no': order_no,
                        'star': star,
                        'context': context,
                        'user_id': user_id,
                        'user_name': user_name,
                        'img_path': img_path,
                        'create_time': create_time
                    }
                }
            }
            result = mydb.update('evaluate', {'_id': ObjectId(book_id)}, value)
            if result and result.modified_count:
                id_list.append(result.modified_count)
        else:
            # 无评论时,直接插入
            rel = mydb.insert(
                'evaluate', {
                    '_id':
                    ObjectId(book_id),
                    'comment': [{
                        'order_no': order_no,
                        'star': star,
                        'context': context,
                        'user_id': user_id,
                        'user_name': user_name,
                        'img_path': img_path,
                        'create_time': create_time
                    }]
                })
            if rel:
                id_list.append(rel.inserted_id)
    if len(id_list) != len(book_ids):
        # print(id_list)
        rlt['error'] = '评论失败,请重试!'
    mydb.close_conn()
    return rlt
Beispiel #7
0
def to_pay_model(user_id, book_ids, addr_id, is_buy_now):
    """去支付"""
    DISCOUNT = 1.01
    create_time = int(time.time())
    order_no = create_orders()
    books = []
    amount = .0
    # 获取收货地址,写入订单号详情
    db_conn = ToMongo()
    address = db_conn.get_col('address').find_one({
        'user_id': user_id,
        '_id': ObjectId(addr_id)
    })
    for book_id in book_ids:
        book = get_book(book_id, False)
        if not is_buy_now:
            db = ToConn()
            sql = 'select book_num from cart where user_id=%s and book_id=%s and is_effe=1'
            book_num = db.get_db(sql, (user_id, book_id)).fetchone()
            if book_num:
                books.append({
                    'book_num': int(book_num['book_num']),
                    'book_id': book_id
                })
                amount += (float(book.get('price')) *
                           int(book_num['book_num']))
            else:
                books.append({'book_num': 1, 'book_id': book_id})
                amount += (float(book.get('price')) * 1)
        else:
            books.append({'book_num': 1, 'book_id': book_id})
            amount += (float(book.get('price')) * 1)

    v = {
        "amount": amount - DISCOUNT,
        "books": books,
        "order_no": order_no,
        "is_processed": 0,
        "user_id": user_id,
        "create_time": create_time,
        "is_effective": 1,
        "address": address,
        "orders_status": 0,
        "pay_status": 0,
        "exp_status": 0,
        "logistics": [],  # 物流信息
    }
    result = db_conn.insert('order', v)
    if result and not is_buy_now:
        db = ToConn()
        conn = db.to_execute()
        cursor = conn.cursor()
        try:
            for book_id in book_ids:
                sql = 'delete from cart where user_id=%s and book_id=%s'
                cursor.execute(sql, (user_id, book_id))
        except Exception as e:
            print('========to_pay=========事务处理失败:', e)
            conn.rollback()  # 事务回滚
        else:
            conn.commit()  # 事务提交

            for book in books:
                # 销量加
                db_conn.update('books', {'_id': ObjectId(book.get('book_id'))},
                               {'$inc': {
                                   'sales': book.get('book_num')
                               }})
                # 库存减
                db_conn.update('books', {'_id': ObjectId(book.get('book_id'))},
                               {'$inc': {
                                   'stock': -book.get('book_num')
                               }})
        db.to_close()
    elif is_buy_now:
        for book in books:
            # 销量加
            db_conn.update('books', {'_id': ObjectId(book.get('book_id'))},
                           {'$inc': {
                               'sales': book.get('book_num')
                           }})
            # 库存减
            db_conn.update('books', {'_id': ObjectId(book.get('book_id'))},
                           {'$inc': {
                               'stock': -book.get('book_num')
                           }})

    db_conn.close_conn()
    return order_no