aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tools
diff options
context:
space:
mode:
Diffstat (limited to 'mediagoblin/tools')
-rw-r--r--mediagoblin/tools/common.py1
-rw-r--r--mediagoblin/tools/mail.py7
-rw-r--r--mediagoblin/tools/pagination.py20
-rw-r--r--mediagoblin/tools/response.py5
-rw-r--r--mediagoblin/tools/template.py14
-rw-r--r--mediagoblin/tools/text.py7
-rw-r--r--mediagoblin/tools/translate.py19
-rw-r--r--mediagoblin/tools/url.py2
8 files changed, 52 insertions, 23 deletions
diff --git a/mediagoblin/tools/common.py b/mediagoblin/tools/common.py
index ea4541a8..12d8309e 100644
--- a/mediagoblin/tools/common.py
+++ b/mediagoblin/tools/common.py
@@ -21,6 +21,7 @@ DISPLAY_IMAGE_FETCHING_ORDER = [u'medium', u'original', u'thumb']
global TESTS_ENABLED
TESTS_ENABLED = False
+
def import_component(import_string):
"""
Import a module component defined by STRING. Probably a method,
diff --git a/mediagoblin/tools/mail.py b/mediagoblin/tools/mail.py
index 826acdbf..9e00be7d 100644
--- a/mediagoblin/tools/mail.py
+++ b/mediagoblin/tools/mail.py
@@ -24,7 +24,7 @@ from mediagoblin.tools import common
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# We have two "test inboxes" here:
-#
+#
# EMAIL_TEST_INBOX:
# ----------------
# If you're writing test views, you'll probably want to check this.
@@ -44,11 +44,12 @@ from mediagoblin.tools import common
# ***IMPORTANT!***
# ----------------
# Before running tests that call functions which send email, you should
-# always call _clear_test_inboxes() to "wipe" the inboxes clean.
+# always call _clear_test_inboxes() to "wipe" the inboxes clean.
EMAIL_TEST_INBOX = []
EMAIL_TEST_MBOX_INBOX = []
+
class FakeMhost(object):
"""
Just a fake mail host so we can capture and test messages
@@ -63,12 +64,14 @@ class FakeMhost(object):
'to': to_addrs,
'message': message})
+
def _clear_test_inboxes():
global EMAIL_TEST_INBOX
global EMAIL_TEST_MBOX_INBOX
EMAIL_TEST_INBOX = []
EMAIL_TEST_MBOX_INBOX = []
+
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### </Special email test stuff>
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/mediagoblin/tools/pagination.py b/mediagoblin/tools/pagination.py
index 859b60fb..5ebc3c5a 100644
--- a/mediagoblin/tools/pagination.py
+++ b/mediagoblin/tools/pagination.py
@@ -19,8 +19,10 @@ import copy
from math import ceil, floor
from itertools import izip, count
+
PAGINATION_DEFAULT_PER_PAGE = 30
+
class Pagination(object):
"""
Pagination class for mongodb queries.
@@ -37,9 +39,9 @@ class Pagination(object):
Args:
- page: requested page
- per_page: number of objects per page
- - cursor: db cursor
- - jump_to_id: ObjectId, sets the page to the page containing the object
- with _id == jump_to_id.
+ - cursor: db cursor
+ - jump_to_id: ObjectId, sets the page to the page containing the
+ object with _id == jump_to_id.
"""
self.page = page
self.per_page = per_page
@@ -51,7 +53,7 @@ class Pagination(object):
cursor = copy.copy(self.cursor)
for (doc, increment) in izip(cursor, count(0)):
- if doc['_id'] == jump_to_id:
+ if doc._id == jump_to_id:
self.page = 1 + int(floor(increment / self.per_page))
self.active_id = jump_to_id
@@ -91,19 +93,19 @@ class Pagination(object):
last = num
def get_page_url_explicit(self, base_url, get_params, page_no):
- """
+ """
Get a page url by adding a page= parameter to the base url
- """
+ """
new_get_params = copy.copy(get_params or {})
new_get_params['page'] = page_no
return "%s?%s" % (
base_url, urllib.urlencode(new_get_params))
def get_page_url(self, request, page_no):
- """
+ """
Get a new page url based of the request, and the new page number.
This is a nice wrapper around get_page_url_explicit()
- """
+ """
return self.get_page_url_explicit(
- request.path_info, request.GET, page_no)
+ request.full_path, request.GET, page_no)
diff --git a/mediagoblin/tools/response.py b/mediagoblin/tools/response.py
index 1477b9bc..b01d31a2 100644
--- a/mediagoblin/tools/response.py
+++ b/mediagoblin/tools/response.py
@@ -17,12 +17,14 @@
from webob import Response, exc
from mediagoblin.tools.template import render_template
+
def render_to_response(request, template, context, status=200):
"""Much like Django's shortcut.render()"""
return Response(
render_template(request, template, context),
status=status)
+
def render_404(request):
"""
Render a 404.
@@ -30,9 +32,10 @@ def render_404(request):
return render_to_response(
request, 'mediagoblin/404.html', {}, status=400)
+
def redirect(request, *args, **kwargs):
"""Returns a HTTPFound(), takes a request and then urlgen params"""
-
+
querystring = None
if kwargs.get('querystring'):
querystring = kwargs.get('querystring')
diff --git a/mediagoblin/tools/template.py b/mediagoblin/tools/template.py
index a773ca99..0986761b 100644
--- a/mediagoblin/tools/template.py
+++ b/mediagoblin/tools/template.py
@@ -17,18 +17,19 @@
from math import ceil
import jinja2
from babel.localedata import exists
-from babel.support import LazyProxy
from mediagoblin import mg_globals
from mediagoblin import messages
from mediagoblin.tools import common
from mediagoblin.tools.translate import setup_gettext
from mediagoblin.middleware.csrf import render_csrf_form_token
+
SETUP_JINJA_ENVS = {}
+
def get_jinja_env(template_loader, locale):
"""
- Set up the Jinja environment,
+ Set up the Jinja environment,
(In the future we may have another system for providing theming;
for now this is good enough.)
@@ -51,15 +52,19 @@ def get_jinja_env(template_loader, locale):
# All templates will know how to ...
# ... fetch all waiting messages and remove them from the queue
# ... construct a grid of thumbnails or other media
+ # ... have access to the global and app config
template_env.globals['fetch_messages'] = messages.fetch_messages
template_env.globals['gridify_list'] = gridify_list
template_env.globals['gridify_cursor'] = gridify_cursor
+ template_env.globals['app_config'] = mg_globals.app_config
+ template_env.globals['global_config'] = mg_globals.global_config
if exists(locale):
SETUP_JINJA_ENVS[locale] = template_env
return template_env
+
# We'll store context information here when doing unit tests
TEMPLATE_TEST_CONTEXT = {}
@@ -76,8 +81,8 @@ def render_template(request, template_path, context):
context['request'] = request
context['csrf_token'] = render_csrf_form_token(request)
rendered = template.render(context)
-
- if common.TESTS_ENABLED:
+
+ if common.TESTS_ENABLED:
TEMPLATE_TEST_CONTEXT[template_path] = context
return rendered
@@ -87,6 +92,7 @@ def clear_test_template_context():
global TEMPLATE_TEST_CONTEXT
TEMPLATE_TEST_CONTEXT = {}
+
def gridify_list(this_list, num_cols=5):
"""
Generates a list of lists where each sub-list's length depends on
diff --git a/mediagoblin/tools/text.py b/mediagoblin/tools/text.py
index de4bb281..be1adb00 100644
--- a/mediagoblin/tools/text.py
+++ b/mediagoblin/tools/text.py
@@ -21,6 +21,7 @@ from lxml.html.clean import Cleaner
from mediagoblin import mg_globals
from mediagoblin.tools import url
+
# A super strict version of the lxml.html cleaner class
HTML_CLEANER = Cleaner(
scripts=True,
@@ -42,6 +43,7 @@ HTML_CLEANER = Cleaner(
host_whitelist=(),
whitelist_tags=set([]))
+
def clean_html(html):
# clean_html barfs on an empty string
if not html:
@@ -49,6 +51,7 @@ def clean_html(html):
return HTML_CLEANER.clean_html(html)
+
def convert_to_tag_list_of_dicts(tag_string):
"""
Filter input from incoming string containing user tags,
@@ -73,6 +76,7 @@ def convert_to_tag_list_of_dicts(tag_string):
'slug': url.slugify(tag.strip())})
return taglist
+
def media_tags_as_string(media_entry_tags):
"""
Generate a string from a media item's tags, stored as a list of dicts
@@ -85,9 +89,11 @@ def media_tags_as_string(media_entry_tags):
[tag['name'] for tag in media_entry_tags])
return media_tag_string
+
TOO_LONG_TAG_WARNING = \
u'Tags must be shorter than %s characters. Tags that are too long: %s'
+
def tag_length_validator(form, field):
"""
Make sure tags do not exceed the maximum tag length.
@@ -105,6 +111,7 @@ def tag_length_validator(form, field):
MARKDOWN_INSTANCE = markdown.Markdown(safe_mode='escape')
+
def cleaned_markdown_conversion(text):
"""
Take a block of text, run it through MarkDown, and clean its HTML.
diff --git a/mediagoblin/tools/translate.py b/mediagoblin/tools/translate.py
index 2c2a710d..b99c4aa4 100644
--- a/mediagoblin/tools/translate.py
+++ b/mediagoblin/tools/translate.py
@@ -65,8 +65,6 @@ def get_locale_from_request(request):
if request_form.has_key('lang'):
return locale_to_lower_upper(request_form['lang'])
- accept_lang_matches = request.accept_language.best_matches()
-
# Your routing can explicitly specify a target language
matchdict = request.matchdict or {}
@@ -74,12 +72,19 @@ def get_locale_from_request(request):
target_lang = matchdict['locale']
elif request.session.has_key('target_lang'):
target_lang = request.session['target_lang']
- # Pull the first acceptable language
- elif accept_lang_matches:
- target_lang = accept_lang_matches[0]
- # Fall back to English
+ # Pull the first acceptable language or English
else:
- target_lang = 'en'
+ # WebOb recently changed how it handles determining best language.
+ # Here's a compromise commit that handles either/or...
+ if hasattr(request.accept_language, "best_matches"):
+ accept_lang_matches = request.accept_language.best_matches()
+ if accept_lang_matches:
+ target_lang = accept_lang_matches[0]
+ else:
+ target_lang = 'en'
+ else:
+ target_lang = request.accept.best_match(
+ request.accept_language, 'en')
return locale_to_lower_upper(target_lang)
diff --git a/mediagoblin/tools/url.py b/mediagoblin/tools/url.py
index 458ef2c8..78b5dd63 100644
--- a/mediagoblin/tools/url.py
+++ b/mediagoblin/tools/url.py
@@ -17,8 +17,10 @@
import re
import translitcodec
+
_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
+
def slugify(text, delim=u'-'):
"""
Generates an ASCII-only slug. Taken from http://flask.pocoo.org/snippets/5/