Exemplo n.º 1
0
 def sitemap(self, dataset):
     self._get_dataset(dataset)
     pages = []
     for action in ['view', 'about']:
         pages.append({
             'loc':
             h.url_for(controller='dataset',
                       action=action,
                       dataset=c.dataset.name,
                       qualified=True),
             'lastmod':
             c.dataset.updated_at,
             'priority':
             0.8
         })
     for view in View.all_by_dataset(c.dataset):
         pages.append({
             'loc':
             h.url_for(controller='view',
                       action='view',
                       dataset=dataset,
                       name=view.name,
                       qualified=True),
             'lastmod':
             view.updated_at
         })
     return sitemap(pages)
Exemplo n.º 2
0
 def member(self, dataset, dimension, name, format="html"):
     self._get_member(dataset, dimension, name)
     handle_request(request, c, c.member, c.dimension.name)
     member = [member_apply_links(dataset, dimension, c.member)]
     if format == "json":
         return write_json(member, response)
     elif format == "csv":
         return write_csv(member, response)
     else:
         # If there are no views set up, then go direct to the entries
         # search page
         if c.view is None:
             return redirect(
                 url_for(
                     controller="dimension", action="entries", dataset=c.dataset.name, dimension=dimension, name=name
                 )
             )
         if "embed" in request.params:
             return redirect(
                 url_for(
                     controller="view",
                     action="embed",
                     dataset=c.dataset.name,
                     widget=c.view.vis_widget.get("name"),
                     state=json.dumps(c.view.vis_state),
                 )
             )
         return templating.render("dimension/member.html")
Exemplo n.º 3
0
 def sitemap_index(self):
     sitemaps = [{'loc': h.url_for(controller='home', action='sitemap', qualified=True),
                  'lastmod': datetime.utcnow()}]
     for dataset in c.datasets:
         sitemaps.append({
             'loc': h.url_for(controller='dataset', action='sitemap',
                            dataset=dataset.name, qualified=True),
             'lastmod': dataset.updated_at
             })
         for ep in range(1, (len(dataset)/30000)+2):
             sitemaps.append({
                 'loc': h.url_for(controller='entry', action='sitemap',
                                dataset=dataset.name, page=ep,
                                qualified=True),
                 'lastmod': dataset.updated_at
                 })
         for dim in dataset.compounds:
             if dim.name == 'time':
                 continue
             sitemaps.append({
                 'loc': h.url_for(controller='dimension', action='sitemap',
                                dataset=dataset.name, dimension=dim.name,
                                qualified=True),
                 'lastmod': dataset.updated_at
                 })
     return sitemapindex(sitemaps)
Exemplo n.º 4
0
 def member(self, dataset, dimension, name, format="html"):
     self._get_member(dataset, dimension, name)
     handle_request(request, c, c.member, c.dimension.name)
     member = [member_apply_links(dataset, dimension, c.member)]
     if format == 'json':
         return write_json(member, response)
     elif format == 'csv':
         return write_csv(member, response)
     else:
         # If there are no views set up, then go direct to the entries
         # search page
         if c.view is None:
             return redirect(
                 url_for(controller='dimension',
                         action='entries',
                         dataset=c.dataset.name,
                         dimension=dimension,
                         name=name))
         if 'embed' in request.params:
             return redirect(
                 url_for(controller='view',
                         action='embed',
                         dataset=c.dataset.name,
                         widget=c.view.vis_widget.get('name'),
                         state=json.dumps(c.view.vis_state)))
         return templating.render('dimension/member.html')
Exemplo n.º 5
0
 def after_login(self):
     self._disable_cache()
     if c.account is not None:
         h.flash_success(_("Welcome back, %s!") % c.account.name)
         redirect(h.url_for(controller='account', action='dashboard'))
     else:
         h.flash_error(_("Incorrect user name or password!"))
         redirect(h.url_for(controller='account', action='login'))
Exemplo n.º 6
0
 def after_login(self):
     self._disable_cache()
     if c.account is not None:
         h.flash_success(_("Welcome back, %s!") % c.account.name)
         redirect(h.url_for(controller='account', action='dashboard'))
     else:
         h.flash_error(_("Incorrect user name or password!"))
         redirect(h.url_for(controller='account', action='login'))
Exemplo n.º 7
0
 def publish(self, dataset):
     self._get_dataset(dataset)
     require.dataset.update(c.dataset)
     if not c.dataset.private:
         abort(400, _("This dataset is already public!"))
     c.dataset.private = False
     db.session.commit()
     public_url = h.url_for(controller='dataset', action='view', 
                        dataset=c.dataset.name, qualified=True)
     h.flash_success(_("Congratulations, the dataset has been " \
             "published. It is now available at: %s") % public_url)
     redirect(h.url_for(controller='editor', action='index', 
                        dataset=c.dataset.name))
Exemplo n.º 8
0
def badge_apply_links(badge):
    """
    Add links or to badge dictionary representation or modify a dictionary
    representation to include a fully qualified domain
    """
    # Add an html_url to represent the html representation of the badge
    badge['html_url'] = url_for(controller='badge', action='information',
                                id=badge['id'], qualified=True)
    # Change the image url to be a fully qualified url if it isn't already
    needs_qualified = not str(badge['image']).startswith('http://')
    badge['image'] = url_for(str(badge['image']), qualified=needs_qualified)

    return badge
Exemplo n.º 9
0
 def publish(self, dataset):
     self._get_dataset(dataset)
     require.dataset.update(c.dataset)
     if not c.dataset.private:
         abort(400, _("This dataset is already public!"))
     c.dataset.private = False
     db.session.commit()
     public_url = h.url_for(controller='dataset', action='view', 
                        dataset=c.dataset.name, qualified=True)
     h.flash_success(_("Congratulations, the dataset has been " \
             "published. It is now available at: %s") % public_url)
     redirect(h.url_for(controller='editor', action='index', 
                        dataset=c.dataset.name))
Exemplo n.º 10
0
def badge_apply_links(badge):
    """
    Add links or to badge dictionary representation or modify a dictionary
    representation to include a fully qualified domain
    """
    # Add an html_url to represent the html representation of the badge
    badge['html_url'] = url_for(controller='badge',
                                action='information',
                                id=badge['id'],
                                qualified=True)
    # Change the image url to be a fully qualified url if it isn't already
    needs_qualified = not str(badge['image']).startswith('http://')
    badge['image'] = url_for(str(badge['image']), qualified=needs_qualified)

    return badge
Exemplo n.º 11
0
    def update(self, dataset, name):
        """
        Update dataset. Does nothing at the moment.
        """
        # Get the dataset for the view
        self._get_dataset(dataset)

        # Get the named view
        view = View.by_name(c.dataset, name)
        # User must be allowed to update the named view
        require.view.update(c.dataset, view)

        # Possible update values
        # We don't update the view's name because it might have been embedded
        view.label = request.params.get('label', view.label)
        try:
            # Try to load the state
            view.state = json.loads(request.params['state'])
        except:
            pass
        view.description = request.params.get('description', view.description)

        # Commit the changes
        db.session.commit()

        # Redirect to the view page for this view
        redirect(h.url_for(controller='view', action='view',
                           dataset=c.dataset.name, name=view.name))
Exemplo n.º 12
0
 def _make_browser(self):
     url = url_for(controller='classifier', action='entries',
             taxonomy=c.classifier['taxonomy'],
             name=c.classifier['name'])
     c.browser = Browser(request.params, url=url)
     c.browser.filter_by("+classifiers:%s" % c.classifier['_id'])
     c.browser.facet_by_dimensions()
Exemplo n.º 13
0
    def load(self, dataset, id):
        """
        Load the dataset into the database. If a url parameter 'sample'
        is provided then its value is converted into a boolean. If the value
        equals true we only perform a sample run, else we do a full load.
        """

        # Get our source (and dataset)
        self._get_source(dataset, id)

        # We require that the user can update the dataset
        require.dataset.update(c.dataset)

        # If the source is already running we flash an error declaring that
        # we're already running this source
        if c.source.is_running:
            h.flash_error(_("Already running!"))
        # If the source isn't already running we try to load it (or sample it)
        else:
            try:
                sample = asbool(request.params.get('sample', 'false'))
                load_source.delay(c.source.id, sample)
                # Let the user know we're loading the source
                h.flash_success(_("Now loading..."))
            except Exception as e:
                abort(400, e)

        # Send the user to the editor index page for this dataset
        redirect(
            h.url_for(controller='editor',
                      action='index',
                      dataset=c.dataset.name))
Exemplo n.º 14
0
    def load(self, dataset, id):
        """
        Load the dataset into the database. If a url parameter 'sample'
        is provided then its value is converted into a boolean. If the value
        equals true we only perform a sample run, else we do a full load.
        """

        # Get our source (and dataset)
        self._get_source(dataset, id)

        # We require that the user can update the dataset
        require.dataset.update(c.dataset)

        # If the source is already running we flash an error declaring that
        # we're already running this source
        if c.source.is_running:
            h.flash_error(_("Already running!"))
        # If the source isn't already running we try to load it (or sample it)
        else:
            try:
                sample = asbool(request.params.get('sample', 'false'))
                load_source.delay(c.source.id, sample)
                # Let the user know we're loading the source
                h.flash_success(_("Now loading..."))
            except Exception as e:
                abort(400, e)

        # Send the user to the editor index page for this dataset
        redirect(h.url_for(controller='editor', action='index',
                           dataset=c.dataset.name))
Exemplo n.º 15
0
    def update(self, dataset, name):
        """
        Update dataset. Does nothing at the moment.
        """
        # Get the dataset for the view
        self._get_dataset(dataset)

        # Get the named view
        view = View.by_name(c.dataset, name)
        # User must be allowed to update the named view
        require.view.update(c.dataset, view)

        # Possible update values
        # We don't update the view's name because it might have been embedded
        view.label = request.params.get('label', view.label)
        try:
            # Try to load the state
            view.state = json.loads(request.params['state'])
        except:
            pass
        view.description = request.params.get('description', view.description)

        # Commit the changes
        db.session.commit()

        # Redirect to the view page for this view
        redirect(h.url_for(controller='view', action='view',
                           dataset=c.dataset.name, name=view.name))
Exemplo n.º 16
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))
Exemplo n.º 17
0
def category_index(datasets):
    """
    Get a list of categories by count of datasets
    """
    # Get the dataset ids in the current list of datasets
    ds_ids = [d.id for d in datasets]
    if len(ds_ids):
        # If we have dataset ids we count the dataset by category
        q = db.select(
            [Dataset.category, db.func.count(Dataset.id)],
            Dataset.id.in_(ds_ids),
            group_by=Dataset.category,
            order_by=db.func.count(Dataset.id).desc())

        # Execute the queery to the the list of categories
        categories = db.session.bind.execute(q).fetchall()
        # Return a list of categories as dicts with category, count, url
        # and label
        return [{
            'category':
            category,
            'count':
            count,
            'url':
            h.url_for(controller='dataset', action='index', category=category),
            'label':
            CATEGORIES.get(category, category)
        } for (category, count) in categories if category is not None]

    # We return an empty string if no datasets found
    return []
Exemplo n.º 18
0
def member_apply_links(dataset_name, dimension, data):
    if isinstance(data, dict) and 'name' in data:
        data['html_url'] = url_for(controller='dimension',
                    action='member', dataset=dataset_name,
                    dimension=dimension, name=data['name'],
                    qualified=True)
    return data
Exemplo n.º 19
0
def dataset_apply_links(dataset):
    dataset['html_url'] = url_for(controller='dataset',
                                  action='view',
                                  dataset=dataset['name'],
                                  qualified=True)
    dataset['badges'] = badges_apply_links(dataset['badges'])
    return dataset
Exemplo n.º 20
0
    def index(self, dataset, format='html'):
        # Get the dataset into the context variable 'c'
        self._get_dataset(dataset)

        # If the format is either json or csv we direct the user to the search
        # API instead
        if format in ['json', 'csv']:
            return redirect(h.url_for(controller='api/version2',
                                      action='search',
                                      format=format, dataset=dataset,
                                      **request.params))

        # Get the default view
        handle_request(request, c, c.dataset)

        # Parse the parameters using the SearchParamParser (used by the API)
        parser = EntryIndexParamParser(request.params)
        params, errors = parser.parse()

        # We have to remove page from the parameters because that's also
        # used in the Solr browser (which fetches the queries)
        params.pop('page')

        # We limit ourselve to only our dataset
        params['filter']['dataset'] = [c.dataset.name]
        facet_dimensions = {field.name: field
                            for field in c.dataset.dimensions
                            if field.facet}
        params['facet_field'] = facet_dimensions.keys()

        # Create a Solr browser and execute it
        b = Browser(**params)
        try:
            b.execute()
        except SolrException as e:
            return {'errors': [unicode(e)]}

        # Get the entries, each item is a tuple of the dataset and entry
        solr_entries = b.get_entries()
        entries = [entry for (dataset, entry) in solr_entries]

        # Get expanded facets for this dataset,
        c.facets = b.get_expanded_facets(c.dataset)

        # Create a pager for the entries
        c.entries = templating.Page(entries, **request.params)

        # Set the search word and default to empty string
        c.search = params.get('q', '')

        # Set filters (but remove the dataset as we don't need it)
        c.filters = params['filter']
        del c.filters['dataset']

        # We also make the facet dimensions and dimension names available
        c.facet_dimensions = facet_dimensions
        c.dimensions = [dimension.name for dimension in c.dataset.dimensions]

        # Render the entries page
        return templating.render('entry/index.html')
Exemplo n.º 21
0
 def create(self):
     require.dataset.create()
     try:
         dataset = dict(request.params)
         dataset['territories'] = request.params.getall('territories')
         dataset['languages'] = request.params.getall('languages')
         model = {'dataset': dataset}
         schema = dataset_schema(ValidationState(model))
         data = schema.deserialize(dataset)
         if Dataset.by_name(data['name']) is not None:
             raise Invalid(
                 SchemaNode(String(), name='dataset.name'),
                 _("A dataset with this identifer already exists!"))
         dataset = Dataset({'dataset': data})
         dataset.private = True
         dataset.managers.append(c.account)
         db.session.add(dataset)
         db.session.commit()
         redirect(
             h.url_for(controller='editor',
                       action='index',
                       dataset=dataset.name))
     except Invalid, i:
         errors = i.asdict()
         return self.new(errors)
Exemplo n.º 22
0
 def _make_browser(self):
     url = url_for(controller='dimension', action='entries',
             dataset=c.dataset.name,
             dimension=c.dimension,
             name=c.member['name'])
     c.browser = Browser(c.dataset, request.params, url=url)
     c.browser.filter_by("+%s:\"%s\"" % (c.dimension, c.member['name']))
     c.browser.facet_by_dimensions()
Exemplo n.º 23
0
 def _make_browser(self):
     url = url_for(controller='dimension', action='entries',
             dataset=c.dataset.name,
             dimension=c.dimension,
             name=c.member['name'])
     c.browser = Browser(c.dataset, request.params, url=url)
     c.browser.filter_by("+%s:\"%s\"" % (c.dimension, c.member['name']))
     c.browser.facet_by_dimensions()
Exemplo n.º 24
0
def dimension_apply_links(dataset_name, dimension):
    name = dimension.get('name', dimension.get('key'))
    dimension['html_url'] = url_for(controller='dimension',
                                    action='view',
                                    dataset=dataset_name,
                                    dimension=name,
                                    qualified=True)
    return dimension
Exemplo n.º 25
0
def get_reset_body(account):
    reset_link = h.url_for(controller='account',
                           action='do_reset',
                           email=account.email,
                           token=account.token,
                           qualified=True)
    d = {'reset_link': reset_link, 'site_title': app_globals.site_title}
    return RESET_LINK_MESSAGE % d
Exemplo n.º 26
0
 def delete(self, dataset):
     self._get_dataset(dataset)
     require.dataset.delete(c.dataset)
     c.dataset.drop()
     db.session.delete(c.dataset)
     db.session.commit()
     h.flash_success(_("The dataset has been deleted."))
     redirect(h.url_for(controller='dataset', action='index'))
Exemplo n.º 27
0
 def sitemap(self, dataset):
     self._get_dataset(dataset)
     pages = []
     for action in ['view', 'about']:
         pages.append({
             'loc': h.url_for(controller='dataset', action=action,
                              dataset=c.dataset.name, qualified=True),
             'lastmod': c.dataset.updated_at,
             'priority': 0.8})
     for view in View.all_by_dataset(c.dataset):
         pages.append({
             'loc': h.url_for(controller='view', action='view',
                              dataset=dataset, name=view.name,
                              qualified=True),
             'lastmod': view.updated_at
             })
     return sitemap(pages)
Exemplo n.º 28
0
def entry_apply_links(dataset_name, entry):
    if isinstance(entry, dict) and 'id' in entry:
        entry['html_url'] = url_for(controller='entry',
            action='view', dataset=dataset_name,
            id=entry['id'], qualified=True)
        for k, v in entry.items():
            entry[k] = member_apply_links(dataset_name, k, v)
    return entry
Exemplo n.º 29
0
def member_apply_links(dataset_name, dimension, data):
    if isinstance(data, dict) and 'name' in data:
        data['html_url'] = url_for(controller='dimension',
                                   action='member',
                                   dataset=dataset_name,
                                   dimension=dimension,
                                   name=data['name'],
                                   qualified=True)
    return data
Exemplo n.º 30
0
 def delete(self, dataset, name):
     self._get_named_view(dataset, name)
     if not can.view.delete(c.dataset, c.named_view):
         abort(403, _("You are not authorized to delete this view."))
     h.flash_success(_("'%s' has been deleted.") % c.named_view.label)
     db.session.delete(c.named_view)
     db.session.commit()
     return redirect(h.url_for(controller='view',
                               action='index', dataset=c.dataset.name))
Exemplo n.º 31
0
 def delete(self, dataset, name):
     self._get_named_view(dataset, name)
     if not can.view.delete(c.dataset, c.named_view):
         abort(403, _("You are not authorized to delete this view."))
     h.flash_success(_("'%s' has been deleted.") % c.named_view.label)
     db.session.delete(c.named_view)
     db.session.commit()
     return redirect(h.url_for(controller='view',
         action='index', dataset=c.dataset.name))
Exemplo n.º 32
0
 def delete(self, dataset):
     self._get_dataset(dataset)
     require.dataset.delete(c.dataset)
     c.dataset.drop()
     solr.drop_index(c.dataset.name)
     db.session.delete(c.dataset)
     db.session.commit()
     h.flash_success(_("The dataset has been deleted."))
     redirect(h.url_for(controller='dataset', action='index'))
Exemplo n.º 33
0
    def index(self, dataset, format="html"):
        self._get_dataset(dataset)

        if format in ["json", "csv"]:
            return redirect(
                h.url_for(controller="api2", action="search", format=format, dataset=dataset, **request.params)
            )

        handle_request(request, c, c.dataset)
        return render("entry/index.html")
Exemplo n.º 34
0
def entry_apply_links(dataset_name, entry):
    if isinstance(entry, dict) and 'id' in entry:
        entry['html_url'] = url_for(controller='entry',
                                    action='view',
                                    dataset=dataset_name,
                                    id=entry['id'],
                                    qualified=True)
        for k, v in entry.items():
            entry[k] = member_apply_links(dataset_name, k, v)
    return entry
Exemplo n.º 35
0
 def sitemap(self, dataset):
     self._get_dataset(dataset)
     pages = []
     for action in ["view", "about"]:
         pages.append(
             {
                 "loc": h.url_for(controller="dataset", action=action, dataset=c.dataset.name, qualified=True),
                 "lastmod": c.dataset.updated_at,
                 "priority": 0.8,
             }
         )
     for view in View.all_by_dataset(c.dataset):
         pages.append(
             {
                 "loc": h.url_for(controller="view", action="view", dataset=dataset, name=view.name, qualified=True),
                 "lastmod": view.updated_at,
             }
         )
     return sitemap(pages)
Exemplo n.º 36
0
 def do_reset(self):
     email = request.params.get('email')
     if email is None or not len(email):
         h.flash_error(_("The reset link is invalid!"))
         redirect(h.url_for(controller='account', action='login'))
     account = Account.by_email(email)
     if account is None:
         h.flash_error(_("No user is registered under this address!"))
         redirect(h.url_for(controller='account', action='login'))
     if request.params.get('token') != account.token:
         h.flash_error(_("The reset link is invalid!"))
         redirect(h.url_for(controller='account', action='login'))
     who_api = request.environ['repoze.who.plugins']['auth_tkt']
     headers = who_api.remember(request.environ,
             {'repoze.who.userid': account.name})
     response.headers.extend(headers)
     h.flash_success(_("Thanks! You have now been signed in - please change "
         + "your password!"))
     redirect(h.url_for(controller='account', action='settings'))
Exemplo n.º 37
0
    def index(self, dataset, format='html'):
        self._get_dataset(dataset)

        if format in ['json', 'csv']:
            return redirect(h.url_for(controller='api2', action='search',
                format=format, dataset=dataset,
                **request.params))

        handle_request(request, c, c.dataset)
        return templating.render('entry/index.html')
Exemplo n.º 38
0
 def drop(self, dataset):
     self._get_dataset(dataset)
     require.dataset.update(c.dataset)
     c.dataset.drop()
     c.dataset.init()
     c.dataset.generate()
     AggregationCache(c.dataset).invalidate()
     db.session.commit()
     h.flash_success(_("The dataset has been cleared."))
     redirect(h.url_for(controller='editor', action='index', 
                        dataset=c.dataset.name))
Exemplo n.º 39
0
 def do_reset(self):
     email = request.params.get('email')
     if email is None or not len(email):
         h.flash_error(_("The reset link is invalid!"))
         redirect(h.url_for(controller='account', action='login'))
     account = Account.by_email(email)
     if account is None:
         h.flash_error(_("No user is registered under this address!"))
         redirect(h.url_for(controller='account', action='login'))
     if request.params.get('token') != account.token:
         h.flash_error(_("The reset link is invalid!"))
         redirect(h.url_for(controller='account', action='login'))
     who_api = request.environ['repoze.who.plugins']['auth_tkt']
     headers = who_api.remember(request.environ,
                                {'repoze.who.userid': account.name})
     response.headers.extend(headers)
     h.flash_success(
         _("Thanks! You have now been signed in - please change " +
           "your password!"))
     redirect(h.url_for(controller='account', action='settings'))
Exemplo n.º 40
0
 def member(self, dataset, dimension, name, format="html"):
     self._get_member(dataset, dimension, name)
     handle_request(request, c, c.member, c.dimension.name)
     member = [member_apply_links(dataset, dimension, c.member)]
     if format == 'json':
         return write_json(member, response)
     elif format == 'csv':
         return write_csv(member, response)
     else:
         # If there are no views set up, then go direct to the entries
         # search page
         if c.view is None:
             return redirect(url_for(controller='dimension', action='entries',
                 dataset=c.dataset.name, dimension=dimension, name=name))
         if 'embed' in request.params:
             return redirect(url_for(controller='view',
                 action='embed', dataset=c.dataset.name,
                 widget=c.view.vis_widget.get('name'),
                 state=json.dumps(c.view.vis_state)))
         return render('dimension/member.html')
Exemplo n.º 41
0
def get_reset_body(account):
    reset_link = h.url_for(controller='account',
                           action='do_reset',
                           email=account.email,
                           token=account.token,
                           qualified=True)
    d = {
        'reset_link': reset_link,
        'site_title': app_globals.site_title
        }
    return RESET_LINK_MESSAGE % d
Exemplo n.º 42
0
 def drop(self, dataset):
     self._get_dataset(dataset)
     require.dataset.update(c.dataset)
     c.dataset.drop()
     c.dataset.init()
     c.dataset.generate()
     AggregationCache(c.dataset).invalidate()
     db.session.commit()
     h.flash_success(_("The dataset has been cleared."))
     redirect(h.url_for(controller='editor', action='index', 
                        dataset=c.dataset.name))
Exemplo n.º 43
0
    def index(self, format='html'):
        c.query = request.params.items()
        c.add_filter = lambda f, v: '?' + urlencode(c.query +
                [(f, v)] if (f, v) not in c.query else c.query)
        c.del_filter = lambda f, v: '?' + urlencode([(k, x) for k, x in
            c.query if (k, x) != (f, v)])
        c.results = c.datasets
        for language in request.params.getall('languages'):
            l = db.aliased(DatasetLanguage)
            c.results = c.results.join(l, Dataset._languages)
            c.results = c.results.filter(l.code == language)
        for territory in request.params.getall('territories'):
            t = db.aliased(DatasetTerritory)
            c.results = c.results.join(t, Dataset._territories)
            c.results = c.results.filter(t.code == territory)
        c.results = list(c.results)
        c.territory_options = [{'code': code,
                                'count': count,
                                'url': h.url_for(controller='dataset',
                                    action='index', territories=code),
                                'label': COUNTRIES.get(code, code)} \
            for (code, count) in DatasetTerritory.dataset_counts(c.results)]
        c.language_options = [{'code': code,
                               'count': count,
                               'url': h.url_for(controller='dataset',
                                    action='index', languages=code),
                               'label': LANGUAGES.get(code, code)} \
            for (code, count) in DatasetLanguage.dataset_counts(c.results)]

        if format == 'json':
            results = map(lambda d: d.as_dict(), c.results)
            results = [dataset_apply_links(r) for r in results]
            return to_jsonp({
                'datasets': results,
                'territories': c.territory_options,
                'languages': c.language_options
                })
        elif format == 'csv':
            results = map(lambda d: d.as_dict(), c.results)
            return write_csv(results, response)
        return render('dataset/index.html')
Exemplo n.º 44
0
 def territories(self):
     q = db.select([DatasetTerritory.code, 
                    db.func.count(DatasetTerritory.dataset_id)],
         group_by=DatasetTerritory.code,
         order_by=db.func.count(DatasetTerritory.dataset_id).desc())
     result = {}
     for territory, count in db.session.bind.execute(q).fetchall():
         result[territory] = {'count': count,
                              'label': h.COUNTRIES[territory],
                              'url': h.url_for(controller='dataset',
                                  action='index', territories=territory)}
     return to_jsonp(result)
Exemplo n.º 45
0
 def sitemap(self):
     sections = ['blog', 'resources', 'about', 'help']
     base = h.url_for(controller='home', action='index', qualified=True)
     pages = []
     for section in sections:
         pages.append({
             'loc': base + section + '/index.html',
             'lastmod': datetime.utcnow(),
             'freq': 'daily',
             'priority': 0.9
             })
     return sitemap(pages)
Exemplo n.º 46
0
    def entries(self, dataset, dimension, name, format='html'):
        self._get_member(dataset, dimension, name)
        if format in ['json', 'csv']:
            return redirect(url_for(controller='api2', action='search',
                format=format, dataset=dataset,
                filter='%s.name:%s' % (dimension, name),
                **request.params))

        handle_request(request, c, c.member, c.dimension.name)
        entries = c.dataset.entries(c.dimension.alias.c.name == c.member['name'])
        entries = (entry_apply_links(dataset, e) for e in entries)
        return render('dimension/entries.html')
Exemplo n.º 47
0
 def retract(self, dataset):
     self._get_dataset(dataset)
     require.dataset.update(c.dataset)
     if c.dataset.private:
         abort(400, _("This dataset is already private!"))
     c.dataset.private = True
     AggregationCache(c.dataset).invalidate()
     db.session.commit()
     h.flash_success(_("The dataset has been retracted. " \
             "It is no longer visible to others."))
     redirect(h.url_for(controller='editor', action='index', 
                        dataset=c.dataset.name))
Exemplo n.º 48
0
 def retract(self, dataset):
     self._get_dataset(dataset)
     require.dataset.update(c.dataset)
     if c.dataset.private:
         abort(400, _("This dataset is already private!"))
     c.dataset.private = True
     AggregationCache(c.dataset).invalidate()
     db.session.commit()
     h.flash_success(_("The dataset has been retracted. " \
             "It is no longer visible to others."))
     redirect(h.url_for(controller='editor', action='index', 
                        dataset=c.dataset.name))
Exemplo n.º 49
0
def territory_index(datasets):
    """
    Get a list of territories by count of datasets
    """
    # Get a list of territories in the current list of datasets
    territories = DatasetTerritory.dataset_counts(datasets)
    # Return a list of territories as dicts with code, count, url and label
    return [{'code': code, 'count': count,
             'url': h.url_for(controller='dataset',
                              action='index', territories=code),
             'label': COUNTRIES.get(code, code)}
            for (code, count) in territories]
Exemplo n.º 50
0
def language_index(datasets):
    """
    Get a list of languages by count of datasets
    """
    # Get a list of languages in the current list of datasets
    languages = DatasetLanguage.dataset_counts(datasets)
    # Return a list of languages as dicts with code, count, url and label
    return [{'code': code, 'count': count,
             'url': h.url_for(controller='dataset',
                              action='index', languages=code),
             'label': LANGUAGES.get(code, code)}
            for (code, count) in languages]
Exemplo n.º 51
0
    def index(self, dataset, format='html'):
        self._get_dataset(dataset)

        if format in ['json', 'csv']:
            return redirect(
                h.url_for(controller='api2',
                          action='search',
                          format=format,
                          dataset=dataset,
                          **request.params))

        handle_request(request, c, c.dataset)
        return templating.render('entry/index.html')
Exemplo n.º 52
0
 def sitemap(self, dataset, dimension):
     self._get_dimension(dataset, dimension)
     etag_cache_keygen(c.dataset.updated_at, 'xml')
     pages = []
     # TODO: Make this work for dimensions with more than 30,000 members.
     for member in c.dimension.members(limit=30000):
         pages.append({
             'loc': url_for(controller='dimension', action='member',
                 dataset=dataset, dimension=dimension, name=member.get('name'),
                 qualified=True),
             'lastmod': c.dataset.updated_at
             })
     return sitemap(pages)
Exemplo n.º 53
0
    def view(self, dataset, format='html'):
        """
        Dataset viewer. Default format is html. This will return either
        an entry index if there is no default view or the defaul view.
        If a request parameter embed is given the default view is
        returned as an embeddable page.

        If json is provided as a format the json representation of the
        dataset is returned.
        """

        # Get the dataset (will be placed in c.dataset)
        self._get_dataset(dataset)

        # Generate the etag for the cache based on updated_at value
        etag_cache_keygen(c.dataset.updated_at)

        # Compute the number of entries in the dataset
        c.num_entries = len(c.dataset)

        # Handle the request for the dataset, this will return
        # a default view in c.view if there is any
        handle_request(request, c, c.dataset)

        if format == 'json':
            # If requested format is json we return the json representation
            return to_jsonp(dataset_apply_links(c.dataset.as_dict()))
        else:
            (earliest_timestamp, latest_timestamp) = c.dataset.timerange()
            if earliest_timestamp is not None:
                c.timerange = {
                    'from': earliest_timestamp,
                    'to': latest_timestamp
                }

            if c.view is None:
                # If handle request didn't return a view we return the
                # entry index
                return EntryController().index(dataset, format)
            if 'embed' in request.params:
                # If embed is requested using the url parameters we return
                # a redirect to an embed page for the default view
                return redirect(
                    h.url_for(controller='view',
                              action='embed',
                              dataset=c.dataset.name,
                              widget=c.view.vis_widget.get('name'),
                              state=json.dumps(c.view.vis_state)))
                # Return the dataset view (for the default view)
            return templating.render('dataset/view.html')
Exemplo n.º 54
0
    def entries(self, dataset, dimension, name, format='html'):
        self._get_member(dataset, dimension, name)
        if format in ['json', 'csv']:
            return redirect(
                url_for(controller='api/version2',
                        action='search',
                        format=format,
                        dataset=dataset,
                        filter='%s.name:%s' % (dimension, name),
                        **request.params))

        handle_request(request, c, c.member, c.dimension.name)
        entries = c.dataset.entries(
            c.dimension.alias.c.name == c.member['name'])
        entries = (entry_apply_links(dataset, e) for e in entries)
        return templating.render('dimension/entries.html')
Exemplo n.º 55
0
    def index_export(self, dataset, format):
        self._get_dataset(dataset)

        # TODO include html_urls in dumps.
        #processor = lambda e: entry_apply_links(c.dataset.name, e)

        if format == 'json':
            return write_json(c.dataset.entries(),
                              response,
                              filename=c.dataset.name + '.json')
        if format == 'csv':
            return write_csv(c.dataset.entries(),
                             response,
                             filename=c.dataset.name + '.csv')
        else:
            return redirect(h.url_for(controller='entry', action='index'))
Exemplo n.º 56
0
def territory_index(datasets):
    """
    Get a list of territories by count of datasets
    """
    # Get a list of territories in the current list of datasets
    territories = DatasetTerritory.dataset_counts(datasets)
    # Return a list of territories as dicts with code, count, url and label
    return [{
        'code':
        code,
        'count':
        count,
        'url':
        h.url_for(controller='dataset', action='index', territories=code),
        'label':
        COUNTRIES.get(code, code)
    } for (code, count) in territories]
Exemplo n.º 57
0
def language_index(datasets):
    """
    Get a list of languages by count of datasets
    """
    # Get a list of languages in the current list of datasets
    languages = DatasetLanguage.dataset_counts(datasets)
    # Return a list of languages as dicts with code, count, url and label
    return [{
        'code':
        code,
        'count':
        count,
        'url':
        h.url_for(controller='dataset', action='index', languages=code),
        'label':
        LANGUAGES.get(code, code)
    } for (code, count) in languages]
Exemplo n.º 58
0
    def trigger_reset(self):
        self._disable_cache()
        if request.method == 'GET':
            return render('account/trigger_reset.html')
        email = request.params.get('email')
        if email is None or not len(email):
            h.flash_error(_("Please enter an email address!"))
            return render('account/trigger_reset.html')
        account = Account.by_email(email)
        if account is None:
            h.flash_error(_("No user is registered under this address!"))
            return render('account/trigger_reset.html')
        send_reset_link(account)

        h.flash_success(
            _("You've received an email with a link to reset your " +
              "password. Please check your inbox."))
        redirect(h.url_for(controller='account', action='login'))
Exemplo n.º 59
0
 def sitemap(self, dataset, dimension):
     self._get_dimension(dataset, dimension)
     etag_cache_keygen(c.dataset.updated_at, 'xml')
     pages = []
     # TODO: Make this work for dimensions with more than 30,000 members.
     for member in c.dimension.members(limit=30000):
         pages.append({
             'loc':
             url_for(controller='dimension',
                     action='member',
                     dataset=dataset,
                     dimension=dimension,
                     name=member.get('name'),
                     qualified=True),
             'lastmod':
             c.dataset.updated_at
         })
     return sitemap(pages)
Exemplo n.º 60
0
    def view(self, dataset, format='html'):
        self._get_dataset(dataset)
        etag_cache_keygen(c.dataset.updated_at)
        c.num_entries = len(c.dataset)
        handle_request(request, c, c.dataset)

        if format == 'json':
            return to_jsonp(dataset_apply_links(c.dataset.as_dict()))
        else:
            if c.view is None:
                return EntryController().index(dataset, format)
            if 'embed' in request.params:
                return redirect(
                    h.url_for(controller='view',
                              action='embed',
                              dataset=c.dataset.name,
                              widget=c.view.vis_widget.get('name'),
                              state=json.dumps(c.view.vis_state)))
            return templating.render('dataset/view.html')