Ejemplo n.º 1
0
 def search(self):
     c.q = request.params.get('q', None)
     if not c.q:
         return render('account/search.html')
     query = search.account_query(c.q)
     c.page = Page(
         collection=query,
         page=int(request.params.get('page', 1)),
         items_per_page=c.items_per_page,
         item_count=query.count(),
         q=c.q,  # Preserve `q` when the user clicks 'next' or 'previous'.
     )
     return render('account/results.html')
Ejemplo n.º 2
0
    def index(self):
        # c.items_per_page = 100
        page = int(request.params.get('page', 1))
        c.name = u'programme_object_group_code'
        c.codes = self._db.coins.distinct(c.name)

        def code_count(code):
            return self._db.coins.find({c.name: code}, [c.name]).count()

        def get_desc(code):
            colname = c.name.replace('_code', '_description')
            out = self._db.coins.find_one({c.name: code}, [colname])
            if colname in out:
                return out[colname]
            else:
                return None

        def get_link(code):
            link = url(controller='coins',
                       action='index',
                       q='%s:%s' % (c.name, code))
            return link

        c.results = [[code,
                      get_link(code),
                      get_desc(code),
                      code_count(code)] for code in c.codes]
        c.page = Page(
            collection=c.results,
            page=page,
            items_per_page=len(c.results),
        )
        return render('pog/index.html')
Ejemplo n.º 3
0
 def view(self, name_or_id=None):
     c.row = self.get_by_name_or_id(model.Slice, name_or_id)
     c.num_accounts = (model.Session.query(
         model.Account).filter_by(slice_=c.row)).count()
     c.num_transactions = (model.Session.query(
         model.Transaction).filter_by(slice_=c.row)).count()
     return render('slice/view.html')
Ejemplo n.º 4
0
 def transactions(self, name_or_id=None):
     c.slice_ = self.get_by_name_or_id(model.Slice, name_or_id)
     query = model.Session.query(
         model.Transaction).filter_by(slice_=c.slice_)
     c.page = Page(collection=query,
                   page=int(request.params.get('page', 1)),
                   items_per_page=c.items_per_page,
                   item_count=query.count())
     return render('slice/transactions.html')
Ejemplo n.º 5
0
 def index(self):
     query = model.Session.query(model.Transaction)
     c.page = Page(
         collection=query,
         page=int(request.params.get('page', 1)),
         items_per_page=c.items_per_page,
         item_count=query.count(),
     )
     return render('transaction/index.html')
Ejemplo n.º 6
0
 def index(self):
     c.rest_url = url(controller='rest', action='index')
     # Construct query strings by hand to keep the parameters in an instructive order.
     c.aggregate_url = url(controller='api', action='aggregate') + \
         '?slice=%s' % c.default_slice + \
         '&exclude-spender=yes&include-cofog1=07&breakdown-dept=yes&breakdown-region=yes&start_date=2004-05&end_date=2004-05'
     c.mytax_url = url(controller='api', action='mytax') + \
         '?income=20000&spending=10000&smoker=yes&driver=yes'
     return render('home/api.html')
Ejemplo n.º 7
0
 def view(self):
     # Read request parameters.
     slice_ = self.get_by_name_or_id(model.Slice,
         name_or_id=request.params.get('slice', c.default_slice))
     c.filters = {}
     for param, value in request.params.items():
         if param.startswith('include-'):
             key = self.get_by_name_or_id(model.Key, name_or_id=unicode(param[8:]))
             c.filters[key] = value
     c.axis = request.params.get('breakdown')
     if c.axis:
         c.axis = self.get_by_name_or_id(model.Key, name_or_id=c.axis)
     # TODO: Generalise this hard-wired data:
     key_spender = self.get_by_name_or_id(model.Key, name_or_id=u'spender')
     # Do the aggregation.
     c.results = aggregator.aggregate(
         slice_=slice_,
         exclude=[(key_spender, u'yes')],
         include=c.filters.items(),
         axes=[c.axis] if c.axis else [],
     )
     # Function for making small changes to the URL.
     def make_url(add_filter_value=None, remove_filter_key=None):
         new_filters = dict(c.filters)
         new_axis = c.axis
         if add_filter_value:
             new_filters[c.axis] = add_filter_value
             new_axis = None # TODO: Make a better choice.
         if remove_filter_key:
             del new_filters[remove_filter_key]
         params = dict([('include-'+str(key.name), value)
             for key, value in new_filters.items()])
         if new_axis:
             params['breakdown'] = new_axis.name
         return url(controller='aggregate', action='view', **params)
     # Generate info for filters.
     c.filter_labels = dict([(key.name, c.filters[key])
         for key in c.filters])
     c.filter_links = dict([(key.name, make_url(remove_filter_key=key))
         for key in c.filters])
     # Get all Keys for the breakdown drop-down menu.
     c.bd_keys = model.Session.query(model.Key).all()
     # Generate info for breakdown row headings.
     if c.axis:
         c.axis_labels = dict([(ev.code, u'%s: %s' % (ev.code, ev.name))
             for ev in model.Session.query(model.EnumerationValue)
                 .filter_by(key=c.axis).all()])
         c.axis_links = dict([(coordinates[0], make_url(add_filter_value=coordinates[0]))
             for coordinates in c.results.matrix.keys()])
     # Compute totals.
     c.totals = [0.0 for date in c.results.dates]
     for data in c.results.matrix.values():
         for i, amount in enumerate(data):
             c.totals[i] += amount
     # TODO: Omit keys that are useless. How to define?
     return render('aggregate/view.html')
Ejemplo n.º 8
0
 def accounts(self, name_or_id=None):
     c.items_per_page = int(request.params.get('items_per_page', 50))
     c.slice_ = self.get_by_name_or_id(model.Slice, name_or_id)
     query = model.Session.query(model.Account).filter_by(
         slice_=c.slice_).order_by(model.Account.name)
     c.page = Page(collection=query,
                   page=int(request.params.get('page', 1)),
                   items_per_page=c.items_per_page,
                   item_count=query.count())
     return render('slice/accounts.html')
Ejemplo n.º 9
0
 def accounts(self, name_or_id=None):
     c.row = self.get_by_name_or_id(model.Key, name_or_id)
     query = (model.Session.query(model.Account).join(
         (model.KeyValue,
          model.Account.id == model.KeyValue.object_id)).filter_by(
              key=c.row).order_by(model.Account.id))
     c.page = Page(collection=query,
                   page=int(request.params.get('page', 1)),
                   items_per_page=c.items_per_page,
                   item_count=query.count())
     return render('key/accounts.html')
Ejemplo n.º 10
0
 def view(self, name_or_id=None):
     c.row = self.get_by_name_or_id(model.Key, name_or_id)
     c.num_accounts = (model.Session.query(model.KeyValue).filter_by(
         key=c.row).filter_by(ns=u'account')).count()
     c.num_enumeration_values = (model.Session.query(
         model.KeyValue).filter_by(key=c.row).filter_by(
             ns=u'enumeration_value')).count()
     query = model.Session.query(
         model.EnumerationValue).filter_by(key_id=c.row.id)
     c.page = Page(collection=query,
                   page=int(request.params.get('page', 1)),
                   items_per_page=c.items_per_page,
                   item_count=query.count())
     return render('key/view.html')
Ejemplo n.º 11
0
    def view(self, year, field):
        dataset = 'fact_table_extract_%s' % year
        c.field = field
        query = app_globals.solr.query('srcid:%s*' % dataset,
                                       facet='true',
                                       facet_field=field)
        c.facet_counts = query.facet_counts['facet_fields'][c.field]

        c.descriptions = {}
        if field.endswith('code'):
            for k in c.facet_counts:
                entry = model.get_one_entry(k, c.field)
                if entry:
                    c.descriptions[k] = entry.get(
                        c.field.replace('code', 'description'), '')
        return render('facet/view.html')
Ejemplo n.º 12
0
 def postings(self, id_=None):
     c.row = self.get_by_id(model.Account, id_)
     query = (model.Session.query(
         model.Posting).filter_by(account=c.row).order_by('timestamp'))
     # TODO: as sql
     # import wdmmg.model.atp as atp
     # amountq = atp.table_posting.select().where(
     #        atp.table_posting.c.account_id==c.row.id
     #        ).group_by(atp.table_posting.c.timestamp)
     # c.amount = amountq.execute().fetchall()
     c.amounts = {}
     for posting in query:
         c.amounts[posting.timestamp] = c.amounts.get(posting.timestamp,
                                                      0) + posting.amount
     c.page = Page(collection=query,
                   page=int(request.params.get('page', 1)),
                   items_per_page=c.items_per_page,
                   item_count=query.count())
     return render('account/postings.html')
Ejemplo n.º 13
0
    def view(self, name_or_id=None, code=None):
        '''
        name_or_id - a `Key.name` or `Key.id`.
        code - an `EnumerationValue.code`.
        '''
        c.key = self.get_by_name_or_id(model.Key, name_or_id)
        c.row = (model.Session.query(model.EnumerationValue).filter_by(
            key=c.key).filter_by(code=code)).first()
        q = model.Session.query(model.Account)
        q = q.join(
            (model.KeyValue, model.KeyValue.object_id == model.Account.id))
        q = q.join(model.Key)
        q = q.join(model.EnumerationValue)
        print q.filter(model.Key.name == c.key.name)

        c.accounts = q.filter(model.EnumerationValue.id == c.row.id).filter(
            model.Key.name == c.key.name, ).distinct().all()

        if not c.row:
            abort(404, 'No record with code %r' % code)
        return render('enumeration_value/view.html')
Ejemplo n.º 14
0
 def index(self):
     slice_ = model.Session.query(model.Slice).first()
     enumeration_value = model.Session.query(model.EnumerationValue).first()
     key = enumeration_value.key
     c.urls = [
         url(controller='rest', action='slice', name_or_id=slice_.name),
         url(controller='rest', action='slice', name_or_id=slice_.id),
         url(controller='rest',
             action='account',
             id_=model.Session.query(model.Account).first().id),
         url(controller='rest',
             action='transaction',
             id_=model.Session.query(model.Transaction).first().id),
         url(controller='rest', action='key', name_or_id=key.name),
         url(controller='rest', action='key', name_or_id=key.id),
         url(controller='rest',
             action='enumeration_value',
             name_or_id=enumeration_value.key.name,
             code=enumeration_value.code),
     ]
     return render('home/rest.html')
Ejemplo n.º 15
0
 def index_solr(self):
     c.items_per_page = 100
     c.q = request.params.get('q', None)
     page = int(request.params.get('page', 1))
     if c.q:
         query = app_globals.solr.query(c.q,
                                        rows=c.items_per_page,
                                        q_op='AND')
     else:
         query = app_globals.solr.query('*',
                                        sort='score desc, value desc',
                                        rows=c.items_per_page)
     c.results = query.results
     c.page = Page(
         collection=c.results,
         page=page,
         items_per_page=c.items_per_page,
         item_count=query.numFound,
         q=c.q,  # Preserve `q` when the user clicks 'next' or 'previous'.
     )
     return render('coins/index.html')
Ejemplo n.º 16
0
 def index(self):
     all_keys = model.Session.query(model.Key).all()
     # Make ourselves an index of keys by name.
     c.by_name = {}  # name -> Key
     for key in all_keys:
         c.by_name[key.name] = key
     parent_key = self.get_by_name_or_id(model.Key, name_or_id=u'parent')
     # Identify keys with/without parents.
     c.parents = {}  # name -> name
     c.by_parent = {}  # name -> list of names
     for key in all_keys:
         if parent_key in key.keyvalues:
             c.parents[key.name] = key.keyvalues[parent_key]
         else:
             c.by_parent[key.name] = []
     # For each key that has a parent, attach it to its ultimate ancestor.
     for name in c.parents:
         parent = c.parents[name]
         while parent in c.parents:
             parent = c.parents[parent]
         c.by_parent[parent].append(name)
     return render('key/index.html')
Ejemplo n.º 17
0
 def total(self):
     values = dict(request.params)
     c.values = values
     # if programme_object_code in request.params:
     # undo hack in link creation
     #    po = request.params['po.replace('|', '/')
     #    q += ' programme_object_code:"%s"' % po
     q = ''
     # id for use in e.g. disqus
     c.pageid = ''
     for k in sorted(values.keys()):
         v = values[k]
         q += '%s:"%s" ' % (k, v)
         c.pageid += '%s::%s::' % (k, v)
     # " are not allowed in the pageid
     c.pageid = c.pageid.replace('"', "'")
     c.pageid = c.pageid.replace('&', "amp;")
     c.pageid = c.pageid.replace('<', "lt;")
     query = app_globals.solr.query(q, rows=2000, q_op='AND')
     c.results = query.results
     c.count = query.numFound
     if c.count > 2000:
         return 'Too many entries (%s) to sum' % c.count
     else:
         # put into years
         # exclude net parliamentary funding
         npf_account_code = '31070000'
         c.total = {}
         c.npf = {}
         for hit in query:
             year = hit['dataset'].split('_')[3]
             if hit['account_code'] != npf_account_code:
                 # convert to millions
                 c.total[year] = c.total.get(year,
                                             0) + hit['value'] / 1000.0
             else:
                 c.npf[year] = c.npf.get(year, 0) + hit['value'] / 1000.0
     return render('facet/total.html')
Ejemplo n.º 18
0
 def index_mongo(self):
     from pymongo import ASCENDING, DESCENDING
     c.items_per_page = 100
     c.q = request.params.get('q', None)
     page = int(request.params.get('page', 1))
     if c.q:
         # textq = '/.*%s.*/i' % c.q
         dbq = self._db.coins.find({'search_field': {'$all': c.q.split()}})
         dbq.sort('value', DESCENDING)
     else:
         dbq = self._db.coins.find().sort('value', DESCENDING)
     c.results = [
         x for x in dbq.limit(c.items_per_page).skip((page - 1) *
                                                     c.items_per_page)
     ]
     c.page = Page(
         collection=c.results,
         page=page,
         items_per_page=c.items_per_page,
         item_count=dbq.count(),
         q=c.q,  # Preserve `q` when the user clicks 'next' or 'previous'.
     )
     return render('coins/index.html')
Ejemplo n.º 19
0
 def index(self):
     c.limit = int(request.params.get('limit',
                                      '100'))  # TODO: Nicer error message.
     c.results = model.Session.query(model.Slice)[:c.limit]
     return render('slice/index.html')
Ejemplo n.º 20
0
 def view_id(self, id_=None):
     '''Deprecated.'''
     c.row = self.get_by_id(model.EnumerationValue, id_)
     c.key = c.row.key
     return render('enumeration_value/view.html')
Ejemplo n.º 21
0
 def view(self, id_=None):
     c.row = self.get_by_id(model.Transaction, id_)
     return render('transaction/view.html')
Ejemplo n.º 22
0
 def view(self, id):
     c.entry = model.get_one_entry(id)
     if not c.entry:
         abort(404)
     return render('coins/entry.html')
Ejemplo n.º 23
0
 def index(self):
     return render('facet/index.html')
Ejemplo n.º 24
0
 def index(self):
     c.slice_count = model.Session.query(model.Slice).count()
     c.account_count = model.Session.query(model.Account).count()
     c.transaction_count = model.Session.query(model.Transaction).count()
     c.key_count = model.Session.query(model.Key).count()
     return render('home/index.html')
Ejemplo n.º 25
0
 def view(self, id_=None):
     c.row = self.get_by_id(model.Account, id_)
     c.num_postings = (model.Session.query(
         model.Posting).filter_by(account=c.row)).count()
     return render('account/view.html')
Ejemplo n.º 26
0
 def view(self, id):
     c.entry = self._db.coins.find_one({'srcid': id})
     if not c.entry:
         abort(404)
     return render('coins/entry.html')