From: Berker Peksag Date: Mon, 2 Jun 2014 17:59:28 +0000 (+0300) Subject: Use six.text_type instead of unicode(). X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=e49b7e02b2150413d2e78db709277fae0887be46;p=mediagoblin.git Use six.text_type instead of unicode(). I will be switch to use ``from __future__ import unicode_literals`` later. --- diff --git a/mediagoblin/auth/tools.py b/mediagoblin/auth/tools.py index 88716e1c..8c919498 100644 --- a/mediagoblin/auth/tools.py +++ b/mediagoblin/auth/tools.py @@ -16,6 +16,8 @@ import logging + +import six import wtforms from sqlalchemy import or_ @@ -140,7 +142,7 @@ def register_user(request, register_form): user.save() # log the user in - request.session['user_id'] = unicode(user.id) + request.session['user_id'] = six.text_type(user.id) request.session.save() # send verification email diff --git a/mediagoblin/auth/views.py b/mediagoblin/auth/views.py index 3d132f84..a90db0ea 100644 --- a/mediagoblin/auth/views.py +++ b/mediagoblin/auth/views.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import six + from itsdangerous import BadSignature from mediagoblin import messages, mg_globals @@ -93,7 +95,7 @@ def login(request): # set up login in session if login_form.stay_logged_in.data: request.session['stay_logged_in'] = True - request.session['user_id'] = unicode(user.id) + request.session['user_id'] = six.text_type(user.id) request.session.save() if request.form.get('next'): diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py index 426080a2..dd96c3f3 100644 --- a/mediagoblin/db/migrations.py +++ b/mediagoblin/db/migrations.py @@ -17,6 +17,8 @@ import datetime import uuid +import six + from sqlalchemy import (MetaData, Table, Column, Boolean, SmallInteger, Integer, Unicode, UnicodeText, DateTime, ForeignKey, Date) @@ -248,7 +250,7 @@ def mediaentry_new_slug_era(db): for row in db.execute(media_table.select()): # no slug, try setting to an id if not row.slug: - append_garbage_till_unique(row, unicode(row.id)) + append_garbage_till_unique(row, six.text_type(row.id)) # has "=" or ":" in it... we're getting rid of those elif u"=" in row.slug or u":" in row.slug: append_garbage_till_unique( @@ -277,7 +279,7 @@ def unique_collections_slug(db): existing_slugs[row.creator].append(row.slug) for row_id in slugs_to_change: - new_slug = unicode(uuid.uuid4()) + new_slug = six.text_type(uuid.uuid4()) db.execute(collection_table.update(). where(collection_table.c.id == row_id). values(slug=new_slug)) diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py index 1fecbaaa..4b2fb632 100644 --- a/mediagoblin/db/models.py +++ b/mediagoblin/db/models.py @@ -304,7 +304,7 @@ class MediaEntry(Base, MediaEntryMixin): return the value of the key. """ media_file = MediaFile.query.filter_by(media_entry=self.id, - name=unicode(file_key)).first() + name=six.text_type(file_key)).first() if media_file: if metadata_key: @@ -317,7 +317,7 @@ class MediaEntry(Base, MediaEntryMixin): Update the file_metadata of a MediaFile. """ media_file = MediaFile.query.filter_by(media_entry=self.id, - name=unicode(file_key)).first() + name=six.text_type(file_key)).first() file_metadata = media_file.file_metadata or {} diff --git a/mediagoblin/edit/views.py b/mediagoblin/edit/views.py index 80590875..52f808b6 100644 --- a/mediagoblin/edit/views.py +++ b/mediagoblin/edit/views.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import six + from datetime import datetime from itsdangerous import BadSignature @@ -77,7 +79,7 @@ def edit_media(request, media): media.tags = convert_to_tag_list_of_dicts( form.tags.data) - media.license = unicode(form.license.data) or None + media.license = six.text_type(form.license.data) or None media.slug = slug media.save() @@ -135,7 +137,7 @@ def edit_attachments(request, media): attachment_public_filepath \ = mg_globals.public_store.get_unique_filepath( - ['media_entries', unicode(media.id), 'attachment', + ['media_entries', six.text_type(media.id), 'attachment', public_filename]) attachment_public_file = mg_globals.public_store.get_file( @@ -200,8 +202,8 @@ def edit_profile(request, url_user=None): bio=user.bio) if request.method == 'POST' and form.validate(): - user.url = unicode(form.url.data) - user.bio = unicode(form.bio.data) + user.url = six.text_type(form.url.data) + user.bio = six.text_type(form.bio.data) user.save() @@ -316,9 +318,9 @@ def edit_collection(request, collection): form.slug.errors.append( _(u'A collection with that slug already exists for this user.')) else: - collection.title = unicode(form.title.data) - collection.description = unicode(form.description.data) - collection.slug = unicode(form.slug.data) + collection.title = six.text_type(form.title.data) + collection.description = six.text_type(form.description.data) + collection.slug = six.text_type(form.slug.data) collection.save() diff --git a/mediagoblin/gmg_commands/addmedia.py b/mediagoblin/gmg_commands/addmedia.py index c33a8c56..34add88b 100644 --- a/mediagoblin/gmg_commands/addmedia.py +++ b/mediagoblin/gmg_commands/addmedia.py @@ -14,8 +14,12 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from __future__ import print_function + import os +import six + from mediagoblin.gmg_commands import util as commands_util from mediagoblin.submit.lib import ( submit_media, get_upload_file_limits, @@ -68,14 +72,14 @@ def addmedia(args): # get the user user = app.db.User.query.filter_by(username=args.username.lower()).first() if user is None: - print "Sorry, no user by username '%s'" % args.username + print("Sorry, no user by username '%s'" % args.username) return # check for the file, if it exists... filename = os.path.split(args.filename)[-1] abs_filename = os.path.abspath(args.filename) if not os.path.exists(abs_filename): - print "Can't find a file with filename '%s'" % args.filename + print("Can't find a file with filename '%s'" % args.filename) return upload_limit, max_file_size = get_upload_file_limits(user) @@ -85,7 +89,7 @@ def addmedia(args): if some_string is None: return None else: - return unicode(some_string) + return six.text_type(some_string) try: submit_media( @@ -98,8 +102,8 @@ def addmedia(args): tags_string=maybe_unicodeify(args.tags) or u"", upload_limit=upload_limit, max_file_size=max_file_size) except FileUploadLimit: - print "This file is larger than the upload limits for this site." + print("This file is larger than the upload limits for this site.") except UserUploadLimit: - print "This file will put this user past their upload limits." + print("This file will put this user past their upload limits.") except UserPastUploadLimit: - print "This user is already past their upload limits." + print("This user is already past their upload limits.") diff --git a/mediagoblin/gmg_commands/users.py b/mediagoblin/gmg_commands/users.py index 4a730d9e..9014da87 100644 --- a/mediagoblin/gmg_commands/users.py +++ b/mediagoblin/gmg_commands/users.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import six + from mediagoblin.gmg_commands import util as commands_util from mediagoblin import auth from mediagoblin import mg_globals @@ -50,8 +52,8 @@ def adduser(args): else: # Create the user entry = db.User() - entry.username = unicode(args.username.lower()) - entry.email = unicode(args.email) + entry.username = six.text_type(args.username.lower()) + entry.email = six.text_type(args.email) entry.pw_hash = auth.gen_password_hash(args.password) default_privileges = [ db.Privilege.query.filter( @@ -81,7 +83,7 @@ def makeadmin(args): db = mg_globals.database user = db.User.query.filter_by( - username=unicode(args.username.lower())).one() + username=six.text_type(args.username.lower())).one() if user: user.all_privileges.append( db.Privilege.query.filter( @@ -108,7 +110,7 @@ def changepw(args): db = mg_globals.database user = db.User.query.filter_by( - username=unicode(args.username.lower())).one() + username=six.text_type(args.username.lower())).one() if user: user.pw_hash = auth.gen_password_hash(args.password) user.save() diff --git a/mediagoblin/media_types/ascii/processing.py b/mediagoblin/media_types/ascii/processing.py index 84030362..6f388573 100644 --- a/mediagoblin/media_types/ascii/processing.py +++ b/mediagoblin/media_types/ascii/processing.py @@ -22,6 +22,8 @@ except ImportError: import Image import logging +import six + from mediagoblin import mg_globals as mgg from mediagoblin.processing import ( create_pub_filepath, FilenameBuilder, @@ -104,7 +106,7 @@ class CommonAsciiProcessor(MediaProcessor): # Encode the unicode instance to ASCII and replace any # non-ASCII with an HTML entity (&# unicode_file.write( - unicode(orig_file.read().decode( + six.text_type(orig_file.read().decode( self.charset)).encode( 'ascii', 'xmlcharrefreplace')) diff --git a/mediagoblin/media_types/blog/views.py b/mediagoblin/media_types/blog/views.py index a367bef8..3a4dfe6a 100644 --- a/mediagoblin/media_types/blog/views.py +++ b/mediagoblin/media_types/blog/views.py @@ -19,6 +19,8 @@ _log = logging.getLogger(__name__) from datetime import datetime +import six + from werkzeug.exceptions import Forbidden from mediagoblin.tools import pluginapi @@ -75,8 +77,8 @@ def blog_edit(request): if request.method=='POST' and form.validate(): _log.info("Here") blog = request.db.Blog() - blog.title = unicode(form.title.data) - blog.description = unicode(cleaned_markdown_conversion((form.description.data))) + blog.title = six.text_type(form.title.data) + blog.description = six.text_type(cleaned_markdown_conversion((form.description.data))) blog.author = request.user.id blog.generate_slug() @@ -112,8 +114,8 @@ def blog_edit(request): 'app_config': mg_globals.app_config}) else: if request.method == 'POST' and form.validate(): - blog.title = unicode(form.title.data) - blog.description = unicode(cleaned_markdown_conversion((form.description.data))) + blog.title = six.text_type(form.title.data) + blog.description = six.text_type(cleaned_markdown_conversion((form.description.data))) blog.author = request.user.id blog.generate_slug() @@ -137,10 +139,10 @@ def blogpost_create(request): blogpost = request.db.MediaEntry() blogpost.media_type = 'mediagoblin.media_types.blogpost' - blogpost.title = unicode(form.title.data) - blogpost.description = unicode(cleaned_markdown_conversion((form.description.data))) + blogpost.title = six.text_type(form.title.data) + blogpost.description = six.text_type(cleaned_markdown_conversion((form.description.data))) blogpost.tags = convert_to_tag_list_of_dicts(form.tags.data) - blogpost.license = unicode(form.license.data) or None + blogpost.license = six.text_type(form.license.data) or None blogpost.uploader = request.user.id blogpost.generate_slug() @@ -187,10 +189,10 @@ def blogpost_edit(request): form = blog_forms.BlogPostEditForm(request.form, **defaults) if request.method == 'POST' and form.validate(): - blogpost.title = unicode(form.title.data) - blogpost.description = unicode(cleaned_markdown_conversion((form.description.data))) + blogpost.title = six.text_type(form.title.data) + blogpost.description = six.text_type(cleaned_markdown_conversion((form.description.data))) blogpost.tags = convert_to_tag_list_of_dicts(form.tags.data) - blogpost.license = unicode(form.license.data) + blogpost.license = six.text_type(form.license.data) set_blogpost_state(request, blogpost) blogpost.generate_slug() blogpost.save() diff --git a/mediagoblin/media_types/image/processing.py b/mediagoblin/media_types/image/processing.py index a9b966ff..ae9ece24 100644 --- a/mediagoblin/media_types/image/processing.py +++ b/mediagoblin/media_types/image/processing.py @@ -24,6 +24,8 @@ import os import logging import argparse +import six + from mediagoblin import mg_globals as mgg from mediagoblin.processing import ( BadMediaFail, FilenameBuilder, @@ -67,7 +69,7 @@ def resize_image(entry, resized, keyname, target_name, new_size, resize_filter = PIL_FILTERS[filter.upper()] except KeyError: raise Exception('Filter "{0}" not found, choose one of {1}'.format( - unicode(filter), + six.text_type(filter), u', '.join(PIL_FILTERS.keys()))) resized.thumbnail(new_size, resize_filter) @@ -116,7 +118,7 @@ def resize_tool(entry, or im.size[1] > new_size[1]\ or exif_image_needs_rotation(exif_tags): resize_image( - entry, im, unicode(keyname), target_name, + entry, im, six.text_type(keyname), target_name, tuple(new_size), exif_tags, conversions_subdir, quality, filter) diff --git a/mediagoblin/plugins/api/views.py b/mediagoblin/plugins/api/views.py index 69fae7bb..a9aaacee 100644 --- a/mediagoblin/plugins/api/views.py +++ b/mediagoblin/plugins/api/views.py @@ -17,6 +17,8 @@ import json import logging +import six + from werkzeug.exceptions import BadRequest from werkzeug.wrappers import Response @@ -55,15 +57,15 @@ def post_entry(request): callback_url = request.form.get('callback_url') if callback_url: - callback_url = unicode(callback_url) + callback_url = six.text_type(callback_url) try: entry = submit_media( mg_app=request.app, user=request.user, submitted_file=request.files['file'], filename=request.files['file'].filename, - title=unicode(request.form.get('title')), - description=unicode(request.form.get('description')), - license=unicode(request.form.get('license', '')), + title=six.text_type(request.form.get('title')), + description=six.text_type(request.form.get('description')), + license=six.text_type(request.form.get('license', '')), upload_limit=upload_limit, max_file_size=max_file_size, callback_url=callback_url) @@ -88,7 +90,7 @@ def post_entry(request): ''' if isinstance(e, InvalidFileType) or \ isinstance(e, FileTypeNotSupported): - raise BadRequest(unicode(e)) + raise BadRequest(six.text_type(e)) else: raise diff --git a/mediagoblin/plugins/basic_auth/tools.py b/mediagoblin/plugins/basic_auth/tools.py index f943bf39..13f240b2 100644 --- a/mediagoblin/plugins/basic_auth/tools.py +++ b/mediagoblin/plugins/basic_auth/tools.py @@ -16,6 +16,8 @@ import bcrypt import random +import six + from mediagoblin import mg_globals from mediagoblin.tools.crypto import get_timed_signer_url from mediagoblin.tools.mail import send_email @@ -66,7 +68,7 @@ def bcrypt_gen_password_hash(raw_pass, extra_salt=None): if extra_salt: raw_pass = u"%s:%s" % (extra_salt, raw_pass) - return unicode( + return six.text_type( bcrypt.hashpw(raw_pass.encode('utf-8'), bcrypt.gensalt())) diff --git a/mediagoblin/plugins/httpapiauth/__init__.py b/mediagoblin/plugins/httpapiauth/__init__.py index 2b2d593c..d7180463 100644 --- a/mediagoblin/plugins/httpapiauth/__init__.py +++ b/mediagoblin/plugins/httpapiauth/__init__.py @@ -16,6 +16,8 @@ import logging +import six + from werkzeug.exceptions import Unauthorized from mediagoblin.auth.tools import check_login_simple @@ -40,7 +42,7 @@ class HTTPAuth(Auth): if not request.authorization: return False - user = check_login_simple(unicode(request.authorization['username']), + user = check_login_simple(six.text_type(request.authorization['username']), request.authorization['password']) if user: diff --git a/mediagoblin/plugins/ldap/views.py b/mediagoblin/plugins/ldap/views.py index aef1bf56..be434daf 100644 --- a/mediagoblin/plugins/ldap/views.py +++ b/mediagoblin/plugins/ldap/views.py @@ -13,6 +13,9 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . + +import six + from mediagoblin import mg_globals, messages from mediagoblin.auth.tools import register_user from mediagoblin.db.models import User @@ -40,7 +43,7 @@ def login(request): if user: # set up login in session - request.session['user_id'] = unicode(user.id) + request.session['user_id'] = six.text_type(user.id) request.session.save() if request.form.get('next'): diff --git a/mediagoblin/plugins/oauth/tools.py b/mediagoblin/plugins/oauth/tools.py index af0a3305..2053d5d4 100644 --- a/mediagoblin/plugins/oauth/tools.py +++ b/mediagoblin/plugins/oauth/tools.py @@ -23,6 +23,8 @@ from datetime import datetime from functools import wraps +import six + from mediagoblin.tools.response import json_response @@ -86,7 +88,7 @@ def create_token(client, user): def generate_identifier(): ''' Generates a ``uuid.uuid4()`` ''' - return unicode(uuid.uuid4()) + return six.text_type(uuid.uuid4()) def generate_token(): @@ -110,5 +112,5 @@ def generate_secret(): ''' # XXX: We might not want it to use bcrypt, since bcrypt takes its time to # generate the result. - return unicode(getrandbits(192)) + return six.text_type(getrandbits(192)) diff --git a/mediagoblin/plugins/oauth/views.py b/mediagoblin/plugins/oauth/views.py index de637d6b..9e3b87c9 100644 --- a/mediagoblin/plugins/oauth/views.py +++ b/mediagoblin/plugins/oauth/views.py @@ -19,6 +19,8 @@ import logging from urllib import urlencode +import six + from werkzeug.exceptions import BadRequest from mediagoblin.tools.response import render_to_response, redirect, json_response @@ -44,11 +46,11 @@ def register_client(request): if request.method == 'POST' and form.validate(): client = OAuthClient() - client.name = unicode(form.name.data) - client.description = unicode(form.description.data) - client.type = unicode(form.type.data) + client.name = six.text_type(form.name.data) + client.description = six.text_type(form.description.data) + client.type = six.text_type(form.type.data) client.owner_id = request.user.id - client.redirect_uri = unicode(form.redirect_uri.data) + client.redirect_uri = six.text_type(form.redirect_uri.data) client.save() diff --git a/mediagoblin/plugins/openid/store.py b/mediagoblin/plugins/openid/store.py index 8f9a7012..24726814 100644 --- a/mediagoblin/plugins/openid/store.py +++ b/mediagoblin/plugins/openid/store.py @@ -16,6 +16,8 @@ import base64 import time +import six + from openid.association import Association as OIDAssociation from openid.store.interface import OpenIDStore from openid.store import nonce @@ -34,12 +36,12 @@ class SQLAlchemyOpenIDStore(OpenIDStore): if not assoc: assoc = Association() - assoc.server_url = unicode(server_url) + assoc.server_url = six.text_type(server_url) assoc.handle = association.handle # django uses base64 encoding, python-openid uses a blob field for # secret - assoc.secret = unicode(base64.encodestring(association.secret)) + assoc.secret = six.text_type(base64.encodestring(association.secret)) assoc.issued = association.issued assoc.lifetime = association.lifetime assoc.assoc_type = association.assoc_type diff --git a/mediagoblin/plugins/openid/views.py b/mediagoblin/plugins/openid/views.py index bb2de7ab..71f444fa 100644 --- a/mediagoblin/plugins/openid/views.py +++ b/mediagoblin/plugins/openid/views.py @@ -13,6 +13,9 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . + +import six + from openid.consumer import consumer from openid.consumer.discover import DiscoveryFailure from openid.extensions.sreg import SRegRequest, SRegResponse @@ -186,7 +189,7 @@ def finish_login(request): if user: # Set up login in session - request.session['user_id'] = unicode(user.id) + request.session['user_id'] = six.text_type(user.id) request.session.save() if request.session.get('next'): diff --git a/mediagoblin/plugins/persona/views.py b/mediagoblin/plugins/persona/views.py index 1bba3b8c..41d38353 100644 --- a/mediagoblin/plugins/persona/views.py +++ b/mediagoblin/plugins/persona/views.py @@ -17,6 +17,8 @@ import json import logging import requests +import six + from werkzeug.exceptions import BadRequest from mediagoblin import messages, mg_globals @@ -63,7 +65,7 @@ def login(request): user = query.user if query else None if user: - request.session['user_id'] = unicode(user.id) + request.session['user_id'] = six.text_type(user.id) request.session['persona_login_email'] = email request.session.save() diff --git a/mediagoblin/plugins/piwigo/views.py b/mediagoblin/plugins/piwigo/views.py index f913a730..1fe1e576 100644 --- a/mediagoblin/plugins/piwigo/views.py +++ b/mediagoblin/plugins/piwigo/views.py @@ -17,6 +17,8 @@ import logging import re +import six + from werkzeug.exceptions import MethodNotAllowed, BadRequest, NotImplemented from werkzeug.wrappers import BaseResponse @@ -133,8 +135,8 @@ def pwg_images_addSimple(request): mg_app=request.app, user=request.user, submitted_file=request.files['image'], filename=request.files['image'].filename, - title=unicode(form.name.data), - description=unicode(form.comment.data), + title=six.text_type(form.name.data), + description=six.text_type(form.comment.data), upload_limit=upload_limit, max_file_size=max_file_size) collection_id = form.category.data diff --git a/mediagoblin/processing/__init__.py b/mediagoblin/processing/__init__.py index 102fd5de..5a88ddea 100644 --- a/mediagoblin/processing/__init__.py +++ b/mediagoblin/processing/__init__.py @@ -24,6 +24,8 @@ except: import logging import os +import six + from mediagoblin import mg_globals as mgg from mediagoblin.db.util import atomic_update from mediagoblin.db.models import MediaEntry @@ -46,7 +48,7 @@ class ProgressCallback(object): def create_pub_filepath(entry, filename): return mgg.public_store.get_unique_filepath( ['media_entries', - unicode(entry.id), + six.text_type(entry.id), filename]) @@ -319,7 +321,7 @@ def mark_entry_failed(entry_id, exc): atomic_update(mgg.database.MediaEntry, {'id': entry_id}, {u'state': u'failed', - u'fail_error': unicode(exc.exception_path), + u'fail_error': six.text_type(exc.exception_path), u'fail_metadata': exc.metadata}) else: _log.warn("No idea what happened here, but it failed: %r", exc) diff --git a/mediagoblin/storage/__init__.py b/mediagoblin/storage/__init__.py index 12109748..8462e08e 100644 --- a/mediagoblin/storage/__init__.py +++ b/mediagoblin/storage/__init__.py @@ -224,7 +224,7 @@ def clean_listy_filepath(listy_filepath): A cleaned list of unicode objects. """ cleaned_filepath = [ - unicode(secure_filename(filepath)) + six.text_type(secure_filename(filepath)) for filepath in listy_filepath] if u'' in cleaned_filepath: diff --git a/mediagoblin/submit/lib.py b/mediagoblin/submit/lib.py index c70e2731..7d3ea8df 100644 --- a/mediagoblin/submit/lib.py +++ b/mediagoblin/submit/lib.py @@ -18,6 +18,8 @@ import logging import uuid from os.path import splitext +import six + from werkzeug.utils import secure_filename from werkzeug.datastructures import FileStorage @@ -127,7 +129,7 @@ def submit_media(mg_app, user, submitted_file, filename, # If the filename contains non ascii generate a unique name if not all(ord(c) < 128 for c in filename): - filename = unicode(uuid.uuid4()) + splitext(filename)[-1] + filename = six.text_type(uuid.uuid4()) + splitext(filename)[-1] # Sniff the submitted media to determine which # media plugin should handle processing @@ -136,7 +138,7 @@ def submit_media(mg_app, user, submitted_file, filename, # create entry and save in database entry = new_upload_entry(user) entry.media_type = media_type - entry.title = (title or unicode(splitext(filename)[0])) + entry.title = (title or six.text_type(splitext(filename)[0])) entry.description = description or u"" @@ -210,7 +212,7 @@ def prepare_queue_task(app, entry, filename): # (If we got it off the task's auto-generation, there'd be # a risk of a race condition when we'd save after sending # off the task) - task_id = unicode(uuid.uuid4()) + task_id = six.text_type(uuid.uuid4()) entry.queued_task_id = task_id # Now store generate the queueing related filename diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py index 42c378a8..b0588599 100644 --- a/mediagoblin/submit/views.py +++ b/mediagoblin/submit/views.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import six + from mediagoblin import messages import mediagoblin.mg_globals as mg_globals @@ -59,9 +61,9 @@ def submit_start(request): mg_app=request.app, user=request.user, submitted_file=request.files['file'], filename=request.files['file'].filename, - title=unicode(submit_form.title.data), - description=unicode(submit_form.description.data), - license=unicode(submit_form.license.data) or None, + title=six.text_type(submit_form.title.data), + description=six.text_type(submit_form.description.data), + license=six.text_type(submit_form.license.data) or None, tags_string=submit_form.tags.data, upload_limit=upload_limit, max_file_size=max_file_size, urlgen=request.urlgen) @@ -117,8 +119,8 @@ def add_collection(request, media=None): if request.method == 'POST' and submit_form.validate(): collection = request.db.Collection() - collection.title = unicode(submit_form.title.data) - collection.description = unicode(submit_form.description.data) + collection.title = six.text_type(submit_form.title.data) + collection.description = six.text_type(submit_form.description.data) collection.creator = request.user.id collection.generate_slug() diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py index e8fd76d8..9b7f9825 100644 --- a/mediagoblin/tests/test_auth.py +++ b/mediagoblin/tests/test_auth.py @@ -18,6 +18,8 @@ import pkg_resources import pytest +import six + import six.moves.urllib.parse as urlparse from mediagoblin import mg_globals @@ -109,7 +111,7 @@ def test_register_views(test_app): ## Make sure user is logged in request = template.TEMPLATE_TEST_CONTEXT[ 'mediagoblin/user_pages/user_nonactive.html']['request'] - assert request.session['user_id'] == unicode(new_user.id) + assert request.session['user_id'] == six.text_type(new_user.id) ## Make sure we get email confirmation, and try verifying assert len(mail.EMAIL_TEST_INBOX) == 1 @@ -307,7 +309,7 @@ def test_authentication_views(test_app): # Make sure user is in the session context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] session = context['request'].session - assert session['user_id'] == unicode(test_user.id) + assert session['user_id'] == six.text_type(test_user.id) # Successful logout # ----------------- diff --git a/mediagoblin/tests/test_http_callback.py b/mediagoblin/tests/test_http_callback.py index 64b7ee8f..d0a8c823 100644 --- a/mediagoblin/tests/test_http_callback.py +++ b/mediagoblin/tests/test_http_callback.py @@ -17,6 +17,8 @@ import json import pytest +import six + from urlparse import urlparse, parse_qs from mediagoblin import mg_globals @@ -63,7 +65,7 @@ class TestHTTPCallback(object): code = parse_qs(urlparse(redirect.location).query)['code'][0] client = self.db.OAuthClient.query.filter( - self.db.OAuthClient.identifier == unicode(client_id)).first() + self.db.OAuthClient.identifier == six.text_type(client_id)).first() client_secret = client.secret diff --git a/mediagoblin/tests/test_ldap.py b/mediagoblin/tests/test_ldap.py index 33cf85d6..e69a3203 100644 --- a/mediagoblin/tests/test_ldap.py +++ b/mediagoblin/tests/test_ldap.py @@ -17,6 +17,7 @@ import pkg_resources import pytest import mock +import six import six.moves.urllib.parse as urlparse @@ -122,6 +123,6 @@ def test_ldap_plugin(ldap_plugin_app): # Make sure user is in the session context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] session = context['request'].session - assert session['user_id'] == unicode(test_user.id) + assert session['user_id'] == six.text_type(test_user.id) _test_authentication() diff --git a/mediagoblin/tests/test_oauth2.py b/mediagoblin/tests/test_oauth2.py index 957f4e65..6bdb729e 100644 --- a/mediagoblin/tests/test_oauth2.py +++ b/mediagoblin/tests/test_oauth2.py @@ -18,6 +18,8 @@ import json import logging import pytest +import six + from urlparse import parse_qs, urlparse from mediagoblin import mg_globals @@ -154,7 +156,7 @@ class TestOAuth(object): code = self.get_code_from_redirect_uri(code_redirect.location) client = self.db.OAuthClient.query.filter( - self.db.OAuthClient.identifier == unicode(client_id)).first() + self.db.OAuthClient.identifier == six.text_type(client_id)).first() token_res = self.test_app.get('/oauth-2/access_token?client_id={0}&\ code={1}&client_secret={2}'.format(client_id, code, client.secret)) @@ -182,7 +184,7 @@ code={1}&client_secret={2}'.format(client_id, code, client.secret)) code = self.get_code_from_redirect_uri(code_redirect.location) client = self.db.OAuthClient.query.filter( - self.db.OAuthClient.identifier == unicode(client_id)).first() + self.db.OAuthClient.identifier == six.text_type(client_id)).first() token_res = self.test_app.get('/oauth-2/access_token?\ code={0}&client_secret={1}'.format(code, client.secret)) diff --git a/mediagoblin/tests/test_openid.py b/mediagoblin/tests/test_openid.py index 0424fdda..7ef01052 100644 --- a/mediagoblin/tests/test_openid.py +++ b/mediagoblin/tests/test_openid.py @@ -18,6 +18,7 @@ import urlparse import pkg_resources import pytest import mock +import six openid_consumer = pytest.importorskip( "openid.consumer.consumer") @@ -206,7 +207,7 @@ class TestOpenIDPlugin(object): # Make sure user is in the session context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] session = context['request'].session - assert session['user_id'] == unicode(test_user.id) + assert session['user_id'] == six.text_type(test_user.id) _test_new_user() diff --git a/mediagoblin/tests/test_persona.py b/mediagoblin/tests/test_persona.py index dbc4797a..f2dd4001 100644 --- a/mediagoblin/tests/test_persona.py +++ b/mediagoblin/tests/test_persona.py @@ -17,6 +17,7 @@ import pkg_resources import pytest import mock +import six import six.moves.urllib.parse as urlparse @@ -142,7 +143,7 @@ class TestPersonaPlugin(object): # Make sure user is in the session context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] session = context['request'].session - assert session['user_id'] == unicode(test_user.id) + assert session['user_id'] == six.text_type(test_user.id) _test_registration() diff --git a/mediagoblin/tests/test_reporting.py b/mediagoblin/tests/test_reporting.py index a154a061..6a9fe205 100644 --- a/mediagoblin/tests/test_reporting.py +++ b/mediagoblin/tests/test_reporting.py @@ -15,6 +15,7 @@ # along with this program. If not, see . import pytest +import six from mediagoblin.tools import template from mediagoblin.tests.tools import (fixture_add_user, fixture_media_entry, @@ -75,7 +76,7 @@ class TestReportFiling: response, context = self.do_post( {'report_reason':u'Testing Media Report', - 'reporter_id':unicode(allie_id)},url= media_uri_slug + "report/") + 'reporter_id':six.text_type(allie_id)},url= media_uri_slug + "report/") assert response.status == "302 FOUND" @@ -110,7 +111,7 @@ class TestReportFiling: response, context = self.do_post({ 'report_reason':u'Testing Comment Report', - 'reporter_id':unicode(allie_id)},url= comment_uri_slug + "report/") + 'reporter_id':six.text_type(allie_id)},url= comment_uri_slug + "report/") assert response.status == "302 FOUND" diff --git a/mediagoblin/tests/test_storage.py b/mediagoblin/tests/test_storage.py index f6f1d18f..21947415 100644 --- a/mediagoblin/tests/test_storage.py +++ b/mediagoblin/tests/test_storage.py @@ -19,6 +19,8 @@ import os import tempfile import pytest +import six + from werkzeug.utils import secure_filename from mediagoblin import storage @@ -78,7 +80,7 @@ def test_storage_system_from_config(): 'mediagoblin.tests.test_storage:FakeStorageSystem'}) assert this_storage.foobie == 'eiboof' assert this_storage.blech == 'hcelb' - assert unicode(this_storage.__class__) == \ + assert six.text_type(this_storage.__class__) == \ u'mediagoblin.tests.test_storage.FakeStorageSystem' diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index 5ddd882c..6143c0ac 100644 --- a/mediagoblin/tests/test_submission.py +++ b/mediagoblin/tests/test_submission.py @@ -20,6 +20,7 @@ sys.setdefaultencoding('utf-8') import os import pytest +import six import six.moves.urllib.parse as urlparse @@ -35,7 +36,7 @@ from .resources import GOOD_JPG, GOOD_PNG, EVIL_FILE, EVIL_JPG, EVIL_PNG, \ BIG_BLUE, GOOD_PDF, GPS_JPG, MED_PNG, BIG_PNG GOOD_TAG_STRING = u'yin,yang' -BAD_TAG_STRING = unicode('rage,' + 'f' * 26 + 'u' * 26) +BAD_TAG_STRING = six.text_type('rage,' + 'f' * 26 + 'u' * 26) FORM_CONTEXT = ['mediagoblin/submit/start.html', 'submit_form'] REQUEST_CONTEXT = ['mediagoblin/user_pages/user.html', 'request'] diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py index 9d9b1c16..e108e9bb 100644 --- a/mediagoblin/tests/test_util.py +++ b/mediagoblin/tests/test_util.py @@ -16,6 +16,8 @@ import email +import six + from mediagoblin.tools import common, url, translate, mail, text, testing testing._activate_testing() @@ -117,13 +119,13 @@ def test_gettext_lazy_proxy(): orig = u"Password" set_thread_locale("es") - p1 = unicode(proxy) + p1 = six.text_type(proxy) p1_should = pass_to_ugettext(orig) assert p1_should != orig, "Test useless, string not translated" assert p1 == p1_should set_thread_locale("sv") - p2 = unicode(proxy) + p2 = six.text_type(proxy) p2_should = pass_to_ugettext(orig) assert p2_should != orig, "Test broken, string not translated" assert p2 == p2_should diff --git a/mediagoblin/tools/url.py b/mediagoblin/tools/url.py index 657c0373..4d97247a 100644 --- a/mediagoblin/tools/url.py +++ b/mediagoblin/tools/url.py @@ -17,6 +17,8 @@ import re from unidecode import unidecode +import six + _punct_re = re.compile(r'[\t !"#:$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+') @@ -27,4 +29,4 @@ def slugify(text, delim=u'-'): result = [] for word in _punct_re.split(text.lower()): result.extend(unidecode(word).split()) - return unicode(delim.join(result)) + return six.text_type(delim.join(result)) diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index 78751a28..1f0b9dcd 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -18,6 +18,8 @@ import logging import datetime import json +import six + from mediagoblin import messages, mg_globals from mediagoblin.db.models import (MediaEntry, MediaTag, Collection, CollectionItem, User) @@ -178,7 +180,7 @@ def media_post_comment(request, media): comment = request.db.MediaComment() comment.media_entry = media.id comment.author = request.user.id - comment.content = unicode(request.form['comment_content']) + comment.content = six.text_type(request.form['comment_content']) # Show error message if commenting is disabled. if not mg_globals.app_config['allow_comments']: @@ -212,7 +214,7 @@ def media_preview_comment(request): if not request.is_xhr: return render_404(request) - comment = unicode(request.form['comment_content']) + comment = six.text_type(request.form['comment_content']) cleancomment = { "content":cleaned_markdown_conversion(comment)} return Response(json.dumps(cleancomment))