Exemple #1
0
def get_popular_demand(role, user, page, t1id, aid, asc_of_price,
                       count_per_page):
    """
    :param role: 
    :param user:
    :param page: start from one
    :param count_per_page: size of page
    :return: 
    """

    if user is None and role is None:
        raise Error400("Either user or role must not be null.")

    if user is not None:
        role = user.role

    qs = ProductDemand.objects.select_related(
        'uid__user_validate', 'qid__t3id__t2id__t1id', 'aid__cid__pid', 'pmid',
        'wcid').filter(in_use=True,
                       match=True).filter(end_time__gt=now()).exclude(
                           t_demand=t_demand_translator.from_role(role))

    qs = filter_and_order_demand(qs, t1id, aid, asc_of_price)

    st, ed, n_pages = get_page_info(
        qs,
        count_per_page,
        page,
        index_error_excepiton=Error400("Page out of range"))

    # return sliced single page
    return qs[st:ed], n_pages
Exemple #2
0
def get_my_demand(user, page, t1id, aid, asc_of_price, count_per_page):
    """
    TODO: using user_sid to get specified page
    :param user:
    :param page:
    :param t1id:
    :param aid:
    :param asc_of_price:
    :param count_per_page
    :return: 
    """

    qs = ProductDemand.objects.select_related('uid__user_validate',
                                              'qid__t3id__t2id__t1id',
                                              'aid__cid__pid', 'pmid',
                                              'wcid').filter(uid=user,
                                                             in_use=True)

    qs = filter_and_order_demand(qs, t1id, aid, asc_of_price)

    st, ed, n_pages = get_page_info(
        qs,
        count_per_page,
        page,
        index_error_excepiton=Error400("Page out of range"))
    # return sliced single page
    return qs[st:ed], n_pages
Exemple #3
0
def get_search_demand(user, page, keyword, t1id, aid, asc_of_price,
                      count_per_page):
    qs = ProductDemand.objects.select_related(
        'uid__user_validate', 'qid__t3id__t2id__t1id', 'aid__cid__pid', 'pmid',
        'wcid').filter(Q(uid__user_validate__company__contains=keyword)
                       | Q(uid__user_validate__contact=keyword)
                       | Q(pid__tname3=keyword),
                       in_use=True).exclude(uid__role=user.role)
    if t1id is not None:
        qs = qs.filter(qid__t3id__t2id__t1id=t1id)
    if aid is not None:
        qs = qs.filter(aid=aid)
    if asc_of_price is not None:
        if asc_of_price:
            qs = qs.order_by("price", "-id")
        else:
            qs = qs.order_by("-price", "-id")
    else:
        qs = qs.order_by("-id")
    st, ed, n_pages = get_page_info(
        qs,
        count_per_page,
        page,
        index_error_excepiton=Error400("Page out of range"))
    # return sliced single page
    return qs[st:ed], n_pages
Exemple #4
0
def publish_demand(user, demand, photo_ids=None):
    """
    
    :param user: 
    :param demand: 
    :param photo_ids: 
    :return: 
    """
    if not user.is_validated:
        raise WLException(
            410, "User's validation does not passed, cannot publish.")

    if demand["min_quantity"] > demand["quantity"]:
        raise Error400("min_quantity must equal to or less than quantity")

    demand_instance = ProductDemand(**demand)

    # Auto fill some of the fields
    demand_instance.t_demand = t_demand_translator.from_role(user.role)
    demand_instance.pid = demand["qid"].t3id
    demand_instance.uid = user
    demand_instance.save()

    if photo_ids is not None:
        append_photo(demand_instance, photo_ids)

    return demand_instance.id
Exemple #5
0
def edit_demand(user, id, demand, photo_ids=None):
    """
    
    :param user:
    :param id: 
    :param demand: 
    :param photo_ids: 
    :return: 
    """
    try:
        demand_object = ProductDemand.objects.get(id=id, uid=user, in_use=True)
    except ProductDemand.DoesNotExist:
        raise Error404("No such demand")

    update_instance_from_dict(instance=demand_object, dic=demand, save=False)

    if demand_object.min_quantity > demand_object.quantity:
        raise Error400("min_quantity must equal to or less than quantity")

    # TODO: check

    with transaction.atomic():
        demand_object.save()
        if photo_ids is not None:
            append_photo(demand_object, photo_ids, True)

    return id
Exemple #6
0
def get_matched_demand(user, id, page, order, asc, count_per_page):
    """
    :param user:
    :param id:
    :param page:
    :param order:
    :param count_per_page:
    :return:
    """
    def confirm_satisfied(self, other):
        # type: (ProductDemand, ProductDemand) -> bool
        return self.quantity_metric() > other.min_quantity_metric()

    def match_key(m_obj):
        # type: (ProductDemand) -> object
        if order == match_order_choice.SCORE:
            return demand.match_score(m_obj)["score_overall"]
        elif order == match_order_choice.QUANTITY:
            return m_obj.quantity_left()
        elif order == match_order_choice.PRICE:
            return m_obj.price_metric().scaled_value()

    try:
        demand = ProductDemand.objects.select_related('uid__user_validate',
                                                      'qid__t3id__t2id__t1id',
                                                      'aid__cid__pid', 'pmid',
                                                      'wcid').get(in_use=True,
                                                                  id=id,
                                                                  uid=user)
    except ProductDemand.DoesNotExist:
        raise Error404("No such demand.")

    match_queryset = ProductDemand.objects.select_related(
        'uid__user_validate', 'qid__t3id__t2id__t1id', 'aid__cid__pid', 'pmid',
        'wcid').filter(
            in_use=True,  # Must be in use
            match=True,
            qid=demand.qid,
            aid__cid__pid=demand.aid.cid.pid).exclude(
                uid__role=user.role  # Exclude same role
            ).filter(end_time__gt=now(), )

    # FIXME: Here we got a efficient issue, Every time user use this api, it will query the full set
    # of matched queryset. We must figure out how to fetch it by page, or ... cache it.
    matches = match_queryset.all()
    matches_list = [m for m in matches if confirm_satisfied(demand, m)]
    matches_list.sort(key=match_key, reverse=not asc)

    st, ed, n_pages = get_page_info_list(
        matches_list,
        count_per_page,
        page,
        index_error_excepiton=Error400("Page out of range"))

    # Set the match field
    if not demand.match:
        demand.match = True
        demand.save()

    # return sliced single page
    return demand, matches_list[st:ed], n_pages