Skip to content

Commit

Permalink
Various cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Feb 20, 2013
1 parent 5652b95 commit 9e790c0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 41 deletions.
43 changes: 5 additions & 38 deletions ckanext/qa/plugin.py
@@ -1,25 +1,21 @@
import json
import datetime
from genshi.input import HTML
from genshi.filters import Transformer
from pylons import request, tmpl_context as c
import html
import reports

import ckan.lib.dictization.model_dictize as model_dictize
import ckan.model as model
import ckan.plugins as p
import ckan.lib.helpers as h
import ckan.lib.celery_app as celery_app
from ckan.model.types import make_uuid
import html
import reports

resource_dictize = model_dictize.resource_dictize
send_task = celery_app.celery.send_task


class QAPlugin(p.SingletonPlugin):

p.implements(p.IConfigurer, inherit=True)
p.implements(p.IConfigurable)
p.implements(p.IGenshiStreamFilter)
p.implements(p.IRoutes, inherit=True)
p.implements(p.IDomainObjectModification, inherit=True)
p.implements(p.IResourceUrlChange)
Expand Down Expand Up @@ -128,7 +124,7 @@ def _create_task(self, resource):

data = json.dumps(resource_dict)

task_id = make_uuid()
task_id = model.types.make_uuid()
task_status = {
'entity_id': resource.id,
'entity_type': u'resource',
Expand All @@ -146,35 +142,6 @@ def _create_task(self, resource):
p.toolkit.get_action('task_status_update')(task_context, task_status)
send_task('qa.update', args=[context, data], task_id=task_id)

def filter(self, stream):
routes = request.environ.get('pylons.routes_dict')

site_url = h.url('/', locale='default')
stream = stream | Transformer('head').append(
HTML(html.HEAD_CODE % site_url)
)

if (routes.get('controller') == 'package' and
routes.get('action') == 'resource_read'):

star_html = self.get_star_html(c.resource.get('id'))
if star_html:
stream = stream | Transformer('body//div[@class="quick-info"]//dl')\
.append(HTML(html.DL_HTML % star_html))

if (routes.get('controller') == 'package' and
routes.get('action') == 'read' and
c.pkg.id):

for resource in c.pkg_dict.get('resources', []):
resource_id = resource.get('id')
star_html = self.get_star_html(resource_id)
if star_html:
stream = stream | Transformer('body//div[@id="%s"]//p[@class="extra-links"]' % resource_id)\
.append(HTML(star_html))

return stream

@classmethod
def get_star_html(cls, resource_id):
report = reports.resource_five_stars(resource_id)
Expand Down
7 changes: 4 additions & 3 deletions ckanext/qa/reports.py
Expand Up @@ -22,12 +22,13 @@ def five_stars(id=None):
# take the maximum openness score among dataset resources to be the
# overall dataset openness core
query = model.Session.query(model.Package.name, model.Package.title,
func.max(model.TaskStatus.value).label('value'))\
model.Resource.id,
model.TaskStatus.value.label('value'))\
.join(model.ResourceGroup, model.Package.id == model.ResourceGroup.package_id)\
.join(model.Resource)\
.join(model.TaskStatus, model.TaskStatus.entity_id == model.Resource.id)\
.filter(model.TaskStatus.key==u'openness_score')\
.group_by(model.Package.name, model.Package.title)\
.group_by(model.Package.name, model.Package.title, model.Resource.id, model.TaskStatus.value)\
.distinct()

if id:
Expand All @@ -37,7 +38,7 @@ def five_stars(id=None):
for row in query:
results.append({
'name': row.name,
'title': row.title,
'title': row.title + u' ' + row.id,
'openness_score': row.value
})

Expand Down

0 comments on commit 9e790c0

Please sign in to comment.