Ejemplo n.º 1
0
 def post(self, request):
     media_data = request.data
     ser_obj = MediaSerializer(data=media_data)
     res = BaseResponse()
     if ser_obj.is_valid():
         ser_obj.save()
         res.msg = "添加媒体成功!"
         res.data = ser_obj.data
         return Response(res.__dict__)
     else:
         res.code = -1001
         res.msg = "添加媒体失败!"
         res.data = None
         return Response(res.__dict__)
Ejemplo n.º 2
0
    def patch(self, request, *args, **kwargs):
        res = BaseResponse()
        plan_id = kwargs.get("id")
        receive = copy.deepcopy(request.data)

        settlement_data = receive.pop("settlement_info")

        plan_data = receive
        plan_data["launch_date"] = datetime.datetime.strptime(
            plan_data["launch_date"], "%Y-%m-%d")

        plan_obj = Plan.objects.filter(id=plan_id).first()

        settlement_obj = plan_obj.settlement
        settlement_ser = SettlementSerializer(instance=settlement_obj,
                                              data=settlement_data,
                                              partial=True)
        if settlement_ser.is_valid():

            plan_ser = PlanSerializer(instance=plan_obj,
                                      data=plan_data,
                                      partial=True)
            if plan_ser.is_valid():
                settlement_ser.save()
                plan_ser.save()
                res.msg = "修改计划成功"
                res.data = plan_ser.data
            else:
                res.msg = plan_ser.errors
                res.code = -1
        else:
            res.msg = settlement_ser.errors
            res.code = -1

        return Response(res.dict)
Ejemplo n.º 3
0
 def post(self, request, *args, **kwargs):
     filter_conditions = {}
     check_data = request.data
     media_id = check_data.get("media")
     start_time = check_data.get("start_time")
     end_time = check_data.get("end_time")
     cost = check_data.get("cost")
     filter_conditions["plan__media"] = media_id
     filter_conditions["plan__launch_date__gte"] = start_time
     filter_conditions["plan__launch_date__lte"] = end_time
     Settlement.objects.filter(**filter_conditions).update(
         m_statement_status=1, m_checkout_time=now())
     res = BaseResponse()
     print(start_time, end_time)
     ser = MediaStatementSerializer(
         data={
             "media": media_id,
             "start_time": start_time + " 00:00:00",
             "end_time": end_time + " 00:00:00",
             "cost": cost
         })
     if ser.is_valid():
         ser.save()
         res.msg = "媒体确认对账成功!"
         res.data = ser.data
     else:
         print(ser.errors)
     return Response(res.dict)
Ejemplo n.º 4
0
 def get(self, request, id):
     query_set = Media.objects.filter(id=id).first()
     media_ser = MediaSerializer(query_set)
     res = BaseResponse()
     res.msg = "获取媒体"
     res.data = media_ser.data
     return Response(res.dict)
Ejemplo n.º 5
0
 def get(self, request, *arg, **kwargs):
     query_set = Advertiser.objects.all().order_by('-id')
     ad_ser = AdvertSerializer(query_set, many=True)
     res = BaseResponse()
     res.msg = "获取所有广告成功!"
     res.data = ad_ser.data
     return Response(res.dict)
Ejemplo n.º 6
0
    def post(self, request, *args, **kwargs):
        res = BaseResponse()

        receive = request.data

        user = receive.get("username")
        pwd = receive.get("password")

        user_obj = auth.authenticate(username=user, password=pwd)
        if user_obj:
            random_str = "".join(str(uuid.uuid4()).split("-"))
            Token.objects.update_or_create(user=user_obj,
                                           defaults={
                                               "key": random_str,
                                               "created": timezone.now()
                                           })
            ser = UserInfoSerializer(instance=user_obj)
            res.msg = "登录成功"
            res.data = ser.data
            res.token = random_str
        else:
            try:
                UserInfo.objects.get(username=user)
                res.msg = "密码错误"
                res.code = -1001
            except UserInfo.DoesNotExist:
                res.msg = "用户不存在"
                res.code = -1002

        return Response(res.dict)
Ejemplo n.º 7
0
    def get(self, request, *args, **kwargs):
        res = BaseResponse()
        user_obj = request.user
        ser = UserInfoSerializer(user_obj)
        res.data = ser.data
        res.msg = "获取用户信息成功"

        return Response(res.dict)
Ejemplo n.º 8
0
 def get(self, request):
     choices = Media._meta.get_field("sort").choices
     li = []
     for c in choices:
         li.append({"sort": c[0], "text": c[1]})
     res = BaseResponse()
     res.msg = "获取媒体分类成功!"
     res.data = li
     return Response(res.__dict__)
Ejemplo n.º 9
0
    def get(self, request, *args, **kwargs):
        plan_id = kwargs.get("id")
        plan_obj = Plan.objects.filter(id=plan_id).first()
        plan_obj_ser = PlanSerializer(instance=plan_obj)

        res = BaseResponse()
        res.msg = "获取计划实例成功!"
        res.data = plan_obj_ser.data

        return Response(res.dict)
Ejemplo n.º 10
0
    def get(self, request, *args, **kwargs):
        slt_id = kwargs.get("id")

        slt_obj = Settlement.objects.filter(id=slt_id).first()
        slt_ser = SettlementSerializer(slt_obj)

        res = BaseResponse()
        res.msg = "获取结算实例成功"
        res.data = slt_ser.data

        return Response(res.dict)
Ejemplo n.º 11
0
 def post(self, request):
     receive = request.data
     ser_obj = AdvertSerializer(data=receive)
     res = BaseResponse()
     if ser_obj.is_valid():
         ser_obj.save()
         res.msg = "添加广告成功!"
         res.data = ser_obj.data
         return Response(res.dict)
     else:
         res.code = -1001
         res.msg = "添加广告失败!"
         return Response(res.dict)
Ejemplo n.º 12
0
    def get(self, request, *args, **kwargs):
        filter_conditions = {}
        for k, v in request.query_params.items():
            if v:
                filter_conditions[k] = v

        queryset = MediaStatement.objects.filter(
            **filter_conditions).order_by("-id")
        ser = MediaStatementSerializer(queryset, many=True)
        res = BaseResponse()
        res.msg = "获取历史账单成功!"
        res.data = ser.data
        return Response(res.dict)
Ejemplo n.º 13
0
    def post(self, request, *args, **kwargs):
        res = BaseResponse()
        receive = request.data
        ser = ChargeSortSerializer(data=receive)

        if ser.is_valid():
            ser.save()
            res.msg = "添加计费类型成功"
            res.data = ser.data
        else:
            res.code = -1
            res.msg = ser.errors
        return Response(res.dict)
Ejemplo n.º 14
0
    def post(self, request, *args, **kwargs):
        receive = copy.deepcopy(request.data)

        settlement_data = receive.pop("settlement_info")
        plan_data = receive
        print(request.data)

        # plan_data = request.data.get("plan")

        t = plan_data.get("launch_date_str") / 1000

        plan_data["launch_date"] = datetime.datetime.fromtimestamp(t)
        res = BaseResponse()
        p_obj = None
        if plan_data:
            print(plan_data)
            p_ser = PlanSerializer(data=plan_data)
            if p_ser.is_valid():
                p_obj = p_ser.save()

            else:
                print(p_ser.errors)

        if p_obj:

            # settlement_data = request.data.get("settlement")
            settlement_data["plan"] = p_obj.pk
            stl_ser = SettlementSerializer(data=settlement_data)
            if stl_ser.is_valid():
                stl_ser.save()
                res.msg = "计划创建成功!"
                res.data = stl_ser.data
            else:
                res.code = -1001
                res.msg = stl_ser.errors

        # MediaResult.objects.create(plan=p_obj)
        # AdResult.objects.create(plan=p_obj)

        # t = request.data.get("launch_date") / 1000
        #
        # launch_date_format = datetime.datetime.fromtimestamp(t)
        # print("launch_date_format", launch_date_format)
        #
        #
        #
        # data["launch_date"] = launch_date_format
        # m_policy_data = request.data.get("media_price_policy")
        # print("m_policy_data", m_policy_data)

        return Response(res.dict)
Ejemplo n.º 15
0
    def patch(self, request, id):
        res = BaseResponse()
        print("id", id)
        query_set = Media.objects.filter(id=id).first()
        media_ser = MediaSerializer(query_set, data=request.data, partial=True)
        if media_ser.is_valid():
            media_ser.save()
            res.msg = "媒体修改成功"
            res.data = media_ser.data
            return Response(res.dict)
        else:
            print(media_ser.errors)
            res.code = 1001
            res.msg = "媒体修改失败"

            return Response(res.dict)
Ejemplo n.º 16
0
    def patch(self, request, *args, **kwargs):
        res = BaseResponse()
        slt_id = kwargs.get("id")

        slt_obj = Settlement.objects.filter(id=slt_id).first()

        print(request.data)
        slt_ser = SettlementSerializer(instance=slt_obj,
                                       data=request.data,
                                       partial=True)
        if slt_ser.is_valid():
            slt_ser.save()
            res.msg = "结算成功!"
            res.data = slt_ser.data
        else:
            print(slt_ser.errors)

        return Response(res.dict)
Ejemplo n.º 17
0
    def get(self, request, *args, **kwargs):
        filter_conditions = {}

        for k, v in request.query_params.items():
            if "page" in k.lower():
                pass
            elif "date" in k.lower():
                t = int(v) / 1000

                date_time = datetime.datetime.fromtimestamp(t)
                filter_conditions["plan__" + k] = date_time

            else:
                filter_conditions["plan__" + k] = v
        query_set = Settlement.objects.filter(**filter_conditions)
        print("query_set", query_set)
        ser = SettlementSerializer(query_set, many=True)
        res = BaseResponse()
        res.data = ser.data
        return Response(res.dict)
Ejemplo n.º 18
0
    def get(self, request, *args, **kwargs):
        filter_conditions = {}

        for k, v in request.query_params.items():
            if "page" in k.lower():
                pass
            elif "date" in k.lower():
                t = int(v) / 1000

                date_time = datetime.datetime.fromtimestamp(t)
                filter_conditions["plan__" + k] = date_time

            else:
                filter_conditions["plan__" + k] = v
        print("filter_conditions", filter_conditions)
        query_set = Settlement.objects.filter(**filter_conditions) \
            .values("plan__advertiser__name", "plan__advertiser__pk") \
            .annotate(income=Sum("income"), start=Min("plan__launch_date"), end=Max("plan__launch_date"))

        # query_set = Settlement.objects.filter(**filter_conditions) \
        #     .values("plan__advertiser__name", "plan__advertiser__pk").annotate(cost=Sum("cost"))
        print("query_set", query_set.query)
        ser = AdvertCheckoutSerializer(query_set, many=True)
        # start_t = int(request.query_params.get("launch_date__gte")) / 1000
        # start_date_f = datetime.datetime.fromtimestamp(start_t).strftime("%Y-%m-%d")
        #
        # end_t = int(request.query_params.get("launch_date__lte")) / 1000
        # end_date_f = datetime.datetime.fromtimestamp(end_t).strftime("%Y-%m-%d")
        #
        # res_data = {
        #     "media": request.query_params.get("media"),
        #     "start_date_str": request.query_params.get("launch_date__gte"),
        #     "start_date": start_date_f,
        #     "end_date_str": request.query_params.get("launch_date__lte"),
        #     "end_date": end_date_f,
        #     "list": ser.data
        # }
        res = BaseResponse()
        res.msg = "广告待对账"
        res.data = ser.data
        return Response(res.dict)
Ejemplo n.º 19
0
    def get(self, request, *arg, **kwargs):
        option_dict = {}
        loc_query_set = AdLocation.objects.all()
        loc_ser = AdLocationSerializer(loc_query_set, many=True)

        option_dict["location"] = loc_ser.data

        port_query_set = Port.objects.all()
        port_ser = PortSerializer(port_query_set, many=True)

        option_dict["port"] = port_ser.data

        sort_query_set = ChargeSort.objects.all()
        sort_ser = ChargeSortSerializer(sort_query_set, many=True)

        option_dict["charge_sort"] = sort_ser.data

        res = BaseResponse()
        res.msg = "获取配置成功!"
        res.data = option_dict
        return Response(res.dict)
Ejemplo n.º 20
0
    def get(self, request, *args, **kwargs):
        print("query_params", request.query_params)
        filter_conditions = {}
        is_paging = True
        if request.query_params.get("noPage"):
            is_paging = False
        for k, v in request.query_params.items():
            if "page" in k.lower():

                pass
            elif "date" in k.lower():
                t = int(v) / 1000

                date_time = datetime.datetime.fromtimestamp(t)
                filter_conditions[k] = date_time

            else:
                filter_conditions[k] = v

        print("filter_conditions", filter_conditions)
        query_set = Plan.objects.filter(**filter_conditions).order_by("-id")
        # query_set = Plan.objects.all().order_by("-id")
        print("query_set.count()", query_set.count())
        if is_paging:
            paginator = MyPagination()
            query_set = paginator.paginate_queryset(query_set,
                                                    request,
                                                    view=None)
            count = paginator.page.paginator.count
        else:
            count = query_set.count()
        plan_ser = PlanSerializer(query_set, many=True)
        # print("plan_ser.data", plan_ser.data)
        data = {"count": count, "list": plan_ser.data}
        res = BaseResponse()
        res.msg = "获取计划列表成功"
        res.data = data

        return Response(res.dict)
Ejemplo n.º 21
0
    def get(self, request):
        request = request  # type:Request
        queryset = Media.objects.all().order_by("-id")
        name_param = request.query_params.get("name")
        if name_param:
            queryset = queryset.filter(name__contains=name_param)
        media_ser = MediaSerializer(queryset, many=True)
        # paginator = MyPagination()
        # page_queryset = paginator.paginate_queryset(queryset, request, view=None)
        #
        # media_ser = MediaSerializer(page_queryset, many=True)
        #
        # data = OrderedDict([
        #     ('count', paginator.page.paginator.count),
        #     ('next', paginator.get_next_link()),
        #     ('previous', paginator.get_previous_link()),
        #     ('results', media_ser.data)
        # ])

        res = BaseResponse()
        res.msg = "获取媒体列表成功!"
        res.data = media_ser.data
        return Response(res.__dict__)
Ejemplo n.º 22
0
    def post(self, request, *args, **kwargs):
        res = BaseResponse()
        receive = request.data

        if "create" in receive:
            create_date = receive.get("create")
            print(create_date)
            date_range = create_date.get("launch_date_range")
            print("date_range", date_range)
            if not date_range:
                res.msg = "没有日期参数"
                return Response(res.dict)
            start_t = int(date_range[0]) / 1000
            start_date = datetime.datetime.fromtimestamp(start_t)
            end_t = int(date_range[1]) / 1000
            end_date = datetime.datetime.fromtimestamp(end_t)

            print(start_date, end_date)

            from tgsAdmin.utils.common import date_Range_list
            plan_obj_list = []
            settlement_data = create_date.pop("settlement_info")

            for date in date_Range_list(start_date,
                                        end_date,
                                        format="%Y-%m-%d %H:%M:%S"):
                print(date)
                plan_data = create_date.copy()
                plan_data["launch_date"] = date
                p_ser = PlanSerializer(data=plan_data)
                if p_ser.is_valid():
                    p_obj = p_ser.save()
                    plan_obj_list.append(p_obj)
                else:
                    res.msg = p_ser.errors
                    res.code = -1
                    return Response(res.dict)

            # print(plan_obj_list)
            settlement_list = []
            plan_pk_list = []
            # settlement_data = create_date.get("settlement").copy()
            for plan in plan_obj_list:
                settlement_list.append(
                    Settlement(
                        m_unit_price=settlement_data.get("m_unit_price"),
                        plan_launch_count=settlement_data.get(
                            "plan_launch_count"),
                        a_unit_price=settlement_data.get("a_unit_price"),
                        plan=plan))
                plan_pk_list.append(plan.pk)

            Settlement.objects.bulk_create(settlement_list)
            plan_query = Plan.objects.filter(id__in=plan_pk_list)
            plan_ser = PlanSerializer(plan_query, many=True)
            for p in plan_query:
                scheduler.add_job(update_plan_status,
                                  'date',
                                  run_date=datetime.datetime.now() +
                                  datetime.timedelta(seconds=20),
                                  args=[p.pk],
                                  jobstore='redis',
                                  replace_existing=True)
            # scheduler.add_job(test_test, 'interval',
            #                   seconds=3, jobstore='redis', replace_existing=True)

            res.msg = "批量创建计划成功!"
            res.data = plan_ser.data
        return Response(res.dict)