Introduce Seat.sorting_rank (#1499)

* Introduce Seat.sorting_rank

* Fix comments

* Comments, for real
This commit is contained in:
Raphael Michel
2019-11-22 13:25:08 +01:00
committed by GitHub
parent 1e63eb743c
commit ee4f75c2fb
3 changed files with 49 additions and 5 deletions

View File

@@ -40,7 +40,7 @@ class SeatingPlan(LoggedModel):
layout = models.TextField(validators=[SeatingPlanLayoutValidator()])
Category = namedtuple('Categrory', 'name')
RawSeat = namedtuple('Seat', 'name guid number row category zone')
RawSeat = namedtuple('Seat', 'name guid number row category zone sorting_rank')
def __str__(self):
return self.name
@@ -60,16 +60,36 @@ class SeatingPlan(LoggedModel):
]
def iter_all_seats(self):
for z in self.layout_data['zones']:
for r in z['rows']:
for s in r['seats']:
# This returns all seats in a plan and assignes each of them a rank. The rank is used for sorting lists of
# seats later. The rank does not say anything about the *quality* of a seat, and is only meant as a heuristic
# to make it easier for humas to process lists of seats. The current algorithm assumes that there are less
# than 10'000 zones, less than 10'000 rows in every zone and less than 10'000 seats in every row.
# Respectively, no row/seat numbers may be numeric with a value of 10'000 or more. The resulting ranks
# *will* have gaps. We chose this way over just sorting the seats and continuously enumerating them as an
# optimization, because this way we do not need to update the rank of very seat if we change a plan a little.
for zi, z in enumerate(self.layout_data['zones']):
for ri, r in enumerate(z['rows']):
try:
row_rank = int(r['row_number'])
except ValueError:
row_rank = ri
for si, s in enumerate(r['seats']):
try:
seat_rank = int(s['seat_number'])
except ValueError:
seat_rank = si
rank = (
10000 * 10000 * zi + 10000 * row_rank + seat_rank
)
yield self.RawSeat(
number=s['seat_number'],
guid=s['seat_guid'],
name='{} {}'.format(r['row_number'], s['seat_number']), # TODO: Zone? Variable scheme?
row=r['row_number'],
zone=z['name'],
category=s['category']
category=s['category'],
sorting_rank=rank
)
@@ -98,6 +118,10 @@ class Seat(models.Model):
seat_guid = models.CharField(max_length=190, db_index=True)
product = models.ForeignKey('Item', null=True, blank=True, related_name='seats', on_delete=models.CASCADE)
blocked = models.BooleanField(default=False)
sorting_rank = models.BigIntegerField(default=0)
class Meta:
ordering = ['sorting_rank', 'seat_guid']
def __str__(self):
parts = []