Exemple #1
0
    def create(self):
        """POST /mapfiles: Create a new item."""
        # get json content from POST request
        content = request.environ['wsgi.input'].read(int(request.environ['CONTENT_LENGTH']))
        #content = content.decode('utf8')   mapfile interface don't like unicode strings... bad...

        # load mapfile
        mapfile = Mapfile()
        dict = simplejson.loads(content)
        mapfile.from_dict(dict)

        # create mapfile
        mapname = mapfile.get_name()
        map_pathname = h.gen_mapname()
        mapfile.to_file(os.path.join(config['mapfiles_dir'], map_pathname))

        # create map in db
        map = self._new_map_from_user(c.user, mapname, map_pathname)

        response.status = 201

        href = h.url_for(controller="mapfiles", action="show", id=map.id)
        wmsproxyurl = h.url_for(controller='mapfiles', action='wms_proxy', id=map.id)
        wmsurl = "%s?%s" %(config['mapserver_url'], urlencode({'map':os.path.join(config['mapfiles_dir'], map.filepath)}))
        return {'name': map.name, 'id': map.id, 'href': href, 'wmsurl': wmsurl, 'wmsproxyurl': wmsproxyurl}
Exemple #2
0
    def register(self):
        # ensure a login and password are not null
        if ("login" not in request.params or request.params["login"] == "") or (
            "password" not in request.params or request.params["password"] == ""
        ):
            session["flash"] = _("Login or password should not be empty. Please register again.")
            session.save()
            redirect_to(controller="main", action="signin")

        # ensure the login doesn't exceed 30 chars
        if len(request.params["login"]) > 30:
            session["flash"] = _("Login should not exceed 30 characters. Please register again.")
            session.save()
            redirect_to(controller="main", action="signin")

        # ensure a user with the same login does not exist
        user = meta.Session.query(User).filter(User.login == request.params["login"]).all()
        if len(user) != 0:
            session["flash"] = _('The login "%s" is already in use. Please register again.') % request.params["login"]
            session.save()
            redirect_to(controller="main", action="signin")

        # create a new user in default group
        gp = meta.Session.query(Group).filter(Group.name == config["default_group"]).one()
        new_user = User()
        new_user.name = request.params["login"]
        new_user.login = request.params["login"]
        new_user.password = request.params["password"]
        new_user.groups.append(gp)
        meta.Session.add(new_user)

        # create a sample mapfile for this new user
        mapfile_name = "Sample mapfile"
        mapfile = create_default_mapfile()
        # special mapfile for demo site
        # mapfile = Mapfile()
        # mapfile.from_file(os.path.join(config['mapserver_dir'],'demo_mapfile.map'))
        dict = mapfile.to_dict()
        if "projection" not in dict:
            dict["projection"] = "init=epsg:4326"
        mapfile.from_dict(dict)
        mapfile.set_name(mapfile_name)
        map_pathname = h.gen_mapname()
        mapfile.to_file(os.path.join(config["mapfiles_dir"], map_pathname))
        # create the map in db
        map = Map(mapfile_name, map_pathname)
        map.user = new_user
        meta.Session.add(map)

        meta.Session.commit()

        session["flash"] = _("You have just registered. Please log in to use Studio.")
        session.save()

        redirect_to(controller="main", action="signin")
Exemple #3
0
    def test_get_symbols(self):
        # Create new mapfile for this test
        filename = h.gen_mapname()
        map = create_mapfile('Dummy mapfile name', filename)
        # login as admin to be allowed to access /mapfiles/1
        log_in(self.app, 'enduser', 'password')

        response = self.app.get(url(controller='mapfiles', action='get_symbols', id=map.id))
        assert response.response.content_type == 'application/json'
        assert '"symbols": [' in response

        # test redirection to /signin when log out
        log_out(self.app)
        response = self.app.get(url(controller='mapfiles', action='get_symbols', id=map.id),
                status=302)
        urlparsed = urlparse(response.location)
        assert urlparsed.path == '/signin' # we are redirected to "signin"

        # Clean the test mapfile
        delete_mapfile(map)
Exemple #4
0
    def test_show(self):
        """ GET /mapfiles/id: Get a specific mapfile owned by the current user. """
        # Create new mapfile for this test
        filename = h.gen_mapname()
        map = create_mapfile('Dummy mapfile name', filename)
        # login as admin to be allowed to access /mapfiles/1
        log_in(self.app, 'enduser', 'password')

        response = self.app.get(url('MapFiles', id=map.id))
        assert response.response.content_type == 'application/json'
        assert '"map": {' in response
        assert 'Dummy mapfile name' in response

        # test redirection to /signin when log out
        log_out(self.app)
        response = self.app.get(url('MapFiles', id=map.id), status=302)
        urlparsed = urlparse(response.location)
        assert urlparsed.path == '/signin' # we are redirected to "signin"

        # Clean the test mapfile
        delete_mapfile(map)
Exemple #5
0
    def test_download(self):
        """ GET /mapfiles/id/download: Download the mapfile as an attachment. """
        # Create new mapfile for this test
        filename = h.gen_mapname()
        map = create_mapfile('Dummy mapfile name', filename)
        # login as admin to be allowed to access /mapfiles/1
        log_in(self.app, 'enduser', 'password')

        response = self.app.get(url(controller='mapfiles', action='download_mapfile', id=map.id))
        assert response.content_type == 'text/plain'
        assert response.body.startswith('MAP')
        assert response.headers['content-disposition'] == 'attachment; filename=%s' \
                %os.path.join(config['mapfiles_dir'], filename)

        # test redirection to /signin when log out
        log_out(self.app)
        response = self.app.get(url(controller='mapfiles', action='download_mapfile', id=map.id),
                status=302)
        urlparsed = urlparse(response.location)
        assert urlparsed.path == '/signin' # we are redirected to "signin"

        # Clean the test mapfile
        delete_mapfile(map)
Exemple #6
0
    def create(self):
        """POST /mapfiles: Create a new item."""
        # get json content from POST request
        content = request.environ['wsgi.input'].read(
            int(request.environ['CONTENT_LENGTH']))
        #content = content.decode('utf8')   mapfile interface don't like unicode strings... bad...

        # load mapfile
        mapfile = Mapfile()
        dict = simplejson.loads(content)
        mapfile.from_dict(dict)

        # create mapfile
        mapname = mapfile.get_name()
        map_pathname = h.gen_mapname()
        mapfile.to_file(os.path.join(config['mapfiles_dir'], map_pathname))

        # create map in db
        map = self._new_map_from_user(c.user, mapname, map_pathname)

        response.status = 201

        href = h.url_for(controller="mapfiles", action="show", id=map.id)
        wmsproxyurl = h.url_for(controller='mapfiles',
                                action='wms_proxy',
                                id=map.id)
        wmsurl = "%s?%s" % (
            config['mapserver_url'],
            urlencode(
                {'map': os.path.join(config['mapfiles_dir'], map.filepath)}))
        return {
            'name': map.name,
            'id': map.id,
            'href': href,
            'wmsurl': wmsurl,
            'wmsproxyurl': wmsproxyurl
        }
Exemple #7
0
    def test_delete(self):
        """DELETE /mapfiles/id: Delete an existing mapfile owned by the current
        user. Deletion of the map entry in db and remove mapfile from filesystem. """
        # Create new mapfile for this test
        filename = h.gen_mapname()
        map = create_mapfile('Dummy mapfile name', filename)
        # login as admin to be allowed to access /mapfiles/1
        log_in(self.app, 'enduser', 'password')

        # delete request that must be tested
        response = self.app.delete(url('MapFiles', id=map.id))

        assert not(os.path.exists(os.path.join(config['mapfiles_dir'], map.filepath)))
        deleted_map = model.meta.Session.query(model.Map).get(map.id)
        assert deleted_map is None

        # assert mapfile doesn't still exist
        response = self.app.delete(url('MapFiles', id=2), status=404)

        # test redirection to /signin when log out
        log_out(self.app)
        response = self.app.delete(url('MapFiles', id=map.id), status=302)
        urlparsed = urlparse(response.location)
        assert urlparsed.path == '/signin' # we are redirected to "signin"
Exemple #8
0
    def test_update(self):
        """PUT /mapfiles/id: Update an existing item."""
        # Create new mapfile for this test
        filename = h.gen_mapname()
        map = create_mapfile('Dummy mapfile name', filename)
        # login as admin to be allowed to access /mapfiles/1
        log_in(self.app, 'enduser', 'password')

        mapfile = Mapfile()
        mapfile.from_file(os.path.join(config['mapserver_dir'], 'dummy_mapfile.map'))
        mapfile.set_name('New dummy name')
        mapfile = change_mapfile_paths(mapfile)
        dict = mapfile.to_dict()
        content = simplejson.dumps(dict)

        # PUT update request that must be tested
        map_id = map.id
        response = self.app.put(url('MapFiles', id=map_id), params=content,
                content_type='application/json')
        updated_map = model.meta.Session.query(model.Map).get(map_id)
        assert updated_map.name == 'New dummy name'
        map_path = os.path.join(config['mapfiles_dir'],updated_map.filepath)
        assert os.path.exists(map_path)
        mapobj = Mapfile()
        mapobj.from_file(map_path)
        assert mapobj.get_name() == 'New dummy name'

        # test redirection to /signin when log out
        log_out(self.app)
        response = self.app.get(url('MapFiles', id=map_id), params=content,
                status=302)
        urlparsed = urlparse(response.location)
        assert urlparsed.path == '/signin' # we are redirected to "signin"

        # Clean the test mapfile
        delete_mapfile(updated_map)
Exemple #9
0
    def register(self):
        # ensure a login and password are not null
        if ('login' not in request.params or request.params['login'] == '') or \
           ('password' not in request.params or request.params['password'] == ''):
            session['flash'] = _(
                'Login or password should not be empty. Please register again.'
            )
            session.save()
            redirect_to(controller='main', action='signin')

        # ensure the login doesn't exceed 30 chars
        if len(request.params['login']) > 30:
            session['flash'] = _(
                'Login should not exceed 30 characters. Please register again.'
            )
            session.save()
            redirect_to(controller='main', action='signin')

        # ensure a user with the same login does not exist
        user = meta.Session.query(User).filter(
            User.login == request.params['login']).all()
        if len(user) != 0:
            session['flash'] = _('The login "%s" is already in use. Please register again.') \
                    %request.params['login']
            session.save()
            redirect_to(controller='main', action='signin')

        # create a new user in default group
        gp = meta.Session.query(Group).filter(
            Group.name == config["default_group"]).one()
        new_user = User()
        new_user.name = request.params['login']
        new_user.login = request.params['login']
        new_user.password = request.params['password']
        new_user.groups.append(gp)
        meta.Session.add(new_user)

        # create a sample mapfile for this new user
        mapfile_name = "Sample mapfile"
        mapfile = create_default_mapfile()
        # special mapfile for demo site
        #mapfile = Mapfile()
        #mapfile.from_file(os.path.join(config['mapserver_dir'],'demo_mapfile.map'))
        dict = mapfile.to_dict()
        if 'projection' not in dict:
            dict['projection'] = "init=epsg:4326"
        mapfile.from_dict(dict)
        mapfile.set_name(mapfile_name)
        map_pathname = h.gen_mapname()
        mapfile.to_file(os.path.join(config['mapfiles_dir'], map_pathname))
        # create the map in db
        map = Map(mapfile_name, map_pathname)
        map.user = new_user
        meta.Session.add(map)

        meta.Session.commit()

        session['flash'] = _(
            'You have just registered. Please log in to use Studio.')
        session.save()

        redirect_to(controller='main', action='signin')