Commit 80ea764c by Jonathan Piacenti Committed by E. Kolpakov

Made errors on Library blocks use validate functionality.

parent 9f85c0f0
...@@ -14,6 +14,7 @@ from xblock.core import XBlock ...@@ -14,6 +14,7 @@ from xblock.core import XBlock
from xblock.fields import Scope, String, List, Integer, Boolean from xblock.fields import Scope, String, List, Integer, Boolean
from xblock.fragment import Fragment from xblock.fragment import Fragment
from xmodule.modulestore.exceptions import ItemNotFoundError from xmodule.modulestore.exceptions import ItemNotFoundError
from xmodule.validation import StudioValidationMessage, StudioValidation
from xmodule.x_module import XModule, STUDENT_VIEW from xmodule.x_module import XModule, STUDENT_VIEW
from xmodule.studio_editable import StudioEditableModule, StudioEditableDescriptor from xmodule.studio_editable import StudioEditableModule, StudioEditableDescriptor
from .xml_module import XmlDescriptor from .xml_module import XmlDescriptor
...@@ -260,6 +261,40 @@ class LibraryContentModule(LibraryContentFields, XModule, StudioEditableModule): ...@@ -260,6 +261,40 @@ class LibraryContentModule(LibraryContentFields, XModule, StudioEditableModule):
})) }))
return fragment return fragment
def validate(self):
"""
Validates the state of this Library Content Module Instance. This
is the override of the general XBlock method, and it will also ask
its superclass to validate.
"""
validation = super(LibraryContentModule, self).validate()
if not isinstance(validation, StudioValidation):
validation = StudioValidation.copy(validation)
if not self.source_libraries:
validation.set_summary(
StudioValidationMessage(
StudioValidationMessage.NOT_CONFIGURED,
_(u"A library has not yet been selected."),
action_class='edit-button',
action_label=_(u"Select a Library")
)
)
return validation
for library_key, version in self.source_libraries: # pylint: disable=unused-variable
library = _get_library(self.runtime.descriptor_runtime.modulestore, library_key)
if library is None:
validation.set_summary(
StudioValidationMessage(
StudioValidationMessage.ERROR,
_(u'Library is invalid, corrupt, or has been deleted.'),
action_class='edit-button',
action_label=_(u"Edit Library List")
)
)
break
return validation
def author_view(self, context): def author_view(self, context):
""" """
Renders the Studio views. Renders the Studio views.
...@@ -284,41 +319,31 @@ class LibraryContentModule(LibraryContentFields, XModule, StudioEditableModule): ...@@ -284,41 +319,31 @@ class LibraryContentModule(LibraryContentFields, XModule, StudioEditableModule):
_('No matching content found in library, no library configured, or not yet loaded from library.') _('No matching content found in library, no library configured, or not yet loaded from library.')
)) ))
else: else:
# When shown on a unit page, don't show any sort of preview - just the status of this block.
LibraryStatus = enum( # pylint: disable=invalid-name
NONE=0, # no library configured
INVALID=1, # invalid configuration or library has been deleted/corrupted
OK=2, # library configured correctly and should be working fine
)
UpdateStatus = enum( # pylint: disable=invalid-name UpdateStatus = enum( # pylint: disable=invalid-name
CANNOT=0, # Cannot update - library is not set, invalid, deleted, etc. CANNOT=0, # Cannot update - library is not set, invalid, deleted, etc.
NEEDED=1, # An update is needed - prompt the user to update NEEDED=1, # An update is needed - prompt the user to update
UP_TO_DATE=2, # No update necessary - library is up to date UP_TO_DATE=2, # No update necessary - library is up to date
) )
# When shown on a unit page, don't show any sort of preview - just the status of this block.
library_ok = bool(self.source_libraries) # True if at least one source library is defined
library_names = [] library_names = []
library_status = LibraryStatus.OK
update_status = UpdateStatus.UP_TO_DATE update_status = UpdateStatus.UP_TO_DATE
if self.source_libraries:
for library_key, version in self.source_libraries: for library_key, version in self.source_libraries:
library = _get_library(self.runtime.descriptor_runtime.modulestore, library_key) library = _get_library(self.runtime.descriptor_runtime.modulestore, library_key)
if library is None: if library is None:
library_status = LibraryStatus.INVALID
update_status = UpdateStatus.CANNOT update_status = UpdateStatus.CANNOT
library_ok = False
break break
library_names.append(library.display_name) library_names.append(library.display_name)
latest_version = library.location.library_key.version_guid latest_version = library.location.library_key.version_guid
if version is None or version != latest_version: if version is None or version != latest_version:
update_status = UpdateStatus.NEEDED update_status = UpdateStatus.NEEDED
# else library is up to date.
else:
library_status = LibraryStatus.NONE
update_status = UpdateStatus.CANNOT
fragment.add_content(self.system.render_template('library-block-author-view.html', { fragment.add_content(self.system.render_template('library-block-author-view.html', {
'library_status': library_status,
'LibraryStatus': LibraryStatus,
'update_status': update_status,
'UpdateStatus': UpdateStatus,
'library_names': library_names, 'library_names': library_names,
'library_ok': library_ok,
'UpdateStatus': UpdateStatus,
'update_status': update_status,
'max_count': self.max_count, 'max_count': self.max_count,
'mode': self.mode, 'mode': self.mode,
'num_children': len(self.children), # pylint: disable=no-member 'num_children': len(self.children), # pylint: disable=no-member
......
...@@ -2,16 +2,12 @@ ...@@ -2,16 +2,12 @@
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
%> %>
<div class="xblock-header-secondary"> <div class="xblock-header-secondary">
% if library_status == LibraryStatus.OK: % if library_ok:
<p>${_('This component will be replaced by {mode} {max_count} components from the {num_children} matching components from {lib_names}.').format(mode=mode, max_count=max_count, num_children=num_children, lib_names=', '.join(library_names))}</p> <p>${_('This component will be replaced by {mode} {max_count} components from the {num_children} matching components from {lib_names}.').format(mode=mode, max_count=max_count, num_children=num_children, lib_names=', '.join(library_names))}</p>
% if update_status == UpdateStatus.NEEDED: % if update_status == UpdateStatus.NEEDED:
<p><strong>${_('This component is out of date.')}</strong> <a href="#" class="library-update-btn">↻ ${_('Update now with latest components from the library')}</a></p> <p><strong>${_('This component is out of date.')}</strong> <a href="#" class="library-update-btn">↻ ${_('Update now with latest components from the library')}</a></p>
% elif update_status == UpdateStatus.UP_TO_DATE: % elif update_status == UpdateStatus.UP_TO_DATE:
<p>${_(u'✓ Up to date.')}</p> <p>${_(u'✓ Up to date.')}</p>
% endif % endif
% elif library_status == LibraryStatus.NONE:
<p>${_('No library or filters configured. Press "Edit" to configure.')}</p>
% else:
<p>${_('Library is invalid, corrupt, or has been deleted.')}</p>
% endif % endif
</div> </div>
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