Ejemplo n.º 1
0
    def authenticate(self, request, **kwargs):

        content = request.POST
        if not (request.path.startswith("/rest/") and content.get('unionid')):
            return None

        openid = content.get('openid')
        unionid = content.get('unionid')
        if not valid_openid(openid) or not valid_openid(unionid):
            return AnonymousUser()

        params = {
            'openid': content.get('openid'),
            'unionid': content.get('unionid'),
            'nickname': content.get('nickname'),
            'headimgurl': content.get('headimgurl'),
        }
        try:
            profile = Customer.objects.get(unionid=unionid,
                                           status=Customer.NORMAL)
            if profile.user:
                user = profile.user
                if not user.is_active:
                    user.is_active = True
                    user.save()
            else:
                user, state = User.objects.get_or_create(username=unionid,
                                                         is_active=True)
                profile.user = user
                profile.save()
            if profile.thumbnail != params.get('headimgurl'):  # 更新头像
                profile.thumbnail = params.get('headimgurl')
                profile.save()

        except Customer.DoesNotExist:
            if not self.create_unknown_user:
                return AnonymousUser()

            user, state = User.objects.get_or_create(username=unionid,
                                                     is_active=True)
            profile, state = Customer.objects.get_or_create(unionid=unionid,
                                                            user=user)
            if profile.nick == '':
                profile.set_nickname(params.get('nickname'))
                profile.thumbnail = params.get('headimgurl')
                profile.save()
            # if not normal user ,no login allowed
            if profile.status != Customer.NORMAL:
                return AnonymousUser()

        task_Refresh_Sale_Customer.delay(params, app_key=settings.WX_APPID)
        return user
Ejemplo n.º 2
0
    def get(self, request):

        openid, unionid = self.get_openid_and_unionid(request)
        if not valid_openid(openid) or not valid_openid(unionid):
            redirect_url = self.get_snsuserinfo_redirct_url(request)
            return redirect(redirect_url)

        xlmm = XiaoluMama.objects.get(openid=unionid)
        referal_list = XiaoluMama.objects.filter(referal_from=xlmm.mobile,
                                                 status=XiaoluMama.EFFECT)
        cashout_objs = CashOut.objects.filter(xlmm=xlmm.pk)

        # day_to   = datetime.datetime.now()
        #         day_from = day_to - datetime.timedelta(days=30)
        # 点击数
        clickcounts = ClickCount.objects.filter(linkid=xlmm.id)
        click_nums = clickcounts.aggregate(
            total_count=Sum('valid_num')).get('total_count') or 0

        # 订单数
        shoppings = StatisticsShopping.objects.filter(linkid=xlmm.id)
        shoppings_count = shoppings.count()

        app_cashouts = cashout_objs.filter(
            status__in=(CashOut.APPROVED,
                        CashOut.COMPLETED)).order_by('created')
        kefu_mobile = '18516655836'
        if app_cashouts.count(
        ) == 0 or app_cashouts[0].created > datetime.datetime(2015, 6, 30, 15):
            kefu_mobile = '18516316989'
        cash_outable = (click_nums >= 150
                        and shoppings_count >= 1) or shoppings_count >= 6
        cash, payment, could_cash_out = get_xlmm_cash_iters(
            xlmm, cash_outable=cash_outable)
        pending_cashouts = cashout_objs.filter(status=CashOut.PENDING)
        data = {
            "xlmm": xlmm,
            "cashout": pending_cashouts.count(),
            'kefu_mobile': kefu_mobile,
            "referal_list": referal_list,
            "could_cash_out": int(could_cash_out)
        }

        response = render(
            request,
            "mama_cashout.html",
            data,
        )
        self.set_cookie_openid_and_unionid(response, openid, unionid)
        return response
Ejemplo n.º 3
0
    def get(self, request):
        self.set_appid_and_secret(settings.WX_PUB_APPID,
                                  settings.WX_PUB_APPSECRET)
        openid, unionid = self.get_openid_and_unionid(request)
        if not valid_openid(openid):
            redirect_url = self.get_snsuserinfo_redirct_url(request)
            return redirect(redirect_url)

        xlmm = None
        wxuser = None
        xlmm_qs = XiaoluMama.objects.filter(openid=unionid)
        if xlmm_qs.exists():
            xlmm = xlmm_qs[0]
        wxuser_qs = WeiXinUser.objects.filter(openid=openid)
        if wxuser_qs.exists():
            wxuser = wxuser_qs[0]

        return render(
            request,
            'wxauth_checkview.html',
            {
                'openid': openid,
                'unionid': unionid,
                'xlmm': xlmm,
                'wxuser': wxuser,
            },
        )
Ejemplo n.º 4
0
    def get(self, request, linkid):
        click_url = request.get_full_path()
        if not self.is_from_weixin(request):
            share_url = WEB_SHARE_URL.format(site_url=settings.M_SITE_URL,
                                             mm_linkid=linkid,
                                             ufrom='web')
            logger.info({
                'action': 'ClickChannelLogView',
                'desc': 'not from weixin',
                'mm_linkid': linkid,
                'redirect_url': share_url,
                'click_url': click_url,
                'created': datetime.datetime.now(),
            })
            return redirect(share_url)
        self.set_appid_and_secret(settings.WX_PUB_APPID,
                                  settings.WX_PUB_APPSECRET)
        openid, unionid = self.get_openid_and_unionid(request)
        if not valid_openid(openid):
            redirect_url = self.get_wxauth_redirct_url(request)
            return redirect(redirect_url)
        click_time = datetime.datetime.now()
        ctasks.task_Create_Click_Record.delay(linkid, openid, unionid,
                                              click_time,
                                              settings.WX_PUB_APPID, click_url)

        if not valid_openid(unionid):
            unionid = get_unionid_by_openid(openid, settings.WX_PUB_APPID)
        xlmms = XiaoluMama.objects.filter(openid=unionid)
        if xlmms.exists():
            share_url = WEB_SHARE_URL.format(site_url=settings.M_SITE_URL,
                                             mm_linkid=xlmms[0].id,
                                             ufrom='wx')
        else:
            share_url = settings.M_SITE_URL
        response = redirect(share_url)
        self.set_cookie_openid_and_unionid(response, openid, unionid)
        return response
Ejemplo n.º 5
0
    def post(self, request):
        status = {"code": 400, "status": "该功能已经停止,妈妈钱包余额已经转入个人钱包"}
        return HttpResponse(json.dumps(status),
                            content_type='application/json')

        content = request.POST
        openid, unionid = self.get_openid_and_unionid(request)
        if not valid_openid(unionid):
            raise Http404
        could_cash_out = 0
        xlmm = XiaoluMama.objects.filter(openid=unionid)
        if xlmm.count() > 0:
            # 点击数
            clickcounts = ClickCount.objects.filter(linkid=xlmm[0].id)
            click_nums = clickcounts.aggregate(
                total_count=Sum('valid_num')).get('total_count') or 0
            # 订单数
            shoppings = StatisticsShopping.objects.filter(linkid=xlmm[0].id)
            shoppings_count = shoppings.count()
            cash_outable = click_nums >= 150 or shoppings_count >= 6
            cash, payment, could_cash_out = get_xlmm_cash_iters(
                xlmm[0], cash_outable=cash_outable)
        v = content.get("v")
        m = re.match(r'^\d+$', v)
        status = {"code": 0, "status": "ok"}
        if m:
            value = int(m.group()) * 100
            could_cash_out_int = int(could_cash_out) * 100
            # 2015-8-28 修改为 只是允许100 和200 提现
            # if value < 2000 or value > 20000 or value > could_cash_out_int:
            if value not in (10000, 20000) or value > could_cash_out_int:
                status = {"code": 3, "status": "input error"}
            else:
                try:
                    xlmm = XiaoluMama.objects.get(openid=unionid)
                    cash_out = CashOut(xlmm=xlmm.pk,
                                       cash_out_type=CashOut.RED_PACKET,
                                       value=value)
                    cash_out.approve_time = datetime.datetime.now()
                    cash_out.save()
                except:
                    status = {"code": 1, "status": "error"}
        else:
            status = {"code": 2, "status": "input error"}

        return HttpResponse(json.dumps(status),
                            content_type='application/json')
Ejemplo n.º 6
0
    def authenticate(self, request, auth_code, wxpub_appid, **kwargs):

        code = auth_code
        wxpub_appsecret = WeiXinAccount.get_wxpub_account_secret(wxpub_appid)
        userinfo = options.get_auth_userinfo(code,
                                             appid=wxpub_appid,
                                             secret=wxpub_appsecret,
                                             request=request)
        openid, unionid = userinfo.get('openid'), userinfo.get('unionid')
        if not openid or not unionid:
            openid, unionid = options.get_cookie_openid(
                request.COOKIES, wxpub_appid)

        if openid and not unionid:
            logger.warn('weixin unionid not return:openid=%s' % openid)
            unionid = self.get_unoinid(openid, wxpub_appid)

        if not valid_openid(unionid):
            return AnonymousUser()

        try:
            with transaction.atomic():
                profile = Customer.objects.get(unionid=unionid,
                                               status=Customer.NORMAL)
                # 如果openid有误,则重新更新openid
                if unionid and openid:
                    WeixinUnionID.objects.get_or_create(openid=openid,
                                                        app_key=wxpub_appid,
                                                        unionid=unionid)
                    task_Refresh_Sale_Customer.delay(userinfo,
                                                     app_key=wxpub_appid)

                if profile.user:
                    if not profile.user.is_active:
                        profile.user.is_active = True
                        profile.user.save()
                    return profile.user
                else:
                    user, state = User.objects.get_or_create(username=unionid,
                                                             is_active=True)
                    profile.user = user
                    profile.save()

        except Customer.DoesNotExist:
            if not self.create_unknown_user or not unionid:
                return AnonymousUser()

            nick = userinfo.get('nickname')
            thumbnail = userinfo.get('headimgurl')
            user, state = User.objects.get_or_create(username=unionid,
                                                     is_active=True)
            profile, state = Customer.objects.get_or_create(unionid=unionid,
                                                            user=user)
            if not (profile.nick == nick
                    and profile.thumbnail == thumbnail) and nick and thumbnail:
                profile.nick = nick
                profile.thumbnail = thumbnail
                profile.save(update_fields=['nick', 'thumbnail'])

            # if not normal user ,no login allowed
            if profile.status != Customer.NORMAL:
                return AnonymousUser()

            WeixinUnionID.objects.get_or_create(openid=openid,
                                                app_key=wxpub_appid,
                                                unionid=unionid)
            task_Refresh_Sale_Customer.delay(userinfo, app_key=wxpub_appid)

        return user
Ejemplo n.º 7
0
    def get(self, request, linkid):
        from django_statsd.clients import statsd

        statsd.incr('xiaolumm.weixin_click')
        content = request.GET
        next_page = content.get('next', None)
        click_url = request.get_full_path()
        # print 'next_page:', next_page
        # logger.error('next_page %s-path:%s' % (next_page, content))
        click_time = datetime.datetime.now()
        if not self.is_from_weixin(request):
            share_url = get_share_url(next_page=next_page,
                                      mm_linkid=linkid,
                                      ufrom='web')
            response = redirect(share_url)
            response.set_cookie('mm_linkid', linkid, max_age=86400)
            if XiaoluSwitch.is_switch_open(10):
                logger.info({
                    'action': 'ClickLogView',
                    'code': 1,
                    'mm_linkid': linkid,
                    'redirect_url': share_url,
                    'click_url': click_url,
                    'created': datetime.datetime.now(),
                })
            return response

        self.set_appid_and_secret(settings.WX_PUB_APPID,
                                  settings.WX_PUB_APPSECRET)
        openid, unionid = self.get_openid_and_unionid(request)
        if not valid_openid(openid):
            redirect_url = self.get_wxauth_redirct_url(request)
            return redirect(redirect_url)

        ctasks.task_Create_Click_Record.delay(linkid, openid, unionid,
                                              click_time,
                                              settings.WX_PUB_APPID, click_url)

        if not valid_openid(unionid):
            unionid = get_unionid_by_openid(openid, settings.WX_PUB_APPID)

        # 2017-2-27 正式的小鹿妈妈为精英妈妈及以上
        if int(linkid) <= 0:
            xlmms = XiaoluMama.objects.filter(
                openid=unionid,
                status=XiaoluMama.EFFECT,
                charge_status=XiaoluMama.CHARGED,
                last_renew_type__gte=XiaoluMama.ELITE)
            linkid = xlmms.exists() and xlmms[0].id or linkid

        if int(linkid) <= 0 and XiaoluSwitch.is_switch_open(10):
            logger.info({
                'action': 'ClickLogView',
                'code': 2,
                'unionid': unionid,
                'mm_linkid': linkid,
                'http_referer': request.META.get('HTTP_REFERER'),
                'click_url': click_url,
                'cookies': request.COOKIES,
                'created': datetime.datetime.now(),
            })

        # 2017-2-27 如果打开链接的人是个你的铺子妈妈,以前是会用你的铺子妈妈id替换next里面的mamaid,现在不这样做,还是使用原来的linkid
        # share_url = get_share_url(next_page=next_page, mm_linkid=mm_linkid, ufrom='wx')
        share_url = get_share_url(next_page=next_page,
                                  mm_linkid=linkid,
                                  ufrom='wx')
        response = redirect(share_url)
        self.set_cookie_openid_and_unionid(response, openid, unionid)
        response.set_cookie('mm_linkid', linkid, max_age=86400)
        return response
Ejemplo n.º 8
0
    def get(self, request):
        content = request.GET
        openid, unionid = self.get_openid_and_unionid(request)
        if not valid_openid(openid) or not valid_openid(unionid):
            redirect_url = self.get_snsuserinfo_redirct_url(request)
            return redirect(redirect_url)

        wx_users = Customer.objects.filter(unionid=unionid,
                                           status=Customer.NORMAL)
        if not wx_users.exists():
            return HttpResponse(
                u'<html><body>你还不是小鹿妈妈,请先<a href="/m/register/">申请</a></body></html>'
            )

        wx_user = wx_users[0]
        unionid = unionid or wx_user.unionid
        daystr = content.get("day", None)
        today = datetime.date.today()
        year, month, day = today.year, today.month, today.day

        target_date = today
        if daystr:
            year, month, day = daystr.split('-')
            target_date = datetime.date(int(year), int(month), int(day))
            if target_date > today:
                target_date = today

        time_from = datetime.datetime(target_date.year, target_date.month,
                                      target_date.day)
        time_to = datetime.datetime(target_date.year, target_date.month,
                                    target_date.day, 23, 59, 59)

        active_start = ctasks.CLICK_ACTIVE_START_TIME.date() == time_from.date(
        )
        prev_day = target_date - datetime.timedelta(days=1)
        next_day = None
        if target_date < today:
            next_day = target_date + datetime.timedelta(days=1)

        data = {}
        try:
            xlmm, state = XiaoluMama.objects.get_or_create(openid=unionid)
            if xlmm.status == XiaoluMama.FROZEN:
                return render(request, "mama_404.html")

            exam_pass = xlmm.exam_Passed()
            order_num = 0
            total_value = 0
            carry = 0
            rebeta_swift = False

            order_list = StatisticsShopping.normal_objects.filter(
                linkid=xlmm.pk, shoptime__range=(time_from, time_to))
            order_stat = StatisticsShoppingByDay.objects.filter(
                linkid=xlmm.pk, tongjidate=target_date)
            carry_confirm = False
            # if target_date >= ORDER_RATEUP_START:
            #                 rebeta_swift = True

            if order_stat.count() > 0:
                order_num = order_stat[0].buyercount
                total_value = order_stat[0].orderamountcount / 100.0
                carry = order_stat[0].todayamountcount / 100.0
                carry_confirm = order_stat[0].carry_Confirm()

            click_price = xlmm.get_Mama_Click_Price_By_Day(
                order_num, day_date=target_date) / 100.0
            futrue_date = datetime.date.today() + datetime.timedelta(days=1)
            futrue_click_price = 0
            if target_date == datetime.date.today():
                futrue_click_price = xlmm.get_Mama_Click_Price_By_Day(
                    0, day_date=futrue_date) / 100.0

            click_num = 0
            click_pay = 0
            ten_click_num = 0
            ten_click_price = click_price + 0.3
            ten_click_pay = 0
            if not active_start:
                click_state = ClickCount.objects.filter(linkid=xlmm.pk,
                                                        date=target_date)
                if click_state.count() > 0:
                    click_num = click_state[0].valid_num
                else:
                    click_list = Clicks.objects.filter(
                        linkid=xlmm.pk,
                        click_time__range=(time_from, time_to),
                        isvalid=True)
                    click_num = click_list.values('openid').distinct().count()

                # 设置最高有效最高点击上限
                max_click_count = xlmm.get_Mama_Max_Valid_Clickcount(
                    order_num, day_date=target_date)
                if time_from.date() >= ctasks.CLICK_MAX_LIMIT_DATE:
                    click_num = min(max_click_count, click_num)

                click_pay = click_price * click_num
            else:
                click_qs = Clicks.objects.filter(linkid=xlmm.pk, isvalid=True)
                click_num = click_qs.filter(click_time__range=(
                    datetime.datetime(2015, 6, 15),
                    datetime.datetime(2015, 6, 15, 10, 0, 0)
                )).values('openid').distinct().count()

                ten_click_num = click_qs.filter(click_time__range=(
                    datetime.datetime(2015, 6, 15, 10),
                    datetime.datetime(2015, 6, 15, 23, 59, 59)
                )).values('openid').distinct().count()

                # 设置最高有效最高点击上限
                max_click_count = xlmm.get_Mama_Max_Valid_Clickcount(
                    order_num, day_date=target_date)
                if time_from.date() >= ctasks.CLICK_MAX_LIMIT_DATE:
                    click_num = min(max_click_count, click_num)
                    ten_click_num = min(max_click_count, ten_click_num)

                click_pay = click_num * click_price
                ten_click_pay = ten_click_num * ten_click_price

            data = {
                "xlmm": xlmm,
                "pk": xlmm.pk,
                'rebeta_swift': rebeta_swift,
                "order_num": order_num,
                "order_list": order_list,
                "exam_pass": exam_pass,
                "total_value": total_value,
                "carry": carry,
                'carry_confirm': carry_confirm,
                "target_date": target_date,
                "prev_day": prev_day,
                "next_day": next_day,
                'active_start': active_start,
                "click_num": click_num,
                "futrue_click_price": futrue_click_price,
                "click_price": click_price,
                "click_pay": click_pay,
                "ten_click_num": ten_click_num,
                "ten_click_price": ten_click_price,
                "ten_click_pay": ten_click_pay
            }

        except Exception, exc:
            logger.error(exc.message, exc_info=True)
Ejemplo n.º 9
0
    def get(self, request):
        if True:
            return redirect('/sale/promotion/appdownload/')
        openid, unionid = self.get_openid_and_unionid(request)
        if not valid_openid(openid) or not valid_openid(unionid):
            redirect_url = self.get_snsuserinfo_redirct_url(request)
            return redirect(redirect_url)

        # service = WeixinUserService(settings.WEIXIN_APPID, openId=openid, unionId=unionid)
        # wx_user = service._wx_user

        wx_users = Customer.objects.filter(unionid=unionid,
                                           status=Customer.NORMAL)
        if not wx_users.exists():
            return HttpResponse(
                u'<html><body>你还不是小鹿妈妈,请先<a href="/m/register/">申请</a></body></html>'
            )

        wx_user = wx_users[0]
        target_date = datetime.date.today()
        yesterday = target_date - datetime.timedelta(days=1)
        time_from = datetime.datetime(target_date.year, target_date.month,
                                      target_date.day)
        time_to = datetime.datetime(target_date.year, target_date.month,
                                    target_date.day, 23, 59, 59)

        mobile = wx_user.mobile
        unionid = unionid or wx_user.unionid
        data = {}
        try:
            referal_num = XiaoluMama.objects.filter(
                referal_from=mobile, status=XiaoluMama.EFFECT).count()
            xlmm, state = XiaoluMama.objects.get_or_create(openid=unionid)
            if xlmm.mobile != mobile:
                xlmm.mobile = mobile
                xlmm.weikefu = xlmm.weikefu or wx_user.nick
                update_model_fields(xlmm, update_fields=['mobile', 'weikefu'])

            if xlmm.status == XiaoluMama.FROZEN:
                return render(request, "mama_404.html")

            mobile_revised = "%s****%s" % (mobile[:3], mobile[-4:])

            mm_clogs = CarryLog.objects.filter(
                xlmm=xlmm.id)  # .exclude(log_type=CarryLog.ORDER_RED_PAC)
            pending_value = mm_clogs.filter(status=CarryLog.PENDING).aggregate(
                total_value=Sum('value')).get('total_value') or 0

            total_income = mm_clogs.filter(
                carry_type=CarryLog.CARRY_IN,
                status=CarryLog.CONFIRMED).aggregate(
                    total_value=Sum('value')).get('total_value') or 0
            total_pay = mm_clogs.filter(
                carry_type=CarryLog.CARRY_OUT,
                status=CarryLog.CONFIRMED).aggregate(
                    total_value=Sum('value')).get('total_value') or 0

            yest_income = mm_clogs.filter(
                carry_type=CarryLog.CARRY_IN, carry_date=yesterday).aggregate(
                    total_value=Sum('value')).get('total_value') or 0
            yest_pay = mm_clogs.filter(
                carry_type=CarryLog.CARRY_OUT, carry_date=yesterday).aggregate(
                    total_value=Sum('value')).get('total_value') or 0

            pending_value = pending_value / 100.0
            total_income = total_income / 100.0
            total_pay = total_pay / 100.0
            yest_income = yest_income / 100.0
            yest_pay = yest_pay / 100.0

            abnormal_cash = xlmm.cash_money - (total_income - total_pay
                                               )  # 异常金额
            order_num = 0
            order_stat = StatisticsShoppingByDay.objects.filter(
                linkid=xlmm.pk, tongjidate=target_date)
            if order_stat.count() > 0:
                order_num = order_stat[0].buyercount

            click_list = Clicks.objects.filter(linkid=xlmm.pk,
                                               click_time__range=(time_from,
                                                                  time_to),
                                               isvalid=True)
            click_num = click_list.values('openid').distinct().count()

            # 设置最高有效最高点击上限
            max_click_count = xlmm.get_Mama_Max_Valid_Clickcount(
                order_num, day_date=target_date)
            if time_from.date() >= ctasks.CLICK_MAX_LIMIT_DATE:
                click_num = min(max_click_count, click_num)

            referal_mm = 0
            # if xlmm.progress != XiaoluMama.PASS :
            #                 if xlmm.referal_from:
            #                     referal_mamas = XiaoluMama.objects.filter(mobile=xlmm.referal_from)
            #                     if referal_mamas.count() > 0:
            #                         referal_mm = referal_mamas[0].id
            #                 else:
            #                     referal_mm = 1
            try:
                adver = XlmmAdvertis.objects.get(show_people=xlmm.agencylevel,
                                                 is_valid=True)
                now = datetime.datetime.now()
                if now >= adver.start_time and now <= adver.end_time:
                    adv_cntnt = adver
                else:
                    adv_cntnt = None
            except XlmmAdvertis.DoesNotExist:
                adv_cntnt = None

            data = {
                "mobile": mobile_revised,
                "click_num": click_num,
                "xlmm": xlmm,
                "advertise": adv_cntnt,
                'referal_mmid': referal_mm,
                "order_num": order_num,
                "pk": xlmm.pk,
                'pending_value': pending_value,
                "referal_num": referal_num,
                'total_income': total_income,
                'total_pay': total_pay,
                'abnormal_cash': abnormal_cash,
                'yest_income': yest_income,
                'yest_pay': yest_pay
            }

        except Exception, exc:
            logger.error(exc.message, exc_info=True)