def edit_reward(request, reward_id, template_name='retailer/edit_reward.html'): try: user = request.user retailer = user.retailer reward = Reward.objects.get(pk=reward_id) if reward not in retailer.rewards.all(): return HttpResponseRedirect(reverse('manage_deals')) except Reward.DoesNotExist: return HttpResponseRedirect(reverse('manage_deals')) if request.method == 'POST': form = RewardForm(request.POST, instance=reward) if form.is_valid(): reward = form.save() num_punches_for_reward = reward.punches subscribed_users = retailer.users_subscribed for user in subscribed_users.all(): num_punches_at_retailer = user.punches.filter(retailer=retailer).count() if num_punches_at_retailer >= num_punches_for_reward: earned_reward = EarnedReward(reward=reward) earned_reward.save() user.earned_rewards.add(earned_reward) reward_update = UserUpdate(action='earn', retailer=retailer, reward=earned_reward.reward, user=user) reward_update.save() return HttpResponseRedirect(reverse('manage_deals')) else: variables = RequestContext(request, { 'form':form, 'reward':reward }) return render_to_response(template_name, variables) else: form = RewardForm(instance=reward) variables = RequestContext(request, { 'form':form, 'reward':reward }) return render_to_response(template_name, variables)
def add_reward_level(request, template_name='retailer/add_reward_level.html'): if request.method == 'POST': form = RewardForm(request.POST) if form.is_valid(): reward = form.save(commit=False) retailer = request.user.retailer reward.retailer = retailer reward.save() retailer.rewards.add(reward) if reward.punches > retailer.max_level: retailer.max_level = reward.punches retailer.save() return HttpResponseRedirect('/retailer/manage/') else: form = RewardForm() variables = RequestContext(request, { 'form':form }) return render_to_response(template_name, variables)