/
models.py
373 lines (288 loc) · 11.5 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# -*- coding: UTF-8 -*-
"""Declarations for models used in the stories app.
Thes e models are maintained by South.
After making a change, generate a migration using this command:
./manage.py schemamigration stories --auto
Migrations can be applied using this command:
./manage.py migrate stories
"""
import sys
import logging
from heapq import heapify, heappop, heappush
import redis
import json
from django.db import models
from django.conf import settings
logger = logging.getLogger(__name__)
class Grid(object):
"""Represents a 2d presentation of stories."""
def __init__(self, rows):
self.rows = rows
def __eq__(self, other):
return self.rows == other.rows
class GridRow(object):
def __init__(self, bins, tags=None):
self.bins = bins
self.tags = tags
def __eq__(self, other):
return self.tags == other.tags and self.bins == other.bins
class GridBin(object):
def __init__(self, stories, tags=None):
self.stories = stories
self.tags = tags
self.element_id = ('bin-' + '-'.join(str(x.id) for x in self.tags)
if tags else 'untagged-bin')
def __eq__(self, other):
return (self.tags == other.tags
and len(self.stories) == len(other.stories)
and all(x.id == y.id for (x, y) in zip(self.stories, other.stories)))
class Board(models.Model):
"""The universe of stories for one team, or group of teams."""
label = models.CharField(max_length=200)
slug = models.SlugField()
created = models.DateTimeField(auto_now_add=True, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)
def __unicode__(self):
return self.label
def make_grid(self, columns_def=None):
stories = toposorted(self.story_set.all())
if columns_def:
tag_idss = [(tag, [inf['id'] for inf in tag.story_set.values('id')])
for tag in columns_def.tag_set.all()]
bins = [GridBin([x for x in stories if x.id in ids], [tag])
for (tag, ids) in tag_idss]
missing = GridBin([x for x in stories if all(x not in bin.stories for bin in bins)])
return Grid([GridRow([missing] + bins)])
return Grid([GridRow([GridBin(stories)])])
def event_stream(self):
"""Return the event stream for this board."""
return EventRepeater().get_stream(self.id)
class Bag(models.Model):
"""A set of tags. One of the axes by which stories are classified."""
board = models.ForeignKey(Board, null=True)
name = models.SlugField(max_length=200)
def __unicode__(self):
return self.name
class Tag(models.Model):
"""One of the values of one axis of classification of stories."""
bag = models.ForeignKey(Bag)
name = models.SlugField(max_length=200)
def __unicode__(self):
return u'{0}:{1}'.format(self.bag.name, self.name)
class Meta:
ordering = ['id']
class Story(models.Model):
"""On thing on a board"""
board = models.ForeignKey(Board)
tag_set = models.ManyToManyField(Tag, related_name='story_set', blank=True)
succ = models.ForeignKey('self', null=True, blank=True,
help_text='Another story that follows this one in the queue.')
label = models.CharField(max_length=200)
slug = models.SlugField()
created = models.DateTimeField(auto_now_add=True, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)
class Meta:
verbose_name = 'story'
verbose_name_plural = 'stories'
ordering = ['created']
def __unicode__(self):
return self.label
def get_tag(self, bag):
"""Get the tag in this bag for this story.
This best used with exclusive bags.
"""
return self.tag_set.get(bag=bag)
def replace_tags(self, axes, tags):
"""Add the tags to this story, removing any from maching bags."""
for old_tag in self.tag_set.filter(bag__in=axes):
self.tag_set.remove(old_tag)
for tag in tags:
self.tag_set.add(tag)
class CyclesException(Exception):
pass
def toposort(xs):
"""Given a list of things with a partial order, sort them.
Things have id and succ_id attributes.
The id is unique. All id values are non-false
(i.e., not zero, empty string, None)
succ_id identfies a thing that goes after x.
A false value for succ_id means x has no successor.
Permute the list so that each x preceeds its successor
(the thing with id==x.succ_id).
Note that xs is sorted in place. As with sort, there is no return value.
"""
xs[:] = topoiter(xs)
def toposorted(xs):
return list(topoiter(xs))
def topoiter(xs):
# Here’s one traditional algorith for the general case.
# We are hoping to simplify based on a simplified graph
# where each node has at most one outgoing edge.
#
# L ← Empty list that will contain the sorted elements
# S ← Set of all nodes with no incoming edges
# while S is non-empty do
# remove a node n from S
# insert n into L
# for each node m with an edge e from n to m do
# remove edge e from the graph
# if m has no other incoming edges then
# insert m into S
# if graph has edges then
# return error (graph has at least one cycle)
# else
# return L (a topologically sorted order)
class Node(object):
__slots__ = ('x', 'index', 'succ', 'in_count')
def __init__(self, x, index):
self.x = x
self.index = index
self.in_count = 0
def __str__(self):
return str(x)
def __repr__(self):
return 'Node({0!r})'.format(self.x)
def __le__(self, other):
return self.index <= other.index
nodes = [Node(x, i) for (i, x) in enumerate(xs)]
nodes_by_id = dict((n.x.id,n) for n in nodes)
for n in nodes:
if n.x.succ_id:
n.succ = nodes_by_id[n.x.succ_id]
n.succ.in_count += 1
queue = [n for n in nodes if not n.in_count]
count = len(nodes)
while count:
if not(queue):
# This shows there are one or more cycles in the input.
# For our purposes it is more important to
# display all the stories than it is to complain
# about cycles. (But they should never happen.)
for n in nodes:
if n.in_count:
# this is a candidate for disentanglement
logger.warn('Breaking cycle during topoiter: changing {0} in-count from {1} to 0'.format(n.x, n.in_count))
n.in_count = 0
queue = [n]
break
else:
pass
else:
# None fond. Probably a bug.
raise CyclesException('Cycles in story succession links of length at least {0}'.format(count))
n = heappop(queue)
count -= 1
yield n.x
if n.x.succ_id:
m = n.succ
m.in_count -= 1
if not m.in_count:
heappush(queue, m)
def rearrange_objects(model, ids):
"""Rearrange entities in the model
Arguments --
model -- the class defining the universal set of entities
ids -- identifies some subset of the entities and
specifies the desired order of them relative to each other
The ids argument can be a subset of the entire
collection of model instances, or all of them.
The effect should be that the items are now in the
specified order, and occupy the position in the overall
ordering that the last item in the sequence once did.
"""
if not ids or len(ids) == 1:
# Nothing to do.
return
objs_by_id = model.objects.in_bulk(ids)
objs = [(objs_by_id[i] if i else None)for i in ids]
# We will insert the new sequence
# where the new FINAL item originally sat.
# Start by plucking all the others out of
# the established partial order.
# We do this by setting the successor of
# their predecessors to their successors.
# Create a list of succ relationships
# we need to patch over.
skips = dict((x.id, x.succ.id if x.succ else None) for x in objs[:-1])
# Reduce the list by eliminating chains within our ordered elements.
# Variant: len(skips)
while skips:
for i, succ_i in skips.items():
if i == succ_i:
# This happens if there are cycles within our items.
skips[i] = 0 # Break the cycle
break
if succ_i in skips:
succ2_i = skips[succ_i]
skips[i] = succ2_i
del skips[succ_i]
break
else:
break
for i, succ_i in skips.items():
model.objects.filter(succ__id=i).update(succ=succ_i)
# Make predecessor of last elt point to start of new order.
model.objects.filter(succ__id=ids[-1]).update(succ=ids[0])
# Establish the order amongs the new items.
for obj, succ in zip(objs, objs[1:]):
obj.succ = succ
obj.save()
_redis_class = None
def get_redis():
global _redis_class
if _redis_class is None:
module_name, class_name = settings.EVENT_REPEATER['ENGINE'].rsplit('.', 1)
module = __import__(module_name, globals(), locals(), [class_name])
_redis_class = getattr(module, class_name)
return _redis_class(**settings.EVENT_REPEATER['PARAMS'])
class EventsExpired(Exception):
pass
class EventRepeater(object):
def __init__(self):
self.redis = get_redis()
def get_stream(self, id):
return EventStream(self, id)
class EventStream(object):
"""Accepts events and repeats them when polled."""
def __init__(self, owner, id):
"""Create a repeater with this ID."""
self.owner = owner
self.redis = owner.redis
self.id = id
self.k_info = 'kanbo:ev:{0}:info'.format(self.id)
self.k_list = 'kanbo:ev:{0}:list'.format(self.id)
def next_seq(self):
x = self.redis.hget(self.k_info, 'next')
return (int(x) if x else 0)
def append(self, event):
jevent = json.dumps(event)
def try_append(p):
next_seq = p.hget(self.k_info, 'next')
len_evs = p.llen(self.k_list)
p.multi()
if not len_evs:
p.hset(self.k_info, 'start', next_seq)
p.hincrby(self.k_info, 'next', 1)
p.rpush(self.k_list, jevent)
p.expire(self.k_list, settings.EVENT_REPEATER['TTL'])
self.redis.transaction(try_append, self.k_info, self.k_list)
def as_json_starting_from(self, seq):
# As a transaction in case events arrive while
# we are calculating the boundaries of the range.
result = [None, None]
def try_transaction(p):
next_str = p.hget(self.k_info, 'next')
next = (int(next_str) if next_str else 0)
result[1] = next
if p.exists(self.k_list):
start_str = p.hget(self.k_info, 'start')
start = (int(start_str) if start_str else 0)
else:
start = next
if seq < start:
raise EventsExpired('{0}: events expired'.format(seq))
p.multi()
p.lrange(self.k_list, seq - start, -1)
jevs, = self.redis.transaction(try_transaction, self.k_info, self.k_list)
result[0] = '[{0}]'.format(', '.join(jevs))
return tuple(result)