def __init__(self, *args, **kwargs): self.modifiers = Modifiers() self.section = kwargs.pop('section', None) super(NameObject, self).__init__() super(Column, self).__init__(self.clsname(), self.data_type) self.manager = kwargs.get('manager') if kwargs.get('base_value') is not None: self.base_value = kwargs.get('base_value') else: self.base_value = self.get_default_value()
class BaseAttribute(NameObject, Column): modify = {} default_value = None chosen = False field = None initial = None data_type = Integer _base_value = None random = False readonly = False def __init__(self, *args, **kwargs): self.modifiers = Modifiers() self.section = kwargs.pop('section', None) super(NameObject, self).__init__() super(Column, self).__init__(self.clsname(), self.data_type) self.manager = kwargs.get('manager') if kwargs.get('base_value') is not None: self.base_value = kwargs.get('base_value') else: self.base_value = self.get_default_value() def _constructor(self, name, type_, **kwargs): """Needed by SQLAlchemy""" col = BaseAttribute(name, type_, **kwargs) return col @property def base_value(self): return self._base_value @base_value.setter def base_value(self, new_value): self._base_value = new_value if self.manager is not None: # update model value setattr(self.manager.character, self.clsname(), self.value) def roll(self): """generate a random value for this attribute""" raise NotImplementedError @property def value(self): """ Return modified base_value """ v = self.base_value for modifier_name, modifier in self.modifiers.items(): v = modifier(v, self.manager.character) return v def __str__(self): return str(self.value) def get_default_value(self): return self.default_value def get_initial_data(self, **kwargs): """ Return initial data for form field """ return self.initial def form_field(self, **kwargs): return self.field( initial=self.get_initial_data(), default=self.get_default_value(), label=self.verbose_name, attribute=self ) @property def modifiers_descriptions(self): """return a list of tuples with modifiers, and the amount modified""" b = self.base_value descriptions = [] for modifier_name, modifier in self.modifiers.items(): mod_value = modifier(b, self.manager.character) - b descriptions.append((modifier_name, mod_value)) return descriptions