def admin_accept_reward(request): if request.method == 'POST': try: response = dict() response['success'] = False access_token = request.POST['access_token'] if 'expires' in request.POST and request.POST['expires'] is not None and request.POST['expires'] != '': expires = int(request.POST['expires']) else: expires = 1000 earned_reward_id = int(request.POST['earned_reward_id']) retailer_id = int(request.POST['retailer_id']) retailer_password = request.POST['retailer_password'] employee_username = request.POST['employee_username'] user = authenticate(access_token=access_token, expires=expires) earned_reward = user.earned_rewards.get(pk=earned_reward_id) if earned_reward.redeemed: response['error'] = 'Reward was Already Redeemed' return HttpResponse(simplejson.dumps(response), mimetype='application/json') retailer = Retailer.objects.get(pk=retailer_id) if retailer.admin_password != retailer_password: response['error'] = 'Incorrect Password' return HttpResponse(simplejson.dumps(response), mimetype='application/json') if earned_reward.reward.retailer != retailer: response['error'] = 'Reward is not valid at this retailer' return HttpResponse(simplejson.dumps(response), mimetype='application/json') employee = retailer.employees.get(username=employee_username) user_update_punch = UserUpdate(action='redeem', retailer=retailer, reward=earned_reward.reward, user=user) user_update_punch.save() earned_reward.redeemed = True earned_reward.save() response['success'] = True response['reward_text'] = earned_reward.reward.text except UserAccount.DoesNotExist: response['error'] = 'Invalid Barcode' except Retailer.DoesNotExist: response['error'] = 'Retailer Does Not Exist' except Employee.DoesNotExist: response['error'] = 'Employee Does Not Exist' except EarnedReward.DoesNotExist: response['error'] = 'Earned Reward Does Not Exist' return HttpResponse(simplejson.dumps(response), mimetype='application/json') else: form = MobileAdminRedeemRewardForm() variables = RequestContext(request, {'form':form}) return render_to_response('mobile/test.html', variables)
def authenticate(self, access_token, expires): fields = "first_name,last_name,email,gender,id" response = urllib2.urlopen('https://graph.facebook.com/me?fields='+fields+'&access_token='+access_token) profile = json.loads(response.read()) id = profile['id'] try: user = UserAccount.objects.get(facebook_uid=id) except UserAccount.DoesNotExist: user = UserAccount() user.facebook_uid = id user.first_name = profile['first_name'] user.last_name = profile['last_name'] user.gender = profile['gender'] user.email = profile['email'] user.username = profile['email'] user.type = 'user' user.set_unusable_password() user.save() joinUpdate = UserUpdate(action='join', user=user) joinUpdate.save() user.access_token = access_token user.token_expiration_date = datetime.datetime.now() + datetime.timedelta(seconds=int(expires)) user.save() return user
def confirm_facebook_post(request): if request.method == 'POST': response = dict() response['success'] = False access_token = request.POST['access_token'] if 'expires' in request.POST and request.POST['expires'] is not None and request.POST['expires'] != '': expires = int(request.POST['expires']) else: expires = 1000 retailer_id = int(request.POST['retailer_id']) try: user = authenticate(access_token=access_token, expires=expires) retailer = Retailer.objects.get(pk=retailer_id) pending_facebook_post = user.pending_facebook_posts.get(retailer=retailer) response = execute_punch(user=user, retailer=retailer) # Log the punch in the user's updates punch_update = UserUpdate(action='fb_punch', retailer=retailer, user=user) punch_update.save() retailer.num_facebook_posts += 1 retailer.save() # Delete the pending facebook post if the user does not have another pending facebook post if not response['facebook_post']: pending_facebook_post.delete() else: pending_facebook_post.save() except UserAccount.DoesNotExist: response['error'] = 'User does not exist' except Retailer.DoesNotExist: response['error'] = 'Retailer does not exist' except PendingFacebookPost.DoesNotExist: response['error'] = 'User cannot post to facebook at this time' return HttpResponse(simplejson.dumps(response), mimetype='application/json') else: form = MobileConfirmFBPostForm() variables = RequestContext(request, {'form':form}) return render_to_response('mobile/test.html', variables)
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 punch(request): if request.method == 'POST': response = dict() access_token = request.POST['access_token'] if 'expires' in request.POST and request.POST['expires'] is not None and request.POST['expires'] != '': expires = int(request.POST['expires']) else: expires = 1000 employee_username = request.POST['employee_username'] retailer_id = int(request.POST['retailer_id']) retailer_password = request.POST['retailer_password'] try: user = authenticate(access_token=access_token, expires=expires) retailer = Retailer.objects.get(pk=retailer_id) # Check that the retailer admin password is correct if retailer.admin_password != retailer_password: response['success'] = False response['error'] = 'Incorrect Password' return HttpResponse(simplejson.dumps(response), mimetype='application/json') response = execute_punch(user=user, retailer=retailer) # Log the punch in the user's updates punch_update = UserUpdate(action='punch', retailer=retailer, user=user) punch_update.save() except UserAccount.DoesNotExist: response['success'] = False response['error'] = "User does not exist" except Retailer.DoesNotExist: response['success'] = False response['error'] = 'Retailer does not exist' return HttpResponse(simplejson.dumps(response), mimetype='application/json') else: form = MobileCodeForm() variables = RequestContext(request, {'form':form}) return render_to_response('mobile/test.html', variables)
def execute_punch(user, retailer): response = dict() response['success'] = False # Record a punch for this user punch = Punch(retailer=retailer) punch.save() user.punches.add(punch) all_punches_at_retailer = user.punches.filter(retailer=retailer) num_punches = len(all_punches_at_retailer) rewards = retailer.rewards.all() # Check if a user has any updates related to this retailer # Option for facebook post first time user was punched response['facebook_post'] = (user.updates.filter(retailer=retailer).exists() is not True) response['reward'] = False # Check if a user needs to be given a reward based on number of punches for reward in rewards: if num_punches == reward.punches: earned_reward = EarnedReward() earned_reward.reward = reward earned_reward.save() user.earned_rewards.add(earned_reward) response['facebook_post'] = True reward_info = dict() reward_info['reward_text'] = reward.text reward_info['punches'] = reward.punches reward_info['retailer_id'] = reward.retailer.pk reward_info['active'] = reward.active reward_info['shareable'] = reward.shareable response['reward'] = reward_info break if response['reward']: reward_update = UserUpdate(action='earn', retailer=retailer, reward=earned_reward.reward, user=user) reward_update.save() # If the user has earned all the rewards, delete the punches if num_punches >= retailer.max_level: all_punches_at_retailer.delete() # Record that the user has a pending facebook post if applicable if response['facebook_post']: try: pending_facebook_post = user.pending_facebook_posts.get(retailer=retailer) except PendingFacebookPost.DoesNotExist: pending_facebook_post = PendingFacebookPost(retailer=retailer) pending_facebook_post.save() user.pending_facebook_posts.add(pending_facebook_post) if num_punches <= 2: if not user.subscribed_retailers.filter(pk=retailer.pk).exists(): user.subscribed_retailers.add(retailer) if not user.visited_retailers.filter(pk=retailer.pk).exists(): user.visited_retailers.add(retailer) response['success'] = True return response