Add new python field.
This commit is contained in:
@@ -2,7 +2,7 @@ import time
|
||||
import logging
|
||||
import re
|
||||
|
||||
from xblock.fields import Field
|
||||
from xblock.fields import Field, String
|
||||
import datetime
|
||||
import dateutil.parser
|
||||
|
||||
@@ -116,3 +116,16 @@ class Timedelta(Field):
|
||||
if cur_value > 0:
|
||||
values.append("%d %s" % (cur_value, attr))
|
||||
return ' '.join(values)
|
||||
|
||||
|
||||
class IsoTime(String):
|
||||
|
||||
def from_json(self, value):
|
||||
if isinstance(value, float):
|
||||
if not value:
|
||||
return "00:00:00"
|
||||
else:
|
||||
return str(datetime.timedelta(seconds=value))
|
||||
else:
|
||||
return super(IsoTime, self).from_json(value)
|
||||
|
||||
|
||||
@@ -28,13 +28,15 @@ from xmodule.editing_module import TabsEditingDescriptor
|
||||
from xmodule.raw_module import EmptyDataRawDescriptor
|
||||
from xmodule.xml_module import is_pointer_tag, name_to_pathname, deserialize_field
|
||||
from xmodule.modulestore import Location
|
||||
from xblock.fields import Scope, String, Boolean, Float, List, Integer, ScopeIds
|
||||
from xblock.fields import Scope, String, Boolean, List, Integer, ScopeIds
|
||||
from xmodule.fields import IsoTime
|
||||
|
||||
from xmodule.modulestore.inheritance import InheritanceKeyValueStore
|
||||
from xblock.runtime import DbModel
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
||||
def parse_time_from_str_to_float(str_time):
|
||||
"""
|
||||
Converts s in '12:34:45' format to seconds.
|
||||
@@ -62,16 +64,6 @@ def parse_time_from_float_to_str(s):
|
||||
else:
|
||||
return str(datetime.timedelta(seconds=s))
|
||||
|
||||
|
||||
class StringThatWasFloat(String):
|
||||
|
||||
def from_json(self, value):
|
||||
if isinstance(value, float):
|
||||
return parse_time_from_float_to_str(value)
|
||||
else:
|
||||
return super(StringThatWasFloat, self).from_json(value)
|
||||
|
||||
|
||||
class VideoFields(object):
|
||||
"""Fields for `VideoModule` and `VideoDescriptor`."""
|
||||
display_name = String(
|
||||
@@ -116,13 +108,13 @@ class VideoFields(object):
|
||||
scope=Scope.settings,
|
||||
default=""
|
||||
)
|
||||
start_time = StringThatWasFloat(
|
||||
start_time = IsoTime(
|
||||
help="Start time for the video.",
|
||||
display_name="Start Time",
|
||||
scope=Scope.settings,
|
||||
default="00:00:00"
|
||||
)
|
||||
end_time = StringThatWasFloat(
|
||||
end_time = IsoTime(
|
||||
help="End time for the video.",
|
||||
display_name="End Time",
|
||||
scope=Scope.settings,
|
||||
|
||||
@@ -13,6 +13,7 @@ from xmodule.modulestore.exceptions import ItemNotFoundError, InsufficientSpecif
|
||||
|
||||
from xblock.core import XBlock
|
||||
from xblock.fields import Scope, Integer, Float, List, XBlockMixin, String
|
||||
from xmodule.fields import IsoTime
|
||||
from xblock.fragment import Fragment
|
||||
from xblock.runtime import Runtime
|
||||
from xmodule.errortracker import exc_info_to_str
|
||||
@@ -708,6 +709,8 @@ class XModuleDescriptor(XModuleMixin, HTMLSnippet, ResourceTemplates, XBlock):
|
||||
editor_type = "Float"
|
||||
elif isinstance(field, List):
|
||||
editor_type = "List"
|
||||
elif isinstance(field, IsoTime):
|
||||
editor_type = "IsoTime"
|
||||
metadata_fields[field.name]['type'] = editor_type
|
||||
metadata_fields[field.name]['options'] = [] if values is None else values
|
||||
|
||||
|
||||
Reference in New Issue
Block a user