Skip to content

Commit

Permalink
[#1251] Refactor cli.py moving SQL queries to Resource model
Browse files Browse the repository at this point in the history
I've also used SQLAlchemy instead of writing bare SQL.
  • Loading branch information
vitorbaptista committed May 7, 2014
1 parent fd71934 commit 4577c24
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 38 deletions.
50 changes: 13 additions & 37 deletions ckan/lib/cli.py
Expand Up @@ -2205,24 +2205,18 @@ def create_text_views(self):
formats = tuple(textplugin.DEFAULT_TEXT_FORMATS + textplugin.DEFAULT_XML_FORMATS +
textplugin.DEFAULT_JSON_FORMATS + textplugin.DEFAULT_JSONP_FORMATS)

results = model.Session.execute(
'''select resource.id, format
from resource
left join resource_view on resource.id = resource_view.resource_id
where lower(format) in %s and resource_view.id is null
''' % str(formats)
)
resources = model.Resource.get_all_without_views(formats)

user = logic.get_action('get_site_user')({'model': model, 'ignore_auth': True}, {})
context = {'model': model, 'session': model.Session, 'user': user['name']}

count = 0
for row in results:
for resource in resources:
count += 1
resource_view = {'title': 'Text View',
'description': 'View of the {format} file'.format(
format=row[1].upper()),
'resource_id': row[0],
format=resource.format.upper()),
'resource_id': resource.id,
'view_type': 'text'}

logic.get_action('resource_view_create')(context, resource_view)
Expand All @@ -2239,23 +2233,17 @@ def create_image_views(self):

print '''Image resource views are being created'''

results = model.Session.execute(
'''select resource.id, format
from resource
left join resource_view on resource.id = resource_view.resource_id
where lower(format) in %s and resource_view.id is null
''' % str(formats)
)
resources = model.Resource.get_all_without_views(formats)

user = logic.get_action('get_site_user')({'model': model, 'ignore_auth': True}, {})
context = {'model': model, 'session': model.Session, 'user': user['name']}

count = 0
for row in results:
for resource in resources:
count += 1
resource_view = {'title': 'Resource Image',
'description': 'View of the Image',
'resource_id': row[0],
'resource_id': resource.id,
'view_type': 'image'}

logic.get_action('resource_view_create')(context, resource_view)
Expand All @@ -2270,23 +2258,17 @@ def create_webpage_views(self):

print '''Web page resource views are being created'''

results = model.Session.execute(
'''select resource.id, format
from resource
left join resource_view on resource.id = resource_view.resource_id
where lower(format) in %s and resource_view.id is null
''' % str(formats)
)
resources = model.Resource.get_all_without_views(formats)

user = logic.get_action('get_site_user')({'model': model, 'ignore_auth': True}, {})
context = {'model': model, 'session': model.Session, 'user': user['name']}

count = 0
for row in results:
for resource in resources:
count += 1
resource_view = {'title': 'Web Page View',
'description': 'View of the webpage',
'resource_id': row[0],
'resource_id': resource.id,
'view_type': 'webpage'}

logic.get_action('resource_view_create')(context, resource_view)
Expand All @@ -2308,23 +2290,17 @@ def create_pdf_views(self):

print '''PDF resource views are being created'''

results = model.Session.execute(
'''select resource.id, format
from resource
left join resource_view on resource.id = resource_view.resource_id
where lower(format) = 'pdf' and resource_view.id is null
'''
)
resources = model.Resource.get_all_without_views(['pdf'])

user = logic.get_action('get_site_user')({'model': model, 'ignore_auth': True}, {})
context = {'model': model, 'session': model.Session, 'user': user['name']}

count = 0
for row in results:
for resource in resources:
count += 1
resource_view = {'title': 'PDF View',
'description': 'PDF view of the resource.',
'resource_id': row[0],
'resource_id': resource.id,
'view_type': 'pdf'}

logic.get_action('resource_view_create')(context, resource_view)
Expand Down
22 changes: 21 additions & 1 deletion ckan/model/resource.py
Expand Up @@ -6,7 +6,7 @@
from pylons import config
import vdm.sqlalchemy
import vdm.sqlalchemy.stateful
from sqlalchemy import types, Column, Table, ForeignKey, and_
from sqlalchemy import types, func, Column, Table, ForeignKey, and_

import meta
import core
Expand All @@ -16,6 +16,7 @@
import activity
import domain_object
import ckan.lib.dictization
import ckan.model

__all__ = ['Resource', 'resource_table',
'ResourceGroup', 'resource_group_table',
Expand Down Expand Up @@ -158,6 +159,25 @@ def get_extra_columns(cls):
setattr(cls, field, DictProxy(field, 'extras'))
return cls.extra_columns

@classmethod
def get_all_without_views(cls, formats=[]):
'''Returns all resources that have no resource views
:param formats: if given, returns only resources that have no resource
views and are in any of the received formats
:type formats: list
:rtype: list of ckan.model.Resource objects
'''
query = meta.Session.query(cls).outerjoin(ckan.model.ResourceView) \
.filter(ckan.model.ResourceView.id == None)

if formats:
lowercase_formats = [f.lower() for f in formats]
query = query.filter(func.lower(cls.format).in_(lowercase_formats))

return query.all()

def related_packages(self):
return [self.resource_group.package]

Expand Down
44 changes: 44 additions & 0 deletions ckan/new_tests/model/test_resource.py
@@ -0,0 +1,44 @@
import nose.tools

import ckan.new_tests.helpers as helpers
import ckan.new_tests.factories as factories
import ckan.model as model

assert_equals = nose.tools.assert_equals
assert_not_equals = nose.tools.assert_not_equals
Resource = model.Resource


class TestResource(object):
@classmethod
def setup_class(cls):
helpers.reset_db()

def setup(self):
model.repo.rebuild_db()

def test_get_all_without_views_returns_all_resources_without_views(self):
# Create resource with resource_view
factories.ResourceView()

expected_resources = [
factories.Resource(format='format'),
factories.Resource(format='other_format')
]

resources = Resource.get_all_without_views()

expected_resources_ids = [r['id'] for r in expected_resources]
resources_ids = [r.id for r in resources]

assert_equals(expected_resources_ids.sort(), resources_ids.sort())

def test_get_all_without_views_accepts_list_of_formats_ignoring_case(self):
factories.Resource(format='other_format')
resource_id = factories.Resource(format='format')['id']

resources = Resource.get_all_without_views(['FORMAT'])

length = len(resources)
assert length == 1, 'Expected 1 resource, but got %d' % length
assert_equals([resources[0].id], [resource_id])

0 comments on commit 4577c24

Please sign in to comment.