Пример #1
0
    def __render_form_page(self, macro):
        # Validate form errors and save
        form_errors = {}
        opt_fields    = {
            'title_show': 'none',
            'title_hide': 'block',
            'notes_show': 'none',
            'notes_hide': 'block',
            }
            
        # Title is optional.
        title  = self.request.get(FORM_SAVE_TITLE, default_value="")[0:TITLE_MAX_LENGTH]

        # Author is optional.
        name   = self.request.get(FORM_SAVE_NAME, default_value="")[0:NAME_MAX_LENGTH]
        
        # Server is optional.
        server = self.request.get(FORM_SAVE_SERVER, default_value="")[0:SERVER_MAX_LENGTH]

        # Notes are optional
        notes  = self.request.get(FORM_SAVE_NOTES, default_value="")[0:NOTES_TEXT_LENGTH]

        # Update optional field status
        if valid(title) or valid(name) or valid(server):
            opt_fields['title_show'] = "block"
            opt_fields['title_hide'] = "none"
        if valid(notes):
            opt_fields['notes_show'] = "block"
            opt_fields['notes_hide'] = "none"        

        # Version defaults to current version.
        version = "%s.%s.%s" % (MAJOR_VERSION,
                                MINOR_VERSION,
                                PATCH_VERSION)
        
        # Must have at least one class, and all must be recognized.
        classes = []
        for c in CLASS_LIST:
            if self.request.get(c.replace(" ", "_")) == '1': classes.append(c)
        if len(classes) == 0:
            form_errors['class_error'] = get_form_error(FORM_SAVE_CLASSES)

        # Must have at least one tag, and all must be recognized.
        # De-dup tags via a set.
        tags = list(set([t for t in re.split("\s*,\s*",
                                             self.request.get(FORM_SAVE_TAGS)) if t]))
        if len(tags) == 0:
            form_errors['tag_error'] = get_form_error(FORM_SAVE_TAGS)
        else:
            if len(self.request.get(FORM_SAVE_TAGS)) > ALL_TAGS_MAX_LENGTH:
                form_errors['tag_error'] = "Too many tags!"
            else:
                longest_tag = max(tags, key=len)
                if len(longest_tag) > SINGLE_TAG_MAX_LENGTH:
                    form_errors['tag_error'] = "Tag %s exceeds the max tag length of %s chars!" % (longest_tag, SINGLE_TAG_MAX_LENGTH)

        # Use memcached to throttle people to saving one macro every 10
        # seconds.    Only do this AFTER the user has fixed errors.
        if len(form_errors) == 0:
            secs_left = throttle_action("save", self.request.remote_addr)
            if secs_left > 0:
                form_errors['spam'] = "You must wait %s seconds before you may save another macro." % (secs_left)

        # Were there any errors?    If success, save and redirect
        if len(form_errors) == 0:
            # Return an error page if something really wrong happens.
            link = None
            link = SavedMacroOps.save_macro(macro, cgi.escape(notes), title, name, classes, tags, version, server)
            # Redirect to the intepreter with the link.
            self.redirect("/%s" % link)

        # Error in validation--display.
        else:
            # Create template data based on input.
            input_vals = {
                'title_data'      : title,
                'name_data'       : name,
                'macro_notes'     : notes,
                'curr_version'    : version,
                'note_limit'      : NOTES_TEXT_LENGTH,
                'note_ch_left'    : NOTES_TEXT_LENGTH - len(notes),
                'class_list'      : translate_classmap(sel=set(classes)),
                # Server list lives in a template.
                'server_list'     : template.render(os.path.join(_TEMPLATE_PATH,
                                                                 'servers.template'),
                                                    {}),
                'tag_def_list'    : TAG_LIST,
                'selected_server' : server,
                'tag_list'        : ",".join(tags),
                }
            # Add in errors.
            input_vals.update(form_errors)

            # Add in optional field status.
            input_vals.update(opt_fields)
            
            # Write out the page
            return generate_edit_page(_TEMPLATE_PATH,
                                      macro,
                                      save_values=input_vals)