Beispiel #1
0
    def core_edit(self, dataset, errors={}, format='html'):
        self._get_dataset(dataset)
        self._disable_cache()
        require.dataset.update(c.dataset)
        c.key_currencies = sorted(
            [(r, n) for (r, (n, k)) in CURRENCIES.items() if k],
            key=lambda k_v: k_v[1])
        c.all_currencies = sorted(
            [(r, n) for (r, (n, k)) in CURRENCIES.items() if not k],
            key=lambda k_v1: k_v1[1])
        c.languages = sorted(LANGUAGES.items(), key=lambda k_v2: k_v2[1])
        c.territories = sorted(COUNTRIES.items(), key=lambda k_v3: k_v3[1])
        c.categories = sorted(CATEGORIES.items(), key=lambda k_v4: k_v4[1])

        if 'time' in c.dataset:
            c.available_times = [m['year']
                                 for m in c.dataset['time'].members()]
            c.available_times = sorted(set(c.available_times), reverse=True)
        else:
            c.available_times = []

        errors = [(k[len('dataset.'):], v) for k, v in errors.items()]
        fill = c.dataset.as_dict()
        if errors:
            fill.update(request.params)
        return templating.render('editor/core.html', form_errors=dict(errors),
                                 form_fill=fill)
Beispiel #2
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 []
Beispiel #3
0
 def new(self, errors={}):
     self._disable_cache()
     if not has.dataset.create():
         return render("dataset/new_cta.html")
     require.dataset.create()
     c.key_currencies = sorted([(r, n) for (r, (n, k)) in CURRENCIES.items() if k], key=lambda (k, v): v)
     c.all_currencies = sorted([(r, n) for (r, (n, k)) in CURRENCIES.items() if not k], key=lambda (k, v): v)
     c.languages = sorted(LANGUAGES.items(), key=lambda (k, v): v)
     c.territories = sorted(COUNTRIES.items(), key=lambda (k, v): v)
     c.categories = sorted(CATEGORIES.items(), key=lambda (k, v): v)
     errors = [(k[len("dataset.") :], v) for k, v in errors.items()]
     return render(
         "dataset/new.html", form_errors=dict(errors), form_fill=request.params if errors else {"currency": "USD"}
     )
Beispiel #4
0
 def new(self, errors={}):
     self._disable_cache()
     if not has.dataset.create():
         return templating.render('dataset/new_cta.html')
     require.dataset.create()
     c.key_currencies = sorted(
         [(r, n) for (r, (n, k)) in CURRENCIES.items() if k],
         key=lambda k_v: k_v[1])
     c.all_currencies = sorted(
         [(r, n) for (r, (n, k)) in CURRENCIES.items() if not k],
         key=lambda k_v1: k_v1[1])
     c.languages = sorted(LANGUAGES.items(), key=lambda k_v2: k_v2[1])
     c.territories = sorted(COUNTRIES.items(), key=lambda k_v3: k_v3[1])
     c.categories = sorted(CATEGORIES.items(), key=lambda k_v4: k_v4[1])
     errors = [(k[len('dataset.'):], v) for k, v in errors.items()]
     return templating.render(
         'dataset/new.html', form_errors=dict(errors),
         form_fill=request.params if errors else {'currency': 'USD'})
Beispiel #5
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 []
Beispiel #6
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)
        category = request.params.get('category')
        if category:
            c.results = c.results.filter(Dataset.category == category)

        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)]

        # TODO: figure out where to put this:
        ds_ids = [d.id for d in c.results]
        if len(ds_ids):
            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())
            c.category_options = [{'category': category,
                                   'count': count,
                                   'url': h.url_for(controller='dataset',
                                        action='index', category=category),
                                   'label': CATEGORIES.get(category, category)} \
                for (category, count) in db.session.bind.execute(q).fetchall() \
                if category is not None]
        else:
            c.category_options = []

        c._must_revalidate = True
        if len(c.results):
            dt = max([r.updated_at for r in c.results])
            etag_cache_keygen(dt)

        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,
                'categories': c.category_options,
                '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)
        c.show_rss = True
        return templating.render('dataset/index.html')
Beispiel #7
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)
        category = request.params.get('category')
        if category:
            c.results = c.results.filter(Dataset.category == category)

        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)]

        # TODO: figure out where to put this:
        ds_ids = [d.id for d in c.results]
        if len(ds_ids):
            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())
            c.category_options = [{'category': category,
                                   'count': count,
                                   'url': h.url_for(controller='dataset',
                                        action='index', category=category),
                                   'label': CATEGORIES.get(category, category)} \
                for (category, count) in db.session.bind.execute(q).fetchall() \
                if category is not None]
        else:
            c.category_options = []

        c._must_revalidate = True
        if len(c.results):
            dt = max([r.updated_at for r in c.results])
            etag_cache_keygen(dt)

        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,
                'categories': c.category_options,
                '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)
        c.show_rss = True
        return templating.render('dataset/index.html')
Beispiel #8
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)
        category = request.params.get("category")
        if category:
            c.results = c.results.filter(Dataset.category == category)

        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)
        ]

        # TODO: figure out where to put this:
        ds_ids = [d.id for d in c.results]
        if len(ds_ids):
            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(),
            )
            c.category_options = [
                {
                    "category": category,
                    "count": count,
                    "url": h.url_for(controller="dataset", action="index", category=category),
                    "label": CATEGORIES.get(category, category),
                }
                for (category, count) in db.session.bind.execute(q).fetchall()
                if category is not None
            ]
        else:
            c.category_options = []

        c._must_revalidate = True
        if len(c.results):
            dt = max([r.updated_at for r in c.results])
            etag_cache_keygen(dt)

        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,
                    "categories": c.category_options,
                    "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")