views.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import six
  17. from mediagoblin import messages
  18. import mediagoblin.mg_globals as mg_globals
  19. import logging
  20. _log = logging.getLogger(__name__)
  21. from mediagoblin.tools.translate import pass_to_ugettext as _
  22. from mediagoblin.tools.response import render_to_response, redirect
  23. from mediagoblin.decorators import require_active_login, user_has_privilege
  24. from mediagoblin.submit import forms as submit_forms
  25. from mediagoblin.messages import add_message, SUCCESS
  26. from mediagoblin.media_types import FileTypeNotSupported
  27. from mediagoblin.submit.lib import \
  28. check_file_field, submit_media, get_upload_file_limits, \
  29. FileUploadLimit, UserUploadLimit, UserPastUploadLimit
  30. @require_active_login
  31. @user_has_privilege(u'uploader')
  32. def submit_start(request):
  33. """
  34. First view for submitting a file.
  35. """
  36. upload_limit, max_file_size = get_upload_file_limits(request.user)
  37. submit_form = submit_forms.get_submit_start_form(
  38. request.form,
  39. license=request.user.license_preference,
  40. max_file_size=max_file_size,
  41. upload_limit=upload_limit,
  42. uploaded=request.user.uploaded)
  43. if request.method == 'POST' and submit_form.validate():
  44. if not check_file_field(request, 'file'):
  45. submit_form.file.errors.append(
  46. _(u'You must provide a file.'))
  47. else:
  48. try:
  49. submit_media(
  50. mg_app=request.app, user=request.user,
  51. submitted_file=request.files['file'],
  52. filename=request.files['file'].filename,
  53. title=six.text_type(submit_form.title.data),
  54. description=six.text_type(submit_form.description.data),
  55. license=six.text_type(submit_form.license.data) or None,
  56. tags_string=submit_form.tags.data,
  57. upload_limit=upload_limit, max_file_size=max_file_size,
  58. urlgen=request.urlgen)
  59. add_message(request, SUCCESS, _('Woohoo! Submitted!'))
  60. return redirect(request, "mediagoblin.user_pages.user_home",
  61. user=request.user.username)
  62. # Handle upload limit issues
  63. except FileUploadLimit:
  64. submit_form.file.errors.append(
  65. _(u'Sorry, the file size is too big.'))
  66. except UserUploadLimit:
  67. submit_form.file.errors.append(
  68. _('Sorry, uploading this file will put you over your'
  69. ' upload limit.'))
  70. except UserPastUploadLimit:
  71. messages.add_message(
  72. request,
  73. messages.WARNING,
  74. _('Sorry, you have reached your upload limit.'))
  75. return redirect(request, "mediagoblin.user_pages.user_home",
  76. user=request.user.username)
  77. except FileTypeNotSupported as e:
  78. submit_form.file.errors.append(e)
  79. except Exception as e:
  80. raise
  81. return render_to_response(
  82. request,
  83. 'mediagoblin/submit/start.html',
  84. {'submit_form': submit_form,
  85. 'app_config': mg_globals.app_config})
  86. @require_active_login
  87. def add_collection(request, media=None):
  88. """
  89. View to create a new collection
  90. """
  91. submit_form = submit_forms.AddCollectionForm(request.form)
  92. if request.method == 'POST' and submit_form.validate():
  93. collection = request.db.Collection()
  94. collection.title = six.text_type(submit_form.title.data)
  95. collection.description = six.text_type(submit_form.description.data)
  96. collection.creator = request.user.id
  97. collection.generate_slug()
  98. # Make sure this user isn't duplicating an existing collection
  99. existing_collection = request.db.Collection.query.filter_by(
  100. creator=request.user.id,
  101. title=collection.title).first()
  102. if existing_collection:
  103. add_message(request, messages.ERROR,
  104. _('You already have a collection called "%s"!') \
  105. % collection.title)
  106. else:
  107. collection.save()
  108. add_message(request, SUCCESS,
  109. _('Collection "%s" added!') % collection.title)
  110. return redirect(request, "mediagoblin.user_pages.user_home",
  111. user=request.user.username)
  112. return render_to_response(
  113. request,
  114. 'mediagoblin/submit/collection.html',
  115. {'submit_form': submit_form,
  116. 'app_config': mg_globals.app_config})