bornhack-website/src/teams/models.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

422 lines
13 KiB
Python

import logging
from django.conf import settings
from django.contrib.postgres.fields import DateTimeRangeField
from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse_lazy
from utils.models import CampRelatedModel, CreatedUpdatedModel, UUIDModel
from utils.slugs import unique_slugify
logger = logging.getLogger("bornhack.%s" % __name__)
TEAM_GUIDE_TEMPLATE = """
## Preparations
...
## Camp setup
...
## During camp
...
## Takedown
...
## Notes for next year
1. Remember to take notes
1. ...
"""
class Team(CampRelatedModel):
camp = models.ForeignKey(
"camps.Camp", related_name="teams", on_delete=models.PROTECT
)
name = models.CharField(max_length=255, help_text="The team name")
slug = models.SlugField(
max_length=255,
blank=True,
help_text="Url slug for this team. Leave blank to generate based on team name",
)
shortslug = models.SlugField(
help_text="Abbreviated version of the slug. Used in places like IRC channel names where space is limited"
)
description = models.TextField()
permission_set = models.CharField(
max_length=100,
blank=True,
default="",
help_text="The name of this Teams set of permissions. Must be a value from camps.models.Permission.Meta.permissions.",
)
needs_members = models.BooleanField(
default=True, help_text="Check to indicate that this team needs more members"
)
members = models.ManyToManyField(
"auth.User", related_name="teams", through="teams.TeamMember"
)
# mailing list related fields
mailing_list = models.EmailField(blank=True)
mailing_list_archive_public = models.BooleanField(
default=False, help_text="Check if the mailing list archive is public"
)
mailing_list_nonmember_posts = models.BooleanField(
default=False,
help_text="Check if the mailinglist allows non-list-members to post",
)
# IRC related fields
public_irc_channel_name = models.CharField(
blank=True,
null=True,
unique=True,
max_length=50,
help_text="The public IRC channel for this team. Will be shown on the team page so people know how to reach the team. Leave empty if the team has no public IRC channel.",
)
public_irc_channel_bot = models.BooleanField(
default=False,
help_text="Check to make the bot join the teams public IRC channel. Leave unchecked to disable the IRC bot for this channel.",
)
public_irc_channel_managed = models.BooleanField(
default=False,
help_text="Check to make the bot manage the teams public IRC channel by registering it with NickServ and setting +Oo for all teammembers.",
)
public_irc_channel_fix_needed = models.BooleanField(
default=False,
help_text="Used to indicate to the IRC bot that this teams public IRC channel is in need of a permissions and ACL fix.",
)
private_irc_channel_name = models.CharField(
blank=True,
null=True,
unique=True,
max_length=50,
help_text="The private IRC channel for this team. Will be shown to team members on the team page. Leave empty if the team has no private IRC channel.",
)
private_irc_channel_bot = models.BooleanField(
default=False,
help_text="Check to make the bot join the teams private IRC channel. Leave unchecked to disable the IRC bot for this channel.",
)
private_irc_channel_managed = models.BooleanField(
default=False,
help_text="Check to make the bot manage the private IRC channel by registering it with NickServ, setting +I and maintaining the ACL.",
)
private_irc_channel_fix_needed = models.BooleanField(
default=False,
help_text="Used to indicate to the IRC bot that this teams private IRC channel is in need of a permissions and ACL fix.",
)
shifts_enabled = models.BooleanField(
default=False,
help_text="Does this team have shifts? This enables defining shifts for this team.",
)
class Meta:
ordering = ["name"]
unique_together = (("name", "camp"), ("slug", "camp"))
guide = models.TextField(
blank=True,
help_text="HowTo guide for this year (and next year)",
verbose_name="team guide (Markdown)",
default=TEAM_GUIDE_TEMPLATE,
)
def __str__(self):
return "{} ({})".format(self.name, self.camp)
def get_absolute_url(self):
return reverse_lazy(
"teams:general",
kwargs={"camp_slug": self.camp.slug, "team_slug": self.slug},
)
def save(self, **kwargs):
# generate slug if needed
if not self.slug:
self.slug = unique_slugify(
self.name,
slugs_in_use=self.__class__.objects.filter(camp=self.camp).values_list(
"slug", flat=True
),
)
# set shortslug if needed
if not self.shortslug:
self.shortslug = self.slug
super().save(**kwargs)
def clean(self):
# make sure the public irc channel name is prefixed with a # if it is set
if self.public_irc_channel_name and self.public_irc_channel_name[0] != "#":
self.public_irc_channel_name = "#%s" % self.public_irc_channel_name
# make sure the private irc channel name is prefixed with a # if it is set
if self.private_irc_channel_name and self.private_irc_channel_name[0] != "#":
self.private_irc_channel_name = "#%s" % self.private_irc_channel_name
# make sure the channel names are not reserved
if (
self.public_irc_channel_name == settings.IRCBOT_PUBLIC_CHANNEL
or self.public_irc_channel_name == settings.IRCBOT_VOLUNTEER_CHANNEL
):
raise ValidationError("The public IRC channel name is reserved")
if (
self.private_irc_channel_name == settings.IRCBOT_PUBLIC_CHANNEL
or self.private_irc_channel_name == settings.IRCBOT_VOLUNTEER_CHANNEL
):
raise ValidationError("The private IRC channel name is reserved")
# make sure public_irc_channel_name is not in use as public or private irc channel for another team, case insensitive
if self.public_irc_channel_name:
if (
Team.objects.filter(
private_irc_channel_name__iexact=self.public_irc_channel_name
)
.exclude(pk=self.pk)
.exists()
or Team.objects.filter(
public_irc_channel_name__iexact=self.public_irc_channel_name
)
.exclude(pk=self.pk)
.exists()
):
raise ValidationError(
"The public IRC channel name is already in use on another team!"
)
# make sure private_irc_channel_name is not in use as public or private irc channel for another team, case insensitive
if self.private_irc_channel_name:
if (
Team.objects.filter(
private_irc_channel_name__iexact=self.private_irc_channel_name
)
.exclude(pk=self.pk)
.exists()
or Team.objects.filter(
public_irc_channel_name__iexact=self.private_irc_channel_name
)
.exclude(pk=self.pk)
.exists()
):
raise ValidationError(
"The private IRC channel name is already in use on another team!"
)
@property
def memberships(self):
"""
Returns all TeamMember objects for this team.
Use self.members.all() to get User objects for all members,
or use self.memberships.all() to get TeamMember objects for all members.
"""
return TeamMember.objects.filter(team=self)
@property
def approved_members(self):
"""
Returns only approved members (returns User objects, not TeamMember objects)
"""
return self.members.filter(teammember__approved=True)
@property
def unapproved_members(self):
"""
Returns only unapproved members (returns User objects, not TeamMember objects)
"""
return self.members.filter(teammember__approved=False)
@property
def responsible_members(self):
"""
Return only approved and responsible members
Used to handle permissions for team management
"""
return self.members.filter(
teammember__approved=True, teammember__responsible=True
)
@property
def regular_members(self):
"""
Return only approved and not responsible members with
an approved public_credit_name.
Used on the people pages.
"""
return self.members.filter(
teammember__approved=True, teammember__responsible=False
)
@property
def unnamed_members(self):
"""
Returns only approved and not responsible members,
without an approved public_credit_name.
"""
return self.members.filter(
teammember__approved=True,
teammember__responsible=False,
profile__public_credit_name_approved=False,
)
class TeamMember(CampRelatedModel):
user = models.ForeignKey(
"auth.User",
on_delete=models.PROTECT,
help_text="The User object this team membership relates to",
)
team = models.ForeignKey(
"teams.Team",
on_delete=models.PROTECT,
help_text="The Team this membership relates to",
)
approved = models.BooleanField(
default=False, help_text="True if this membership is approved. False if not."
)
responsible = models.BooleanField(
default=False,
help_text="True if this teammember is responsible for this Team. False if not.",
)
irc_acl_fix_needed = models.BooleanField(
default=False,
help_text="Maintained by the IRC bot, manual editing should not be needed. Will be set to true when a teammember sets or changes NickServ username, and back to false after the ACL has been fixed by the bot.",
)
class Meta:
ordering = ["-responsible", "-approved"]
def __str__(self):
return "{} is {} {} member of team {}".format(
self.user,
"" if self.approved else "an unapproved",
"" if not self.responsible else "a responsible",
self.team,
)
@property
def camp(self):
""" All CampRelatedModels must have a camp FK or a camp property """
return self.team.camp
camp_filter = "team__camp"
class TeamTask(CampRelatedModel):
team = models.ForeignKey(
"teams.Team",
related_name="tasks",
on_delete=models.PROTECT,
help_text="The team this task belongs to",
)
name = models.CharField(max_length=100, help_text="Short name of this task")
slug = models.SlugField(
max_length=255, blank=True, help_text="url slug, leave blank to autogenerate"
)
description = models.TextField(
help_text="Description of the task. Markdown is supported."
)
when = DateTimeRangeField(
blank=True,
null=True,
help_text="When does this task need to be started and/or finished?",
)
completed = models.BooleanField(
help_text="Check to mark this task as completed.", default=False
)
class Meta:
ordering = ["completed", "when", "name"]
unique_together = (("name", "team"), ("slug", "team"))
def get_absolute_url(self):
return reverse_lazy(
"teams:task_detail",
kwargs={
"camp_slug": self.team.camp.slug,
"team_slug": self.team.slug,
"slug": self.slug,
},
)
@property
def camp(self):
""" All CampRelatedModels must have a camp FK or a camp property """
return self.team.camp
camp_filter = "team__camp"
def save(self, **kwargs):
# generate slug if needed
if not self.slug:
self.slug = unique_slugify(
self.name,
slugs_in_use=self.__class__.objects.filter(team=self.team).values_list(
"slug", flat=True
),
)
super().save(**kwargs)
class TaskComment(UUIDModel, CreatedUpdatedModel):
task = models.ForeignKey(
"teams.TeamTask", on_delete=models.PROTECT, related_name="comments"
)
author = models.ForeignKey("teams.TeamMember", on_delete=models.PROTECT)
comment = models.TextField()
class TeamShift(CampRelatedModel):
class Meta:
ordering = ("shift_range",)
team = models.ForeignKey(
"teams.Team",
related_name="shifts",
on_delete=models.PROTECT,
help_text="The team this shift belongs to",
)
shift_range = DateTimeRangeField()
team_members = models.ManyToManyField(TeamMember, blank=True)
people_required = models.IntegerField(default=1)
@property
def camp(self):
""" All CampRelatedModels must have a camp FK or a camp property """
return self.team.camp
camp_filter = "team__camp"
def __str__(self):
return "{} team shift from {} to {}".format(
self.team.name, self.shift_range.lower, self.shift_range.upper
)
@property
def users(self):
return [member.user for member in self.team_members.all()]