Example #1
0
 def _add_cpmd_val(self, cpmd_tag, fld, tag):
     """
     cpmd_tag: compound key to add val to
     fld: form name of compound tag
     tag: str representation of type of tag
     """
     # DOTO: need to run through all possible elements of this tag in the form
     # at moment just grabbing the default named fld.
     # need a regex for 'compound_#_img_#'
     if cpmd_tag not in self.cmpd_vals:
         self.cmpd_vals[cpmd_tag] = {}
     rgx = re.compile('([\w]+_[\d]+_)([\w]+_)([\d]+)')
     def increment(match):
         cmpd_int = match.group(1)
         fld_tag = match.group(2)
         fld_int = int(match.group(3))+1
         return cmpd_int+fld_tag+str(fld_int)
     frm_obj = self.form[fld]
     while(True):
         if frm_obj is not None and type(frm_obj.file) is not file: # text
             logging.debug('cmpd tag: %s \t field: %s' % (tag, fld))
             if not self.commands[tag].multi: # DOTO: need to match up fields to tag obj
                 self.cmpd_vals[cpmd_tag][tag] = frm_obj.value
             elif tag not in self.cmpd_vals[cpmd_tag]:
                 self.cmpd_vals[cpmd_tag][tag] = [frm_obj.value]
             elif frm_obj not in self.cmpd_vals[cpmd_tag][tag]:
                 self.cmpd_vals[cpmd_tag][tag].append(frm_obj.value)
             else:
                 logging.debug('already swept it up on the first greedy pass...')
                 break
             # DOTO: if img then intermediate step needed - save to img dir, return url
         elif frm_obj is not None: # file
             logging.debug('tackling img file input: %s' % frm_obj.filename)
             tmp = tools.urlise(frm_obj.filename) # this change suddenly stopped us reading in 2nd file below. Not sure about a 3rd...
             # save to dir should be handled by form subclass, just need url val
             if tag not in self.cmpd_vals[cpmd_tag]:
                 self.cmpd_vals[cpmd_tag][tag] = [os.path.join(self.img_path, tmp)]
             elif os.path.join(self.img_path, tmp) not in self.cmpd_vals[cpmd_tag][tag]:
                 self.cmpd_vals[cpmd_tag][tag].append(os.path.join(self.img_path, tmp))
             else:
                 logging.debug('file already swept up on first greedy pass')
                 break
             with open(os.path.join(self.img_path, tmp), 'wb') as fout:
                 while 1:
                     line = frm_obj.file.readline()
                     if not line: break
                     fout.write(line)
         else: # f****d
             logging.error('field not found in form: %s, %s' % (tag, fld))
             break
         fld= rgx.sub(increment, fld)
         if fld in self.form:
             frm_obj = self.form[fld]
         else:
             break
Example #2
0
    def _add_val(self, fld, tag):
        """
        fld: form name of submitted tag
        tag: str representation of type of tag

        Internal method used to add a req form value to self.vals dict
        And compound form values to their own special self.cmpd_vals dict, 
        each entry of which basically follows the same {tag:[name,...]} 
        structure as self.vals
        """
        frm_obj = self.form[fld]
        # logging.debug('fieldstorage? %s' % frm_obj)
        if frm_obj is not None and type(frm_obj.file) is not file: # text
            logging.debug('tag: %s \t field: %s' % (tag, fld))
            if not self.commands[tag].multi: # DOTO: need to match up fields to tag obj
                self.vals[tag] = frm_obj.value
            elif tag not in self.vals:
                self.vals[tag] = [frm_obj.value]
            else:
                self.vals[tag].append(frm_obj.value)
            # DOTO: if img then intermediate step needed - save to img dir, return url
        elif frm_obj is not None: # file
            # DOTO: make title url safe (and in equivalent cpmd func)
            logging.debug('tackling img file input: %s' % frm_obj.filename)
            tmp = tools.urlise(frm_obj.filename)
            with open(os.path.join(self.img_path, tmp), 'wb') as fout:
                while 1:
                    line = frm_obj.file.readline()
                    if not line: break
                    fout.write(line)
            # save to dir should be handled by form subclass, just need url val
            if tag not in self.vals:
                self.vals[tag] = [os.path.join(self.img_path, tmp)]
            else:
                self.vals[tag].append(os.path.join(self.img_path, tmp))
        else: # f****d
            logging.error('field not found in form: %s, %s' % (tag, fld))