Subversion Repositories SmartDukaan

Rev

Rev 2403 | Rev 2428 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2403 Rev 2404
Line 16... Line 16...
16
import datetime
16
import datetime
17
import sys
17
import sys
18
from shop2020.utils.Utils import log_entry, to_py_date, log_risky_flag
18
from shop2020.utils.Utils import log_entry, to_py_date, log_risky_flag
19
from sqlalchemy import desc, asc
19
from sqlalchemy import desc, asc
20
from shop2020.model.v1.catalog.impl.CategoryManager import CategoryManager
20
from shop2020.model.v1.catalog.impl.CategoryManager import CategoryManager
21
from sqlalchemy.sql.expression import and_
21
from sqlalchemy.sql.expression import and_, distinct, func
22
 
22
 
23
def initialize():
23
def initialize():
24
    DataService.initialize()
24
    DataService.initialize()
25
    
25
    
26
 
26
 
Line 551... Line 551...
551
        raise InventoryServiceException(101, "bad item")
551
        raise InventoryServiceException(101, "bad item")
552
    
552
    
553
    warehouses = item.currentInventory.warehouse
553
    warehouses = item.currentInventory.warehouse
554
    return warehouses
554
    return warehouses
555
 
555
 
-
 
556
def get_child_categories(category):
-
 
557
    cm = CategoryManager()
-
 
558
    return cm.getCategory(category).children_category_ids
-
 
559
 
556
def get_best_sellers(start_index, stop_index, category=-1):
560
def get_best_sellers(start_index, stop_index, category=-1):
-
 
561
    '''
-
 
562
    Returns the Best Sellers between the start and the stop index in the given category
-
 
563
    '''
557
    query = get_best_sellers_query(category, None)
564
    query = get_best_sellers_query(category, None)
558
    best_sellers = query.all()[start_index:stop_index]
565
    best_sellers = query.all()[start_index:stop_index]
559
    return get_thrift_item_list(best_sellers)
566
    return get_thrift_item_list(best_sellers)
560
 
567
 
561
def get_best_sellers_count(category=-1):
568
def get_best_sellers_count(category=-1):
-
 
569
    '''
-
 
570
    Returns the number of best sellers in the given category
-
 
571
    '''
562
    count = get_best_sellers_query(category, None).count()
572
    count = get_best_sellers_query(category, None).count()
563
    if count is None:
573
    if count is None:
564
        count = 0
574
        count = 0
565
    return count
575
    return count
566
 
576
 
567
def get_best_sellers_catalog_ids(start_index, stop_index, brand, category=-1):
577
def get_best_sellers_catalog_ids(start_index, stop_index, brand, category=-1):
-
 
578
    '''
-
 
579
    Returns the Best sellers for the given brand and category between the start and the stop index.
-
 
580
    Ignores the category if it's passed as -1 and the brand if it's passed as None. 
-
 
581
    '''
568
    query = get_best_sellers_query(category, brand)
582
    query = get_best_sellers_query(category, brand)
569
    best_sellers = query.all()[start_index:stop_index]
583
    best_sellers = query.all()[start_index:stop_index]
570
    return [item.catalog_item_id for item in best_sellers]
584
    return [item.catalog_item_id for item in best_sellers]
571
 
-
 
572
def get_child_categories(category):
-
 
573
    cm = CategoryManager()
-
 
574
    return cm.getCategory(category).children_category_ids
-
 
575
    
585
    
576
def get_best_sellers_query(category, brand):
586
def get_best_sellers_query(category, brand):
-
 
587
    '''
-
 
588
    Returns the query to be used for getting Best Sellers.
-
 
589
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
-
 
590
    '''
577
    query = Item.query.filter_by(status=status.ACTIVE).filter(Item.bestSellingRank != None)
591
    query = Item.query.filter_by(status=status.ACTIVE).filter(Item.bestSellingRank != None)
578
    if category != -1:
592
    if category != -1:
579
        all_categories = [category]
593
        all_categories = [category]
580
        child_categories = get_child_categories(category)
594
        child_categories = get_child_categories(category)
581
        if child_categories is not None:
595
        if child_categories is not None:
Line 585... Line 599...
585
        query = query.filter_by(brand=brand)
599
        query = query.filter_by(brand=brand)
586
    query = query.order_by(asc(Item.bestSellingRank))
600
    query = query.order_by(asc(Item.bestSellingRank))
587
    return query
601
    return query
588
 
602
 
589
def get_best_deals(category=-1):
603
def get_best_deals(category=-1):
-
 
604
    '''
-
 
605
    Returns the Best deals in the given category. Ignores the category if it's passed as -1.
-
 
606
    '''
590
    query = get_best_deals_query(category, None)
607
    query = get_best_deals_query(Item, category, None)
591
    items = query.all()
608
    items = query.all()
592
    return get_thrift_item_list(items)
609
    return get_thrift_item_list(items)
593
 
610
 
594
def get_best_deals_count(category=-1):
611
def get_best_deals_count(category=-1):
-
 
612
    '''
-
 
613
    Returns the count of best deals in the given category.
-
 
614
    Ignores the category if it's -1.
-
 
615
    '''
595
    count = get_best_deals_query(category, None).count()
616
    count = get_best_deals_counting_query(func.count(distinct(Item.catalog_item_id)), category, None).scalar()
596
    if count is None:
617
    if count is None:
597
        count = 0
618
        count = 0
598
    return count
619
    return count
599
    
620
    
600
def get_best_deals_catalog_ids(start_index, stop_index, brand, category=-1):
621
def get_best_deals_catalog_ids(start_index, stop_index, brand, category=-1):
-
 
622
    '''
-
 
623
    Returns the catalog_item_ids of best deal items for the given brand and category.
-
 
624
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
-
 
625
    '''
601
    query = get_best_deals_query(category, brand)
626
    query = get_best_deals_query(Item, category, brand)
602
    best_deal_items = query.all()[start_index:stop_index]
627
    best_deal_items = query.all()[start_index:stop_index]
603
    return [item.catalog_item_id for item in best_deal_items]
628
    return [item.catalog_item_id for item in best_deal_items]
604
 
629
 
605
def get_best_deals_query(category, brand):
630
def get_best_deals_counting_query(obj, category, brand):
-
 
631
    '''
-
 
632
    Returns the query to be used to select the best deals in the given brand and category.
-
 
633
    Ignores the category if it's passed as -1 and the brand if it's passed as None. 
-
 
634
    '''
606
    query = Item.query.filter_by(status=status.ACTIVE).filter(Item.bestDealValue != None)
635
    query = session.query(obj).filter_by(status=status.ACTIVE).filter(Item.bestDealValue != None)
607
    if category != -1:
636
    if category != -1:
608
        all_categories = [category]
637
        all_categories = [category]
609
        child_categories = get_child_categories(category)
638
        child_categories = get_child_categories(category)
610
        if child_categories is not None:
639
        if child_categories is not None:
611
            all_categories = all_categories + child_categories 
640
            all_categories = all_categories + child_categories 
612
        query = query.filter(Item.category.in_(all_categories))
641
        query = query.filter(Item.category.in_(all_categories))
613
    if brand is not None:
642
    if brand is not None:
614
        query = query.filter_by(brand=brand)
643
        query = query.filter_by(brand=brand)
-
 
644
    return query
-
 
645
 
-
 
646
def get_best_deals_query(obj, category, brand):
-
 
647
    '''
-
 
648
    Returns the query to be used to get the best deals in the given category and brand.
-
 
649
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
-
 
650
    '''
-
 
651
    query = get_best_deals_counting_query(obj, category, brand)
615
    query = query.group_by(Item.catalog_item_id).order_by(desc(Item.bestDealValue))
652
    query = query.group_by(Item.catalog_item_id).order_by(desc(Item.bestDealValue))
616
    return query
653
    return query
617
 
654
 
618
def get_latest_arrivals(limit, category=-1):
655
def get_latest_arrivals(limit, category=-1):
-
 
656
    '''
-
 
657
    Returns up to limit number of Latest Arrivals in the given category.
-
 
658
    '''
619
    query = get_latest_arrivals_query(category, None)
659
    query = get_latest_arrivals_query(Item, category, None)
620
    items = query.all()[0:limit]
660
    items = query.all()[0:limit]
621
    return get_thrift_item_list(items)
661
    return get_thrift_item_list(items)
622
    
662
    
623
def get_latest_arrivals_count(limit, category=-1):
663
def get_latest_arrivals_count(limit, category=-1):
-
 
664
    '''
-
 
665
    Returns the number of latest arrivals which will be displayed on the website.
-
 
666
    Ignores the category if it's passed as -1.
-
 
667
    '''
624
    count = get_latest_arrivals_query(category, None).count()
668
    count = get_latest_arrivals_counting_query(func.count(distinct(Item.catalog_item_id)), category, None).scalar()
625
    if count is None:
669
    if count is None:
626
        count = 0
670
        count = 0
627
    count = min(count, limit)
671
    count = min(count, limit)
628
    return count
672
    return count
629
    
673
    
630
def get_latest_arrivals_catalog_ids(start_index, stop_index, brand, category=-1):
674
def get_latest_arrivals_catalog_ids(start_index, stop_index, brand, category=-1):
-
 
675
    '''
-
 
676
    Returns the catalog_item_ids of the latest arrivals between the start and the stop index
-
 
677
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
-
 
678
    '''
631
    query = get_latest_arrivals_query(category, brand)
679
    query = get_latest_arrivals_query(Item, category, brand)
632
    latest_arrivals = query.all()[start_index:stop_index]
680
    latest_arrivals = query.all()[start_index:stop_index]
633
    return [item.catalog_item_id for item in latest_arrivals]
681
    return [item.catalog_item_id for item in latest_arrivals]
634
 
682
 
635
def get_latest_arrivals_query(category, brand):
683
def get_latest_arrivals_counting_query(obj, category, brand):
-
 
684
    '''
-
 
685
    Returns the query to be used to count Latest arrivals.
-
 
686
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
-
 
687
    '''
636
    query = Item.query.filter_by(status=status.ACTIVE)
688
    query = session.query(obj).filter_by(status=status.ACTIVE)
637
    if category != -1:
689
    if category != -1:
638
        all_categories = [category]
690
        all_categories = [category]
639
        child_categories = get_child_categories(category)
691
        child_categories = get_child_categories(category)
640
        if child_categories is not None:
692
        if child_categories is not None:
641
            all_categories = all_categories + child_categories 
693
            all_categories = all_categories + child_categories 
642
        query = query.filter(Item.category.in_(all_categories))
694
        query = query.filter(Item.category.in_(all_categories))
643
    if brand is not None:
695
    if brand is not None:
644
        query = query.filter_by(brand=brand)
696
        query = query.filter_by(brand=brand)
-
 
697
    return query
-
 
698
 
-
 
699
def get_latest_arrivals_query(obj, category, brand):
-
 
700
    '''
-
 
701
    Returns the query to be used to retrieve Latest Arrivals.
-
 
702
    Ignores the category if it's passed as -1 and the brand if it's passed as None.
-
 
703
    '''
-
 
704
    query = get_latest_arrivals_counting_query(obj, category, brand)
645
    query = query.group_by(Item.catalog_item_id).order_by(desc(Item.startDate))
705
    query = query.group_by(Item.catalog_item_id).order_by(desc(Item.startDate))
646
    return query
706
    return query
647
 
707
 
648
def get_thrift_item_list(items):
708
def get_thrift_item_list(items):
649
    return [to_t_item(item) for item in items if item != None]
709
    return [to_t_item(item) for item in items if item != None]