Beispiel #1
0
def upload_photo(user, photo_from_object, dmid=None):
    """

    :param user:
    :param dmid:
    :param photo_from_object:
    :return:
    """

    if not user.is_validated:
        raise WLException(
            410, "User's validation does not passed, cannot publish.")

    # user exits
    photo = ProductDemandPhoto()
    if dmid is not None:
        try:
            demand = ProductDemand.objects.get(id=dmid)
            if demand.uid != user:
                raise Error404("No such demand")
        except ProductDemand.DoesNotExist:
            raise Error404("No such demand")

        photo.dmid = demand
        photo.inuse = True
    else:
        photo.inuse = False

    form = UploadPhotoForm(files=photo_from_object, instance=photo)
    # TODO: photo snapshot editing
    if form.is_valid():
        form.save()
        return photo.id
    else:
        raise Error403(str(form.errors))
Beispiel #2
0
def get_banner(count):
    # type: (int) -> list(Banner)
    try:
        photos = Banner.objects.filter(in_use=True).order_by('-id')[:3]
        return photos
    except Banner.DoesNotExist:
        raise Error404("No banner")
Beispiel #3
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
Beispiel #4
0
def get_popular_demand(page, **kw):
    """
    #TODO: choose one from role and user_sid
    :param role: 
    :param user_sid: 
    :param page: start from one
    :return: 
    """
    if 'role' in kw.keys():
        number_objects = ProductDemand.objects.filter(
            in_use=True).user_demand.objects.filter(in_use=True,
                                                    role=kw['role'])
    elif 'user_sid' in kw.keys():
        user_role = ProductDemand.objects.filter(
            uid=kw['user_sid']).user_demand.objects.filter(in_use=True).role
        number_objects = ProductDemand.objects.filter(
            in_use=True).user_demand.objects.filter(in_use=True,
                                                    role=user_role)

    # return sliced single page
    if page < 1 or page > number_objects.count():
        raise Error404('Page number exceeds range')
    else:
        startPage = page * page_size
        endPage = (page + 1) * page_size
        return ProductDemand.objects.filter(in_use=True)[startPage:endPage]
Beispiel #5
0
def get_specified_photo(id):
    """
    
    :param id: 
    :return:
    """
    try:
        return ProductDemandPhoto.objects.get(id=id).demand_photo.path
    except ProductDemandPhoto.DoesNotExist:
        raise Error404("No such photo.")
Beispiel #6
0
def get_my_demand(user_sid, page):
    """
    TODO: using user_sid to get specified page
    :param user_sid: 
    :param page: 
    :return: 
    """
    # check user
    user = sid_getuser(user_sid)
    if user is None:
        raise Error404("user_id do not exist")
    # page_size = 3  # number of items per page
    page = int(page)

    # return sliced single page
    if page < 1:
        raise Error404('Page number must be positive int')
    else:
        return ProductDemand.objects.filter(
            in_use=True)[(page - 1) * page_size:page * page_size]
Beispiel #7
0
def shut_demand(user, id):
    """

    :param user:
    :param id:
    :return:
    """

    try:
        demand_object = ProductDemand.objects.get(id=id, uid=user, in_use=True)
        demand_object.match = False
        demand_object.save()
    except ProductDemand.DoesNotExist:
        raise Error404("No such demand")
Beispiel #8
0
def delete_photo(user, id):
    """
    
    :param user_sid: 
    :param id: 
    :return: 
    """
    #  check whether photo id exists
    photo_to_delete = user.demand_photo.filter(inuse=True, id=id)
    if photo_to_delete.is_valid():
        photo_to_delete.inuse = False
        photo_to_delete.save()
    else:
        raise Error404('user_sid not found')
Beispiel #9
0
def get_matched_demand(user_sid, id, page):
    """
    TODO: DemandMatchingScore not set 
    :param user_sid: 
    :param id:
    :param page: 
    :return: 
    """
    # check user
    user = sid_getuser(user_sid)
    if user is None:
        raise Error404("user_id do not exist")
    # page_size = 3  # number of items per page
    page = int(page)

    # TODO: match the specific id
    demand = ProductDemand.objects.filter(id=id)

    # return sliced single page
    if page < 1:
        raise Error404('Page number must be positive int')
    else:
        return ProductDemand.objects.filter(
            in_use=True)[(page - 1) * page_size:page * page_size]
Beispiel #10
0
def get_demand_detail(user, id):
    """

    :param user:
    :param id:
    :return:
    """

    try:
        demand = ProductDemand.objects.select_related('uid__user_validate',
                                                      'qid__t3id__t2id__t1id',
                                                      'aid__cid__pid', 'pmid',
                                                      'wcid').get(in_use=True,
                                                                  id=id)
    except ProductDemand.DoesNotExist:
        raise Error404("No such demand.")
    return demand
Beispiel #11
0
def delete_photo(user, id):
    """
    
    :param user_sid: 
    :param id: 
    :return: 
    """
    #  check whether photo id exists
    try:
        photo_to_delete = ProductDemandPhoto.objects.get(id=id, inuse=True)
        if photo_to_delete.dmid.uid != user:
            raise ProductDemandPhoto.DoesNotExist
    except ProductDemandPhoto.DoesNotExist:
        raise Error404("No such photo")

    photo_to_delete.dmid = None
    photo_to_delete.inuse = False
    photo_to_delete.save()
Beispiel #12
0
def get_customed_demand(user_sid, id):
    """
    
    :param user_sid: 
    :param id: 
    :param page: 
    :return: 
    """
    # check user
    user = sid_getuser(user_sid)
    if user is None:
        raise Error404("user_id do not exist")

    # TODO: set the page parameter

    customed_demand = ProductDemand.objects.filter(in_use=True, id=id)
    return customed_demand, customed_demand.demand_photo.object.filter(
        in_use=True)
Beispiel #13
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