Exemple #1
0
def mk_role_forms(shift=None):
    forms = []
    initial = mk_initial(shift)
    for rname, attrs in settings.CONTRACTOR_ROLES:
        # fdict = { '<group_name>', [(<fname>, <ftype>), ... ], ... }
        fdict = {}
        dynfields = {}
        dynfieldsets = []
        
        for attr in attrs:
            grp_name, ftype = attr_info(attr)
            if grp_name in fdict:
                fdict[grp_name].append((attr, ftype))
            else:
                fdict[grp_name] = [(attr, ftype)]
        
        for grp_name, _ in settings.ATTRIBUTES:
            if grp_name in fdict:
                fields = fdict[grp_name]
                newfields = dict(join(mk_fields(field, rname) for field in fields))
                dynfields.update(newfields)
                dynfieldsets.append( (grp_name, {'fields': newfields.keys()}) )
            
            
        forms.append( {'name': rname.replace('/', '-'),
                       'form': dyn_form(dynfields, initial, dynfieldsets)} )
        
    return forms
Exemple #2
0
 def display_val(self):
     if self.type() == CHOICE_FIELD:
         grp, choices = attr_info(self.field_name)
         return choice_assoc(self.choice, choices)
     elif self.type() == BOOL_FIELD:
         return self.bool
     else:
         return self.val()
Exemple #3
0
    def update_filters(self, attrs):
        self.filters.all().delete()
        errors = []

        for field_name, field_val in attrs.items():
            try:
                _, field_type = attr_info(field_name)
            except:
                print 'INVALID FIELD: {0}'.format(field_name)
                return
            if attr_info == None:
                errors.append('{0} is not a valid field'.format(field_name))
            
            filter = ShiftFilter(field_name=field_name)
            
            if field_type == CHAR_FIELD:
                filter.char = field_val
            elif field_type == INT_FIELD:
                min_val, max_val = field_val.split(',')
                try:
                    filter.min_int = int(min_val)
                    filter.max_int = int(max_val)
                except:
                    errors.append('{0} had invalid values ({1})'.format(field_name, field_val))
                    continue
            elif field_type == FLOAT_FIELD:
                min_val, max_val = field_val.split(',')
                try:
                    filter.min_float = float(min_val)
                    filter.max_float = float(max_val)
                except:
                    errors.append('{0} had invalid values ({1})'.format(field_name, field_val))
                    continue
            elif field_type == BOOL_FIELD:
                bool_val = int(field_val)
                if bool_val in BOOL_MAP:
                    filter.bool = BOOL_MAP[bool_val]
                else:
                    # ignore the value, it is set to Unknown in the form
                    continue
            elif isinstance(field_type, tuple):
                try:
                    choice = int(field_val)
                    filter.choice = choice
                except:
                    errors.append('{0} had invalid choice value ({1})'.format(
                            field_name, field_val))
                    continue
            else:
                raise ValueError('Invalid field type')
            
            self.filters.add(filter)
            
        self.save()
        return errors