Esempio n. 1
0
class CProductLike():
    def __init__(self):
        self.sproductlike = SProductLike()
        self.sproduct = SProduct()

    @verify_token_decorator
    def add_like(self):
        """添加(删除)喜欢(收藏)"""
        if is_tourist():
            return TOKEN_ERROR()
        json_data = parameter_required('prid')
        prid = json_data.get('prid')
        # 该用户是否已经收藏了此商品
        already_like = self.sproductlike.get_productlike_by_usidprid(
            request.user.id, prid)
        if not already_like:
            pl_dict = dict(plid=str(uuid.uuid4()),
                           usid=request.user.id,
                           prid=prid)
            dict_add_models('ProductLike', pl_dict)
            self.sproduct.update_like_num(prid)
            data = import_status('add_productlike_success', 'OK')
        else:
            # 删除
            self.sproductlike.del_productlike_usidprid(request.user.id, prid)
            self.sproduct.update_like_num(prid, -1)
            data = import_status('cancel_product_like_success', 'OK')
        plid = already_like.PLid if already_like else pl_dict['plid']
        data['data'] = {'plid': plid}
        return data

    @verify_token_decorator
    def get_like_list(self):
        """获取用户的收藏列表"""
        if is_tourist():
            return TOKEN_ERROR(u'未登录')
        args = request.args.to_dict()
        logger.debug("get like list args is %s", args)
        parameter_required("page_size", "page_num")
        page_num = args.get("page_num")
        page_size = args.get("page_size")
        page_num = 1 if not page_num else int(page_num)
        page_size = 5 if not page_size else int(page_size)
        try:
            productlike_list = self.sproductlike.get_productlike_list_by_usid(
                request.user.id, page_num, page_size)
            if not productlike_list:
                raise NOT_FOUND(u'用户无收藏信息')
            logger.info("get product like list success")
            map(self.fill_productinfo, productlike_list)
            # 获取转发数
            from WeiDian.service.SActivity import SActivity
            from WeiDian.service.SActivityFoward import SActivityFoward
            total_forward = 0
            for prlike in productlike_list:
                forward_act = SActivity().get_acid_by_filterid({
                    'AClinkvalue':
                    prlike.PRid,
                    'ACSkipType':
                    2,
                })
                for act in forward_act:
                    forward_num = SActivityFoward().get_fowardnum_by_acid(
                        act.ACid)
                    total_forward = total_forward + forward_num

                prlike.forwardnum = total_forward
                prlike.add("forwardnum")
            prlikecount = self.sproductlike.get_prlike_count_by_usid(
                request.user.id)
            data = import_status("get_product_like_success", "OK")
            data["count"] = prlikecount
            data["data"] = productlike_list
            return data
        except Exception as e:
            logger.exception("get product like error")
            raise SYSTEM_ERROR(u'收藏信息不存在')

    @verify_token_decorator
    def batch_delete_prlike(self):
        if is_tourist():
            return TOKEN_ERROR(u'未登录')
        data = request.json
        plid_list = data.get("plid").split(
            ',') if data and 'plid' in data else None
        logger.info("batch del prlike data is %s", data)
        print plid_list
        try:
            self.sproductlike.batch_delete_prlike(plid_list)
            response = import_status('delete_activity_success', 'OK')
            response['data'] = {"plidlist": plid_list}
            return response
        except:
            logger.exception("batch del prlike error")
            return SYSTEM_ERROR

    def fill_productinfo(self, prlike):
        prid = prlike.PRid
        prlike.productinfo = self.sproduct.get_product_by_prid(prid)
        if not prlike.productinfo:
            raise SYSTEM_ERROR(u'无此收藏商品')
        prlike.productinfo.fields = ['PRmainpic', 'PRsalestatus']
        prlike.add('productinfo')
        return prlike
Esempio n. 2
0
class CActivityLike():
    def __init__(self):
        self.sactivity = SActivity()
        self.salike = SActivityLike()
        self.sproduct = SProduct()
        self.sproductlike = SProductLike()

    @verify_token_decorator
    def like_or_cancel(self):
        if is_tourist():
            raise AUTHORITY_ERROR(u"未登录")
        print(request.user.USname.encode('utf8'))
        json_data = request.json
        logger.debug("act like data is %s", json_data)
        parameter_required('acid')
        acid = json_data.get('acid')
        usid = request.user.id
        already_like = self.salike.is_like(usid, acid)
        activity = self.sactivity.get_activity_by_acid(acid)
        if not already_like:
            logger.info("this is not already_like to add activitylike")
            al_dict = {
                'ALid': str(uuid.uuid4()),
                'USid': request.user.id,
                'ACid': acid
            }
            self.salike.add_model('ActivityLike', **al_dict)
            self.salike.add_like_by_acid(acid)
            data = import_status('add_activity_like_success', 'OK')
            # data = import_status('add_productlike_success', 'OK')
            if activity.ACSkipType == 2 and activity.AClinkvalue:
                logger.info("this is add productlike")
                pl_dict = {
                    'PLid': str(uuid.uuid4()),
                    'USid': request.user.id,
                    'PRid': activity.AClinkvalue
                }
                self.sproductlike.add_model('ProductLike', **pl_dict)
                self.sproduct.update_like_num(activity.AClinkvalue)
                data = import_status('add_ac_pr_like_success', 'OK')
        # elif not already_like:
        # al_dict = dict(
        #     alid=str(uuid.uuid4()),
        #     usid=request.user.id,
        #     acid=acid
        # )
        # dict_add_models('ActivityLike', al_dict)
        # self.salike.add_like_by_acid(acid)
        # data = import_status('add_activity_like_success', 'OK')
        # # data = import_status('add_productlike_success', 'OK')

        # elif already_like and not activity.PRid:
        #     self.salike.del_like(request.user.id, acid)
        #     self.salike.cancel_like_by_acid(acid)
        #     data = import_status('cancel_activity_like_success', 'OK')
        else:
            logger.info("this is already like activity to cancel like")
            self.salike.del_like(usid, acid)
            product_like = self.sproductlike.get_product_is_like_by_prid(
                usid, activity.AClinkvalue)
            logger.info("this is already like product to cancel like")
            if product_like:
                self.sproductlike.del_productlike_usidprid(
                    usid, activity.AClinkvalue)
            self.salike.cancel_like_by_acid(acid)
            self.sproduct.update_like_num(activity.AClinkvalue, -1)
            data = import_status('cancel_activity_like_success', 'OK')
        alid = already_like.ALid if already_like else al_dict['ALid']
        data['data'] = {'alid': alid}
        return data