From 95060f39301f21242eebc835df569f33e57a7a2b Mon Sep 17 00:00:00 2001 From: Alexander Kryklia Date: Mon, 24 Dec 2012 14:39:52 +0200 Subject: [PATCH] updated python part to sync with new interface --- common/lib/capa/capa/inputtypes.py | 60 ++++++++++++------- .../capa/templates/drag_and_drop_input.html | 14 ++--- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index ec00a64833..46c23fb3a0 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -45,6 +45,7 @@ import re import shlex # for splitting quoted strings import sys import os +import unidecode from registry import TagRegistry @@ -798,29 +799,44 @@ class DragAndDropInput(InputTypeBase): template = 'drag_and_drop_input.html' tags = ['drag_and_drop_input'] - @classmethod - def get_attributes(cls): - """ - Note: height, width, images_directory_path are required. - """ - # import ipdb; ipdb.set_trace() - return [Attribute('height'), - Attribute('width'), - Attribute('images_directory_path'), - ] - def setup(self): - # import ipdb; ipdb.set_trace() - imagepath = self.loaded_attributes['images_directory_path'] - # import ipdb; ipdb.set_trace() - try: - images_list = self.system.filestore.listdir(os.path.join('static', - 'images', imagepath)) - images_list = ['/static/images/' + img for img in images_list] - except: - images_list = [] - self.loaded_attributes['images_list'] = images_list - self.to_render.add('images_list') + + def slugify(s): + """Makes slug from string""" + s = unidecode.unidecode(s).lower() + return re.sub(r'\W+', '-', s) + + def parse(x): + """Parses xml element to dictionary. + + Args: + xml etree element with optional + name or label or icon attributes. At least one + attribute must be presented. + + Returns: + dict{'name': smth, 'label': smth, 'icon': smth}. + """ + dic = dict() + for attr_name in ('name', 'label', 'icon'): + try: + dic[attr_name] = Attribute(attr_name).parse_from_xml(x) + except ValueError: + dic[attr_name] = None + dic['name'] = dic['name'] or slugify(dic['label']) + dic['label'] = dic['label'] or dic['name'] + + return dic + + to_js = dict() + to_js['target'] = Attribute('img').parse_from_xml(self.xml) + to_js['draggable'] = [parse(draggable) for draggable in + self.xml.getchildren()] + + self.loaded_attributes['drag_and_drop_json'] = json.dumps(to_js) + self.to_render.add('drag_and_drop_json') + self.loaded_attributes['course_folder'] = self.system.course_id.split('/')[1] + self.to_render.add('course_folder') registry.register(DragAndDropInput) diff --git a/common/lib/capa/capa/templates/drag_and_drop_input.html b/common/lib/capa/capa/templates/drag_and_drop_input.html index 58fed37c84..f939dd58eb 100644 --- a/common/lib/capa/capa/templates/drag_and_drop_input.html +++ b/common/lib/capa/capa/templates/drag_and_drop_input.html @@ -1,16 +1,12 @@
- % if images_list: -
    - % for item in images_list: -
  • - % endfor -
- % endif + data-plain-id="${id}" data-course-folder="${course_folder}">
-
+ + +
% if status == 'unsubmitted':