def sites_view(): form = RegisterSiteForm(request.form) sites = current_user.sites.all() if form.validate_on_submit(): site = Site() form.populate_obj(site) site.user_id = current_user.id db.session.add(site) db.session.commit() return redirect('/sites/') return render_template('tracking/index.html', form=form, sites=sites)
def test_visitors_location_is_derived_from_ip(self): user = User.create(name="Joe", email="*****@*****.**", password="******") site = Site.create(user_id=user.id) mock_geodata = Mock(name="get_geodata") mock_geodata.return_value = { 'city': 'Los Angeles', 'zipcode': '90001', 'latitude': '34.05', 'longitude': '-118.25' } url = url_for("tracking.register_visit", site_id=site.id) wsgi_environment = {"REMOTE_ADDR": "1.2.3.4"} with patch.object(app.tracking.views, "get_geodata", mock_geodata): with self.client: self.client.get(url, environ_overrides=wsgi_environment) visits = Visit.query.all() mock_geodata.assert_called_once_with("1.2.3.4") self.assertEquals(1, len(visits)) self.assertEquals("Los Angeles, 90001", visits[0].location) self.assertEquals("Los Angeles, 90001, 34.05, -118.25", visits[0].location_full)
def register_visit(site_id): site = Site.get_by_id(site_id) if site: browser = request.headers.get('User-Agent') date = datetime.now() event = request.args.get('event') url = request.url ip_address = request.remote_addr geo = get_geodata(ip_address) location_full = ", ".join([geo['city'],geo['zipcode'],geo['latitude'],geo['longitude']]) location = ", ".join([geo['city'],geo['zipcode']]) visit = Visit(browser, date, event, url, ip_address, location_full, location) visit.site_id = site_id db.session.add(visit) db.session.commit() return Response("visit recorded", content_type="text/plain")
def register_visit(site_id): site = Site.get_by_id(site_id) if site: browser = request.headers.get('User-Agent') date = datetime.now() event = request.args.get('event') url = request.url ip_address = request.remote_addr geo = get_geodata(ip_address) location_full = ", ".join( [geo['city'], geo['zipcode'], geo['latitude'], geo['longitude']]) location = ", ".join([geo['city'], geo['zipcode']]) visit = Visit(browser, date, event, url, ip_address, location_full, location) visit.site_id = site_id db.session.add(visit) db.session.commit() return Response("visit recorded", content_type="text/plain")