Skip to content

Commit

Permalink
WIP: Add simple AssetCreateView #4647
Browse files Browse the repository at this point in the history
Create basic form for creating a Mediathread asset based on an
external image or video source.
  • Loading branch information
nikolas committed Oct 3, 2022
1 parent 8668729 commit ae7ee03
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 17 deletions.
14 changes: 7 additions & 7 deletions mediathread/assetmgr/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
EMBED_WIDTH, EMBED_HEIGHT,
asset_workspace_courselookup,
RedirectToExternalCollectionView,
RedirectToUploaderView, AssetCreateView, AssetEmbedListView,
RedirectToUploaderView, ExternalAssetCreateView, AssetEmbedListView,
_parse_domain, AssetEmbedView, annotation_delete,
annotation_create_global, annotation_create, AssetUpdateView
)
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_sources_from_args(self):
'image': 'x' * 5000, # too long
'url': 'https://www.youtube.com/abcdefghi'}
request = RequestFactory().post('/save/', data)
sources = AssetCreateView.sources_from_args(request)
sources = ExternalAssetCreateView.sources_from_args(request)

self.assertEquals(len(sources.keys()), 0)

Expand All @@ -95,7 +95,7 @@ def test_sources_from_args(self):
'image': 'https://www.flickr.com/',
'image-metadata': ['w720h526;text/html']}
request = RequestFactory().post('/save/', data)
sources = AssetCreateView.sources_from_args(request)
sources = ExternalAssetCreateView.sources_from_args(request)
self.assertEquals(len(sources.keys()), 2)
self.assertEquals(sources['image'].url, 'https://www.flickr.com/')
self.assertTrue(sources['image'].primary)
Expand All @@ -111,7 +111,7 @@ def test_sources_from_args(self):
'metadata-description': 'Video description',
}
request = RequestFactory().post('/save/', data)
sources = AssetCreateView.sources_from_args(request)
sources = ExternalAssetCreateView.sources_from_args(request)
self.assertEquals(len(sources.keys()), 2)
self.assertEquals(sources['video'].url,
'http://www.example.com/video.mp4')
Expand All @@ -120,7 +120,7 @@ def test_sources_from_args(self):
self.assertEquals(sources['video'].height, 358)

def test_parse_user(self):
view = AssetCreateView()
view = ExternalAssetCreateView()
request = RequestFactory().get('/')
request.course = self.sample_course

Expand Down Expand Up @@ -222,7 +222,7 @@ def test_asset_create_via_bookmarklet(self):
request.user = self.instructor_one
request.course = self.sample_course

view = AssetCreateView()
view = ExternalAssetCreateView()
view.request = request
response = view.post(request)

Expand All @@ -240,7 +240,7 @@ def test_asset_create_via_bookmarklet(self):
request.user = self.instructor_one
request.course = self.sample_course

view = AssetCreateView()
view = ExternalAssetCreateView()
view.request = request
response = view.post(request)

Expand Down
62 changes: 61 additions & 1 deletion mediathread/assetmgr/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def post(self, request):
# This view is used by Mediathread's browser extension, so disable CSRF
# until we implement this in the extension.
@method_decorator(csrf_exempt, name='dispatch')
class AssetCreateView(View):
class ExternalAssetCreateView(View):
OPERATION_TAGS = ('jump', 'title', 'noui', 'v', 'share',
'as', 'set_course', 'secret')

Expand Down Expand Up @@ -328,6 +328,66 @@ def post(self, request):
return HttpResponseRedirect(asset_url)


class AssetCreateView(LoggedInCourseMixin, View):
"""
View for creating an Asset via an uploaded media object.
"""
http_method_names = ['post']

def dispatch(self, request, *args, **kwargs):
r = super().dispatch(request, *args, **kwargs)

# @todo - update for pdf permissions when that's ready to go
if not course_details.can_upload_image(request.user, request.course):
raise PermissionDenied

return r

def post(self, request, *args, **kwargs):
if (not request.POST.get('title')) or \
(not request.POST.get('url')):
return HttpResponseBadRequest(
'Title and URL are required to make an asset.')

title = request.POST.get('title').strip()
url = request.POST.get('url')

author = request.user
if request.user.is_staff and request.POST.get('as'):
upload_as = request.POST.get('as')
author = get_object_or_404(User, username=upload_as)

asset = Asset.objects.create(
course=request.course, title=title, author=author)
asset.global_annotation(request.user, True)

label = 'image'
if url.endswith('.pdf'):
label = 'pdf'
# Dimensions are not needed for PDF display.
width = 0
height = 0
else:
width = request.POST.get('width')
height = request.POST.get('height')

Source.objects.create(
asset=asset, url=url,
primary=True,
width=width, height=height,
label=label)

asset_url = reverse('asset-view', args=[request.course.pk, asset.pk])

messages.success(
request,
'The <a href="{}"><strong>{}</strong></a> item '.format(
asset_url, asset.title
) + 'has been added to your collection.')

return redirect('asset-view', request.course.pk, asset.pk)


class UploadedAssetCreateView(LoggedInCourseMixin, View):
"""
View for creating an Asset via an uploaded media object.
Expand Down
54 changes: 48 additions & 6 deletions mediathread/templates/main/collection_add.html
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,58 @@ <h6 class="card-title mt-2">IMAGE</h6>
<div class="card">
<div class="card-body">
<h5 class="card-title text-center">Import Media</h5>

<form>
<div class="form-group">
<label for="import-source-url">
Source URL
</label>
<input type="text"
id="import-source-url"
class="form-control"
name="source_url"
aria-describedby="sourceURL" />
<small class="form-text text-muted">
Right-click source image and "Copy image address"
</small>
</div>
<div class="form-group">
<label for="import-name">Name</label>
<input type="text" class="form-control" id="import-name">
</div>
<button type="submit" class="btn btn-primary">
Submit
</button>
</form>

<hr />

<p class="card-text">
Install Mediathread’s Google Chrome
extension to import assets like video, audio, and
Alternatively, you can install
Mediathread’s Google Chrome extension
to import assets like video, audio, and
images into this course from various
sites across the web.

<ul><li>Visit the Chrome Web Store and make sure to click Add To Chrome.</li>
<li>Once added to your browser, you can click on the Extension icon next
to the Address Bar to pin the Mediathread extension for easier access.</li>
<li>You can click the extension to collect single media items from sites like Flickr, YouTube, and Google Images.</li>
<ul>
<li>
Visit the Chrome Web Store and
make sure to click Add To
Chrome.
</li>
<li>
Once added to your browser, you
can click on the Extension icon
next to the Address Bar to pin
the Mediathread extension for
easier access.
</li>
<li>
You can click the extension to
collect single media items from
sites like Flickr, YouTube, and
Google Images.
</li>
</ul>

You must be using a browser in the <a href="https://en.wikipedia.org/wiki/Chromium_(web_browser)#Browsers_based_on_Chromium">Chrome family</a>
Expand Down
7 changes: 4 additions & 3 deletions mediathread/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
AssetDetailView, ReactAssetDetailView,
TagCollectionView,
RedirectToExternalCollectionView, RedirectToUploaderView,
AssetCreateView, BookmarkletMigrationView, AssetUpdateView)
ExternalAssetCreateView, BookmarkletMigrationView, AssetUpdateView
)
from mediathread.main.forms import CustomRegistrationForm
from mediathread.main.views import (
error_500,
Expand Down Expand Up @@ -284,8 +285,8 @@
path('course/<int:course_pk>/reports/',
include('mediathread.reports.urls')),

# Bookmarklet, Wardenclyffe, Staff custom asset entry
path('save/', AssetCreateView.as_view(), name='asset-save'),
# Browser Extension, Wardenclyffe, Staff custom asset entry
path('save/', ExternalAssetCreateView.as_view(), name='asset-save'),
path('update/', AssetUpdateView.as_view(), name='asset-update-view'),

path('setting/<slug:user_name>/', set_user_setting),
Expand Down

0 comments on commit ae7ee03

Please sign in to comment.