| 29 | |
| 30 | |
| 31 | class Objective(FirstClassObjectInterface, BaseObject): |
| 32 | |
| 33 | schema = ObjectiveSchema() |
| 34 | |
| 35 | @property |
| 36 | def unique(self): |
| 37 | return self.hash('%s' % self.id) |
| 38 | |
| 39 | @property |
| 40 | def percentage(self): |
| 41 | if len(self.goals) > 0: |
| 42 | return 100 * (len([g for g in self.goals if g.satisfied() is True])/len(self.goals)) |
| 43 | return 0 |
| 44 | |
| 45 | def completed(self, facts=None): |
| 46 | return not any(x.satisfied(facts) is False for x in self.goals) |
| 47 | |
| 48 | def __init__(self, id='', name='', description='', goals=None): |
| 49 | super().__init__() |
| 50 | self.id = id if id else str(uuid.uuid4()) |
| 51 | self.name = name |
| 52 | self.description = description |
| 53 | self.goals = goals if goals else [] |
| 54 | |
| 55 | def store(self, ram): |
| 56 | existing = self.retrieve(ram['objectives'], self.unique) |
| 57 | if not existing: |
| 58 | ram['objectives'].append(self) |
| 59 | return self.retrieve(ram['objectives'], self.unique) |
| 60 | existing.update('name', self.name) |
| 61 | existing.update('description', self.description) |
| 62 | existing.update('goals', self.goals) |
| 63 | return existing |