This repository was archived by the owner on Jun 7, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcrud.py
More file actions
889 lines (769 loc) · 29.6 KB
/
crud.py
File metadata and controls
889 lines (769 loc) · 29.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# *************************************************
# |docname| - reusable functions for our data model
# *************************************************
# Create Retrieve Update and Delete (CRUD) functions for database tables
#
# Rather than litter the code with raw database queries the vast majority should be
# turned into reusable functions that are defined in this file.
#
# Imports
# =======
# These are listed in the order prescribed by `PEP 8`_.
#
# Standard library
# ----------------
import datetime
import json
from collections import namedtuple
from random import randrange
from typing import Dict, List, Optional, Tuple
# Third-party imports
# -------------------
from fastapi.exceptions import HTTPException
from pydal.validators import CRYPT
from sqlalchemy import and_, distinct, func, update
from sqlalchemy.sql import select, text
from . import schemas
# Local application imports
# -------------------------
from .applogger import rslogger
from .config import BookServerConfig, settings
from .db import async_session
from .internal.utils import http_422error_detail
from .models import (
Assignment,
AssignmentQuestion,
AssignmentQuestionValidator,
AuthUser,
AuthUserValidator,
Chapter,
Code,
CodeValidator,
Competency,
CourseInstructor,
CourseInstructorValidator,
Courses,
CoursesValidator,
Question,
QuestionValidator,
SelectedQuestion,
SelectedQuestionValidator,
SubChapter,
TimedExam,
TimedExamValidator,
Useinfo,
UseinfoValidation,
UserChapterProgress,
UserChapterProgressValidator,
UserExperiment,
UserExperimentValidator,
UserState,
UserStateValidator,
UserSubChapterProgress,
UserSubChapterProgressValidator,
runestone_component_dict,
)
from .schemas import SeedRequest
# Map from the ``event`` field of a ``LogItemIncoming`` to the database table used to store data associated with this event.
EVENT2TABLE = {
"clickableArea": "clickablearea_answers",
"codelens1": "codelens_answers",
"dragNdrop": "dragndrop_answers",
"fillb": "fitb_answers",
"lp_build": "lp_answers",
"mChoice": "mchoice_answers",
"parsons": "parsons_answers",
"shortanswer": "shortanswer_answers",
"unittest": "unittest_answers",
"timedExam": "timed_exam",
}
# useinfo
# -------
async def create_useinfo_entry(log_entry: UseinfoValidation) -> UseinfoValidation:
async with async_session.begin() as session:
new_entry = Useinfo(**log_entry.dict())
rslogger.debug(f"timestamp = {log_entry.timestamp} ")
rslogger.debug(f"New Entry = {new_entry}")
rslogger.debug(f"session = {session}")
session.add(new_entry)
rslogger.debug(new_entry)
return UseinfoValidation.from_orm(new_entry)
async def count_useinfo_for(
div_id: str, course_name: str, start_date: datetime.datetime
) -> List[tuple]:
"""
return a list of tuples that include the [(act, count), (act, count)]
act is a freeform field in the useinfo table that varies from event
type to event type.
"""
query = (
select(Useinfo.act, func.count(Useinfo.act).label("count"))
.where(
(Useinfo.div_id == div_id)
& (Useinfo.course_id == course_name)
& (Useinfo.timestamp > start_date)
)
.group_by(Useinfo.act)
)
async with async_session() as session:
res = await session.execute(query)
rslogger.debug(f"res = {res}")
return res.all()
async def fetch_page_activity_counts(
chapter: str, subchapter: str, base_course: str, course_name: str, username: str
) -> Dict[str, int]:
"""
Used for the progress bar at the bottom of each page. This function
finds all of the components for a particular page (chaper/subchapter)
and then finds out which of those elements the student has interacted
with. It returns a dictionary of {divid: 0/1}
"""
where_clause_common = (
(Question.subchapter == subchapter)
& (Question.chapter == chapter)
& (Question.from_source == True) # noqa: E712
& ((Question.optional == False) | (Question.optional == None))
& (Question.base_course == base_course)
)
query = select(Question).where(where_clause_common)
async with async_session() as session:
page_divids = await session.execute(query)
rslogger.debug(f"PDVD {page_divids}")
div_counts = {q.name: 0 for q in page_divids.scalars()}
query = select(distinct(Useinfo.div_id)).where(
where_clause_common
& (Question.name == Useinfo.div_id)
& (Useinfo.course_id == course_name)
& (Useinfo.sid == username)
)
async with async_session() as session:
sid_counts = await session.execute(query)
for row in sid_counts.scalars():
div_counts[row.div_id] = 1
return div_counts
async def fetch_poll_summary(div_id: str, course_name: str) -> List[tuple]:
"""
find the last answer for each student and then aggregate
those answers to provide a summary of poll responses for the
given question. for a poll the value of act is a response
number 0--N where N is the number of different choices.
"""
query = text(
"""select act, count(*) from useinfo
join (select sid, max(id) mid
from useinfo where event='poll' and div_id = :div_id and course_id = :course_name group by sid) as T
on id = T.mid group by act"""
)
async with async_session() as session:
rows = await session.execute(
query, params=dict(div_id=div_id, course_name=course_name)
)
return rows.all()
async def fetch_top10_fitb(dbcourse: CoursesValidator, div_id: str) -> List[tuple]:
"Return the top 10 answers to a fill in the blank question"
rcd = runestone_component_dict["fitb_answers"]
tbl = rcd.model
query = (
select(tbl.answer, func.count(tbl.answer).label("count"))
.where(
(tbl.div_id == div_id)
& (tbl.course_name == dbcourse.course_name)
& (tbl.timestamp > dbcourse.term_start_date)
)
.group_by(tbl.answer)
.order_by(func.count(tbl.answer).desc())
.limit(10)
)
async with async_session() as session:
rows = await session.execute(query)
return rows.all()
# xxx_answers
# -----------
async def create_answer_table_entry(
# The correct type is one of the validators for an answer table; we use LogItemIncoming as a generalization of this.
log_entry: schemas.LogItemIncoming,
# The event type.
event: str,
) -> schemas.LogItemIncoming:
rslogger.debug(f"hello from create at {log_entry}")
rcd = runestone_component_dict[EVENT2TABLE[event]]
new_entry = rcd.model(**log_entry.dict()) # type: ignore
async with async_session.begin() as session:
session.add(new_entry)
rslogger.debug(f"returning {new_entry}")
return rcd.validator.from_orm(new_entry) # type: ignore
# Create a new random seed for a problem, discarding any previous answers.
async def create_seed(seed_request: SeedRequest):
sr = seed_request.dict()
event = sr.pop("event")
rcd = runestone_component_dict[EVENT2TABLE[event]]
new_entry = rcd.model(**sr, correct=False, timestamp=datetime.datetime.utcnow())
new_entry.seed = randrange(2 ** 32 - 1)
async with async_session.begin() as session:
session.add(new_entry)
# Return the last table entry as a Pydantic schema and a True if this table contains a seed, or False if not.
async def fetch_last_answer_table_entry(
query_data: schemas.AssessmentRequest,
) -> Tuple[schemas.LogItemIncoming, bool]:
rcd = runestone_component_dict[EVENT2TABLE[query_data.event]]
tbl = rcd.model
query = (
select(tbl)
.where(
and_(
tbl.div_id == query_data.div_id,
tbl.course_name == query_data.course,
tbl.sid == query_data.sid,
)
)
.order_by(tbl.timestamp.desc())
)
async with async_session() as session:
res = (await session.execute(query)).scalars().first()
rslogger.debug(f"res = {res}")
return rcd.validator.from_orm(res), bool(getattr(tbl, "seed", False)) # type: ignore
async def fetch_last_poll_response(sid: str, course_name: str, poll_id: str) -> str:
"""
Return a student's (sid) last response to a given poll (poll_id)
"""
query = (
select(Useinfo.act)
.where(
(Useinfo.sid == sid)
& (Useinfo.course_id == course_name)
& (Useinfo.div_id == poll_id)
)
.order_by(Useinfo.id.desc())
)
async with async_session() as session:
res = await session.execute(query)
return res.first()
# Courses
# -------
async def fetch_course(course_name: str) -> CoursesValidator:
query = select(Courses).where(Courses.course_name == course_name)
async with async_session() as session:
res = await session.execute(query)
# When selecting ORM entries it is useful to use the ``scalars`` method
# This modifies the result so that you are getting the ORM object
# instead of a Row object. `See <https://docs.sqlalchemy.org/en/14/orm/queryguide.html#selecting-orm-entities-and-attributes>`_
course = res.scalars().one_or_none()
return CoursesValidator.from_orm(course)
async def fetch_base_course(base_course: str) -> CoursesValidator:
query = select(Courses).where(
(Courses.base_course == base_course) & (Courses.course_name == base_course)
)
async with async_session() as session:
res = await session.execute(query)
# When selecting ORM entries it is useful to use the ``scalars`` method
# This modifies the result so that you are getting the ORM object
# instead of a Row object. `See <https://docs.sqlalchemy.org/en/14/orm/queryguide.html#selecting-orm-entities-and-attributes>`_
base_course = res.scalars().one_or_none()
return CoursesValidator.from_orm(base_course)
async def create_course(course_info: CoursesValidator) -> None:
new_course = Courses(**course_info.dict())
async with async_session.begin() as session:
session.add(new_course)
# auth_user
# ---------
async def fetch_user(user_name: str) -> AuthUserValidator:
query = select(AuthUser).where(AuthUser.username == user_name)
async with async_session() as session:
res = await session.execute(query)
user = res.scalars().one_or_none()
return AuthUserValidator.from_orm(user)
async def create_user(user: AuthUserValidator) -> Optional[AuthUserValidator]:
"""
The given user will have the password in plain text. First we will hash
the password then add this user to the database.
"""
if await fetch_user(user.username):
raise HTTPException(
status_code=422,
detail=http_422error_detail(
["body", "username"], "duplicate username", "integrity_error"
),
)
new_user = AuthUser(**user.dict())
crypt = CRYPT(key=settings.web2py_private_key, salt=True)
new_user.password = str(crypt(user.password)[0])
async with async_session.begin() as session:
session.add(new_user)
return AuthUserValidator.from_orm(new_user)
# instructor_courses
# ------------------
async def fetch_instructor_courses(
instructor_id: int, course_id: Optional[int] = None
) -> List[CourseInstructorValidator]:
"""
return a list of courses for which the given userid is an instructor.
If the optional course_id value is included then return the row for that
course to verify that instructor_id is an instructor for course_id
"""
query = select(CourseInstructor)
if course_id is not None:
query = query.where(
and_(
CourseInstructor.instructor == instructor_id,
CourseInstructor.course == course_id,
)
)
else:
query = query.where(CourseInstructor.instructor == instructor_id)
async with async_session() as session:
res = await session.execute(query)
course_list = [
CourseInstructorValidator.from_orm(x) for x in res.scalars().fetchall()
]
return course_list
# Code
# ----
async def create_code_entry(data: CodeValidator) -> CodeValidator:
new_code = Code(**data.dict())
async with async_session.begin() as session:
session.add(new_code)
return CodeValidator.from_orm(new_code)
async def fetch_code(sid: str, acid: str, course_id: int) -> List[CodeValidator]:
query = (
select(Code)
.where((Code.sid == sid) & (Code.acid == acid) & (Code.course_id == course_id))
.order_by(Code.id)
)
async with async_session() as session:
res = await session.execute(query)
code_list = [CodeValidator.from_orm(x) for x in res.scalars().fetchall()]
return code_list
# Server-side grading
# -------------------
# Return the feedback associated with this question if this question should be graded on the server instead of on the client; otherwise, return None.
async def is_server_feedback(div_id, course):
# Get the information about this question.
query = (
select(Question, Courses)
.where(Question.name == div_id)
.join(Courses, Question.base_course == Courses.base_course)
.where(Courses.course_name == course)
)
async with async_session() as session:
query_results = (await session.execute(query)).first()
# Get the feedback, if it exists.
feedback = query_results and query_results.Question.feedback
# If there's feedback and a login is required (necessary for server-side grading), return the decoded feedback.
if feedback and query_results.Courses.login_required:
return json.loads(feedback), query_results.Courses.base_course
# Otherwise, grade on the client.
return None, None
# Development and Testing Utils
# -----------------------------
# This function is useful for development. It recreates the database
# and populates it with the common base courses and creates a test user
#
async def create_initial_courses_users():
# never ever drop tables in a production environment
rslogger.debug(f"HELLO {settings.book_server_config} - {settings.drop_tables}")
if (
settings.book_server_config
in [BookServerConfig.development, BookServerConfig.test]
and settings.drop_tables == "Yes"
):
rslogger.debug("Populating Courses")
BASE_COURSES = [
"ac1",
"cppds",
"cppforpython",
"csawesome",
"csjava",
"fopp",
"httlads",
"java4python",
"JS4Python",
"learnwebgl2",
"MasteringDatabases",
"overview",
"py4e-int",
"pythonds",
"pythonds3",
"StudentCSP",
"TeacherCSP",
"thinkcpp",
"thinkcspy",
"webfundamentals",
"test_course_1",
]
for c in BASE_COURSES:
new_course = CoursesValidator(
course_name=c,
base_course=c,
term_start_date=datetime.date(2000, 1, 1),
login_required=False,
allow_pairs=False,
downloads_enabled=False,
courselevel="",
institution="",
new_server=True,
)
await create_course(new_course)
# make a user
await create_user(
AuthUserValidator(
username="testuser1",
first_name="test",
last_name="user",
password="xxx",
email="testuser1@example.com",
course_name="overview",
course_id=12,
donated=True,
active=True,
accept_tcp=True,
created_on=datetime.datetime(2020, 1, 1, 0, 0, 0),
modified_on=datetime.datetime(2020, 1, 1, 0, 0, 0),
registration_key="",
registration_id="",
reset_password_key="",
)
)
# User Progress
# -------------
async def create_user_state_entry(user_id: int, course_name: str) -> UserStateValidator:
new_us = UserState(user_id=user_id, course_name=course_name)
async with async_session.begin() as session:
session.add(new_us)
return UserStateValidator.from_orm(new_us)
async def update_user_state(user_data: schemas.LastPageData):
ud = user_data.dict()
# LastPageData contains information for both user_state and user_sub_chapter_progress tables
# we do not need the completion flag in the user_state table
ud.pop("completion_flag")
rslogger.debug(f"user data = {ud}")
stmt = (
update(UserState)
.where(
(UserState.user_id == user_data.user_id)
& (UserState.course_name == user_data.course_name)
)
.values(**ud)
)
async with async_session.begin() as session:
await session.execute(stmt)
rslogger.debug("SUCCESS")
async def update_sub_chapter_progress(user_data: schemas.LastPageData):
ud = user_data.dict()
ud.pop("last_page_url")
ud.pop("last_page_scroll_location")
ud.pop("last_page_accessed_on")
ud["status"] = ud.pop("completion_flag")
ud["chapter_id"] = ud.pop("last_page_chapter")
ud["sub_chapter_id"] = ud.pop("last_page_subchapter")
stmt = (
update(UserSubChapterProgress)
.where(
(UserSubChapterProgress.user_id == user_data.user_id)
& (UserSubChapterProgress.chapter_id == user_data.last_page_chapter)
& (UserSubChapterProgress.sub_chapter_id == user_data.last_page_subchapter)
& (
(UserSubChapterProgress.course_name == user_data.course_name)
| (
UserSubChapterProgress.course_name == None # noqa 711
) # Back fill for old entries without course
)
)
.values(**ud)
)
async with async_session.begin() as session:
await session.execute(stmt)
async def fetch_last_page(user: AuthUserValidator, course_name: str):
course = await fetch_course(course_name)
query = (
select(
[
UserState.last_page_url,
UserState.last_page_hash,
Chapter.chapter_name,
UserState.last_page_scroll_location,
SubChapter.sub_chapter_name,
]
)
.where(
(UserState.user_id == user.id)
& (UserState.last_page_chapter == Chapter.chapter_label)
& (UserState.course_name == course.course_name)
& (SubChapter.chapter_id == Chapter.id)
& (UserState.last_page_subchapter == SubChapter.sub_chapter_label)
& (Chapter.course_id == course.base_course)
)
.order_by(UserState.last_page_accessed_on.desc())
)
async with async_session() as session:
res = await session.execute(query)
# for A query like this one with columns from multiple tables
# res.first() returns a tuple
rslogger.debug(f"LP {res}")
PageData = namedtuple("PageData", [col for col in res.keys()]) # type: ignore
rdata = res.first()
rslogger.debug(f"{rdata=}")
if rdata:
return PageData(*rdata)
else:
return None
async def fetch_user_sub_chapter_progress(
user, last_page_chapter=None, last_page_subchapter=None
) -> UserSubChapterProgressValidator:
where_clause = (UserSubChapterProgress.user_id == user.id) & (
UserSubChapterProgress.course_name == user.course_name
)
if last_page_chapter:
where_clause = (
where_clause
& (UserSubChapterProgress.chapter_id == last_page_chapter)
& (UserSubChapterProgress.sub_chapter_id == last_page_subchapter)
)
query = select(UserSubChapterProgress).where(where_clause)
async with async_session() as session:
res = await session.execute(query)
rslogger.debug(f"{res=}")
return [
UserSubChapterProgressValidator.from_orm(x)
for x in res.scalars().fetchall()
]
async def create_user_sub_chapter_progress_entry(
user, last_page_chapter, last_page_subchapter, status=-1
) -> UserSubChapterProgressValidator:
new_uspe = UserSubChapterProgress(
user_id=user.id,
chapter_id=last_page_chapter,
sub_chapter_id=last_page_subchapter,
status=status,
start_date=datetime.datetime.utcnow(),
course_name=user.course_name,
)
async with async_session.begin() as session:
session.add(new_uspe)
return UserSubChapterProgressValidator.from_orm(new_uspe)
async def fetch_user_chapter_progress(
user, last_page_chapter: str
) -> UserChapterProgressValidator:
query = select(UserChapterProgress).where(
(
UserChapterProgress.user_id == str(user.id)
) # TODO: this is bad! the DB has user.id as a string!
& (UserChapterProgress.chapter_id == last_page_chapter)
)
async with async_session() as session:
res = await session.execute(query)
rslogger.debug(f"{res=}")
return UserChapterProgressValidator.from_orm(res.scalars().first())
async def create_user_chapter_progress_entry(
user, last_page_chapter, status
) -> UserChapterProgressValidator:
new_ucp = UserChapterProgress(
user_id=str(user.id),
chapter_id=last_page_chapter,
status=status,
start_date=datetime.datetime.utcnow(),
)
async with async_session.begin() as session:
session.add(new_ucp)
return UserChapterProgressValidator.from_orm(new_ucp)
#
# Select Question Support
# -----------------------
async def create_selected_question(
sid: str,
selector_id: str,
selected_id: str,
points: Optional[int] = None,
competency: Optional[str] = None,
) -> SelectedQuestionValidator:
new_sqv = SelectedQuestion(
sid=sid,
selector_id=selector_id,
selected_id=selected_id,
points=points,
competency=competency,
)
async with async_session.begin() as session:
session.add(new_sqv)
return SelectedQuestionValidator.from_orm(new_sqv)
async def fetch_selected_question(
sid: str, selector_id: str
) -> SelectedQuestionValidator:
"""
Used with selectquestions. This returns the information about
a question previously chosen for the given (selector_id) question
for a particular student (sid) - see `get_question_source` for
more info on select questions.
"""
query = select(SelectedQuestion).where(
(SelectedQuestion.sid == sid) & (SelectedQuestion.selector_id == selector_id)
)
async with async_session() as session:
res = await session.execute(query)
rslogger.debug(f"{res=}")
return SelectedQuestionValidator.from_orm(res.scalars().first())
async def update_selected_question(sid: str, selector_id: str, selected_id: str):
"""
Used in conjunction with the toggle feature of select question to update
which question the student has chosen to work on.
"""
stmt = (
update(SelectedQuestion)
.where(
(SelectedQuestion.sid == sid)
& (SelectedQuestion.selector_id == selector_id)
)
.values(selected_id=selected_id)
)
async with async_session.begin() as session:
await session.execute(stmt)
rslogger.debug("SUCCESS")
# Questions and Assignments
# -------------------------
async def fetch_question(
name: str, basecourse: Optional[str] = None
) -> QuestionValidator:
"""
Fetch a single matching question row from the database that matches
the name (div_id) of the question. If the base course is provided
make sure the question comes from that basecourse. basecourse,name pairs
are guaranteed to be unique in the questions table
More and more questions have globally unique names in the runestone
database and that is definitely a direction to keep pushing. But
it is possible that there are duplicates but we are not going to
worry about that we are just going to return the first one we find.
"""
where_clause = Question.name == name
if basecourse:
where_clause = where_clause & (Question.base_course == basecourse)
query = select(Question).where(where_clause)
async with async_session() as session:
res = await session.execute(query)
rslogger.debug(f"{res=}")
return QuestionValidator.from_orm(res.scalars().first())
auto_gradable_q = [
"clickablearea",
"mchoice",
"parsonsprob",
"dragndrop",
"fillintheblank",
"lp",
]
async def fetch_matching_questions(request_data: schemas.SelectQRequest) -> List[str]:
"""
Return a list of question names (div_ids) that match the criteria
for a particular question. This is used by select questions and in
particular `get_question_source`
"""
if request_data.questions:
questionlist = request_data.questions.split(",")
questionlist = [q.strip() for q in questionlist]
elif request_data.proficiency:
prof = request_data.proficiency
where_clause = (Competency.competency == prof) & (
Competency.question == Question.id
)
if request_data.primary:
where_clause = where_clause & (Competency.is_primary == True) # noqa E712
if request_data.min_difficulty:
where_clause = where_clause & (
Question.difficulty >= float(request_data.min_difficulty)
)
if request_data.max_difficulty:
where_clause = where_clause & (
Question.difficulty <= float(request_data.max_difficulty)
)
if request_data.autogradable:
where_clause = where_clause & (
(Question.autograde == "unittest")
| Question.question_type.in_(auto_gradable_q)
)
query = select(Question).where(where_clause)
async with async_session() as session:
res = await session.execute(query)
rslogger.debug(f"{res=}")
questionlist = [q.name for q in res]
return questionlist
async def fetch_assignment_question(
assignment_name: str, question_name: str
) -> AssignmentQuestionValidator:
"""
Get an assignment question row object
"""
query = select(AssignmentQuestion).where(
(Assignment.name == assignment_name)
& (Assignment.id == AssignmentQuestion.assignment_id)
& (AssignmentQuestion.question_id == Question.id)
& (Question.name == question_name)
)
async with async_session() as session:
res = await session.execute(query)
rslogger.debug(f"{res=}")
return AssignmentQuestionValidator.from_orm(res.scalars().first())
async def fetch_user_experiment(sid: str, ab_name: str) -> int:
"""
When a question is part of an AB experiement (ab_name) get the experiment
group for a particular student (sid). The group number will have
been randomly assigned by the initial question selection.
This number indicates whether the student will see the 1st or 2nd
question in the question list.
"""
query = (
select(UserExperiment.exp_group)
.where((UserExperiment.sid == sid) & (UserExperiment.experiment_id == ab_name))
.order_by(UserExperiment.id)
)
async with async_session() as session:
res = await session.execute(query)
r = res.scalars().first()
rslogger.debug(f"{r=}")
return r
async def create_user_experiment_entry(
sid: str, ab: str, group: int
) -> UserExperimentValidator:
"""
Store the number of the group number (group) this student (sid) hass been assigned to
for this particular experiment (ab)
"""
new_ue = UserExperiment(sid=sid, exp_group=group, experiment_id=ab)
async with async_session.begin() as session:
session.add(new_ue)
return UserExperimentValidator.from_orm(new_ue)
async def fetch_viewed_questions(sid: str, questionlist: List[str]) -> List[str]:
"""
Used for the selectquestion `get_question_source` to filter out questions
that a student (sid) has seen before. One criteria of a select question
is to make sure that a student has never seen a question before.
The best approximation we have for that is that they will have clicked on the
run button for that quesiton. Of course they may have seen said question
but not run it but this is the best we can do.
"""
query = select(Useinfo).where(
(Useinfo.sid == sid) & (Useinfo.div_id.in_(questionlist))
)
async with async_session() as session:
res = await session.execute(query)
rslogger.debug(f"{res=}")
rlist = [row.div_id for row in res]
return rlist
async def fetch_previous_selections(sid) -> List[str]:
query = select(SelectedQuestion).where(SelectedQuestion.sid == sid)
async with async_session() as session:
res = await session.execute(query)
rslogger.debug(f"{res=}")
return [row.selected_id for row in res.scalars().all()]
async def fetch_timed_exam(
sid: str, exam_id: str, course_name: str
) -> TimedExamValidator:
query = (
select(TimedExam)
.where(
(TimedExam.div_id == exam_id)
& (TimedExam.sid == sid)
& (TimedExam.course_name == course_name)
)
.order_by(TimedExam.id.desc())
)
async with async_session() as session:
res = await session.execute(query)
rslogger.debug(f"{res=}")
return TimedExamValidator.from_orm(res.scalars().first())