def record_experiment_goal(request, goal_name): try: GoalRecord.record(goal_name, WebUser(request)) except Exception as e: l.warning("unknown goal type '%s': %s" % (goal_name, e)) return HttpResponse(TRANSPARENT_1X1_PNG, mimetype="image/png")
def obj_update(self, bundle, pk='', **kwargs): is_offline_booking = bool(bundle.data.pop("is_offline_booking", False)) require_keys(["state"], bundle.data) new_state = bundle.data["state"].lower() booking = Booking.objects.get(pk=pk) def error(error_str): raise ImmediateHttpResponse(response=HttpResponseBadRequest( content_type='application/json', content=json.dumps({"error": error_str}))) def assert_current_state_is(from_state): if type(from_state) is list: wrong_status = booking.state not in from_state else: wrong_status = booking.state != from_state if wrong_status: raise ImmediateHttpResponse(response=error( "Need to be in state %s to switch to state %s" % (from_state, booking.state))) if booking.owner != bundle.request.user: error("Can't modify booking : Wrong user") if new_state == 'pending': if booking.product.payment_type == PAYMENT_TYPE.PAYPAL: is_valid = booking.owner.is_valid is_confirmed = booking.owner.is_confirmed if not booking.owner.has_paypal(): return error("The owner doesn't have paypal account") elif not is_verified: return error("The owner's paypal account is invalid") elif not is_confirmed: return error("The owner's paypal email is not confirmed") assert_current_state_is("authorized") booking.send_acceptation_email() GoalRecord.record('rent_object_accepted', WebUser(bundle.request)) elif new_state == 'rejected': assert_current_state_is("authorized") booking.send_rejection_email() GoalRecord.record('rent_object_rejected', WebUser(bundle.request)) elif new_state == 'closed': assert_current_state_is(["closing", "ongoing"]) if not is_offline_booking: booking.init_payment_processor() booking.close() return super(BookingResource, self).obj_update(bundle, request=bundle.request, pk=pk, **kwargs)
def gr(request, goalname): "Record a GoalRecord" try: GoalRecord.record(goalname, WebUser(request)) except Exception as e: return dumps({'status': str(type(e)), 'error': str(e)}) else: return '{"status": "okay"}'
def testMissingGoalType(self): anonymous_visitor = AnonymousVisitor() anonymous_visitor.save() goal_type = GoalType(name="existing-goal") goal_type.save() nb_types = GoalType.objects.all().count() nb_records = GoalRecord.objects.all().count() GoalRecord.record('existing-goal', TestUser(anonymous_visitor=anonymous_visitor)) self.assertEquals(nb_records + 1, GoalRecord.objects.all().count()) self.assertEqual(nb_types, GoalType.objects.all().count()) with patch(settings, 'LEAN_AUTOCREATE_GOAL_TYPES', False): GoalRecord.record('inexistant-goal', TestUser(anonymous_visitor=anonymous_visitor)) self.assertEquals(nb_records + 1, GoalRecord.objects.all().count()) self.assertEqual(nb_types, GoalType.objects.all().count()) with patch(settings, 'LEAN_AUTOCREATE_GOAL_TYPES', NotImplemented): GoalRecord.record('inexistant-goal', TestUser(anonymous_visitor=anonymous_visitor)) self.assertEquals(nb_records + 1, GoalRecord.objects.all().count()) self.assertEqual(nb_types, GoalType.objects.all().count()) with patch(settings, 'LEAN_AUTOCREATE_GOAL_TYPES', True): GoalRecord.record('inexistant-goal', TestUser(anonymous_visitor=anonymous_visitor)) self.assertEquals(nb_records + 2, GoalRecord.objects.all().count()) self.assertEqual(nb_types + 1, GoalType.objects.all().count())
def test_record(self): KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(AnonymousUser()) as experiment_user: KM.identify(analytics._id_from_session(experiment_user.session)) KM.record(action="Goal Recorded", props={"Goal Type": "Goal Type"}) self.mox.ReplayAll() goal_type = GoalType.objects.create(name="Goal Type") goal_record = GoalRecord.record(goal_name=goal_type.name, experiment_user=experiment_user) analytics.record(goal_record=goal_record, experiment_user=experiment_user) self.mox.VerifyAll()
def test_record(self): KM = self.mox.CreateMockAnything() analytics = KissMetrics(KM=KM) with self.web_user(AnonymousUser()) as experiment_user: KM.identify(analytics._id_from_session(experiment_user.session)) KM.record(action='Goal Recorded', props={'Goal Type': 'Goal Type'}) self.mox.ReplayAll() goal_type = GoalType.objects.create(name='Goal Type') goal_record = GoalRecord.record(goal_name=goal_type.name, experiment_user=experiment_user) analytics.record(goal_record=goal_record, experiment_user=experiment_user) self.mox.VerifyAll()
def test_record(self): import time tracker = self.mox.CreateMockAnything() analytics = Mixpanel(tracker=tracker) now = time.gmtime() self.mox.StubOutWithMock(time, "gmtime") time.gmtime().AndReturn(now) with self.web_user(AnonymousUser()) as experiment_user: properties = { "time": "%d" % time.mktime(now), "distinct_id": ("Session %s" % experiment_user.session.session_key), "Goal Type": "Goal Type", } tracker.run(event_name="Goal Recorded", properties=properties) self.mox.ReplayAll() goal_type = GoalType.objects.create(name="Goal Type") goal_record = GoalRecord.record(goal_name=goal_type.name, experiment_user=experiment_user) analytics.record(goal_record=goal_record, experiment_user=experiment_user) self.mox.VerifyAll()
def test_record(self): import time tracker = self.mox.CreateMockAnything() analytics = Mixpanel(tracker=tracker) now = time.gmtime() self.mox.StubOutWithMock(time, 'gmtime') time.gmtime().AndReturn(now) with self.web_user(AnonymousUser()) as experiment_user: properties = { 'time': '%d' % time.mktime(now), 'distinct_id': ('Session %s' % experiment_user.session.session_key), 'Goal Type': 'Goal Type' } tracker.run(event_name='Goal Recorded', properties=properties) self.mox.ReplayAll() goal_type = GoalType.objects.create(name='Goal Type') goal_record = GoalRecord.record(goal_name=goal_type.name, experiment_user=experiment_user) analytics.record(goal_record=goal_record, experiment_user=experiment_user) self.mox.VerifyAll()
def record_experiment_goal(request, goal_name): try: GoalRecord.record(goal_name, WebUser(request)) except Exception, e: l.warn("unknown goal type '%s': %s" % (goal_name, e))
def testGoals(self): anonymous_visitor = AnonymousVisitor() anonymous_visitor.save() # required fields for GoalType self.assertRaises(Exception, lambda: GoalType(name=None).save()) goal_type = GoalType(name="test-goal") goal_type.save() # unique constraint on GoalType.name self.assertRaises(Exception, lambda: GoalType(name="test-goal").save()) # required fields for GoalRecord self.assertRaises(Exception, lambda: GoalRecord().save()) self.assertRaises(Exception, lambda: GoalRecord(anonymous_visitor=anonymous_visitor).save()) self.assertRaises(Exception, lambda: GoalRecord(goal_type=goal_type).save()) now = datetime.now() goal_record = GoalRecord(anonymous_visitor=anonymous_visitor, goal_type=goal_type) goal_record.save() self.assertTrue(goal_record.created >= now and goal_record.created <= datetime.now()) # it's OK for the same user to record the same goal multiple times goal_record2 = GoalRecord(anonymous_visitor=anonymous_visitor, goal_type=goal_type) goal_record2.save() nb_records = GoalRecord.objects.all().count() GoalRecord.record('test-goal', TestUser()) self.assertEquals(nb_records, GoalRecord.objects.all().count()) GoalRecord.record('test-goal', TestUser(username='******')) self.assertEquals(nb_records, GoalRecord.objects.all().count()) GoalRecord.record('test-goal', TestUser(anonymous_visitor=anonymous_visitor)) self.assertEquals(nb_records + 1, GoalRecord.objects.all().count()) GoalRecord.record('test-goal', TestUser(anonymous_visitor=anonymous_visitor)) self.assertEquals(nb_records + 2, GoalRecord.objects.all().count())
def testGoals(self): anonymous_visitor = AnonymousVisitor() anonymous_visitor.save() # required fields for GoalType self.assertRaises(Exception, lambda: GoalType(name=None).save()) goal_type = GoalType(name="test-goal") goal_type.save() # unique constraint on GoalType.name self.assertRaises(Exception, lambda: GoalType(name="test-goal").save()) # required fields for GoalRecord self.assertRaises(Exception, lambda: GoalRecord().save()) self.assertRaises( Exception, lambda: GoalRecord(anonymous_visitor=anonymous_visitor).save()) self.assertRaises(Exception, lambda: GoalRecord(goal_type=goal_type).save()) now = datetime.now() goal_record = GoalRecord(anonymous_visitor=anonymous_visitor, goal_type=goal_type) goal_record.save() self.assertTrue(goal_record.created >= now and goal_record.created <= datetime.now()) # it's OK for the same user to record the same goal multiple times goal_record2 = GoalRecord(anonymous_visitor=anonymous_visitor, goal_type=goal_type) goal_record2.save() nb_records = GoalRecord.objects.all().count() GoalRecord.record('test-goal', TestUser()) self.assertEquals(nb_records, GoalRecord.objects.all().count()) GoalRecord.record('test-goal', TestUser(username='******')) self.assertEquals(nb_records, GoalRecord.objects.all().count()) GoalRecord.record('test-goal', TestUser(anonymous_visitor=anonymous_visitor)) self.assertEquals(nb_records + 1, GoalRecord.objects.all().count()) GoalRecord.record('test-goal', TestUser(anonymous_visitor=anonymous_visitor)) self.assertEquals(nb_records + 2, GoalRecord.objects.all().count())