issue.py (3622B)
1 # standard imports 2 import uuid 3 import json 4 import datetime 5 6 # local imports 7 from piknik.identity import Identity 8 from piknik.error import UnknownIdentityError 9 from piknik.error import ExistsError 10 11 12 def id_generator(): 13 return str(uuid.uuid4()) 14 15 class Issue: 16 17 def __init__(self, title, issue_id=None, alias=None, id_generator=id_generator): 18 if issue_id == None: 19 issue_id = id_generator() 20 self.id = issue_id 21 self.title = title 22 self.assigned = [] 23 self.assigned_time = [] 24 self.dependencies = [] 25 self.alias = alias 26 self.owner_idx = 0 27 28 29 @staticmethod 30 def from_str(s): 31 r = json.loads(s) 32 o = Issue(title=r['title'], issue_id=r['id']) 33 for i, k in enumerate(r['assigned'].keys()): 34 p = Identity(k) 35 o.assigned.append(p) 36 t = datetime.datetime.utcfromtimestamp(r['assigned'][k]) 37 o.assigned_time.append(t) 38 if r['owner'] == None or k == r['owner']: 39 r['owner'] = k 40 o.owner_idx = i 41 for v in r['dependencies']: 42 o.dep(v) 43 o.alias = r.get('alias') 44 return o 45 46 47 def assign(self, identity, t=None): 48 if identity in self.assigned: 49 raise ExistsError(identity) 50 if t == None: 51 t = datetime.datetime.utcnow() 52 self.assigned.append(identity) 53 self.assigned_time.append(t) 54 55 56 def dep(self, dependency_issue_id): 57 if dependency_issue_id in self.dependencies: 58 raise ExistsError(self.id, dependency_issue_id) 59 self.dependencies.append(dependency_issue_id) 60 61 62 def undep(self, dependency_issue_id=None): 63 if dependency_issue_id == None: 64 self.dependencies = [] 65 return 66 self.dependencies.remove(dependency_issue_id) 67 68 69 def get_assigned(self): 70 return list(zip(self.assigned, self.assigned_time)) 71 72 73 def get_dependencies(self): 74 return self.dependencies 75 76 77 def unassign(self, identity): 78 for i, v in enumerate(self.assigned): 79 if v == identity: 80 self.assigned.remove(v) 81 if i == self.owner_idx: 82 self.owner_idx = 0 83 return True 84 raise UnknownIdentityError(identity) 85 86 87 def owner(self): 88 try: 89 return self.assigned[self.owner_idx] 90 except IndexError: 91 pass 92 93 raise UnknownIdentityError 94 95 96 def set_owner(self, identity): 97 r = self.owner() 98 if identity == r: 99 return False 100 101 for i, v in enumerate(self.assigned): 102 if v == identity: 103 self.owner_idx = i 104 return True 105 106 raise UnknownIdentityError(identity) 107 108 109 def __str__(self): 110 o = { 111 'id': str(self.id), 112 'title': self.title, 113 'assigned': {}, 114 'dependencies': self.dependencies, 115 'alias': self.alias, 116 'owner': None, 117 } 118 119 for i, v in enumerate(self.get_assigned()): 120 aid = v[0].id() 121 o['assigned'][aid] = v[1].timestamp() 122 if self.owner_idx == i: 123 o['owner'] = aid 124 125 return json.dumps(o) 126 127 128 def __eq__(self, o): 129 if o.id != self.id: 130 return False 131 if o.title != self.title: 132 return False 133 if len(self.assigned) != len(o.assigned): 134 return False 135 for i, v in enumerate(self.assigned): 136 if o.assigned[i] != v: 137 return False 138 139 return True