Пример #1
0
def add_deal(request):
    data = json.loads(request.body)

    DRINK_CATEGORIES = {'beer': 1, 'wine': 2, 'liquor': 3}

    DEAL_TYPES = {'price': 1, 'percent-off': 2, 'price-off': 3}

    location_id = data.get('location_id')

    location = Location.objects.get(id=location_id)
    deal_data = data.get('deal')
    deal = Deal()
    deal.save()

    for day in deal_data.get('daysOfWeek'):
        for tp_data in deal_data.get('timePeriods'):
            # push the time periods to the time_periods array
            activeHour = ActiveHour()
            activeHour.dayofweek = day
            activeHour.start = tp_data.get("startTime")
            activeHour.end = tp_data.get("endTime")

            if activeHour.end == "":
                activeHour.end = None

            activeHour.save()
            deal.activeHours.add(activeHour)

    deal_detail_data = deal_data.get('dealDetails')
    for detail in deal_detail_data:
        drink_names = detail.get("names")
        category = DRINK_CATEGORIES[detail.get("category")]
        type = DEAL_TYPES[detail.get("dealType")]
        dealDetail = DealDetail(drinkName=drink_names,
                                drinkCategory=category,
                                detailType=type,
                                value=detail.get("dealValue"))

        dealDetail.save()
        deal.dealDetails.add(dealDetail)

    location.deals.add(deal)
    location.save()

    return HttpResponse('success')