Skip to content

Commit

Permalink
[#262] Move externals from lib helpers to ckan.common
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Jan 11, 2013
1 parent 0a707df commit 691e670
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 37 deletions.
17 changes: 17 additions & 0 deletions ckan/common.py
@@ -0,0 +1,17 @@
# This file contains commonly used parts of external libraries. The idea is
# to help in removing helpers from being used as a dependency by many files
# but at the same time making it easy to change for example the json lib
# used.
#
# NOTE: This file is specificaly created for
# from ckan.common import x, y, z to be allowed


from pylons.i18n import _, ungettext
from pylons import g, c, request, session
import simplejson as json

try:
from collections import OrderedDict # from python 2.7
except ImportError:
from sqlalchemy.util import OrderedDict
4 changes: 1 addition & 3 deletions ckan/lib/base.py
Expand Up @@ -30,9 +30,7 @@
import ckan.lib.app_globals as app_globals
from ckan.plugins import PluginImplementations, IGenshiStreamFilter
import ckan.model as model

# horrible hack to keep lib.base broken
json = h.json
from ckan.common import json

log = logging.getLogger(__name__)

Expand Down
5 changes: 3 additions & 2 deletions ckan/lib/field_types.py
Expand Up @@ -6,7 +6,8 @@
with warnings.catch_warnings():
warnings.filterwarnings('ignore', '.*compile_mappers.*')
import formalchemy
import ckan.lib.helpers as h

from ckan.common import OrderedDict

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

Expand All @@ -19,7 +20,7 @@ class DateType(object):
* User inputs in form DD/MM/YYYY and it is stored in db as YYYY-MM-DD.
'''
format_types = ('form', 'db')
datetime_fields = h.OrderedDict([('year', (1000, 2100, 4, 'YYYY')),
datetime_fields = OrderedDict([('year', (1000, 2100, 4, 'YYYY')),
('month', (1, 12, 2, 'MM')),
('day', (1, 31, 2, 'DD')),
('hour', (0, 23, 2, 'HH')),
Expand Down
20 changes: 4 additions & 16 deletions ckan/lib/helpers.py
Expand Up @@ -30,33 +30,21 @@
from alphabet_paginate import AlphaPage
import i18n
import ckan.exceptions
from pylons import request
from pylons import session
from pylons import c, g
from pylons.i18n import _, ungettext

import ckan.lib.fanstatic_resources as fanstatic_resources
import ckan.model as model
import ckan.lib.formatters as formatters
import ckan.lib.datapreview as datapreview

from ckan.common import (
_, ungettext, g, c, request, session, json, OrderedDict
)

get_available_locales = i18n.get_available_locales
get_locales_dict = i18n.get_locales_dict

log = logging.getLogger(__name__)

try:
from collections import OrderedDict # from python 2.7
except ImportError:
from sqlalchemy.util import OrderedDict

try:
import json
except ImportError:
import simplejson as json

_log = logging.getLogger(__name__)


def redirect_to(*args, **kw):
'''A routes.redirect_to wrapper to retain the i18n settings'''
Expand Down
6 changes: 2 additions & 4 deletions ckan/lib/jsonp.py
@@ -1,13 +1,11 @@
import decorator

from pylons import request, response

import ckan.lib.helpers as h
from ckan.common import json, request, response


def to_jsonp(data):
content_type = 'application/json;charset=utf-8'
result = h.json.dumps(data, sort_keys=True)
result = json.dumps(data, sort_keys=True)
if 'callback' in request.params:
response.headers['Content-Type'] = content_type
cbname = request.params['callback']
Expand Down
12 changes: 8 additions & 4 deletions ckan/lib/search/query.py
@@ -1,13 +1,17 @@
import re
import logging

from pylons import config
from solr import SolrException
from paste.deploy.converters import asbool
from paste.util.multidict import MultiDict

from ckan import model
from ckan.logic import get_action
import ckan.lib.helpers as h
from ckan.common import json
from common import make_connection, SearchError, SearchQueryError
import logging


log = logging.getLogger(__name__)

_open_licenses = None
Expand Down Expand Up @@ -273,7 +277,7 @@ def get_index(self,reference):
raise SearchError('SOLR returned an error running query: %r Error: %r' %
(query, e.reason))
try:
data = h.json.loads(solr_response)
data = json.loads(solr_response)

if data['response']['numFound'] == 0:
raise SearchError('Dataset not found in the search index: %s' % reference)
Expand Down Expand Up @@ -359,7 +363,7 @@ def run(self, query):
raise SearchError('SOLR returned an error running query: %r Error: %r' %
(query, e.reason))
try:
data = h.json.loads(solr_response)
data = json.loads(solr_response)
response = data['response']
self.count = response.get('numFound', 0)
self.results = response.get('docs', [])
Expand Down
7 changes: 4 additions & 3 deletions ckan/migration/versions/022_add_group_extras.py
Expand Up @@ -5,7 +5,8 @@
import uuid
from sqlalchemy import types

import ckan.lib.helpers as h
from ckan.common import json

class JsonType(types.TypeDecorator):
'''Store data as JSON serializing on save and unserializing on use.
'''
Expand All @@ -16,13 +17,13 @@ def process_bind_param(self, value, engine):
return None
else:
# ensure_ascii=False => allow unicode but still need to convert
return unicode(h.json.dumps(value, ensure_ascii=False))
return unicode(json.dumps(value, ensure_ascii=False))

def process_result_value(self, value, engine):
if value is None:
return None
else:
return h.json.loads(value)
return json.loads(value)

def copy(self):
return JsonType(self.impl.length)
Expand Down
10 changes: 5 additions & 5 deletions ckan/migration/versions/029_version_groups.py
@@ -1,14 +1,14 @@
import uuid

from sqlalchemy import *
from sqlalchemy import types
from migrate import *
from datetime import datetime
import migrate.changeset
import uuid
from migrate.changeset.constraint import ForeignKeyConstraint

from ckan.common import json


import ckan.lib.helpers as h
class JsonType(types.TypeDecorator):
'''Store data as JSON serializing on save and unserializing on use.
'''
Expand All @@ -19,13 +19,13 @@ def process_bind_param(self, value, engine):
return None
else:
# ensure_ascii=False => allow unicode but still need to convert
return unicode(h.json.dumps(value, ensure_ascii=False))
return unicode(json.dumps(value, ensure_ascii=False))

def process_result_value(self, value, engine):
if value is None:
return None
else:
return h.json.loads(value)
return json.loads(value)

def copy(self):
return JsonType(self.impl.length)
Expand Down

0 comments on commit 691e670

Please sign in to comment.