Commit a260115b by jkarni

Merge pull request #685 from edx/jkarni/fix/cms_pylint

Contentstore views pylint fixes
parents 796667cb 80619da4
...@@ -26,12 +26,16 @@ def has_access(user, location, role=STAFF_ROLE_NAME): ...@@ -26,12 +26,16 @@ def has_access(user, location, role=STAFF_ROLE_NAME):
There is a super-admin permissions if user.is_staff is set There is a super-admin permissions if user.is_staff is set
Also, since we're unifying the user database between LMS and CAS, Also, since we're unifying the user database between LMS and CAS,
I'm presuming that the course instructor (formally known as admin) I'm presuming that the course instructor (formally known as admin)
will not be in both INSTRUCTOR and STAFF groups, so we have to cascade our queries here as INSTRUCTOR will not be in both INSTRUCTOR and STAFF groups, so we have to cascade our
has all the rights that STAFF do queries here as INSTRUCTOR has all the rights that STAFF do
''' '''
course_location = get_course_location_for_item(location) course_location = get_course_location_for_item(location)
_has_access = is_user_in_course_group_role(user, course_location, role) _has_access = is_user_in_course_group_role(user, course_location, role)
# if we're not in STAFF, perhaps we're in INSTRUCTOR groups # if we're not in STAFF, perhaps we're in INSTRUCTOR groups
if not _has_access and role == STAFF_ROLE_NAME: if not _has_access and role == STAFF_ROLE_NAME:
_has_access = is_user_in_course_group_role(user, course_location, INSTRUCTOR_ROLE_NAME) _has_access = is_user_in_course_group_role(
user,
course_location,
INSTRUCTOR_ROLE_NAME
)
return _has_access return _has_access
...@@ -4,6 +4,7 @@ import os ...@@ -4,6 +4,7 @@ import os
import tarfile import tarfile
import shutil import shutil
import cgi import cgi
from functools import partial
from tempfile import mkdtemp from tempfile import mkdtemp
from path import path from path import path
...@@ -34,7 +35,8 @@ from .access import get_location_and_verify_access ...@@ -34,7 +35,8 @@ from .access import get_location_and_verify_access
from util.json_request import JsonResponse from util.json_request import JsonResponse
__all__ = ['asset_index', 'upload_asset', 'import_course', 'generate_export_course', 'export_course'] __all__ = ['asset_index', 'upload_asset', 'import_course',
'generate_export_course', 'export_course']
def assets_to_json_dict(assets): def assets_to_json_dict(assets):
...@@ -58,7 +60,8 @@ def assets_to_json_dict(assets): ...@@ -58,7 +60,8 @@ def assets_to_json_dict(assets):
obj["thumbnail"] = thumbnail obj["thumbnail"] = thumbnail
id_info = asset.get("_id") id_info = asset.get("_id")
if id_info: if id_info:
obj["id"] = "/{tag}/{org}/{course}/{revision}/{category}/{name}".format( obj["id"] = "/{tag}/{org}/{course}/{revision}/{category}/{name}" \
.format(
org=id_info.get("org", ""), org=id_info.get("org", ""),
course=id_info.get("course", ""), course=id_info.get("course", ""),
revision=id_info.get("revision", ""), revision=id_info.get("revision", ""),
...@@ -132,14 +135,14 @@ def asset_index(request, org, course, name): ...@@ -132,14 +135,14 @@ def asset_index(request, org, course, name):
@login_required @login_required
def upload_asset(request, org, course, coursename): def upload_asset(request, org, course, coursename):
''' '''
This method allows for POST uploading of files into the course asset library, which will This method allows for POST uploading of files into the course asset
be supported by GridFS in MongoDB. library, which will be supported by GridFS in MongoDB.
''' '''
# construct a location from the passed in path # construct a location from the passed in path
location = get_location_and_verify_access(request, org, course, coursename) location = get_location_and_verify_access(request, org, course, coursename)
# Does the course actually exist?!? Get anything from it to prove its existance # Does the course actually exist?!? Get anything from it to prove its
# existence
try: try:
modulestore().get_item(location) modulestore().get_item(location)
except: except:
...@@ -150,9 +153,10 @@ def upload_asset(request, org, course, coursename): ...@@ -150,9 +153,10 @@ def upload_asset(request, org, course, coursename):
if 'file' not in request.FILES: if 'file' not in request.FILES:
return HttpResponseBadRequest() return HttpResponseBadRequest()
# compute a 'filename' which is similar to the location formatting, we're using the 'filename' # compute a 'filename' which is similar to the location formatting, we're
# nomenclature since we're using a FileSystem paradigm here. We're just imposing # using the 'filename' nomenclature since we're using a FileSystem paradigm
# the Location string formatting expectations to keep things a bit more consistent # here. We're just imposing the Location string formatting expectations to
# keep things a bit more consistent
upload_file = request.FILES['file'] upload_file = request.FILES['file']
filename = upload_file.name filename = upload_file.name
mime_type = upload_file.content_type mime_type = upload_file.content_type
...@@ -160,20 +164,25 @@ def upload_asset(request, org, course, coursename): ...@@ -160,20 +164,25 @@ def upload_asset(request, org, course, coursename):
content_loc = StaticContent.compute_location(org, course, filename) content_loc = StaticContent.compute_location(org, course, filename)
chunked = upload_file.multiple_chunks() chunked = upload_file.multiple_chunks()
sc_partial = partial(StaticContent, content_loc, filename, mime_type)
if chunked: if chunked:
content = StaticContent(content_loc, filename, mime_type, upload_file.chunks()) content = sc_partial(upload_file.chunks())
temp_filepath = upload_file.temporary_file_path()
else: else:
content = StaticContent(content_loc, filename, mime_type, upload_file.read()) content = sc_partial(upload_file.read())
tempfile_path = None
thumbnail_content = None thumbnail_content = None
thumbnail_location = None thumbnail_location = None
# first let's see if a thumbnail can be created # first let's see if a thumbnail can be created
(thumbnail_content, thumbnail_location) = contentstore().generate_thumbnail(content, (thumbnail_content, thumbnail_location) = contentstore().generate_thumbnail(
tempfile_path=None if not chunked else content,
upload_file.temporary_file_path()) tempfile_path=tempfile_path
)
# delete cached thumbnail even if one couldn't be created this time (else the old thumbnail will continue to show) # delete cached thumbnail even if one couldn't be created this time (else
# the old thumbnail will continue to show)
del_cached_content(thumbnail_location) del_cached_content(thumbnail_location)
# now store thumbnail location only if we could create it # now store thumbnail location only if we could create it
if thumbnail_content is not None: if thumbnail_content is not None:
...@@ -186,11 +195,13 @@ def upload_asset(request, org, course, coursename): ...@@ -186,11 +195,13 @@ def upload_asset(request, org, course, coursename):
# readback the saved content - we need the database timestamp # readback the saved content - we need the database timestamp
readback = contentstore().find(content.location) readback = contentstore().find(content.location)
response_payload = {'displayname': content.name, response_payload = {
'displayname': content.name,
'uploadDate': get_default_time_display(readback.last_modified_at), 'uploadDate': get_default_time_display(readback.last_modified_at),
'url': StaticContent.get_url_path_from_location(content.location), 'url': StaticContent.get_url_path_from_location(content.location),
'portable_url': StaticContent.get_static_path_from_location(content.location), 'portable_url': StaticContent.get_static_path_from_location(content.location),
'thumb_url': StaticContent.get_url_path_from_location(thumbnail_location) if thumbnail_content is not None else None, 'thumb_url': StaticContent.get_url_path_from_location(thumbnail_location)
if thumbnail_content is not None else None,
'msg': 'Upload completed' 'msg': 'Upload completed'
} }
...@@ -202,8 +213,8 @@ def upload_asset(request, org, course, coursename): ...@@ -202,8 +213,8 @@ def upload_asset(request, org, course, coursename):
@login_required @login_required
def remove_asset(request, org, course, name): def remove_asset(request, org, course, name):
''' '''
This method will perform a 'soft-delete' of an asset, which is basically to copy the asset from This method will perform a 'soft-delete' of an asset, which is basically to
the main GridFS collection and into a Trashcan copy the asset from the main GridFS collection and into a Trashcan
''' '''
get_location_and_verify_access(request, org, course, name) get_location_and_verify_access(request, org, course, name)
......
...@@ -30,7 +30,8 @@ def get_checklists(request, org, course, name): ...@@ -30,7 +30,8 @@ def get_checklists(request, org, course, name):
modulestore = get_modulestore(location) modulestore = get_modulestore(location)
course_module = modulestore.get_item(location) course_module = modulestore.get_item(location)
# If course was created before checklists were introduced, copy them over from the template. # If course was created before checklists were introduced, copy them over
# from the template.
copied = False copied = False
if not course_module.checklists: if not course_module.checklists:
course_module.checklists = CourseDescriptor.checklists.default course_module.checklists = CourseDescriptor.checklists.default
...@@ -68,7 +69,8 @@ def update_checklist(request, org, course, name, checklist_index=None): ...@@ -68,7 +69,8 @@ def update_checklist(request, org, course, name, checklist_index=None):
if checklist_index is not None and 0 <= int(checklist_index) < len(course_module.checklists): if checklist_index is not None and 0 <= int(checklist_index) < len(course_module.checklists):
index = int(checklist_index) index = int(checklist_index)
course_module.checklists[index] = json.loads(request.body) course_module.checklists[index] = json.loads(request.body)
# seeming noop which triggers kvs to record that the metadata is not default # seeming noop which triggers kvs to record that the metadata is
# not default
course_module.checklists = course_module.checklists course_module.checklists = course_module.checklists
checklists, _ = expand_checklist_action_urls(course_module) checklists, _ = expand_checklist_action_urls(course_module)
course_module.save() course_module.save()
...@@ -76,10 +78,13 @@ def update_checklist(request, org, course, name, checklist_index=None): ...@@ -76,10 +78,13 @@ def update_checklist(request, org, course, name, checklist_index=None):
return JsonResponse(checklists[index]) return JsonResponse(checklists[index])
else: else:
return HttpResponseBadRequest( return HttpResponseBadRequest(
"Could not save checklist state because the checklist index was out of range or unspecified.", ( "Could not save checklist state because the checklist index "
content_type="text/plain") "was out of range or unspecified."),
content_type="text/plain"
)
elif request.method == 'GET': elif request.method == 'GET':
# In the JavaScript view initialize method, we do a fetch to get all the checklists. # In the JavaScript view initialize method, we do a fetch to get all
# the checklists.
checklists, modified = expand_checklist_action_urls(course_module) checklists, modified = expand_checklist_action_urls(course_module)
if modified: if modified:
course_module.save() course_module.save()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment