Beispiel #1
0
def get_filter_forms_javascript(filter_form_classes):
    """ Javascript is cached in user session (must be there, beucause each user 
        can have other forms, because of different permissions. 
    """
    output = u''
    all_fields_dict = {}
    #    import ipdb; ipdb.set_trace()
    for form_class in filter_form_classes:
        form = form_class()
        # Function for generating field of form
        try:
            output_part, fields_js_dict = form.layout_class(
                form).get_javascript_gener_field()
        except ServerNotAvailableError:
            # We need to connect to a CORBA server to get field values for some
            # filters. If the connection attempt fails, skip the filter.
            # TODO(tom): Should we really just skip it?
            error("Could not get filter for object %s!" % form_class)
            continue
        output += output_part

        all_fields_dict[form.get_object_name()] = fields_js_dict

        # Function that generates empty form:
        output += "function getEmpty%s() {\n" % form.__class__.__name__
        output += "    return '%s'\n" % escape_js_literal(unicode(form))
        output += "}\n"
    output += u'allFieldsDict = %s' % (simplejson.dumps(all_fields_dict) +
                                       u'\n')
    return output
Beispiel #2
0
    def get_javascript_gener_field(self):
        # --- function createRow and variable allFieldsDict---
        
        output = u'function createRow%s(fieldName, fieldLabel) {\n' % self.form.get_object_name()
        output += u'var row = "";\n'

        output += u'switch (fieldName) {\n'
        base_fields = deepcopy(self.form.base_fields)
        output += u"default:\n" # if not specified, first field is taken
        fields_js_dict = SortedDict()
        for field_num, (name, field) in enumerate(base_fields.items()):
            field.name = name
            output += u"case '%s':\n" % name
            rendered_field = unicode(
                self.build_field_row(
                    field, for_javascript=True))
            rendered_field = escape_js_literal(rendered_field)
            output += u"    row += '%s';\n" % rendered_field
            if isinstance(field, CompoundFilterField):
                output += u"    row = row.replace(/%s/g, getEmpty%s());\n" % (REPLACE_ME_WITH_EMPTY_FORM, field.form_class.__name__)
                fields_js_dict[name] = {'label': field.label, 'fieldNum': field_num, 'formName': field.form_class.get_object_name()}
            else:
                fields_js_dict[name] = {'label': field.label, 'fieldNum': field_num}
            output += u"    break;\n"
        output += u'}\n' # end of switch
        output += u'row = row.replace(/%s/g, fieldLabel);\n' % REPLACE_ME_WITH_LABEL
        output += u'return row;\n'
        output += u'}\n' # end of createRow function
        
        return (output, fields_js_dict)
Beispiel #3
0
def get_filter_forms_javascript(filter_form_classes):
    """ Javascript is cached in user session (must be there, beucause each user 
        can have other forms, because of different permissions. 
    """
    output = u''
    all_fields_dict = {}
#    import ipdb; ipdb.set_trace()
    for form_class in filter_form_classes: 
        form = form_class()
        # Function for generating field of form
        try:
            output_part, fields_js_dict = form.layout_class(form).get_javascript_gener_field()
        except ServerNotAvailableError:
            # We need to connect to a CORBA server to get field values for some
            # filters. If the connection attempt fails, skip the filter.
            # TODO(tom): Should we really just skip it?
            error("Could not get filter for object %s!" % form_class)
            continue
        output += output_part
        
        all_fields_dict[form.get_object_name()] = fields_js_dict
        
        # Function that generates empty form:
        output += "function getEmpty%s() {\n" % form.__class__.__name__
        output += "    return '%s'\n" % escape_js_literal(unicode(form))
        output += "}\n"
    output += u'allFieldsDict = %s' % (simplejson.dumps(all_fields_dict) + u'\n')
    return output
Beispiel #4
0
    def union_form_js(self):
        output = u'function buildOrRow() {\n'
        output += u"var row = '%s';\n" % escape_js_literal(unicode(self.build_or_row()))
        output += u'return row;\n'
        output += u'}\n\n'
        
        output += u'function buildForm() {\n'
        output += u"var row = '<td>';\n"
        output += u"row += getEmpty%s();\n" % self.form.form_class.__name__
        output += u"row += '</td>';\n"
        output += u'return row;\n'
        output += u'}\n\n'
        

        return output
    def union_form_js(self):
        output = u'function buildOrRow() {\n'
        output += u"var row = '%s';\n" % escape_js_literal(
            unicode(self.build_or_row()))
        output += u'return row;\n'
        output += u'}\n\n'

        output += u'function buildForm() {\n'
        output += u"var row = '<td>';\n"
        output += u"row += getEmpty%s();\n" % self.form.form_class.__name__
        output += u"row += '</td>';\n"
        output += u'return row;\n'
        output += u'}\n\n'

        return output
    def get_javascript_gener_field(self):
        # --- function createRow and variable allFieldsDict---

        output = u'function createRow%s(fieldName, fieldLabel) {\n' % self.form.get_object_name(
        )
        output += u'var row = "";\n'

        output += u'switch (fieldName) {\n'
        base_fields = deepcopy(self.form.base_fields)
        output += u"default:\n"  # if not specified, first field is taken
        fields_js_dict = SortedDict()
        for field_num, (name, field) in enumerate(base_fields.items()):
            field.name = name
            output += u"case '%s':\n" % name
            rendered_field = unicode(
                self.build_field_row(field, for_javascript=True))
            rendered_field = escape_js_literal(rendered_field)
            output += u"    row += '%s';\n" % rendered_field
            if isinstance(field, CompoundFilterField):
                output += u"    row = row.replace(/%s/g, getEmpty%s());\n" % (
                    REPLACE_ME_WITH_EMPTY_FORM, field.form_class.__name__)
                fields_js_dict[name] = {
                    'label': field.label,
                    'fieldNum': field_num,
                    'formName': field.form_class.get_object_name()
                }
            else:
                fields_js_dict[name] = {
                    'label': field.label,
                    'fieldNum': field_num
                }
            output += u"    break;\n"
        output += u'}\n'  # end of switch
        output += u'row = row.replace(/%s/g, fieldLabel);\n' % REPLACE_ME_WITH_LABEL
        output += u'return row;\n'
        output += u'}\n'  # end of createRow function

        return (output, fields_js_dict)