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()
def mk_val(self, val): attr_val = ContractorAttributeVal(attribute=self) field_type = choice_assoc(self.field_type, FIELD_TYPE_CHOICES) if field_type == INT_FIELD: attr_val.int_val = int(from_measurement(val)) elif field_type == FLOAT_FIELD: attr_val.float_val = float(from_measurement(val)) elif field_type == BOOL_FIELD: attr_val.bool_val = (val.lower() == 'y') elif field_type == CHOICE_FIELD: attr_val.choice_val = choice_rassoc(val, self.choices) if attr_val.choice_val == None: raise ValueError('Invalid choice') elif field_type == CHAR_FIELD: attr_val.char_val = str(val) return attr_val
def __unicode__(self): fmt = '{0}={1}' field_name = self.attribute.field_name # normal values keys = ['char_val', 'int_val', 'float_val', 'bool_val'] for k in keys: val = getattr(self, k) if val != None: return fmt.format(field_name, val) # choice value if self.choice_val != None: return fmt.format(field_name, choice_assoc(self.choice_val, self.choices)) # all values = None return '{0} unset'.format(field_name)
def type(self): return choice_assoc(self.field_type, FIELD_TYPE_CHOICES)