aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/gmg_commands/batchaddmedia.py
diff options
context:
space:
mode:
Diffstat (limited to 'mediagoblin/gmg_commands/batchaddmedia.py')
-rw-r--r--mediagoblin/gmg_commands/batchaddmedia.py60
1 files changed, 33 insertions, 27 deletions
diff --git a/mediagoblin/gmg_commands/batchaddmedia.py b/mediagoblin/gmg_commands/batchaddmedia.py
index 4931bda2..274d72bc 100644
--- a/mediagoblin/gmg_commands/batchaddmedia.py
+++ b/mediagoblin/gmg_commands/batchaddmedia.py
@@ -14,11 +14,18 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-import os
-import requests, codecs
+from __future__ import print_function
+
+import codecs
import csv
-from urlparse import urlparse
+import os
+import requests
+import six
+
+from six.moves.urllib.parse import urlparse
+
+from mediagoblin.db.models import LocalUser
from mediagoblin.gmg_commands import util as commands_util
from mediagoblin.submit.lib import (
submit_media, get_upload_file_limits,
@@ -58,13 +65,14 @@ def batchaddmedia(args):
files_uploaded, files_attempted = 0, 0
# get the user
- user = app.db.User.query.filter_by(username=args.username.lower()).first()
+ user = app.db.LocalUser.query.filter(
+ LocalUser.username==args.username.lower()
+ ).first()
if user is None:
- print _(u"Sorry, no user by username '{username}' exists".format(
- username=args.username))
+ print(_(u"Sorry, no user by username '{username}' exists".format(
+ username=args.username)))
return
- upload_limit, max_file_size = get_upload_file_limits(user)
temp_files = []
if os.path.isfile(args.metadata_path):
@@ -73,19 +81,18 @@ def batchaddmedia(args):
else:
error = _(u'File at {path} not found, use -h flag for help'.format(
path=args.metadata_path))
- print error
+ print(error)
return
abs_metadata_filename = os.path.abspath(metadata_path)
abs_metadata_dir = os.path.dirname(abs_metadata_filename)
- upload_limit, max_file_size = get_upload_file_limits(user)
def maybe_unicodeify(some_string):
# this is kinda terrible
if some_string is None:
return None
else:
- return unicode(some_string)
+ return six.text_type(some_string)
with codecs.open(
abs_metadata_filename, 'r', encoding='utf-8') as all_metadata:
@@ -110,13 +117,13 @@ def batchaddmedia(args):
license = file_metadata.get('license')
try:
json_ld_metadata = compact_and_validate(file_metadata)
- except ValidationError, exc:
+ except ValidationError as exc:
error = _(u"""Error with media '{media_id}' value '{error_path}': {error_msg}
Metadata was not uploaded.""".format(
media_id=media_id,
error_path=exc.path[0],
error_msg=exc.message))
- print error
+ print(error)
continue
url = urlparse(original_location)
@@ -136,9 +143,9 @@ Metadata was not uploaded.""".format(
try:
media_file = file(file_abs_path, 'r')
except IOError:
- print _(u"""\
+ print(_(u"""\
FAIL: Local file {filename} could not be accessed.
-{filename} will not be uploaded.""".format(filename=filename))
+{filename} will not be uploaded.""".format(filename=filename)))
continue
try:
submit_media(
@@ -150,33 +157,33 @@ FAIL: Local file {filename} could not be accessed.
description=maybe_unicodeify(description),
license=maybe_unicodeify(license),
metadata=json_ld_metadata,
- tags_string=u"",
- upload_limit=upload_limit, max_file_size=max_file_size)
- print _(u"""Successfully submitted {filename}!
+ tags_string=u"")
+ print(_(u"""Successfully submitted {filename}!
Be sure to look at the Media Processing Panel on your website to be sure it
-uploaded successfully.""".format(filename=filename))
+uploaded successfully.""".format(filename=filename)))
files_uploaded += 1
except FileUploadLimit:
- print _(
-u"FAIL: This file is larger than the upload limits for this site.")
+ print(_(
+u"FAIL: This file is larger than the upload limits for this site."))
except UserUploadLimit:
- print _(
-"FAIL: This file will put this user past their upload limits.")
+ print(_(
+"FAIL: This file will put this user past their upload limits."))
except UserPastUploadLimit:
- print _("FAIL: This user is already past their upload limits.")
- print _(
+ print(_("FAIL: This user is already past their upload limits."))
+ print(_(
"{files_uploaded} out of {files_attempted} files successfully submitted".format(
files_uploaded=files_uploaded,
- files_attempted=files_attempted))
+ files_attempted=files_attempted)))
def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
# csv.py doesn't do Unicode; encode temporarily as UTF-8:
+ # TODO: this probably won't be necessary in Python 3
csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
dialect=dialect, **kwargs)
for row in csv_reader:
# decode UTF-8 back to Unicode, cell by cell:
- yield [unicode(cell, 'utf-8') for cell in row]
+ yield [six.text_type(cell, 'utf-8') for cell in row]
def utf_8_encoder(unicode_csv_data):
for line in unicode_csv_data:
@@ -203,4 +210,3 @@ def parse_csv_file(file_contents):
objects_dict[media_id] = (line_dict)
return objects_dict
-