Teddy Xinyuan Chen
commited on
Commit
Β·
977aa4a
1
Parent(s):
9c29307
2024-10-08T20-08-36Z
Browse files- django-quiz-py.txt +893 -0
- src/quizsite/settings.py +1 -0
- src/quizsite/utils.py +0 -0
- src/quizzes/admin.py +1 -1
- src/quizzes/migrations/0003_alter_freetextanswer_correct_answer_and_more.py +42 -0
- src/quizzes/migrations/0004_remove_llmgradedanswer_rubrics_question_rubrics.py +26 -0
- src/quizzes/models.py +25 -23
- src/quizzes/templates/quizzes/display.html +64 -54
- src/quizzes/templates/quizzes/partial.html +4 -4
- src/requirements.txt +2 -0
django-quiz-py.txt
ADDED
@@ -0,0 +1,893 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
The following text is a Git repository with code. The structure of the text are sections that begin with ----, followed by a single line containing the file path and file name, followed by a variable amount of lines containing the file contents. The text representing the Git repository ends when the symbols --END-- are encounted. Any further text beyond --END-- are meant to be interpreted as instructions using the aforementioned Git repository as context.
|
2 |
+
----
|
3 |
+
src/manage.py
|
4 |
+
#!/usr/bin/env python
|
5 |
+
"""Django's command-line utility for administrative tasks."""
|
6 |
+
import os
|
7 |
+
import sys
|
8 |
+
|
9 |
+
|
10 |
+
def main():
|
11 |
+
"""Run administrative tasks."""
|
12 |
+
|
13 |
+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "quizsite.settings")
|
14 |
+
|
15 |
+
try:
|
16 |
+
from django.core.management import execute_from_command_line
|
17 |
+
except ImportError as exc:
|
18 |
+
raise ImportError(
|
19 |
+
"Couldn't import Django. Are you sure it's installed and "
|
20 |
+
"available on your PYTHONPATH environment variable? Did you "
|
21 |
+
"forget to activate a virtual environment?"
|
22 |
+
) from exc
|
23 |
+
execute_from_command_line(sys.argv)
|
24 |
+
|
25 |
+
|
26 |
+
if __name__ == "__main__":
|
27 |
+
main()
|
28 |
+
|
29 |
+
----
|
30 |
+
src/quizsite/__init__.py
|
31 |
+
|
32 |
+
----
|
33 |
+
src/quizsite/asgi.py
|
34 |
+
"""
|
35 |
+
ASGI config for quizsite project.
|
36 |
+
|
37 |
+
It exposes the ASGI callable as a module-level variable named ``application``.
|
38 |
+
|
39 |
+
For more information on this file, see
|
40 |
+
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/
|
41 |
+
"""
|
42 |
+
|
43 |
+
import os
|
44 |
+
|
45 |
+
from django.core.asgi import get_asgi_application
|
46 |
+
|
47 |
+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "quizsite.settings")
|
48 |
+
|
49 |
+
application = get_asgi_application()
|
50 |
+
|
51 |
+
----
|
52 |
+
src/quizsite/postgresql/__init__.py
|
53 |
+
|
54 |
+
----
|
55 |
+
src/quizsite/postgresql/base.py
|
56 |
+
from azure.identity import DefaultAzureCredential
|
57 |
+
from django.db.backends.postgresql import base
|
58 |
+
|
59 |
+
|
60 |
+
class DatabaseWrapper(base.DatabaseWrapper):
|
61 |
+
def get_connection_params(self):
|
62 |
+
params = super().get_connection_params()
|
63 |
+
if params.get("host", "").endswith(".database.azure.com"):
|
64 |
+
azure_credential = DefaultAzureCredential()
|
65 |
+
dbpass = azure_credential.get_token("https://ossrdbms-aad.database.windows.net/.default").token
|
66 |
+
params["password"] = dbpass
|
67 |
+
return params
|
68 |
+
|
69 |
+
----
|
70 |
+
src/quizsite/settings.py
|
71 |
+
"""
|
72 |
+
Django settings for quizsite project.
|
73 |
+
|
74 |
+
Generated by 'django-admin startproject' using Django 4.1.1.
|
75 |
+
|
76 |
+
For more information on this file, see
|
77 |
+
https://docs.djangoproject.com/en/4.1/topics/settings/
|
78 |
+
|
79 |
+
For the full list of settings and their values, see
|
80 |
+
https://docs.djangoproject.com/en/4.1/ref/settings/
|
81 |
+
"""
|
82 |
+
|
83 |
+
import os
|
84 |
+
from pathlib import Path
|
85 |
+
|
86 |
+
import environ
|
87 |
+
|
88 |
+
env = environ.Env(
|
89 |
+
# set casting, default value
|
90 |
+
DEBUG=(bool, False),
|
91 |
+
DBENGINE=(str, "django.db.backends.postgresql_psycopg2"),
|
92 |
+
DBSSL=(str, "disable"),
|
93 |
+
ADMIN_URL=(str, "admin/"),
|
94 |
+
STATIC_BACKEND=(str, "django.contrib.staticfiles.storage.StaticFilesStorage"),
|
95 |
+
)
|
96 |
+
|
97 |
+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
98 |
+
BASE_DIR = Path(__file__).resolve().parent.parent
|
99 |
+
|
100 |
+
# Take environment variables from .env file
|
101 |
+
environ.Env.read_env(os.path.join(BASE_DIR.parent, ".env"))
|
102 |
+
|
103 |
+
# Quick-start development settings - unsuitable for production
|
104 |
+
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
|
105 |
+
|
106 |
+
# SECURITY WARNING: keep the secret key used in production secret!
|
107 |
+
SECRET_KEY = env("SECRET_KEY")
|
108 |
+
|
109 |
+
# SECURITY WARNING: don't run with debug turned on in production!
|
110 |
+
DEBUG = env("DEBUG")
|
111 |
+
# This allows us to use the VS Code debugger to break on exceptions
|
112 |
+
DEBUG_PROPAGATE_EXCEPTIONS = env("DEBUG")
|
113 |
+
|
114 |
+
# Configure the domain name using the environment variable
|
115 |
+
# that Azure automatically creates for us.
|
116 |
+
if env.get_value("WEBSITE_HOSTNAME", default=None):
|
117 |
+
ALLOWED_HOSTS = [os.environ["WEBSITE_HOSTNAME"]]
|
118 |
+
CSRF_TRUSTED_ORIGINS = ["https://" + os.environ["WEBSITE_HOSTNAME"]]
|
119 |
+
else:
|
120 |
+
ALLOWED_HOSTS = []
|
121 |
+
CSRF_TRUSTED_ORIGINS = [
|
122 |
+
"http://localhost:8000",
|
123 |
+
"https://mq-django-quiz-app.vercel.app",
|
124 |
+
"https://mq-quiz.teddysc.me",
|
125 |
+
]
|
126 |
+
if env.get_value("CODESPACE_NAME", default=None):
|
127 |
+
CSRF_TRUSTED_ORIGINS.append(
|
128 |
+
f"https://{env('CODESPACE_NAME')}-8000.{env('GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN')}"
|
129 |
+
)
|
130 |
+
|
131 |
+
ADMIN_URL = env("ADMIN_URL")
|
132 |
+
|
133 |
+
# Application definition
|
134 |
+
|
135 |
+
INSTALLED_APPS = [
|
136 |
+
"markdownify.apps.MarkdownifyConfig",
|
137 |
+
"quizzes.apps.QuizzesConfig",
|
138 |
+
"django.contrib.admin",
|
139 |
+
"django.contrib.auth",
|
140 |
+
"django.contrib.contenttypes",
|
141 |
+
"django.contrib.sessions",
|
142 |
+
"django.contrib.messages",
|
143 |
+
"whitenoise.runserver_nostatic",
|
144 |
+
"django.contrib.staticfiles",
|
145 |
+
]
|
146 |
+
|
147 |
+
MIDDLEWARE = [
|
148 |
+
"django.middleware.security.SecurityMiddleware",
|
149 |
+
"whitenoise.middleware.WhiteNoiseMiddleware",
|
150 |
+
"django.contrib.sessions.middleware.SessionMiddleware",
|
151 |
+
"django.middleware.common.CommonMiddleware",
|
152 |
+
"django.middleware.csrf.CsrfViewMiddleware",
|
153 |
+
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
154 |
+
"django.contrib.messages.middleware.MessageMiddleware",
|
155 |
+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
156 |
+
]
|
157 |
+
|
158 |
+
ROOT_URLCONF = "quizsite.urls"
|
159 |
+
|
160 |
+
TEMPLATES = [
|
161 |
+
{
|
162 |
+
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
163 |
+
"DIRS": [],
|
164 |
+
"APP_DIRS": True,
|
165 |
+
"OPTIONS": {
|
166 |
+
"context_processors": [
|
167 |
+
"django.template.context_processors.debug",
|
168 |
+
"django.template.context_processors.request",
|
169 |
+
"django.contrib.auth.context_processors.auth",
|
170 |
+
"django.contrib.messages.context_processors.messages",
|
171 |
+
],
|
172 |
+
},
|
173 |
+
},
|
174 |
+
]
|
175 |
+
|
176 |
+
WSGI_APPLICATION = "quizsite.wsgi.application"
|
177 |
+
|
178 |
+
|
179 |
+
# Database
|
180 |
+
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
|
181 |
+
|
182 |
+
DATABASES = {
|
183 |
+
"default": {
|
184 |
+
"ENGINE": "quizsite.postgresql",
|
185 |
+
"NAME": env("DBNAME"),
|
186 |
+
"HOST": env("DBHOST"),
|
187 |
+
"USER": env("DBUSER"),
|
188 |
+
"PASSWORD": env("DBPASS", default="PASSWORD_WILL_BE_SET_LATER"),
|
189 |
+
"OPTIONS": {"sslmode": env("DBSSL")},
|
190 |
+
"CONN_MAX_AGE": 60 * 60 * 6, # 6 hours
|
191 |
+
}
|
192 |
+
}
|
193 |
+
|
194 |
+
|
195 |
+
# Password validation
|
196 |
+
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
|
197 |
+
|
198 |
+
AUTH_PASSWORD_VALIDATORS = [
|
199 |
+
{
|
200 |
+
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
|
201 |
+
},
|
202 |
+
{
|
203 |
+
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
|
204 |
+
},
|
205 |
+
{
|
206 |
+
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
|
207 |
+
},
|
208 |
+
{
|
209 |
+
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
|
210 |
+
},
|
211 |
+
]
|
212 |
+
|
213 |
+
|
214 |
+
# Internationalization
|
215 |
+
# https://docs.djangoproject.com/en/4.1/topics/i18n/
|
216 |
+
|
217 |
+
LANGUAGE_CODE = "en-us"
|
218 |
+
|
219 |
+
TIME_ZONE = "America/Los_Angeles"
|
220 |
+
|
221 |
+
USE_I18N = True
|
222 |
+
|
223 |
+
USE_TZ = True
|
224 |
+
|
225 |
+
|
226 |
+
# Static files (CSS, JavaScript, Images)
|
227 |
+
# https://docs.djangoproject.com/en/4.1/howto/static-files/
|
228 |
+
|
229 |
+
STATIC_URL = "static/"
|
230 |
+
|
231 |
+
# https://whitenoise.evans.io/en/stable/django.html
|
232 |
+
STORAGES = {
|
233 |
+
"staticfiles": {
|
234 |
+
"BACKEND": env("STATIC_BACKEND"),
|
235 |
+
},
|
236 |
+
}
|
237 |
+
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
|
238 |
+
|
239 |
+
# Default primary key field type
|
240 |
+
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
|
241 |
+
|
242 |
+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
243 |
+
|
244 |
+
----
|
245 |
+
src/quizsite/urls.py
|
246 |
+
"""quizsite URL Configuration
|
247 |
+
|
248 |
+
The `urlpatterns` list routes URLs to views. For more information please see:
|
249 |
+
https://docs.djangoproject.com/en/4.1/topics/http/urls/
|
250 |
+
Examples:
|
251 |
+
Function views
|
252 |
+
1. Add an import: from my_app import views
|
253 |
+
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
254 |
+
Class-based views
|
255 |
+
1. Add an import: from other_app.views import Home
|
256 |
+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
257 |
+
Including another URLconf
|
258 |
+
1. Import the include() function: from django.urls import include, path
|
259 |
+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
260 |
+
"""
|
261 |
+
|
262 |
+
from django.conf import settings
|
263 |
+
from django.contrib import admin
|
264 |
+
from django.urls import include, path
|
265 |
+
|
266 |
+
urlpatterns = [
|
267 |
+
path("", include("quizzes.urls")),
|
268 |
+
path(settings.ADMIN_URL, admin.site.urls),
|
269 |
+
]
|
270 |
+
|
271 |
+
----
|
272 |
+
src/quizsite/wsgi.py
|
273 |
+
"""
|
274 |
+
WSGI config for quizsite project.
|
275 |
+
|
276 |
+
It exposes the WSGI callable as a module-level variable named ``application``.
|
277 |
+
|
278 |
+
For more information on this file, see
|
279 |
+
https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/
|
280 |
+
"""
|
281 |
+
|
282 |
+
import os
|
283 |
+
|
284 |
+
from django.core.wsgi import get_wsgi_application
|
285 |
+
|
286 |
+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "quizsite.settings")
|
287 |
+
|
288 |
+
application = get_wsgi_application()
|
289 |
+
|
290 |
+
app = application
|
291 |
+
|
292 |
+
----
|
293 |
+
src/quizzes/__init__.py
|
294 |
+
|
295 |
+
----
|
296 |
+
src/quizzes/admin.py
|
297 |
+
from django.contrib import admin
|
298 |
+
|
299 |
+
from .models import FreeTextAnswer, MultipleChoiceAnswer, Question, Quiz, LLMGradedAnswer
|
300 |
+
|
301 |
+
admin.site.register(Quiz)
|
302 |
+
|
303 |
+
|
304 |
+
class FreeTextAnswerInline(admin.StackedInline):
|
305 |
+
model = FreeTextAnswer
|
306 |
+
|
307 |
+
|
308 |
+
class MultipleChoiceAnswerInline(admin.StackedInline):
|
309 |
+
model = MultipleChoiceAnswer
|
310 |
+
|
311 |
+
|
312 |
+
class LLMGradedAnswerInline(admin.StackedInline):
|
313 |
+
model = LLMGradedAnswer
|
314 |
+
|
315 |
+
|
316 |
+
class QuestionAdmin(admin.ModelAdmin):
|
317 |
+
inlines = [FreeTextAnswerInline, MultipleChoiceAnswerInline, LLMGradedAnswerInline]
|
318 |
+
|
319 |
+
|
320 |
+
admin.site.register(Question, QuestionAdmin)
|
321 |
+
|
322 |
+
----
|
323 |
+
src/quizzes/apps.py
|
324 |
+
from django.apps import AppConfig
|
325 |
+
|
326 |
+
|
327 |
+
class QuizzesConfig(AppConfig):
|
328 |
+
default_auto_field = "django.db.models.BigAutoField"
|
329 |
+
name = "quizzes"
|
330 |
+
|
331 |
+
----
|
332 |
+
src/quizzes/migrations/0001_initial.py
|
333 |
+
# Generated by Django 4.1.1 on 2022-09-14 18:17
|
334 |
+
|
335 |
+
import django.contrib.postgres.fields
|
336 |
+
import django.db.models.deletion
|
337 |
+
from django.db import migrations, models
|
338 |
+
|
339 |
+
|
340 |
+
class Migration(migrations.Migration):
|
341 |
+
initial = True
|
342 |
+
|
343 |
+
dependencies = []
|
344 |
+
|
345 |
+
operations = [
|
346 |
+
migrations.CreateModel(
|
347 |
+
name="Quiz",
|
348 |
+
fields=[
|
349 |
+
(
|
350 |
+
"id",
|
351 |
+
models.BigAutoField(
|
352 |
+
auto_created=True,
|
353 |
+
primary_key=True,
|
354 |
+
serialize=False,
|
355 |
+
verbose_name="ID",
|
356 |
+
),
|
357 |
+
),
|
358 |
+
("name", models.CharField(max_length=100)),
|
359 |
+
],
|
360 |
+
),
|
361 |
+
migrations.CreateModel(
|
362 |
+
name="Question",
|
363 |
+
fields=[
|
364 |
+
(
|
365 |
+
"id",
|
366 |
+
models.BigAutoField(
|
367 |
+
auto_created=True,
|
368 |
+
primary_key=True,
|
369 |
+
serialize=False,
|
370 |
+
verbose_name="ID",
|
371 |
+
),
|
372 |
+
),
|
373 |
+
("prompt", models.CharField(max_length=200)),
|
374 |
+
(
|
375 |
+
"answer_status",
|
376 |
+
models.CharField(default="unanswered", max_length=16),
|
377 |
+
),
|
378 |
+
(
|
379 |
+
"quiz",
|
380 |
+
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="quizzes.quiz"),
|
381 |
+
),
|
382 |
+
],
|
383 |
+
),
|
384 |
+
migrations.CreateModel(
|
385 |
+
name="MultipleChoiceAnswer",
|
386 |
+
fields=[
|
387 |
+
(
|
388 |
+
"id",
|
389 |
+
models.BigAutoField(
|
390 |
+
auto_created=True,
|
391 |
+
primary_key=True,
|
392 |
+
serialize=False,
|
393 |
+
verbose_name="ID",
|
394 |
+
),
|
395 |
+
),
|
396 |
+
("correct_answer", models.CharField(max_length=200)),
|
397 |
+
(
|
398 |
+
"choices",
|
399 |
+
django.contrib.postgres.fields.ArrayField(
|
400 |
+
base_field=models.CharField(blank=True, max_length=200),
|
401 |
+
size=None,
|
402 |
+
),
|
403 |
+
),
|
404 |
+
(
|
405 |
+
"question",
|
406 |
+
models.ForeignKey(
|
407 |
+
on_delete=django.db.models.deletion.CASCADE,
|
408 |
+
to="quizzes.question",
|
409 |
+
),
|
410 |
+
),
|
411 |
+
],
|
412 |
+
),
|
413 |
+
migrations.CreateModel(
|
414 |
+
name="FreeTextAnswer",
|
415 |
+
fields=[
|
416 |
+
(
|
417 |
+
"id",
|
418 |
+
models.BigAutoField(
|
419 |
+
auto_created=True,
|
420 |
+
primary_key=True,
|
421 |
+
serialize=False,
|
422 |
+
verbose_name="ID",
|
423 |
+
),
|
424 |
+
),
|
425 |
+
("correct_answer", models.CharField(max_length=200)),
|
426 |
+
("case_sensitive", models.BooleanField(default=False)),
|
427 |
+
(
|
428 |
+
"question",
|
429 |
+
models.ForeignKey(
|
430 |
+
on_delete=django.db.models.deletion.CASCADE,
|
431 |
+
to="quizzes.question",
|
432 |
+
),
|
433 |
+
),
|
434 |
+
],
|
435 |
+
),
|
436 |
+
]
|
437 |
+
|
438 |
+
----
|
439 |
+
src/quizzes/migrations/0002_remove_question_answer_status_and_more.py
|
440 |
+
# Generated by Django 4.1.1 on 2022-09-15 00:57
|
441 |
+
|
442 |
+
import django.db.models.deletion
|
443 |
+
from django.db import migrations, models
|
444 |
+
|
445 |
+
|
446 |
+
class Migration(migrations.Migration):
|
447 |
+
dependencies = [
|
448 |
+
("quizzes", "0001_initial"),
|
449 |
+
]
|
450 |
+
|
451 |
+
operations = [
|
452 |
+
migrations.RemoveField(
|
453 |
+
model_name="question",
|
454 |
+
name="answer_status",
|
455 |
+
),
|
456 |
+
migrations.AlterField(
|
457 |
+
model_name="freetextanswer",
|
458 |
+
name="question",
|
459 |
+
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to="quizzes.question"),
|
460 |
+
),
|
461 |
+
migrations.AlterField(
|
462 |
+
model_name="multiplechoiceanswer",
|
463 |
+
name="question",
|
464 |
+
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to="quizzes.question"),
|
465 |
+
),
|
466 |
+
]
|
467 |
+
|
468 |
+
----
|
469 |
+
src/quizzes/migrations/0003_alter_freetextanswer_correct_answer_and_more.py
|
470 |
+
# Generated by Django 4.2.7 on 2024-10-08 23:30
|
471 |
+
|
472 |
+
from django.db import migrations, models
|
473 |
+
import django.db.models.deletion
|
474 |
+
|
475 |
+
|
476 |
+
class Migration(migrations.Migration):
|
477 |
+
|
478 |
+
dependencies = [
|
479 |
+
("quizzes", "0002_remove_question_answer_status_and_more"),
|
480 |
+
]
|
481 |
+
|
482 |
+
operations = [
|
483 |
+
migrations.AlterField(
|
484 |
+
model_name="freetextanswer",
|
485 |
+
name="correct_answer",
|
486 |
+
field=models.CharField(default="", max_length=200),
|
487 |
+
),
|
488 |
+
migrations.AlterField(
|
489 |
+
model_name="multiplechoiceanswer",
|
490 |
+
name="correct_answer",
|
491 |
+
field=models.CharField(default="", max_length=200),
|
492 |
+
),
|
493 |
+
migrations.CreateModel(
|
494 |
+
name="LLMGradedAnswer",
|
495 |
+
fields=[
|
496 |
+
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
497 |
+
(
|
498 |
+
"rubrics",
|
499 |
+
models.TextField(
|
500 |
+
blank=True,
|
501 |
+
null=True,
|
502 |
+
verbose_name="Grading Rubrics - For LLM-graded questions only. You can leave this empty.",
|
503 |
+
),
|
504 |
+
),
|
505 |
+
("question", models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to="quizzes.question")),
|
506 |
+
],
|
507 |
+
options={
|
508 |
+
"abstract": False,
|
509 |
+
},
|
510 |
+
),
|
511 |
+
]
|
512 |
+
|
513 |
+
----
|
514 |
+
src/quizzes/migrations/0004_remove_llmgradedanswer_rubrics_question_rubrics.py
|
515 |
+
# Generated by Django 4.2.7 on 2024-10-08 23:46
|
516 |
+
|
517 |
+
from django.db import migrations, models
|
518 |
+
|
519 |
+
|
520 |
+
class Migration(migrations.Migration):
|
521 |
+
|
522 |
+
dependencies = [
|
523 |
+
("quizzes", "0003_alter_freetextanswer_correct_answer_and_more"),
|
524 |
+
]
|
525 |
+
|
526 |
+
operations = [
|
527 |
+
migrations.RemoveField(
|
528 |
+
model_name="llmgradedanswer",
|
529 |
+
name="rubrics",
|
530 |
+
),
|
531 |
+
migrations.AddField(
|
532 |
+
model_name="question",
|
533 |
+
name="rubrics",
|
534 |
+
field=models.TextField(
|
535 |
+
blank=True,
|
536 |
+
null=True,
|
537 |
+
verbose_name="Grading Rubrics - For LLM-graded questions only. You can leave this empty.",
|
538 |
+
),
|
539 |
+
),
|
540 |
+
]
|
541 |
+
|
542 |
+
----
|
543 |
+
src/quizzes/migrations/__init__.py
|
544 |
+
|
545 |
+
----
|
546 |
+
src/quizzes/models.py
|
547 |
+
import typing
|
548 |
+
|
549 |
+
from django.contrib.postgres import fields
|
550 |
+
from django.db import models
|
551 |
+
|
552 |
+
|
553 |
+
class Quiz(models.Model):
|
554 |
+
name = models.CharField(max_length=100)
|
555 |
+
|
556 |
+
def __str__(self):
|
557 |
+
return self.name
|
558 |
+
|
559 |
+
|
560 |
+
class Question(models.Model):
|
561 |
+
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
|
562 |
+
prompt = models.CharField(max_length=200)
|
563 |
+
rubrics = models.TextField(
|
564 |
+
blank=True, null=True, verbose_name="Grading Rubrics - For LLM-graded questions only. You can leave this empty."
|
565 |
+
)
|
566 |
+
|
567 |
+
def __str__(self):
|
568 |
+
return self.prompt
|
569 |
+
|
570 |
+
def get_answer(self) -> typing.Union["Answer", None]:
|
571 |
+
return (
|
572 |
+
getattr(self, "multiplechoiceanswer", None)
|
573 |
+
or getattr(self, "freetextanswer", None)
|
574 |
+
or getattr(self, "llmgradedanswer", None)
|
575 |
+
)
|
576 |
+
|
577 |
+
|
578 |
+
class Answer(models.Model):
|
579 |
+
question = models.OneToOneField(Question, on_delete=models.CASCADE)
|
580 |
+
|
581 |
+
class Meta:
|
582 |
+
abstract = True
|
583 |
+
|
584 |
+
def __str__(self) -> str:
|
585 |
+
return (
|
586 |
+
getattr(self, "correct_answer", None) or getattr(self, "rubrics", None) or "No answer or rubrics provided"
|
587 |
+
)
|
588 |
+
|
589 |
+
def is_correct(self, user_answer) -> bool:
|
590 |
+
return user_answer == getattr(self, "correct_answer", None)
|
591 |
+
|
592 |
+
|
593 |
+
class FreeTextAnswer(Answer):
|
594 |
+
correct_answer = models.CharField(max_length=200, default="")
|
595 |
+
case_sensitive = models.BooleanField(default=False)
|
596 |
+
|
597 |
+
def is_correct(self, user_answer) -> bool:
|
598 |
+
if not self.case_sensitive:
|
599 |
+
return user_answer.lower() == self.correct_answer.lower()
|
600 |
+
return user_answer == self.correct_answer
|
601 |
+
|
602 |
+
|
603 |
+
class LLMGradedAnswer(Answer):
|
604 |
+
def grade(self, user_answer, rubrics) -> dict:
|
605 |
+
import requests
|
606 |
+
|
607 |
+
"""
|
608 |
+
Grades the user's answer by calling the grading API.
|
609 |
+
|
610 |
+
Args:
|
611 |
+
user_answer (str): The answer provided by the user.
|
612 |
+
rubrics (str): The grading rubrics.
|
613 |
+
|
614 |
+
Returns:
|
615 |
+
bool: True if the user's answer is correct, False otherwise.
|
616 |
+
"""
|
617 |
+
api_url = "http://localhost/api/grade"
|
618 |
+
payload = {"user_answer": user_answer, "rubrics": rubrics}
|
619 |
+
|
620 |
+
try:
|
621 |
+
response = requests.post(api_url, json=payload)
|
622 |
+
response.raise_for_status() # Raise an error for bad status codes
|
623 |
+
result = response.json()
|
624 |
+
|
625 |
+
# Assuming the API returns a JSON object with a 'correct' field
|
626 |
+
return result
|
627 |
+
except requests.RequestException as e:
|
628 |
+
# Handle any errors that occur during the request
|
629 |
+
print(f"An error occurred: {e}")
|
630 |
+
return {"result": "error", "message": str(e)}
|
631 |
+
|
632 |
+
|
633 |
+
class MultipleChoiceAnswer(Answer):
|
634 |
+
correct_answer = models.CharField(max_length=200, default="")
|
635 |
+
choices = fields.ArrayField(models.CharField(max_length=200, blank=True))
|
636 |
+
|
637 |
+
def __str__(self) -> str:
|
638 |
+
return f"{self.correct_answer} from {self.choices}"
|
639 |
+
|
640 |
+
----
|
641 |
+
src/quizzes/tests.py
|
642 |
+
from django.test import TestCase
|
643 |
+
from django.urls import reverse
|
644 |
+
|
645 |
+
from .models import FreeTextAnswer, MultipleChoiceAnswer, Question, Quiz
|
646 |
+
|
647 |
+
|
648 |
+
def create_quiz():
|
649 |
+
quiz = Quiz.objects.create(name="Butterflies")
|
650 |
+
question = Question.objects.create(quiz=quiz, prompt="What plant do Swallowtail caterpillars eat?")
|
651 |
+
answer = MultipleChoiceAnswer.objects.create(
|
652 |
+
question=question, correct_answer="Dill", choices=["Thistle", "Milkweed", "Dill"]
|
653 |
+
)
|
654 |
+
return quiz, question, answer
|
655 |
+
|
656 |
+
|
657 |
+
class FreeTextAnswerModelTests(TestCase):
|
658 |
+
def test_case_insensitive(self):
|
659 |
+
ans = FreeTextAnswer(correct_answer="Milkweed", case_sensitive=False)
|
660 |
+
self.assertTrue(ans.is_correct("Milkweed"))
|
661 |
+
self.assertTrue(ans.is_correct("milkweed"))
|
662 |
+
self.assertFalse(ans.is_correct("thistle"))
|
663 |
+
|
664 |
+
def test_case_sensitive(self):
|
665 |
+
ans = FreeTextAnswer(correct_answer="Armeria Maritima", case_sensitive=True)
|
666 |
+
self.assertFalse(ans.is_correct("armeria maritima"))
|
667 |
+
self.assertTrue(ans.is_correct("Armeria Maritima"))
|
668 |
+
|
669 |
+
|
670 |
+
class MultipleChoiceAnswerModelTests(TestCase):
|
671 |
+
def test_choices(self):
|
672 |
+
ans = MultipleChoiceAnswer(correct_answer="Dill", choices=["Milkweed", "Dill", "Thistle"])
|
673 |
+
self.assertTrue(ans.is_correct("Dill"))
|
674 |
+
self.assertFalse(ans.is_correct("dill"))
|
675 |
+
self.assertFalse(ans.is_correct("Milkweed"))
|
676 |
+
|
677 |
+
|
678 |
+
class QuizModelTests(TestCase):
|
679 |
+
def test_quiz_relations(self):
|
680 |
+
quiz = Quiz.objects.create(name="Butterflies")
|
681 |
+
q1 = Question.objects.create(quiz=quiz, prompt="What plant do Swallowtail caterpillars eat?")
|
682 |
+
a1 = MultipleChoiceAnswer.objects.create(
|
683 |
+
question=q1, correct_answer="Dill", choices=["Thistle", "Milkweed", "Dill"]
|
684 |
+
)
|
685 |
+
q2 = Question.objects.create(quiz=quiz, prompt="What plant do Monarch caterpillars eat?")
|
686 |
+
a2 = FreeTextAnswer.objects.create(question=q2, correct_answer="Milkweed", case_sensitive=False)
|
687 |
+
self.assertEqual(len(quiz.question_set.all()), 2)
|
688 |
+
self.assertEqual(q1.multiplechoiceanswer, a1)
|
689 |
+
self.assertEqual(q2.freetextanswer, a2)
|
690 |
+
|
691 |
+
|
692 |
+
class IndexViewTests(TestCase):
|
693 |
+
def test_no_quizzes(self):
|
694 |
+
response = self.client.get(reverse("quizzes:index"))
|
695 |
+
self.assertEqual(response.status_code, 200)
|
696 |
+
self.assertContains(response, "No quizzes are available.")
|
697 |
+
self.assertQuerySetEqual(response.context["quiz_list"], [])
|
698 |
+
|
699 |
+
def test_one_quiz(self):
|
700 |
+
quiz, _, _ = create_quiz()
|
701 |
+
response = self.client.get(reverse("quizzes:index"))
|
702 |
+
self.assertQuerySetEqual(
|
703 |
+
response.context["quiz_list"],
|
704 |
+
[quiz],
|
705 |
+
)
|
706 |
+
|
707 |
+
|
708 |
+
class DisplayQuizViewTests(TestCase):
|
709 |
+
def test_quiz_404(self):
|
710 |
+
url = reverse("quizzes:display_quiz", args=(12,))
|
711 |
+
response = self.client.get(url)
|
712 |
+
self.assertEqual(response.status_code, 404)
|
713 |
+
|
714 |
+
def test_quiz_redirects(self):
|
715 |
+
quiz, question, _ = create_quiz()
|
716 |
+
url = reverse("quizzes:display_quiz", args=(quiz.pk,))
|
717 |
+
response = self.client.get(url)
|
718 |
+
self.assertRedirects(response, reverse("quizzes:display_question", args=(quiz.pk, question.pk)))
|
719 |
+
|
720 |
+
|
721 |
+
class DisplayQuestionViewTests(TestCase):
|
722 |
+
def test_quiz_404(self):
|
723 |
+
url = reverse("quizzes:display_question", args=(12, 1))
|
724 |
+
response = self.client.get(url)
|
725 |
+
self.assertEqual(response.status_code, 404)
|
726 |
+
|
727 |
+
def test_question_404(self):
|
728 |
+
quiz, question, _ = create_quiz()
|
729 |
+
url = reverse("quizzes:display_question", args=(quiz.pk, question.pk + 100))
|
730 |
+
response = self.client.get(url)
|
731 |
+
self.assertContains(response, "that question doesn't exist")
|
732 |
+
|
733 |
+
def test_quiz_question_exists(self):
|
734 |
+
quiz, question, answer = create_quiz()
|
735 |
+
|
736 |
+
url = reverse("quizzes:display_question", args=(quiz.pk, question.pk))
|
737 |
+
response = self.client.get(url)
|
738 |
+
self.assertContains(response, quiz.name)
|
739 |
+
self.assertContains(response, question.prompt)
|
740 |
+
self.assertContains(response, answer.choices[0])
|
741 |
+
|
742 |
+
|
743 |
+
class GradeQuestionViewTests(TestCase):
|
744 |
+
def test_question_404(self):
|
745 |
+
url = reverse("quizzes:grade_question", args=(12,))
|
746 |
+
response = self.client.get(url)
|
747 |
+
self.assertEqual(response.status_code, 404)
|
748 |
+
|
749 |
+
def test_question_correct(self):
|
750 |
+
_, question, answer = create_quiz()
|
751 |
+
|
752 |
+
url = reverse("quizzes:grade_question", args=(question.pk,))
|
753 |
+
response = self.client.post(url, {"answer": answer.correct_answer})
|
754 |
+
self.assertTrue(response.context["is_correct"])
|
755 |
+
self.assertEqual(response.context["correct_answer"], answer.correct_answer)
|
756 |
+
|
757 |
+
----
|
758 |
+
src/quizzes/urls.py
|
759 |
+
from django.urls import path
|
760 |
+
|
761 |
+
from . import views
|
762 |
+
|
763 |
+
app_name = "quizzes"
|
764 |
+
|
765 |
+
urlpatterns = [
|
766 |
+
path("", views.IndexView.as_view(), name="index"),
|
767 |
+
path("quizzes/<int:quiz_id>/", views.display_quiz, name="display_quiz"),
|
768 |
+
path("quizzes/<int:quiz_id>/questions/<int:question_id>", views.display_question, name="display_question"),
|
769 |
+
path("questions/<int:question_id>/grade/", views.grade_question, name="grade_question"),
|
770 |
+
]
|
771 |
+
|
772 |
+
----
|
773 |
+
src/quizzes/views.py
|
774 |
+
from django.shortcuts import get_object_or_404, redirect, render
|
775 |
+
from django.urls import reverse
|
776 |
+
from django.views import generic
|
777 |
+
|
778 |
+
from .models import Question, Quiz
|
779 |
+
|
780 |
+
|
781 |
+
class IndexView(generic.ListView):
|
782 |
+
model = Quiz
|
783 |
+
template_name = "quizzes/index.html"
|
784 |
+
|
785 |
+
|
786 |
+
def display_quiz(request, quiz_id):
|
787 |
+
quiz = get_object_or_404(Quiz, pk=quiz_id)
|
788 |
+
question = quiz.question_set.first()
|
789 |
+
return redirect(reverse("quizzes:display_question", kwargs={"quiz_id": quiz_id, "question_id": question.pk}))
|
790 |
+
|
791 |
+
|
792 |
+
def display_question(request, quiz_id, question_id):
|
793 |
+
quiz = get_object_or_404(Quiz, pk=quiz_id)
|
794 |
+
# fetch ALL of the questions to find current and next question
|
795 |
+
questions = quiz.question_set.all()
|
796 |
+
current_question, next_question = None, None
|
797 |
+
for ind, question in enumerate(questions):
|
798 |
+
if question.pk == question_id:
|
799 |
+
current_question = question
|
800 |
+
if ind != len(questions) - 1:
|
801 |
+
next_question = questions[ind + 1]
|
802 |
+
|
803 |
+
return render(
|
804 |
+
request,
|
805 |
+
"quizzes/display.html",
|
806 |
+
{"quiz": quiz, "question": current_question, "next_question": next_question},
|
807 |
+
)
|
808 |
+
|
809 |
+
|
810 |
+
def grade_question(request, question_id):
|
811 |
+
question = get_object_or_404(Question, pk=question_id)
|
812 |
+
answer = question.get_answer()
|
813 |
+
if answer is None:
|
814 |
+
return render(request, "quizzes/partial.html", {"error": "Question must have an answer"}, status=422)
|
815 |
+
is_correct = answer.is_correct(request.POST.get("answer"))
|
816 |
+
return render(
|
817 |
+
request,
|
818 |
+
"quizzes/partial.html",
|
819 |
+
{"is_correct": is_correct, "correct_answer": answer.correct_answer},
|
820 |
+
)
|
821 |
+
|
822 |
+
----
|
823 |
+
src/setup_postgres_azurerole.py
|
824 |
+
import argparse
|
825 |
+
import logging
|
826 |
+
|
827 |
+
import psycopg2
|
828 |
+
from azure.identity import DefaultAzureCredential
|
829 |
+
|
830 |
+
logger = logging.getLogger("scripts")
|
831 |
+
|
832 |
+
|
833 |
+
def assign_role_for_webapp(postgres_host, postgres_username, app_identity_name):
|
834 |
+
if not postgres_host.endswith(".database.azure.com"):
|
835 |
+
logger.info("This script is intended to be used with Azure Database for PostgreSQL.")
|
836 |
+
logger.info("Please set the environment variable DBHOST to the Azure Database for PostgreSQL server hostname.")
|
837 |
+
return
|
838 |
+
|
839 |
+
logger.info("Authenticating to Azure Database for PostgreSQL using Azure Identity...")
|
840 |
+
azure_credential = DefaultAzureCredential()
|
841 |
+
token = azure_credential.get_token("https://ossrdbms-aad.database.windows.net/.default")
|
842 |
+
conn = psycopg2.connect(
|
843 |
+
database="postgres", # You must connect to postgres database when assigning roles
|
844 |
+
user=postgres_username,
|
845 |
+
password=token.token,
|
846 |
+
host=postgres_host,
|
847 |
+
sslmode="require",
|
848 |
+
)
|
849 |
+
|
850 |
+
conn.autocommit = True
|
851 |
+
cur = conn.cursor()
|
852 |
+
|
853 |
+
cur.execute(f"select * from pgaadauth_list_principals(false) WHERE rolname = '{app_identity_name}'")
|
854 |
+
identities = cur.fetchall()
|
855 |
+
if len(identities) > 0:
|
856 |
+
logger.info(f"Found an existing PostgreSQL role for identity {app_identity_name}")
|
857 |
+
else:
|
858 |
+
logger.info(f"Creating a PostgreSQL role for identity {app_identity_name}")
|
859 |
+
cur.execute(f"SELECT * FROM pgaadauth_create_principal('{app_identity_name}', false, false)")
|
860 |
+
|
861 |
+
logger.info(f"Granting permissions to {app_identity_name}")
|
862 |
+
# set role to azure_pg_admin
|
863 |
+
cur.execute(f'GRANT USAGE ON SCHEMA public TO "{app_identity_name}"')
|
864 |
+
cur.execute(f'GRANT CREATE ON SCHEMA public TO "{app_identity_name}"')
|
865 |
+
cur.execute(f'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "{app_identity_name}"')
|
866 |
+
cur.execute(
|
867 |
+
f"ALTER DEFAULT PRIVILEGES IN SCHEMA public "
|
868 |
+
f'GRANT SELECT, UPDATE, INSERT, DELETE ON TABLES TO "{app_identity_name}"'
|
869 |
+
)
|
870 |
+
|
871 |
+
cur.close()
|
872 |
+
|
873 |
+
|
874 |
+
if __name__ == "__main__":
|
875 |
+
|
876 |
+
logging.basicConfig(level=logging.WARNING)
|
877 |
+
logger.setLevel(logging.INFO)
|
878 |
+
parser = argparse.ArgumentParser(description="Create database schema")
|
879 |
+
parser.add_argument("--host", type=str, help="Postgres host")
|
880 |
+
parser.add_argument("--username", type=str, help="Postgres username")
|
881 |
+
parser.add_argument("--app-identity-name", type=str, help="Azure App Service identity name")
|
882 |
+
|
883 |
+
args = parser.parse_args()
|
884 |
+
if not args.host.endswith(".database.azure.com"):
|
885 |
+
logger.info("This script is intended to be used with Azure Database for PostgreSQL, not local PostgreSQL.")
|
886 |
+
exit(1)
|
887 |
+
|
888 |
+
assign_role_for_webapp(args.host, args.username, args.app_identity_name)
|
889 |
+
logger.info("Role created successfully.")
|
890 |
+
|
891 |
+
--END--
|
892 |
+
|
893 |
+
reply 'ok' and only 'ok' if you read.
|
src/quizsite/settings.py
CHANGED
@@ -63,6 +63,7 @@ ADMIN_URL = env("ADMIN_URL")
|
|
63 |
# Application definition
|
64 |
|
65 |
INSTALLED_APPS = [
|
|
|
66 |
"quizzes.apps.QuizzesConfig",
|
67 |
"django.contrib.admin",
|
68 |
"django.contrib.auth",
|
|
|
63 |
# Application definition
|
64 |
|
65 |
INSTALLED_APPS = [
|
66 |
+
"markdownify.apps.MarkdownifyConfig",
|
67 |
"quizzes.apps.QuizzesConfig",
|
68 |
"django.contrib.admin",
|
69 |
"django.contrib.auth",
|
src/quizsite/utils.py
ADDED
File without changes
|
src/quizzes/admin.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
from django.contrib import admin
|
2 |
|
3 |
-
from .models import FreeTextAnswer, MultipleChoiceAnswer, Question, Quiz
|
4 |
|
5 |
admin.site.register(Quiz)
|
6 |
|
|
|
1 |
from django.contrib import admin
|
2 |
|
3 |
+
from .models import FreeTextAnswer, LLMGradedAnswer, MultipleChoiceAnswer, Question, Quiz
|
4 |
|
5 |
admin.site.register(Quiz)
|
6 |
|
src/quizzes/migrations/0003_alter_freetextanswer_correct_answer_and_more.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Generated by Django 4.2.7 on 2024-10-08 23:30
|
2 |
+
|
3 |
+
import django.db.models.deletion
|
4 |
+
from django.db import migrations, models
|
5 |
+
|
6 |
+
|
7 |
+
class Migration(migrations.Migration):
|
8 |
+
|
9 |
+
dependencies = [
|
10 |
+
("quizzes", "0002_remove_question_answer_status_and_more"),
|
11 |
+
]
|
12 |
+
|
13 |
+
operations = [
|
14 |
+
migrations.AlterField(
|
15 |
+
model_name="freetextanswer",
|
16 |
+
name="correct_answer",
|
17 |
+
field=models.CharField(default="", max_length=200),
|
18 |
+
),
|
19 |
+
migrations.AlterField(
|
20 |
+
model_name="multiplechoiceanswer",
|
21 |
+
name="correct_answer",
|
22 |
+
field=models.CharField(default="", max_length=200),
|
23 |
+
),
|
24 |
+
migrations.CreateModel(
|
25 |
+
name="LLMGradedAnswer",
|
26 |
+
fields=[
|
27 |
+
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
28 |
+
(
|
29 |
+
"rubrics",
|
30 |
+
models.TextField(
|
31 |
+
blank=True,
|
32 |
+
null=True,
|
33 |
+
verbose_name="Grading Rubrics - For LLM-graded questions only. You can leave this empty.",
|
34 |
+
),
|
35 |
+
),
|
36 |
+
("question", models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to="quizzes.question")),
|
37 |
+
],
|
38 |
+
options={
|
39 |
+
"abstract": False,
|
40 |
+
},
|
41 |
+
),
|
42 |
+
]
|
src/quizzes/migrations/0004_remove_llmgradedanswer_rubrics_question_rubrics.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Generated by Django 4.2.7 on 2024-10-08 23:46
|
2 |
+
|
3 |
+
from django.db import migrations, models
|
4 |
+
|
5 |
+
|
6 |
+
class Migration(migrations.Migration):
|
7 |
+
|
8 |
+
dependencies = [
|
9 |
+
("quizzes", "0003_alter_freetextanswer_correct_answer_and_more"),
|
10 |
+
]
|
11 |
+
|
12 |
+
operations = [
|
13 |
+
migrations.RemoveField(
|
14 |
+
model_name="llmgradedanswer",
|
15 |
+
name="rubrics",
|
16 |
+
),
|
17 |
+
migrations.AddField(
|
18 |
+
model_name="question",
|
19 |
+
name="rubrics",
|
20 |
+
field=models.TextField(
|
21 |
+
blank=True,
|
22 |
+
null=True,
|
23 |
+
verbose_name="Grading Rubrics - For LLM-graded questions only. You can leave this empty.",
|
24 |
+
),
|
25 |
+
),
|
26 |
+
]
|
src/quizzes/models.py
CHANGED
@@ -1,5 +1,7 @@
|
|
|
|
1 |
import typing
|
2 |
|
|
|
3 |
from django.contrib.postgres import fields
|
4 |
from django.db import models
|
5 |
|
@@ -14,6 +16,9 @@ class Quiz(models.Model):
|
|
14 |
class Question(models.Model):
|
15 |
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
|
16 |
prompt = models.CharField(max_length=200)
|
|
|
|
|
|
|
17 |
|
18 |
def __str__(self):
|
19 |
return self.prompt
|
@@ -22,7 +27,8 @@ class Question(models.Model):
|
|
22 |
return (
|
23 |
getattr(self, "multiplechoiceanswer", None)
|
24 |
or getattr(self, "freetextanswer", None)
|
25 |
-
or getattr(self, "llmgradedanswer", None)
|
|
|
26 |
)
|
27 |
|
28 |
|
@@ -52,35 +58,31 @@ class FreeTextAnswer(Answer):
|
|
52 |
|
53 |
|
54 |
class LLMGradedAnswer(Answer):
|
55 |
-
|
56 |
-
blank=True, null=True, verbose_name="Grading Rubrics - For LLM-graded questions only. You can leave this empty."
|
57 |
-
)
|
58 |
-
|
59 |
-
def grade(self, user_answer, rubrics) -> dict:
|
60 |
-
import requests
|
61 |
-
|
62 |
"""
|
63 |
Grades the user's answer by calling the grading API.
|
64 |
-
|
65 |
Args:
|
66 |
user_answer (str): The answer provided by the user.
|
67 |
-
|
68 |
-
|
69 |
Returns:
|
70 |
-
|
71 |
"""
|
72 |
-
api_url = "http://localhost/api/grade"
|
73 |
-
payload = {"user_answer": user_answer, "rubrics": rubrics}
|
74 |
-
|
75 |
try:
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
print(f"An error occurred: {e}")
|
85 |
return {"result": "error", "message": str(e)}
|
86 |
|
|
|
1 |
+
import os
|
2 |
import typing
|
3 |
|
4 |
+
import openai
|
5 |
from django.contrib.postgres import fields
|
6 |
from django.db import models
|
7 |
|
|
|
16 |
class Question(models.Model):
|
17 |
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
|
18 |
prompt = models.CharField(max_length=200)
|
19 |
+
rubrics = models.TextField(
|
20 |
+
blank=True, null=True, verbose_name="Grading Rubrics - For LLM-graded questions only. You can leave this empty."
|
21 |
+
)
|
22 |
|
23 |
def __str__(self):
|
24 |
return self.prompt
|
|
|
27 |
return (
|
28 |
getattr(self, "multiplechoiceanswer", None)
|
29 |
or getattr(self, "freetextanswer", None)
|
30 |
+
# or getattr(self, "llmgradedanswer", None)
|
31 |
+
or self.llmgradedanswer # type: ignore
|
32 |
)
|
33 |
|
34 |
|
|
|
58 |
|
59 |
|
60 |
class LLMGradedAnswer(Answer):
|
61 |
+
def grade(self, user_answer) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
"""
|
63 |
Grades the user's answer by calling the grading API.
|
64 |
+
|
65 |
Args:
|
66 |
user_answer (str): The answer provided by the user.
|
67 |
+
|
|
|
68 |
Returns:
|
69 |
+
dict: The result of the grading.
|
70 |
"""
|
|
|
|
|
|
|
71 |
try:
|
72 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
73 |
+
prompt = f"Grade the following answer based on the rubric:\nRubric: {self.question.rubrics}\nAnswer: {user_answer}"
|
74 |
+
|
75 |
+
response = openai.ChatCompletion.create(
|
76 |
+
model="gpt-4o",
|
77 |
+
messages=[
|
78 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
79 |
+
{"role": "user", "content": prompt},
|
80 |
+
],
|
81 |
+
)
|
82 |
+
|
83 |
+
return {"result": "success", "message": response.choices[0].message["content"]}
|
84 |
+
|
85 |
+
except openai.error.OpenAIError as e:
|
86 |
print(f"An error occurred: {e}")
|
87 |
return {"result": "error", "message": str(e)}
|
88 |
|
src/quizzes/templates/quizzes/display.html
CHANGED
@@ -1,73 +1,83 @@
|
|
1 |
-
<!DOCTYPE
|
2 |
<html>
|
3 |
-
<head>
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
<div class="container">
|
13 |
-
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
<legend>{{ question.prompt }}</legend>
|
22 |
{% if question.freetextanswer %}
|
23 |
<div class="form-group">
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
</label>
|
31 |
-
<input type="text" name="answer" class="form-control">
|
32 |
</div>
|
33 |
-
{%
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
<div class="radio">
|
36 |
-
|
37 |
-
<input type="radio" name="answer" value="{{ choice }}"
|
38 |
{{ choice}}
|
39 |
-
|
40 |
</div>
|
41 |
-
{% endfor %}
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
</form>
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
Sorry, that question doesn't exist in this quiz.
|
51 |
-
{% endif %}
|
52 |
-
</section>
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
59 |
</div>
|
60 |
|
61 |
<script>
|
62 |
-
|
63 |
-
|
64 |
e.preventDefault();
|
65 |
-
fetch(form.action, {method:
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
</script>
|
72 |
-
</body>
|
73 |
</html>
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
<html>
|
3 |
+
<head>
|
4 |
+
<meta charset="utf-8" />
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
6 |
+
<title>Quiz: {{ quiz.name }}</title>
|
7 |
+
{% load static %}
|
8 |
+
<link
|
9 |
+
rel="stylesheet"
|
10 |
+
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
|
11 |
+
integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu"
|
12 |
+
crossorigin="anonymous"
|
13 |
+
/>
|
14 |
+
<link rel="stylesheet" href="{% static 'quizzes/style.css' %}" />
|
15 |
+
</head>
|
16 |
+
<body>
|
17 |
<div class="container">
|
18 |
+
<h1>Quiz: {{ quiz.name }}</h1>
|
19 |
|
20 |
+
<section class="question">
|
21 |
+
{% if question %}
|
22 |
+
<form
|
23 |
+
id="question-form"
|
24 |
+
action="{% url 'quizzes:grade_question' question.id %}"
|
25 |
+
method="post"
|
26 |
+
>
|
27 |
+
{% csrf_token %}
|
28 |
|
29 |
+
<fieldset class="form-group">
|
30 |
<legend>{{ question.prompt }}</legend>
|
31 |
{% if question.freetextanswer %}
|
32 |
<div class="form-group">
|
33 |
+
<label for="answer">
|
34 |
+
{% if question.freetextanswer.case_sensitive %} Type your answer
|
35 |
+
in (capitalization matters!): {% else %} Type your answer in
|
36 |
+
(don't worry about capitalization): {% endif %}
|
37 |
+
</label>
|
38 |
+
<input type="text" name="answer" class="form-control" />
|
|
|
|
|
39 |
</div>
|
40 |
+
{% elif question.llmgradedanswer %} The rubrics: {{ question.rubrics }}
|
41 |
+
<br />
|
42 |
+
<div class="form-group">
|
43 |
+
<label for="answer"> Type your answer: </label>
|
44 |
+
<input type="text" name="answer" class="form-control" />
|
45 |
+
</div>
|
46 |
+
{% else %} {% for choice in question.multiplechoiceanswer.choices %}
|
47 |
<div class="radio">
|
48 |
+
<label>
|
49 |
+
<input type="radio" name="answer" value="{{ choice }}" />
|
50 |
{{ choice}}
|
51 |
+
</label>
|
52 |
</div>
|
53 |
+
{% endfor %} {% endif %}
|
54 |
+
</fieldset>
|
55 |
+
<button type="submit" class="btn btn-primary">Check answers</button>
|
56 |
+
</form>
|
|
|
57 |
|
58 |
+
<div id="question-feedback" style="margin-top: 16px"></div>
|
59 |
+
{% else %} Sorry, that question doesn't exist in this quiz. {% endif %}
|
60 |
+
</section>
|
|
|
|
|
|
|
61 |
|
62 |
+
{% if next_question %}
|
63 |
+
<div style="margin-top: 12px; font-size: 16px">
|
64 |
+
<a href="{% url 'quizzes:display_question' quiz.id next_question.id %}"
|
65 |
+
>β Next question</a
|
66 |
+
>
|
67 |
+
</div>
|
68 |
+
{% endif %}
|
69 |
</div>
|
70 |
|
71 |
<script>
|
72 |
+
const form = document.getElementById("question-form");
|
73 |
+
form.addEventListener("submit", (e) => {
|
74 |
e.preventDefault();
|
75 |
+
fetch(form.action, { method: "post", body: new FormData(form) })
|
76 |
+
.then((response) => response.text())
|
77 |
+
.then((text) => {
|
78 |
+
document.getElementById("question-feedback").innerHTML = text;
|
79 |
+
});
|
80 |
+
});
|
81 |
</script>
|
82 |
+
</body>
|
83 |
</html>
|
src/quizzes/templates/quizzes/partial.html
CHANGED
@@ -1,19 +1,19 @@
|
|
|
|
|
|
1 |
{% if error %}
|
2 |
<div class="alert alert-danger" role="alert">
|
3 |
{{ error }}
|
4 |
</div>
|
5 |
{% else %}
|
6 |
|
|
|
7 |
{% if is_correct %}
|
8 |
β
You got it!
|
9 |
{% elif llm_answer %}
|
10 |
β
LLM's results based on rubrics:
|
11 |
|
12 |
-
{{ correct_answer|
|
13 |
-
|
14 |
-
<br/>
|
15 |
|
16 |
-
The rubrics: {{ rubrics }}
|
17 |
{% else %}
|
18 |
β Sorry! The correct answer is {{ correct_answer }}
|
19 |
{% endif %}
|
|
|
1 |
+
|
2 |
+
|
3 |
{% if error %}
|
4 |
<div class="alert alert-danger" role="alert">
|
5 |
{{ error }}
|
6 |
</div>
|
7 |
{% else %}
|
8 |
|
9 |
+
{% load markdownify %}
|
10 |
{% if is_correct %}
|
11 |
β
You got it!
|
12 |
{% elif llm_answer %}
|
13 |
β
LLM's results based on rubrics:
|
14 |
|
15 |
+
{{ correct_answer|markdownify }}
|
|
|
|
|
16 |
|
|
|
17 |
{% else %}
|
18 |
β Sorry! The correct answer is {{ correct_answer }}
|
19 |
{% endif %}
|
src/requirements.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
requests
|
2 |
Django==4.2.7
|
3 |
psycopg2==2.9.9
|
|
|
1 |
+
openai
|
2 |
+
django-markdownify
|
3 |
requests
|
4 |
Django==4.2.7
|
5 |
psycopg2==2.9.9
|