bornhack-website/src/utils/range_fields.py
Thomas Steen Rasmussen eff4bfaf1c
SpeakerAvailability, EventSession, autoscheduler, and other goodies (#497)
* fix old bug where the get_days() method would return the wrong number of days, this was not discovered because our bootstrap script has been creating 9 day camps instead of 8 day camps (this has been fixed in a different commit)

* remove stray debug print

* output camp days in local timezone (CEST usually), not UTC

* speakeravailability commit of doom, originally intended for #385 but goes a bit further than that. Adds SpeakerAvailability and EventSession models, and models for the new autoscheduler. Update bootstrap script and more. New conference_autoscheduler dependency. Work in progress, but ready for playing around!

* add conference-scheduler to requirements

* rework migrations, work at bit with postgres range fields and bounds, change how speakeravailability is saved (continuous ranges instead of 1 hour chunks), add tests for utils/range_fields.py including adding hypothesis to requirements/dev.txt, add a test which runs our bootstrap script

* catch name collision in the right place, and load missing postgres extension in the migration

* add some verbosity to see what the travis issue might be

* manually create btree_gist extension in postgres, not sure why the BtreeGistExtension() operation in program/migrations/0085... isn't working in travis?

* create extension in the right database maybe

* lets try this then

* ok so the problem is not that the btree_gist extension isn't getting loaded, the problem is that GIST indexes do not work with uuid fields in postgres 9.6, lets take another stab at getting pg10 with postgis to work with in travis

* lets try normal socket connection

* add SPEAKER_AVAILABILITY_DAYCHUNK_HOURS=3 to travis environment_settings.py

* rework migrations, change so an autoschedule can work with multiple eventtypes, change AutoSlot model to use a DateTimeRangeField so we can use the database for more efficient lookups, add 'conflicts' self m2m for EventLocation to indicate when a room conflicts with another room, add a support_autoscheduling bool to EventType, add workshops to bootstrap script, add timing output to bootstrap script

* update README a bit, move some functionality to model methods, update jquery and jquery.datatables, include datatables in base.html instead of in each page, start adding backoffice schedule management views (unfinished), yolo commit so I can show valberg something

* Switch to a more simple way of using the autoscheduler, meaning we can remove the whole autoscheduler app and all models. All autoscheduler code is now in program/autoscheduler.py and a bit in backoffice views. Add more backoffice CRUD views for schedule management. Add datatables moment.js plugin to help table sorting of dates. Add Speaker{Proposal}EventConflict model to allow speakers to inform us which events they want to attend so we dont schedule them at the same time. Add EventTag model. New models not hooked up to anything yet.

* handle cases where there is no solution without failing, also dont return anything here

* wrong block kiddo

* switch from EventInstance to EventSlot as the way we schedule events. Finish backoffice content team views (mostly). Many small changes. Prod will need data migration of EventInstances -> EventSlots when the time comes.

* keep speakeravailability stuff a bit more DRY by using the AvailabilityMatrixViewMixin everywhere, add event_duration_minutes to EventSession create/update form, reverse the order we delete/create EventSlot objects when updating an EventSession

* go through all views, fix various little bugs here and there

* add missing migration

* add django-taggit, add tags for Events, add tags in bootstrap script, make AutoScheduler use tags. Add tags in forms and templates.

* fix taggit entry in requirements

* Fix our iCal view: Add uuid field to Event, add uuid property to EventSlot which calculates a consitent UUID for an event at a start time at a location. Use this as the schedule uuid. While here fix so our iCal export is valid, a few fields were missing, the iCal file now validates 100% OK.

* fix our FRAB xml export view

* comment the EventSlot.uuid property better

* typo in comment

* language

Co-Authored-By: Benjamin Balder Bach <benjamin@overtag.dk>

* language

Co-Authored-By: Benjamin Balder Bach <benjamin@overtag.dk>

* Update src/backoffice/templates/autoschedule_debug_events.html

Co-Authored-By: Benjamin Balder Bach <benjamin@overtag.dk>

* add a field to make this form look less weird. No difference in functionality.

* remove stray print and refactor this form init a bit

* fix ScheduleView

* only show slots where all speakers are available when scheduling events manually in backoffice

* make event list sortable by video recording column

* update description on {speaker|event}proposal models reason field

* remove badge showing number of scheduled slots for each event in backoffice eventlist. it was unclear what the number meant and it doesn't really fit

* remember to consider events in the same location when deciding whether a slot is available or not

* add is_available() method to EventLocation, add clean_location() method to EventSlot, call it from EventSlot.clean(), update a bit of text in eventslotunschedule template

* fix EventSession.get_available_slots() so it doesnt return busy slots as available, and since this means we can no longer schedule stuff in the lunchbreak lower the number of talks in the bootstrap script a bit so we have a better chance of having a solvable problem

* fix the excludefilter in EventSession.get_available_slots() for real this time, also fix an icon and add link in event schedule template in backoffice

* show message when no slots are available for manual scheduling in backoffice

* add event_conflicts to SpeakerUpdateView form in backoffice

* fix link to speaker object in speakerproposal list in backoffice

* allow blank tags

* make duration validation depend on the eventtype event_duration_minutes if we have one. fix help_text and label and placeholder for all duration fields

* allow music acts up to 180 mins in the bootstrap data

* fix wrong eventtype name for recreational events in speakerproposalform

* stretch the colspan one cell more

* save event_conflicts m2m when submitting speaker and event together

* form not self, and add succes message

* move js function toggleclass() to bornhack.js and rename to toggle_sa_form_class(), function is used in several templates and was missing when submitting combined proposals

* move the no-js removal to the top of ready() function

This will allow other javascript initialization (eg. DataTable) to see the elements and initialize accordingly (eg. column width for tables)

* Fixed problem with event feedback detail view

* Fixed problem with event feedback list view

* introduce a get_tzrange_days() function and use that to get the relevant days for the matrix instead of camp.get_days(), thereby fixing some display issues when eventsessions cross dates

* show submitting user and link to proposal on backoffice event detail page, change User to Submitter in backoffice speaker list table

* show warning by the buttons when a proposal cannot be approved, and show better text on approve/reject buttons

* disable js schedule, save m2m, prefetch some stuff

* fix broken date header in table

* remove use of djangos regular slugify function, use the new utils.slugs.unique_slugify() instead

Co-authored-by: Thomas Steen Rasmussen <tykling@bornhack.org>
Co-authored-by: Benjamin Balder Bach <benjamin@overtag.dk>
Co-authored-by: Thomas Flummer <tf@flummer.net>
2020-06-03 21:18:06 +02:00

304 lines
8.7 KiB
Python

"""
Borrowed from https://gist.github.com/schinckel/aeea9c0f807dd009bf47566df7ac5054
This module overrides the Range.__and__ function, so that it returns a boolean value
based on if the two objects overlap.
The rationale behind this is that it mirrors the `range && range` operator in postgres.
There are tests for this, that hit the database with randomly generated ranges and ensure
that the database and this method agree upon the results.
There is also a more complete `isempty()` method, which examines the bounds types and values,
and determines if the object is indeed empty. This is required when python-created range objects
are dealt with, as these are not normalised the same way that postgres does.
"""
import datetime
from psycopg2.extras import Range
OFFSET = {
int: 1,
datetime.date: datetime.timedelta(1),
}
def normalise(instance):
"""
In the case of discrete ranges (integer, date), then we normalise the values
so it is in the form [start,finish), the same way that postgres does.
If the lower value is None, we normalise this to (None,finish)
"""
if instance.isempty:
return instance
lower = instance.lower
upper = instance.upper
bounds = list(instance._bounds)
if lower is not None and lower == upper and instance._bounds != "[]":
return instance.__class__(empty=True)
if lower is None:
bounds[0] = "("
elif bounds[0] == "(" and type(lower) in OFFSET:
lower += OFFSET[type(lower)]
bounds[0] = "["
if upper is None:
bounds[1] = ")"
elif bounds[1] == "]" and type(upper) in OFFSET:
upper += OFFSET[type(upper)]
bounds[1] = ")"
if lower is not None and lower == upper and bounds != ["[", "]"]:
return instance.__class__(empty=True)
return instance.__class__(lower, upper, "".join(bounds))
def __and__(self, other):
if not isinstance(other, self.__class__):
raise TypeError(
"unsupported operand type(s) for &: '{}' and '{}'".format(
self.__class__.__name__, other.__class__.__name__
)
)
self = normalise(self)
other = normalise(other)
# If _either_ object is empty, then it will never overlap with any other one.
if self.isempty or other.isempty:
return False
if other < self:
return other & self
# Because we can't compare None with a datetime.date(), we need to deal
# with the cases where one (or both) of the parts are None first.
if self.lower is None:
if self.upper is None or other.lower is None:
return True
if self.upper_inc and other.lower_inc:
return self.upper >= other.lower
return self.upper > other.lower
if self.upper is None:
if other.upper is None:
return True
if self.lower_inc and other.upper_inc:
return self.lower <= other.upper
return self.lower < other.upper
# Now, all we care about is self.upper_inc and other.lower_inc
if self.upper_inc and other.lower_inc:
return self.upper >= other.lower
else:
return self.upper > other.lower
def __eq__(self, other):
if not isinstance(other, Range):
return False
self = normalise(self)
other = normalise(other)
return (
self._lower == other._lower
and self._upper == other._upper
and self._bounds == other._bounds
)
def range_merge(self, other):
"Union"
self = normalise(self)
other = normalise(other)
bounds = [None, None]
if self.isempty:
return self
if other.isempty:
return other
if self > other:
self, other = other, self
if self.upper is not None and other.lower is not None:
if not self.upper_inc and other.lower <= self.upper:
# They overlap.
pass
else:
raise ValueError("Result of range union would not be contiguous")
if self.lower is None:
lower = None
bounds[0] = "("
elif self.lower_inc != other.lower_inc:
# The bounds differ, so we need to use the complicated logic.
raise NotImplementedError()
else:
# The bounds are the same, so we can just use the lower value.
lower = min(self.lower, other.lower)
bounds[0] = self._bounds[0]
if self.upper is None or other.upper is None:
upper = None
bounds[1] = ")"
elif self.upper_inc != other.upper_inc:
raise NotImplementedError()
else:
upper = max(self.upper, other.upper)
bounds[1] = self._bounds[1]
return normalise(self.__class__(lower, upper, "".join(bounds)))
def range_intersection(self, other):
self = normalise(self)
other = normalise(other)
if not self & other:
return self.__class__(empty=True)
# We need to use custom comparisons because non-number range types will fail to compare.
# Also, min(X, None) means min(X, Infinity), really.
if self.lower is None:
lower = other.lower
elif other.lower is None:
lower = self.lower
else:
lower = max(self.lower, other.lower)
if self.upper is None:
upper = other.upper
elif other.upper is None:
upper = self.upper
else:
upper = min(self.upper, other.upper)
return normalise(self.__class__(lower, upper, "[)"))
def range_contains(self, other):
if self._bounds is None:
return False
if type(self) == type(other):
return self & other and self + other == self
# We have two tests to make in each case - is the value out of the lower bound,
# and is the value out on the upper bound. We can make a series of tests, and if we ever find
# a situation where we _are_ out of bounds, return at that point.
if self.lower is not None:
if self.lower_inc:
if other < self.lower:
return False
elif other <= self.lower:
return False
if self.upper is not None:
if self.upper_inc:
if other > self.upper:
return False
elif other >= self.upper:
return False
return True
def deconstruct(self):
return (
"{}.{}".format(self.__class__.__module__, self.__class__.__name__),
[self.lower, self.upper, self._bounds],
{},
)
Range.__add__ = range_merge
Range.__and__ = __and__
Range.__eq__ = __eq__
Range.__mul__ = range_intersection
Range.__contains__ = range_contains
Range.deconstruct = deconstruct
_BOUNDS_SWAP = {"[": ")", "]": "(", "(": "]", ")": "[",}.get
def safe_subtract(initial, subtract):
"""
Subtract the range "subtract" from the range "initial".
Always return an array of ranges (which may be empty).
"""
_Range = initial.__class__
sub_bounds = "".join(map(_BOUNDS_SWAP, subtract._bounds))
# Simplest case - ranges are the same, or the source one is fully contained within
# the subtracting one, then we get an empty list of ranges.
if subtract == initial or initial in subtract:
return []
# If the ranges don't overlap, then we retain the source.
if not initial & subtract:
return [initial]
# We will have either one or two objects, depending upon if the subtractor overlaps one of the bounds or not.
# We know that both of them will not overlap the bounds, because that case has already been dealt with.
if initial.upper in subtract or (
not initial.upper_inc and initial.upper == subtract.upper
):
return [
_Range(
initial.lower,
subtract.lower,
"{}{}".format(initial._bounds[0], sub_bounds[0],),
)
]
elif initial.lower in subtract or (
not initial.lower_inc and initial.lower == subtract.lower
):
return [
_Range(
subtract.upper,
initial.upper,
"{}{}".format(sub_bounds[1], initial._bounds[1]),
)
]
else:
return [
_Range(
initial.lower,
subtract.lower,
"{}{}".format(initial._bounds[0], sub_bounds[0]),
),
_Range(
subtract.upper,
initial.upper,
"{}{}".format(sub_bounds[1], initial._bounds[1]),
),
]
def array_subtract(initial, subtract):
"""
subtract the range from each item in the initial array.
"""
result = []
for _range in initial:
result.extend(safe_subtract(_range, subtract))
return result
def array_subtract_all(initial, subtract):
"""
Subtract all overlapping ranges in subtract from all ranges in initial.
"""
result = list(initial)
for other in subtract:
result = array_subtract(result, other)
return result