Skip to content

Commit

Permalink
Add PrairieTest integration
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Sep 11, 2024
1 parent 9deafd9 commit 4dfba19
Show file tree
Hide file tree
Showing 10 changed files with 522 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .ci/run-mypy.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#! /bin/bash

mypy relate course accounts
mypy relate course accounts prairietest
Empty file added prairietest/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions prairietest/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import annotations

from django import forms
from django.contrib import admin

from prairietest.models import AllowEvent, DenyEvent, PrairieTestFacility


class PrairieTestFacilityAdminForm(forms.ModelForm):
class Meta:
model = PrairieTestFacility
fields = "__all__"
widgets = {
"secret": forms.PasswordInput,
}


@admin.register(PrairieTestFacility)
class PrairieTestFacilityAdmin(admin.ModelAdmin):
list_display = ["identifier", "course"]
list_filter = list_display

form = PrairieTestFacilityAdminForm


@admin.register(AllowEvent)
class AllowEventAdmin(admin.ModelAdmin):
list_display = [
"event_id", "test_facility", "user_uid", "start", "end", "exam_uuid"]
list_filter = ["test_facility", "user_uid", "exam_uuid"]


@admin.register(DenyEvent)
class DenyEventAdmin(admin.ModelAdmin):
list_display = [
"event_id", "test_facility", "start", "end", "deny_uuid"]
list_filter = ["test_facility"]
158 changes: 158 additions & 0 deletions prairietest/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Generated by Django 5.1 on 2024-09-11 15:09

import django.core.validators
import django.db.models.deletion
import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = [
("course", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="PrairieTestFacility",
fields=[
("id", models.BigAutoField(primary_key=True, serialize=False)),
(
"identifier",
models.CharField(
db_index=True,
max_length=200,
unique=True,
validators=[
django.core.validators.RegexValidator(
"^(?P<test_facility_id>[a-zA-Z][a-zA-Z0-9_]*)$"
)
],
),
),
("description", models.TextField(blank=True, null=True)),
("secret", models.CharField(blank=True, max_length=220, null=True)),
(
"course",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="course.course"
),
),
],
options={
"verbose_name": "PrairieTest facility",
"verbose_name_plural": "PrairieTest facilities",
},
),
migrations.CreateModel(
name="PrairieTestEvent",
fields=[
("id", models.BigAutoField(primary_key=True, serialize=False)),
("event_id", models.UUIDField()),
("created", models.DateTimeField(verbose_name="Created time")),
(
"received_time",
models.DateTimeField(
default=django.utils.timezone.now, verbose_name="Received time"
),
),
(
"test_facility",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="prairietest.prairietestfacility",
),
),
],
),
migrations.CreateModel(
name="AllowEvent",
fields=[
(
"prairietestevent_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="prairietest.prairietestevent",
),
),
("user_uid", models.CharField(max_length=200)),
("user_uin", models.CharField(max_length=200)),
("exam_uuid", models.UUIDField()),
("start", models.DateTimeField(verbose_name="Start time")),
("end", models.DateTimeField(verbose_name="End time")),
("cidr_blocks", models.JSONField()),
],
options={
"indexes": [
models.Index(
fields=["user_uid", "exam_uuid", "start"],
name="prairietest_user_ui_e93827_idx",
),
models.Index(
fields=["user_uid", "exam_uuid", "end"],
name="prairietest_user_ui_e11aa4_idx",
),
],
},
bases=("prairietest.prairietestevent",),
),
migrations.CreateModel(
name="DenyEvent",
fields=[
(
"prairietestevent_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="prairietest.prairietestevent",
),
),
("deny_uuid", models.UUIDField()),
(
"start",
models.DateTimeField(db_index=True, verbose_name="Start time"),
),
("end", models.DateTimeField(db_index=True, verbose_name="End time")),
("cidr_blocks", models.JSONField()),
],
options={
"indexes": [
models.Index(
fields=["deny_uuid", "start"],
name="prairietest_deny_uu_b13822_idx",
),
models.Index(
fields=["deny_uuid", "end"],
name="prairietest_deny_uu_3c9537_idx",
),
],
},
bases=("prairietest.prairietestevent",),
),
migrations.AddIndex(
model_name="prairietestfacility",
index=models.Index(
fields=["course", "identifier"], name="prairietest_course__462b09_idx"
),
),
migrations.AddIndex(
model_name="prairietestevent",
index=models.Index(
fields=["event_id"], name="prairietest_event_i_1b94e1_idx"
),
),
migrations.AddIndex(
model_name="prairietestevent",
index=models.Index(
fields=["created"], name="prairietest_created_334e1a_idx"
),
),
]
Empty file.
83 changes: 83 additions & 0 deletions prairietest/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from __future__ import annotations

from django.core.validators import RegexValidator
from django.db import models
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _

from course.models import Course


TEST_FACILITY_ID_REGEX = "(?P<test_facility_id>[a-zA-Z][a-zA-Z0-9_]*)"


class PrairieTestFacility(models.Model):
id = models.BigAutoField(primary_key=True)

course = models.ForeignKey(Course, on_delete=models.CASCADE)
identifier = models.CharField(max_length=200, unique=True,
validators=[RegexValidator(f"^{TEST_FACILITY_ID_REGEX}$")],
db_index=True)

description = models.TextField(blank=True, null=True)
secret = models.CharField(max_length=220, blank=True, null=True)

class Meta:
indexes = [
models.Index(fields=["course", "identifier"]),
]
verbose_name = _("PrairieTest facility")
verbose_name_plural = _("PrairieTest facilities")

def __str__(self) -> str:
return f"PrairieTest Facility '{self.identifier}' in {self.course.identifier}"


class PrairieTestEvent(models.Model):
id = models.BigAutoField(primary_key=True)

test_facility = models.ForeignKey(PrairieTestFacility, on_delete=models.CASCADE)
event_id = models.UUIDField()
created = models.DateTimeField(verbose_name=_("Created time"))
received_time = models.DateTimeField(default=now,
verbose_name=_("Received time"))

class Meta:
indexes = [
models.Index(fields=["event_id"]),
models.Index(fields=["created"]),
]


class AllowEvent(PrairieTestEvent):
user_uid = models.CharField(max_length=200)
user_uin = models.CharField(max_length=200)
exam_uuid = models.UUIDField()
start = models.DateTimeField(verbose_name=_("Start time"))
end = models.DateTimeField(verbose_name=_("End time"))
cidr_blocks = models.JSONField()

def __str__(self) -> str:
return f"PrairieTest allow event {self.event_id} for {self.user_uid}"

class Meta:
indexes = [
models.Index(fields=["user_uid", "exam_uuid", "start"]),
models.Index(fields=["user_uid", "exam_uuid", "end"]),
]


class DenyEvent(PrairieTestEvent):
deny_uuid = models.UUIDField()
start = models.DateTimeField(verbose_name=_("Start time"), db_index=True)
end = models.DateTimeField(verbose_name=_("End time"), db_index=True)
cidr_blocks = models.JSONField()

def __str__(self) -> str:
return f"PrairieTest deny event {self.event_id} with {self.deny_uuid}"

class Meta:
indexes = [
models.Index(fields=["deny_uuid", "start"]),
models.Index(fields=["deny_uuid", "end"]),
]
41 changes: 41 additions & 0 deletions prairietest/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from __future__ import annotations


__copyright__ = "Copyright (C) 2024 University of Illinois Board of Trustees"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

from django.urls import re_path

from course.constants import COURSE_ID_REGEX
from prairietest.models import TEST_FACILITY_ID_REGEX
from prairietest.views import webhook


urlpatterns = [
re_path(
r"^course"
"/" + COURSE_ID_REGEX
+ "/webhook"
"/" + TEST_FACILITY_ID_REGEX
+ "/",
webhook)
]
Loading

0 comments on commit 4dfba19

Please sign in to comment.