예제 #1
0
def get():
    try:
        site = "classbook"
        if 'site' in request.vars:
            site = request.vars.site
        check_cache = myredis.get_cache(BANNER + site)
        if check_cache['result'] and check_cache['data'] is not None:
            data = json.loads(check_cache['data'])
            data['cache'] = True
            return data
        select_banner = db(db.clsb30_banner.active_status == 1)\
                (db.clsb30_banner.banner_site.like(site)).select(orderby=db.clsb30_banner.banner_order)
        banners = list()
        for b in select_banner:
            temp = dict()
            temp['id'] = b['id']
            temp['title'] = b['banner_title']
            temp['url'] = b['banner_url']
            temp['type'] = b['action_type']
            temp['data'] = b['action_data']
            banners.append(temp)
        data = dict(banners=banners)
        myredis.write_cache(BANNER + site, str(json.dumps(data)), DEFAULT_TIME)
        data['cache'] = False
        return data
    except Exception as ex:
        return dict(error=str(ex) + " on line: " +
                    str(sys.exc_traceback.tb_lineno))
예제 #2
0
def get():
    check_cache = myredis.get_cache(SUBJECT)
    if check_cache['result'] and check_cache['data'] is not None:
        data = json.loads(check_cache['data'])
        data['cache'] = True
        return data
    if not table in db.tables(): NOT_EXIST
    try:
        rows = db().select(db.clsb_subject.ALL).as_list()
        lenR = len(rows)
        for i in range(0, lenR - 1):
            for j in range(i + 1, lenR):
                if int(rows[i]['subject_order']) > int(
                        rows[j]['subject_order']):
                    tmp = rows[i]
                    rows[i] = rows[j]
                    rows[j] = tmp

        l = list()
        for row in rows:
            temp = dict()
            temp['id'] = row['id']
            temp['subject_name'] = row['subject_name']
            temp['subject_code'] = row['subject_code']
            temp['subject_order'] = row['subject_order']
            temp['subject_description'] = row['subject_description']

            l.append(temp)
        data = dict(items=l)
        myredis.write_cache(SUBJECT, str(json.dumps(data)), DEFAULT_TIME)
        data['cache'] = False
        return data
    except Exception as e:
        return dict(error=str(e))
예제 #3
0
def get_tree():
    try:
        except_id = [83, 86]
        show_on = ""
        if request.vars and "version" in request.vars:
            show_on = request.vars.version
        isWindow = False
        if request.vars and "app_version" in request.vars:
            show_on = ""
            if "WINDOWAPP" in request.vars.app_version:
                isWindow = True
                except_id.append(3)
        check_cache = myredis.get_cache(CATE_TREE + show_on)
        if check_cache['result'] and check_cache['data'] is not None:
            data = json.loads(check_cache['data'])
            data['cache'] = True
            return data
        root = list()
        db_query = db(
            db.clsb_category.category_type == db.clsb_product_type.id)
        db_query = db_query(~db.clsb_category.id.belongs(except_id))
        if request.args:
            root_id = request.args[0]
            db_query = db_query(db.clsb_category.id == root_id)
        else:
            db_query = db_query(db.clsb_category.category_parent == None)
        rows = db_query.select(db.clsb_category.id,
                               db.clsb_category.category_name,
                               db.clsb_category.category_order,
                               db.clsb_category.category_code,
                               db.clsb_category.category_parent,
                               db.clsb_product_type.type_name,
                               orderby=~db.clsb_category.category_order)

        for row in rows:
            temp = dict()
            temp['category_id'] = row.clsb_category.id
            temp['category_name'] = row.clsb_category.category_name
            temp['category_order'] = row.clsb_category.category_order
            temp[
                'category_parent'] = row.clsb_category.category_parent if row.clsb_category.category_parent else 0
            temp['category_code'] = row.clsb_category.category_code
            temp['category_type'] = row.clsb_product_type.type_name
            if temp['category_id'] in enable_package:
                temp['buy_package'] = True
            else:
                temp['buy_package'] = False
            temp = get_categories(temp, show_on)
            root.append(temp)
        data = dict(categories=root)
        myredis.write_cache(CATE_TREE + show_on, str(json.dumps(data)),
                            DEFAULT_TIME)
        data['cache'] = False
        return data
    except Exception as ex:
        return str(ex) + " on line " + str(sys.exc_traceback.tb_lineno)
예제 #4
0
def toppay():
    try:
        check_cache = myredis.get_cache(HL_TOP_PAY)
        if check_cache['result'] and check_cache['data'] is not None:
            data = json.loads(check_cache['data'])
            data['cache'] = True
            return data
        query = db(db.clsb_download_archieve.product_id == db.clsb_product.id)
        query = query(
            db.clsb_product.product_creator == db.clsb_dic_creator.id)
        query = query(
            db.clsb_product.id == db.clsblib_category_product.product_id)
        query = query(db.clsb_product.product_category == db.clsb_category.id)
        import datetime
        from dateutil.relativedelta import relativedelta
        end = datetime.datetime.now()
        start = end - relativedelta(months=2)
        query = query(db.clsb_download_archieve.download_time > start)\
                (db.clsb_download_archieve.download_time < end)\
                (db.clsb_download_archieve.price > 0)
        count = db.clsb_download_archieve.id.count()
        select_product = query.select(
            db.clsb_product.ALL,
            count,
            db.clsb_dic_creator.creator_name,
            groupby=db.clsb_download_archieve.product_id,
            orderby=~count,
            limitby=(0, 12))
        products = list()
        for row in select_product:
            temp = dict()
            temp['id'] = row['clsb_product']['id']
            temp['cover_price'] = 0
            temp['product_category'] = ""
            temp['creator_name'] = row['clsb_dic_creator']['creator_name']
            temp['product_publisher'] = ""
            temp['product_title'] = row['clsb_product']['product_title']
            temp['product_code'] = row['clsb_product']['product_code']
            temp['product_cover'] = "http://classbook.vn/static/covers/" + \
                                        row['clsb_product']['product_code'] + "/thumb.png"
            temp['product_price'] = row['clsb_product']['product_price']
            temp['category_name'] = ""
            temp['category_code'] = ""
            temp['count'] = row[count]
            temp['category_type'] = "book"
            products.append(temp)
        data = dict(items=products)
        myredis.write_cache(HL_TOP_PAY, str(json.dumps(data)), DEFAULT_TIME)
        data['cache'] = False
        return data
    except Exception as e:
        print(str(e) + " on line " + str(sys.exc_traceback.tb_lineno))
        return str(e) + " on line " + str(sys.exc_traceback.tb_lineno)
예제 #5
0
def top_new():
    try:
        check_cache = myredis.get_cache(HL_TOP_NEW)
        if check_cache['result'] and check_cache['data'] is not None:
            data = json.loads(check_cache['data'])
            data['cache'] = True
            return data
        qset = db(db.clsb_product.product_category == db.clsb_category.id)
        qset = qset(db.clsb_product.product_creator == db.clsb_dic_creator.id)
        qset = qset(
            db.clsb_product.product_publisher == db.clsb_dic_publisher.id)
        qset = qset(
            db.clsb_product.id == db.clsblib_category_product.product_id)

        rows = qset.select(db.clsb_product.id,
                           db.clsb_product.created_on,
                           db.clsb_product.product_category,
                           db.clsb_product.product_title,
                           db.clsb_product.product_code,
                           db.clsb_product.product_price,
                           db.clsb_dic_publisher.publisher_name,
                           db.clsb_category.category_name,
                           db.clsb_category.category_code,
                           db.clsb_dic_creator.creator_name,
                           orderby=~db.clsb_product.created_on,
                           limitby=(0, 12))
        d = list()
        for row in rows:
            temp = dict()
            temp['id'] = row['clsb_product']['id']
            temp['cover_price'] = 0
            temp['product_category'] = row['clsb_product']['product_category']
            temp['created_on'] = str(row['clsb_product']['created_on'])
            temp['creator_name'] = row['clsb_dic_creator']['creator_name']
            temp['product_publisher'] = row['clsb_dic_publisher'][
                'publisher_name']
            temp['product_title'] = row['clsb_product']['product_title']
            temp['product_code'] = row['clsb_product']['product_code']
            temp['product_cover'] = "http://classbook.vn/static/covers/" + \
                                        row['clsb_product']['product_code'] + "/thumb.png"
            temp['product_price'] = row['clsb_product']['product_price']
            temp['category_name'] = row['clsb_category']['category_name']
            temp['category_code'] = row['clsb_category']['category_code']
            temp['category_type'] = "book"
            d.append(temp)
        data = dict(items=d)
        myredis.write_cache(HL_TOP_NEW, str(json.dumps(data)), DEFAULT_TIME)
        data['cache'] = False
        return data
    except Exception as e:
        return dict(error=e.message + " on line: " +
                    str(sys.exc_traceback.tb_lineno))
예제 #6
0
def category_tree():
    try:
        check_cache = myredis.get_cache(HL_CATE_TREE)
        if check_cache['result'] and check_cache['data'] is not None:
            data = json.loads(check_cache['data'])
            data['cache'] = True
            return data
        root = list()
        db_query = db(
            db.clsblib_category.category_type == db.clsb_product_type.id)
        if request.args:
            root_id = request.args[0]
            db_query = db_query(db.clsblib_category.id == root_id)
        else:
            db_query = db_query(db.clsblib_category.category_parent == None)
        rows = db_query.select(db.clsblib_category.id,
                               db.clsblib_category.category_name,
                               db.clsblib_category.category_order,
                               db.clsblib_category.category_code,
                               db.clsblib_category.category_parent,
                               db.clsb_product_type.type_name,
                               orderby=~db.clsblib_category.category_order)

        for row in rows:
            temp = dict()
            temp['category_id'] = row.clsblib_category.id
            temp['category_name'] = row.clsblib_category.category_name
            temp['category_order'] = row.clsblib_category.category_order
            temp[
                'category_parent'] = row.clsblib_category.category_parent if row.clsblib_category.category_parent else 0
            temp['category_code'] = row.clsblib_category.category_code
            temp['category_type'] = row.clsb_product_type.type_name
            temp['buy_package'] = True
            temp = get_categories(temp)
            root.append(temp)
        data = dict(categories=root)
        myredis.write_cache(HL_CATE_TREE, str(json.dumps(data)), DEFAULT_TIME)
        data['cache'] = False
        return data
    except Exception as ex:
        import sys
        print(ex.message + " on line: " + str(sys.exc_traceback.tb_lineno))
예제 #7
0
def classes():  #args: subj_id
    try:
        subject_id = request.args(0)
        check_cache = myredis.get_cache(CLASSES + subject_id)
        if check_cache['result'] and check_cache['data'] is not None:
            data = json.loads(check_cache['data'])
            data['cache'] = True
            return data
#         subject_id =  db(db.clsb_subject.subject_code == subject_code).select(db.clsb_subject.id).as_list()[0]['id']
#         print subject_id

        rows =  db(db.clsb_class.id == db.clsb_subject_class.class_id)\
                    (db.clsb_subject_class.subject_id == subject_id).select(db.clsb_class.ALL, db.clsb_subject_class.id).as_list()

        #         return dict(d= rows)
        lenR = len(rows)
        for i in range(0, lenR - 1):
            for j in range(i + 1, lenR):
                if int(rows[i]['clsb_class']['class_order']) > int(
                        rows[j]['clsb_class']['class_order']):
                    tmp = rows[i]
                    rows[i] = rows[j]
                    rows[j] = tmp
        l = list()
        for row in rows:
            temp = dict()
            temp['class_id'] = row['clsb_class']['id']
            temp['class_name'] = row['clsb_class']['class_name']
            temp['class_code'] = row['clsb_class']['class_code']
            temp['class_order'] = row['clsb_class']['class_order']
            temp['class_description'] = row['clsb_class']['class_description']
            temp['subject_class_id'] = row['clsb_subject_class']['id']
            l.append(temp)
        data = dict(items=l)
        myredis.write_cache(CLASSES + subject_id, str(json.dumps(data)),
                            DEFAULT_TIME)
        data['cache'] = False
        return data
    except Exception as e:
        return dict(error=str(e))
예제 #8
0
def get_published_topic():
    try:
        check_cache = myredis.get_cache(PUB_TOPIC)
        if check_cache['result'] and check_cache['data'] is not None:
            data = json.loads(check_cache['data'])
            data['cache'] = True
            return data
        topics = list()
        select_topic = db(db.clsb_home_topic.status.like("published")).select(
            orderby=db.clsb_home_topic.topic_order)
        for topic in select_topic:
            temp = dict()
            temp['id'] = topic['id']
            temp['topic_name'] = topic['topic_name']
            topics.append(temp)
        data = dict(topics=topics)
        myredis.write_cache(PUB_TOPIC, str(json.dumps(data)), DEFAULT_TIME)
        data['cache'] = False
        return data
    except Exception as err:
        return dict(error=str(err) + " on line: " +
                    str(sys.exc_traceback.tb_lineno))
예제 #9
0
def get():
    """
        Get all product by topic item id
    """
    try:
        if not table in db.tables():
            return NOT_EXIST
        version_app = ""
        if request.vars and 'version' in request.vars:
            version_app = request.vars.version
        if request.args and len(request.args) == 1:
            check_cache = myredis.get_cache(TOPIC_ITEM + request.args(0) + version_app)
            if check_cache['result'] and check_cache['data'] is not None:
                data = json.loads(check_cache['data'])
                data['cache'] = True
                return data
            rows = db(db[table].topic_id == request.args(0))
            rows = rows(db[table].product_id == db.clsb_product.id)
            rows = rows(db.clsb_product.product_status == "Approved")
            if version_app != "":
                rows = rows(db.clsb_product.show_on.like('%' + version_app.upper() + '%'))
            rows = rows(db.clsb_product.product_creator == db.clsb_dic_creator.id)
            rows = rows(db.clsb_product.product_category == db.clsb_category.id)
            rows = rows(db.clsb_category.category_type == db.clsb_product_type.id)
            rows = rows(db.clsb_product.product_category == db.clsb_category.id)
            rows = rows(db.clsb_product.product_publisher == db.clsb_dic_publisher.id)
            rows = rows.select(db[table].id, db[table].topic_id, db[table].product_id, db[table].topic_item_order,
                               db[table].item_path, db.clsb_product.ALL, db.clsb_product_type.type_name,
                               db.clsb_dic_creator.creator_name, db.clsb_category.category_name,
                               db.clsb_dic_publisher.publisher_name, db.clsb_category.category_code,
                               orderby=db[table].topic_item_order).as_list()
            d = list()

            for row in rows:
                row['clsb_home_topic_item']['item_path'] = URL('download', args=row['clsb_home_topic_item']['item_path'],
                                                               host=True)
                temp = dict()
                temp['home_topic_item_id'] = row['clsb_home_topic_item']['id']
                temp['topic_id'] = row['clsb_home_topic_item']['topic_id']
                temp['topic_item_order'] = row['clsb_home_topic_item']['topic_item_order']
                temp['item_path'] = row['clsb_home_topic_item']['item_path']
                temp['product_id'] = row['clsb_home_topic_item']['product_id']
                cover_price = db(db.clsb_product_metadata.product_id == temp['product_id'])\
                                        (db.clsb_product_metadata.metadata_id == db.clsb_dic_metadata.id)\
                                        (db.clsb_dic_metadata.metadata_name == 'cover_price').select(db.clsb_product_metadata.metadata_value).as_list()
                if cover_price:
                    try:
                        temp['cover_price'] = int(cover_price[0]['metadata_value'])
                    except Exception as e:
                        print str(e)
                else:
                    temp['cover_price'] = 0
                temp['product_category'] = row['clsb_product']['product_category']
                temp['product_collection'] = row['clsb_product']['product_collection']
                temp['product_creator'] = row['clsb_dic_creator']['creator_name']
                temp['product_publisher'] = row['clsb_dic_publisher']['publisher_name']
                temp['product_title'] = row['clsb_product']['product_title']
                temp['product_price'] = row['clsb_product']['product_price']
                temp['product_code'] = row['clsb_product']['product_code']
                temp['total_download'] = row['clsb_product']['total_download']
                temp['product_cover'] = URL(a = 'cbs', c = 'download', f = 'thumb',
                     scheme = True, host = True, args = row['clsb_product']['product_code'])
                temp['creator_name'] = row['clsb_dic_creator']['creator_name']
                temp['type_name'] = row['clsb_product_type']['type_name']
                temp['category_name'] = row['clsb_category']['category_name']
                temp['category_code'] = row['clsb_category']['category_code']
                temp['free'] = False
                d.append(temp)
            data = dict(items=d)
            myredis.write_cache(TOPIC_ITEM + request.args(0) + version_app, str(json.dumps(data)), DEFAULT_TIME)
            data['cache'] = False
            return data
        else:
            return dict(error=LACK_ARGS)
    except Exception as e:
        return str(e) + " on line " + str(sys.exc_traceback.tb_lineno)
예제 #10
0
def get_product():
    try:
        products = list()
        db_product = list()
        page = 0
        items_per_page = settings.items_per_page
        product_query = db(db.clsb_product.product_status.like('Approved'))
        cat_id = ""
        if request.args:
            cat_id = request.args[0]
            check_child = db(
                db.clsblib_category.category_parent == cat_id).select()
            if len(check_child) == 0:
                product_query = product_query(db.clsb_product.id == db.clsblib_category_product.product_id)\
                    (db.clsblib_category_product.category_code == db.clsblib_category.category_code)\
                    (db.clsblib_category.id == cat_id)
            else:
                product_query = product_query(db.clsb_product.id == db.clsblib_category_product.product_id)\
                    (db.clsblib_category_product.category_code == db.clsblib_category.category_code)\
                    (db.clsblib_category.category_parent == cat_id)

            #Pagination
            try:
                if len(request.args) > 1: page = int(request.args[1])
                if len(request.args) > 2: items_per_page = int(request.args[2])
                if len(request.args) > 3:
                    version_view = request.args[3]
                    product_query = product_query(
                        db.clsb_product.show_on.like("%" + version_view + "%"))
            except (TypeError, ValueError):
                pass
        else:
            product_query = product_query(db.clsb_product.id > 0)
        check_cache = myredis.get_cache(HL_GET_PRODUCT + cat_id)
        if check_cache['result'] and check_cache['data'] is not None:
            data = json.loads(check_cache['data'])
            data['cache'] = True
            return data

        limitby = (page * items_per_page, (page + 1) * items_per_page)

        total_items = product_query(db.clsb_product.product_creator == db.clsb_dic_creator.id) \
                (db.clsb_product.product_publisher == db.clsb_dic_publisher.id) \
                (db.clsblib_category.category_type == db.clsb_product_type.id).count()

        total_pages = total_items / items_per_page + 1 if total_items % items_per_page > 0 else total_items / items_per_page

        db_product = product_query(db.clsb_product.product_creator == db.clsb_dic_creator.id) \
                (db.clsb_product.product_publisher == db.clsb_dic_publisher.id) \
                (db.clsblib_category.category_type == db.clsb_product_type.id) \
                (db.clsb_product.device_shelf_code == db.clsb_device_shelf.id).select(db.clsb_product.id,
                                                                                      db.clsblib_category.ALL,
                                                                                      db.clsb_product_type.type_name,
                                                                                      db.clsb_dic_creator.creator_name,
                                                                                      db.clsb_dic_publisher.publisher_name,
                                                                                      db.clsb_product.id,
                                                                                      db.clsb_product.product_title,
                                                                                      db.clsb_product.product_code,
                                                                                      db.clsb_product.product_price,
                                                                                      db.clsb_device_shelf.device_shelf_code,
                                                                                      db.clsb_device_shelf.device_shelf_type,
                                                                                      db.clsb_device_shelf.device_shelf_name,
                                                                                      orderby=~db.clsb_product.created_on,
                                                                                      limitby=limitby).as_list()

        if db_product:
            for row in db_product:
                temp = dict()
                temp['id'] = row['clsb_product']['id']
                temp['category_id'] = row['clsblib_category']['id']
                temp['free'] = False
                temp['category_name'] = row['clsblib_category'][
                    'category_name']
                temp['category_code'] = row['clsblib_category'][
                    'category_code']
                temp['category_type'] = row['clsb_product_type']['type_name']
                temp['device_self_code'] = row['clsb_device_shelf'][
                    'device_shelf_code']
                temp['device_self_type'] = row['clsb_device_shelf'][
                    'device_shelf_type']
                temp['device_shelf_name'] = row['clsb_device_shelf'][
                    'device_shelf_name']

                temp['creator_name'] = row['clsb_dic_creator']['creator_name']
                temp['publisher_name'] = row['clsb_dic_publisher'][
                    'publisher_name']
                temp['product_title'] = row['clsb_product']['product_title']
                temp['product_cover'] = "http://classbook.vn/static/covers/" + \
                                        row['clsb_product']['product_code'] + "/thumb.png"
                temp['product_code'] = row['clsb_product']['product_code']
                temp['product_price'] = row['clsb_product']['product_price']
                temp['cover_price'] = 0
                products.append(temp)
        data = dict(page=page,
                    items_per_page=items_per_page,
                    total_items=total_items,
                    total_pages=total_pages,
                    products=products)
        myredis.write_cache(HL_GET_PRODUCT + cat_id, str(json.dumps(data)),
                            DEFAULT_TIME)
        data['cache'] = False
        return data
    except Exception as ex:
        return dict(type='error',
                    value=str(ex) + " on line: " +
                    str(sys.exc_traceback.tb_lineno))