当前位置: 首页>>代码示例>>Python>>正文


Python Activity.average_price方法代码示例

本文整理汇总了Python中models.Activity.average_price方法的典型用法代码示例。如果您正苦于以下问题:Python Activity.average_price方法的具体用法?Python Activity.average_price怎么用?Python Activity.average_price使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Activity的用法示例。


在下文中一共展示了Activity.average_price方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: post

# 需要导入模块: from models import Activity [as 别名]
# 或者: from models.Activity import average_price [as 别名]
    def post(self, request, format=None):
        date = request.DATA.get('date')
        try:
            activity = Activity.objects.get(name=request.DATA.get('activity'))
        except Activity.DoesNotExist:
            activity = Activity(name=request.DATA.get('activity'), average_price=0)
            activity.save()

        p = Process(date=request.DATA.get('date'), activity=activity, average_price=0)
        p.save()

        consumer_list = json.loads(request.DATA.get('list'))
        user_to_be_saved = []
        total = 0
        try:
            for num, i in enumerate(consumer_list):
                name = i.get('name')
                prepay = i.get('prepay')
                consume = i.get('consume')
                #check name
                user = User.objects.get(name=name)
                p.participants.add(user)

                #user money
                user.remain += prepay - consume

                user_to_be_saved.append(user)
                total += consume
                #send email
        except:
            print sys.exc_info()
            p.delete()
            return Response(status=404)

        #caculate the average price of process
        p.average_price =total/(num +1)
        #caculate the average price of activity
        if p.average_price == 0:
            activity.average_price = p.average_price
        else:
            activity.average_price = (p.average_price + activity.average_price)/2

        p.save()
        for i in user_to_be_saved:
            i.save()
        return Response({"detail":"success"} ,status=200)
开发者ID:thesues,项目名称:tutorial,代码行数:48,代码来源:views.py


注:本文中的models.Activity.average_price方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。