Files
PyRIGS/RIGS/views/ical.py
AJ 08171767a0 Port to Django 5.2 (#635)
* Port to Django 5.2

* Pin pluggy to 1.2.0

Any newer and the mystery importlib metadata error appears. Weird! >_>

* Update for premailer changed default

* Update view logic for is_ajax being changed to a template context processor

* Port a few more tests to pytest proper

Having two distinct test flavours is giving me a headache

* Version 1 dockerfile

Makes a VERY big image, I suspect we can optimise this a lot...

* Optimise dockerfile a little lot a bit

* fix(users): change logout link to POST request

* fix(tests): fix some syntax errors in test code

still got lots of failed tests :(

* fix: replace deprecated Django APIs

* ci: update Dockerfile

* ci: update Dockerfile

* fix(dependencies+tests): update EoL/vulnerable dependencies and improve test stability

* Upgrade Python from 3.10 to 3.12
* Update frontend dependencies, replace node-sass with sass

* chore(logging): ignore dangling obj reference warning from pypdf

* ci: update compose.yml for prod deployment

- Add Nginx as a reverse proxy
- Add cert-selfsign.sh for generating self-signed certificates
- Add certbot-issue.sh for Let's Encrypt certificate issuance and renewal
- Add cron-install.sh and cron-uninstall.sh for system cron management
- Add .env.example as an environment variable template

* style: reformat code

* ci: add more sleep trying to pass tests

* ci: ignore browser-based tests during ci testing

* fix: create home for the new user during Dockerfile building

* chore: remove heroku conf file

* fix: RIGS not franken anymore

* fix: restore is_ajax as a boolean and split out the context processor

* test: remove Selenium interaction tests and their CI workarounds

* fix: harden production settings (CSRF origins, env casts, ADMINS) and drop dead debug toolbar code

* deploy: serve static files from nginx, pin PGDATA, and make cert scripts loud on failure

* build: switch .dockerignore to an allowlist

* build: drop unused dependencies, soft-pin the rest and target Python 3.14

* build: move image to Python 3.14 / Node 24 and force DEBUG off

* ci: add dependabot config for uv, npm, docker and actions

* build: narrow Sass deprecation silencing to @import and require Node 24

* build: provide placeholder env for collectstatic now that DEBUG is off in the image

* deploy: add plain-HTTP nginx config for local development and use it in compose

* deploy: remove self-signed cert script

* build: make the image multi-arch with official node and python base images

* ci: lint and test on PRs, build the image on PRs and push it to GHCR on master

* docs: add local running and sample data instructions

* fix: report a form error instead of crashing when big power has no Power MIC

* fix: upgrade pypdf and urllib3 to patched releases

* fix: apply non-breaking npm audit fixes

* fix: strip whitespace in ALLOWED_HOSTS and drop misleading HSTS preload

* deploy: add a pyrigs healthcheck and make nginx wait for it

* build: pin the uv image version

* fix: correct the check-in person picker condition and use BeautifulSoup's string argument

* refactor: replace unique_together with UniqueConstraint

* build: replace pycodestyle with ruff and fix what it found

Removes unused imports and variables, and fixes a few real problems it
surfaced:
- EventCheckIn.active() referenced an undefined name and raised NameError;
  it now returns whether the check-in has no end time
- RIGS.admin defined EventChecklistAdmin twice; the second is now
  PowerTestRecordAdmin
- RIGS/tests/conftest.py used date/timedelta without importing them
- the signal-registering imports in apps.py are kept with noqa

pycodestyle config in setup.cfg is dropped.

* style: format the codebase with ruff

* style: normalise line endings, trailing whitespace and end-of-file newlines

* ci: run ruff and file hygiene through prek, and document it

---------

Co-authored-by: Hang <me@hangxu.me>
Co-authored-by: Joe Banks <joe@jb3.dev>
2026-10-01 23:43:41 +01:00

198 lines
6.4 KiB
Python

import datetime
import pytz
from django.conf import settings
from django.db.models import Q
from django_ical.views import ICalFeed
from RIGS import models
class CalendarICS(ICalFeed):
"""
A simple event calender
"""
# Metadata which is passed on to clients
product_id = "RIGS"
title = "RIGS Calendar"
timezone = settings.TIME_ZONE
file_name = "rigs.ics"
# Cancelled = 'cancelled' = False
# Dry Hire = 'dry-hire' = True
# Non Rig = 'non-rig' = True
# Rig = 'rig' = True
# Provisional = 'provisional' = True
# Confirmed/Booked = 'confirmed' = True
# Only MIC = 'mic' = False
def get_object(self, request, *args, **kwargs):
params = {}
params["dry-hire"] = request.GET.get("dry-hire", "true") == "true"
params["non-rig"] = request.GET.get("non-rig", "true") == "true"
params["rig"] = request.GET.get("rig", "true") == "true"
params["cancelled"] = request.GET.get("cancelled", "false") == "true"
params["provisional"] = request.GET.get("provisional", "true") == "true"
params["confirmed"] = request.GET.get("confirmed", "true") == "true"
params["only_mic"] = request.GET.get("only_mic", "false") == "true"
params["user"] = kwargs["user"]
return params
def description(self, params):
desc = (
"Calendar generated by RIGS system. This includes event types: "
+ ("Rig, " if params["rig"] else "")
+ ("Non-rig, " if params["non-rig"] else "")
+ ("Dry Hire " if params["dry-hire"] else "")
+ "\n"
)
desc = (
desc
+ "Includes events with status: "
+ ("Cancelled, " if params["cancelled"] else "")
+ ("Provisional, " if params["provisional"] else "")
+ ("Confirmed/Booked, " if params["confirmed"] else "")
)
return desc
def items(self, params):
# include events from up to 1 year ago
start = datetime.datetime.now() - datetime.timedelta(days=365)
filter = Q(start_date__gte=start)
typeFilters = Q(pk=None) # Need something that is false for every entry
if params["dry-hire"]:
typeFilters = typeFilters | Q(dry_hire=True, is_rig=True)
if params["non-rig"]:
typeFilters = typeFilters | Q(is_rig=False)
if params["rig"]:
typeFilters = typeFilters | Q(is_rig=True, dry_hire=False)
statusFilters = Q(pk=None) # Need something that is false for every entry
if params["cancelled"]:
statusFilters = statusFilters | Q(status=models.Event.CANCELLED)
if params["provisional"]:
statusFilters = statusFilters | Q(status=models.Event.PROVISIONAL)
if params["confirmed"]:
statusFilters = statusFilters | Q(status=models.Event.CONFIRMED) | Q(status=models.Event.BOOKED)
filter = filter & typeFilters & statusFilters
if params["only_mic"]:
filter = filter & Q(mic=params["user"])
return (
models.Event.objects.filter(filter)
.order_by("-start_date")
.select_related("person", "organisation", "venue", "mic")
)
def item_title(self, item):
title = ""
# Prefix title with status (if it's a critical status)
if item.cancelled:
title += "CANCELLED: "
if not item.is_rig:
title += "NON-RIG: "
if item.dry_hire:
title += "DRY HIRE: "
# Add the rig name
title += item.name
# Add the status
title += f" ({item.get_status_display()})"
return title
def item_start_datetime(self, item):
return item.earliest_time
def item_end_datetime(self, item):
# if isinstance(item.latest_time, datetime.date): # Ical end_datetime is non-inclusive, so add a day
# return item.latest_time + datetime.timedelta(days=1)
return item.latest_time
def item_location(self, item):
return item.venue
def item_description(self, item):
# Create a nice information-rich description
# note: only making use of information available to "non-keyholders"
tz = pytz.timezone(self.timezone)
desc = f"Rig ID = {item.display_id}\n"
desc += f"Event = {item.name}\n"
desc += "Venue = " + (item.venue.name if item.venue else "---") + "\n"
if item.is_rig and item.person:
desc += (
"Client = "
+ item.person.name
+ ((" for " + item.organisation.name) if item.organisation else "")
+ "\n"
)
desc += f"Status = {item.get_status_display()}\n"
desc += "MIC = " + (item.mic.name if item.mic else "---") + "\n"
desc += "\n"
if item.meet_at:
desc += (
"Crew Meet = "
+ (item.meet_at.astimezone(tz).strftime("%Y-%m-%d %H:%M") if item.meet_at else "---")
+ "\n"
)
if item.access_at:
desc += (
"Access At = "
+ (item.access_at.astimezone(tz).strftime("%Y-%m-%d %H:%M") if item.access_at else "---")
+ "\n"
)
if item.start_date:
desc += (
"Event Start = "
+ item.start_date.strftime("%Y-%m-%d")
+ ((" " + item.start_time.strftime("%H:%M")) if item.has_start_time else "")
+ "\n"
)
if item.end_date:
desc += (
"Event End = "
+ item.end_date.strftime("%Y-%m-%d")
+ ((" " + item.end_time.strftime("%H:%M")) if item.has_end_time else "")
+ "\n"
)
desc += "\n"
if item.description:
desc += f"Event Description:\n{item.description}\n\n"
# if item.notes: // Need to add proper keyholder checks before this gets put back
# desc += 'Notes:\n'+item.notes+'\n\n'
desc += f"URL = https://rigs.nottinghamtec.co.uk{item.get_absolute_url()}"
return desc
def item_link(self, item):
# Make a link to the event in the web interface
return item.get_absolute_url()
def item_updated(self, item): # some ical clients will display this
return item.last_edited_at
def item_guid(self, item): # use the rig-id as the ical unique event identifier
return item.pk