Exemple #1
0
    def about(self, dataset, format='html'):
        self._get_dataset(dataset)
        etag_cache_keygen(c.dataset.updated_at)
        handle_request(request, c, c.dataset)
        c.sources = list(c.dataset.sources)
        c.managers = list(c.dataset.managers)

        # Get all badges if user is admin because they can then
        # give badges to the dataset on its about page.
        if c.account and c.account.admin:
            c.badges = list(Badge.all())

        return templating.render('dataset/about.html')
Exemple #2
0
    def index(self, format='html'):
        """
        List all badges in the system. Default is to present the
        user with an html site, but the user can request a json list
        of badges.
        """
        c.badges = Badge.all()

        # If the requested format is json return a list of badges
        if format == 'json':
            return to_jsonp({"badges":badges_apply_links([b.as_dict() 
                                                          for b in c.badges])})

        # Return html representation
        return templating.render('badge/index.html')
Exemple #3
0
    def information(self, id, format='html'):
        """
        Show information about the badge. Default is to present the
        user with the badge on an html site, but the user can request a
        json representation of the badge
        """

        # Get the badge
        c.badge = Badge.by_id(id=id)

        # Return a json representation if the format requested is 'json'
        if format == 'json':
            return to_jsonp({"badge":badge_apply_links(c.badge.as_dict())})
        
        # Return html representation
        return templating.render('badge/information.html')
Exemple #4
0
    def create(self):
        """
        Create a new badge in the system
        """
        # Check if user is allowed to create a badge
        require.badge.create()

        import shutil

        label = request.params['badge-label']
        description = request.params['badge-description']
        image = request.POST['badge-image']

        try:
            # Get upload directory for Badge and generate a random filename
            upload_dir = h.get_object_upload_dir(Badge)
            random_filename = h.get_uuid_filename(image.filename)
            
            # Open the filename and copy the uploaded image
            permanent_filename = os.path.join(upload_dir, random_filename)
            permanent_image = open(permanent_filename, 'w')
            shutil.copyfileobj(image.file, permanent_image)

            upload_image_path = h.upload(random_filename, Badge)
            # Close image files
            image.file.close()
            permanent_image.close()
        except OSError:
            upload_image_path = ''
            h.flash_error(_('Uploading files not supported at the moment.'))

        badge = Badge(label, upload_image_path, description, c.account)
        db.session.add(badge)
        db.session.commit()

        redirect(h.url_for(controller='badge', action='information', 
                           id=badge.id))
Exemple #5
0
    def give(self, dataset):
        """
        Award a given badge to a given dataset.
        """
        # Get the dataset
        self._get_dataset(dataset)

        # Get the badge
        badge_id = request.params.get('badge', None)
        badge = Badge.by_id(id=badge_id)

        if badge:
            # See if user can award this badge to a this dataset
            require.badge.give(badge, c.dataset)
            # Add the dataset to the badge datasets and commit to database
            badge.datasets.append(c.dataset)
            db.session.commit()
        else:
            # If we don't find the badge id we flash an error message
            h.flash_error(_('Badge not found.'))

        # Go to the dataset's main page
        redirect(h.url_for(controller='dataset', action='view',
                           dataset=c.dataset.name))
Exemple #6
0
    def test_give_badge(self):
        """
        Test giving dataset a badge. Only administrators should be able to
        give datasets a badge.
        """

        badge = Badge('give-me', 'testimage', 'give me', self.admin)
        db.session.add(badge)
        db.session.commit()

        # Check if non-user can award badges
        response = self.app.post(url(controller='badge',
                                     action='give',
                                     dataset='cra'),
                                 params={'badge': badge.id},
                                 expect_errors=True)
        # Check if it returned Forbidden (which is http status code 403)
        # This should actually return 401 Unauthorized but that's an
        # authentication implementation failure (which should be fixed)
        assert '403' in response.status, \
            "Non-user should get an error when trying to give a badge"

        # Check to see that badge hasn't been awarded to any datasets
        badge_json = self.app.get(
            url(controller='badge',
                action='information',
                id=badge.id,
                format='json'))
        badge_info = json.loads(badge_json.body)
        assert len(badge_info['badge']['datasets']) == 0, \
            "A non-user was able to award a badge"

        # Check if normal user can award badges
        response = self.app.post(url(controller='badge',
                                     action='give',
                                     dataset='cra'),
                                 params={'badge': badge.id},
                                 extra_environ={'REMOTE_USER': '******'},
                                 expect_errors=True)
        # Check if it returned Forbidden (which is http status code 403)
        assert '403' in response.status, \
            "A normal user should get an error when trying to give a badge"

        # Check to see that badge hasn't been awarded to any datasets
        badge_json = self.app.get(
            url(controller='badge',
                action='information',
                id=badge.id,
                format='json'))
        badge_info = json.loads(badge_json.body)
        assert len(badge_info['badge']['datasets']) == 0, \
            "A normal user was able to award a badge"

        # Finally we check if admin user can award badges
        response = self.app.post(url(controller='badge',
                                     action='give',
                                     dataset='cra'),
                                 params={'badge': 'not an id'},
                                 extra_environ={'REMOTE_USER': '******'},
                                 expect_errors=True)

        # Check to see that badge hasn't been awarded to the dataset
        badge_json = self.app.get(
            url(controller='badge',
                action='information',
                id=badge.id,
                format='json'))
        badge_info = json.loads(badge_json.body)
        # Check if admin was able to give the badge to a dataset
        assert len(badge_info['badge']['datasets']) == 0, \
            "Admin user was able to award a badge"

        # Finally we check if admin user can award badges
        response = self.app.post(url(controller='badge',
                                     action='give',
                                     dataset='cra'),
                                 params={'badge': badge.id},
                                 extra_environ={'REMOTE_USER': '******'})

        # Check to see that badge has been awarded to the dataset
        badge_json = self.app.get(
            url(controller='badge',
                action='information',
                id=badge.id,
                format='json'))
        badge_info = json.loads(badge_json.body)
        # Check if admin was able to give the badge to a dataset
        assert len(badge_info['badge']['datasets']) == 1, \
            "Admin user wasn't able to award a badge"
        # Check if admin gave it to the write dataset
        assert self.dataset.name in badge_info['badge']['datasets'], \
            "Admin user gave the badge to the incorrect dataset"