def obj_create(self, bundle, request=None, **kwargs): email = bundle.data['email'] device_id = bundle.data['device_id'] app_id = bundle.data['app_id'] app_version = bundle.data['app_version'] platform = bundle.data['platform'] visitor=None if device_id: visitors = Visitor.objects.filter(device_id=device_id) if visitors: visitor=visitors[0] visitor.app_id=app_id visitor.app_version=app_version elif email: visitors = Visitor.objects.filter(email= email) if visitors: visitor=visitors[0] visitor.app_id=app_id visitor.app_version=app_version if not visitor: visitor=Visitor(email=email,device_id=device_id,app_version=app_version,app_id=app_id,platform=platform) visitor.save() bundle.obj=visitor return bundle
def test_store_payload(self): account = Account(name="Foo 4") visitor = Visitor() account.visitors.append(visitor) db.session.add(account) db.session.commit() args = dict() args['user_agent'] = "Foo Bar" # Full UserAgent string args['platform_name'] = "linux" # Platform name from UA args['browser_name'] = "chrome" # Browser name from UA args['browser_version'] = "1.2.3" # Browser version from UA args['request_address'] = "0.0.0.0" # Request IP address args['visitor_id'] = "12345" # The APE cookie visitor_id args['debug'] = False # Debug switch args['page_url'] = "http://example.com/foo/bar" # Page URL args['referrer_url'] = "http://example.com/foo/baz" # Referrer URL if set args['page_title'] = "Foo Bar Baz" # Page title args['event'] = "" # Event args['account_id'] = "67890" # The customer account ID args['timestamp'] = epoch() # Epoch timestamp args['language'] = "en-GB" # Browser language args['placeholders'] = [] # The set of Placeholder ids on this page args['prefix'] = "ape" # Placeholder class prefix args['screen_colour'] = 256 # Screen colour depth args['screen_height'] = 1000 # Screen height args['screen_width'] = 1000 # Screen width args['script_version'] = "0.0.0" # Version number of this script visitor_data = visitor.store_payload(args) self.assertIsInstance(visitor_data, dict)
def test_visitor_get_or_create(self): account_1 = Account(name="Foo 1") account_2 = Account(name="Foo 2") visitor = Visitor() account_2.visitors.append(visitor) db.session.add(account_1) db.session.add(account_2) db.session.commit() # Return new Visitor if visitor uuid empty visitor_a = Visitor.get_or_create(account_1, "") self.assertIsInstance(visitor_a, Visitor) self.assertEqual(visitor_a.account, account_1) self.assertIn(visitor_a, account_1.visitors) # Return new Visitor with specified uuid if unknown visitor_b = Visitor.get_or_create(account_1, "new-visitor-uuid") self.assertIsInstance(visitor_b, Visitor) self.assertEqual(visitor_b.account, account_1) self.assertEqual(visitor_b.uuid, "new-visitor-uuid") self.assertIn(visitor_b, account_1.visitors) # Return new Visitor if known but unknown to Account visitor_c = Visitor.get_or_create(account_1, visitor.uuid) self.assertIsInstance(visitor_c, Visitor) self.assertEqual(visitor_c.account, account_1) self.assertEqual(visitor_c.uuid, visitor.uuid) self.assertIn(visitor_c, account_1.visitors) # Return Visitor if known to Account visitor_d = Visitor.get_or_create(account_2, visitor.uuid) self.assertIsInstance(visitor_d, Visitor) self.assertEqual(visitor_d.account, account_2) self.assertEqual(visitor_d, visitor) self.assertIn(visitor_d, account_2.visitors)
def test_visitor_guid(self): account = Account(name="Foo 3") visitor = Visitor() account.visitors.append(visitor) db.session.add(account) db.session.commit() required_guid = "%s-%s" % (account.uuid, visitor.uuid) self.assertEqual(visitor.guid(), required_guid)
def new_visitor(): email = request.json.get('email') password = request.json.get('password') if not (email and password): abort(400) # missing arguments if Visitor.query.filter_by(email=email).first() is not None: abort(400) # existing visitor visitor = Visitor(email=email) visitor.hash_password(password) db.session.add(visitor) db.session.commit() return jsonify({ 'email': visitor.email }), 201, {'Location': url_for('get_visitor', visitor_id = visitor.id, _external = True)}
def new_visitor(): email = request.json.get('email') password = request.json.get('password') if not (email and password): abort(400) # missing arguments if Visitor.query.filter_by(email=email).first() is not None: abort(400) # existing visitor visitor = Visitor(email=email) visitor.hash_password(password) db.session.add(visitor) db.session.commit() return jsonify({'email': visitor.email}), 201, { 'Location': url_for('get_visitor', visitor_id=visitor.id, _external=True) }
def record_vistors(): running_info = RunningInfo.query.first() now = datetime.utcnow() if running_info == None: running_info = RunningInfo(vistors=0) ipaddr = request.remote_addr visitor = Visitor.query.filter_by(visitor_ip=ipaddr).first() if (visitor == None): visitor = Visitor(visitor_ip=ipaddr, visit_counts=0) running_info.vistors = running_info.vistors + 1 else: if (now - visitor.last_visit_time).days > 0: running_info.vistors = running_info.vistors + 1 visitor.visit_counts = visitor.visit_counts + 1 visitor.last_visit_time = now db.session.add(running_info) db.session.add(visitor)
def verify_password(email_or_token, password): # first try to authenticate by token visitor = Visitor.verify_auth_token(email_or_token) if not visitor: # try to authenticate with username/password visitor = Visitor.query.filter_by(email=email_or_token).first() if not visitor or not visitor.verify_password(password): return False g.visitor = visitor return True
def test_visitor_crud(self): # Create visitor = Visitor() db.session.add(visitor) db.session.commit() self.assertIn(visitor, Visitor.query.all()) self.assertIsInstance(visitor.uuid, unicode) self.assertIsInstance(visitor.created_at, DT.datetime) # Read uuid = visitor.uuid visitor = Visitor.query.filter_by(uuid=uuid).first() self.assertEqual(uuid, visitor.uuid) # Update visitor.uuid = 'FooBar' visitor = Visitor.query.filter_by(uuid='FooBar').first() self.assertIsInstance(visitor, Visitor) self.assertEqual('FooBar', visitor.uuid) # Delete db.session.delete(visitor) count = Visitor.query.filter_by(uuid='FooBar').count() self.assertEqual(0, count)
def entrance(): name = request.form.get('name') email = request.form.get('email') person_visited = request.form.get('person_visited') try: visitor = Visitor(name=name, email=email, person_visited=person_visited) db.session.add(visitor) flash('Value successfully recorded') except Exception as e: print(e) flash( 'Value was not recorded, there is something wrong with the entered values' ) return redirect(url_for('.index'))