-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add exercises tags #326
base: master
Are you sure you want to change the base?
Changes from 4 commits
1204de3
865a4f4
23d0479
4c74eb2
53fb793
200be26
07c743c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -346,6 +346,27 @@ def on_notification_saved( | |
instance.delete_instance() | ||
|
||
|
||
class Tag(BaseModel): | ||
text = TextField() | ||
course = ForeignKeyField(Course) | ||
date_created = DateTimeField(default=datetime.now) | ||
|
||
class Meta: | ||
indexes = ( | ||
(('text', 'course_id'), True), | ||
) | ||
|
||
orronai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@classmethod | ||
def create_tag(cls, text: str, course: Course) -> 'Tag': | ||
orronai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
instance, _ = cls.get_or_create( | ||
**{cls.text.name: html.escape(text), cls.course.name: course}, | ||
) | ||
return instance | ||
|
||
def __str__(self): | ||
return self.text | ||
|
||
|
||
class Exercise(BaseModel): | ||
subject = CharField() | ||
date = DateTimeField() | ||
|
@@ -376,12 +397,8 @@ def is_number_exists(cls, number: int) -> bool: | |
return cls.select().where(cls.number == number).exists() | ||
|
||
@classmethod | ||
def get_objects( | ||
cls, user_id: int, fetch_archived: bool = False, | ||
from_all_courses: bool = False, | ||
): | ||
user = User.get(User.id == user_id) | ||
exercises = ( | ||
def by_user(cls, user_id: int): | ||
return ( | ||
cls | ||
.select() | ||
.join(Course) | ||
|
@@ -390,10 +407,20 @@ def get_objects( | |
.switch() | ||
.order_by(UserCourse.date, Exercise.number, Exercise.order) | ||
) | ||
|
||
@classmethod | ||
def get_objects( | ||
cls, user_id: int, fetch_archived: bool = False, | ||
from_all_courses: bool = False, exercise_tag: Optional[str] = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should find a better way. Creating these godlike functions with trillion parameters is a bad habit. Can you please find another way to do it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really find a better way |
||
): | ||
user = User.get(User.id == user_id) | ||
exercises = cls.by_user(user_id) | ||
if not from_all_courses: | ||
exercises = exercises.where( | ||
UserCourse.course == user.last_course_viewed, | ||
) | ||
if exercise_tag: | ||
exercises = Exercise.by_tags(exercises, exercise_tag) | ||
if not fetch_archived: | ||
exercises = exercises.where(cls.is_archived == False) # NOQA: E712 | ||
return exercises | ||
|
@@ -408,12 +435,22 @@ def as_dict(self) -> Dict[str, Any]: | |
'exercise_number': self.number, | ||
'course_id': self.course.id, | ||
'course_name': self.course.name, | ||
'tags': ExerciseTag.by_exercise(self), | ||
} | ||
|
||
@staticmethod | ||
def as_dicts(exercises: Iterable['Exercise']) -> ExercisesDictById: | ||
return {exercise.id: exercise.as_dict() for exercise in exercises} | ||
|
||
@staticmethod | ||
def by_tags(exercises: Iterable['Exercise'], exercise_tag: str): | ||
return ( | ||
exercises | ||
.join(ExerciseTag) | ||
.join(Tag) | ||
.where(Tag.text == exercise_tag) | ||
) | ||
|
||
def __str__(self): | ||
return self.subject | ||
|
||
|
@@ -426,6 +463,23 @@ def exercise_number_save_handler(model_class, instance, created): | |
instance.number = model_class.get_highest_number() + 1 | ||
|
||
|
||
class ExerciseTag(BaseModel): | ||
exercise = ForeignKeyField(Exercise) | ||
tag = ForeignKeyField(Tag) | ||
date = DateTimeField(default=datetime.now) | ||
|
||
@classmethod | ||
def by_exercise( | ||
cls, exercise: Exercise, | ||
) -> Union[Iterable['ExerciseTag'], 'ExerciseTag']: | ||
return ( | ||
cls | ||
.select() | ||
.where(cls.exercise == exercise) | ||
.order_by(cls.date) | ||
) | ||
|
||
|
||
orronai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class SolutionState(enum.Enum): | ||
CREATED = 'Created' | ||
IN_CHECKING = 'In checking' | ||
|
@@ -561,11 +615,11 @@ def test_results(self) -> Iterable[dict]: | |
@classmethod | ||
def of_user( | ||
cls, user_id: int, with_archived: bool = False, | ||
from_all_courses: bool = False, | ||
from_all_courses: bool = False, exercise_tag: Optional[str] = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same - we should try not to write such godlike functions |
||
) -> Iterable[Dict[str, Any]]: | ||
db_exercises = Exercise.get_objects( | ||
user_id=user_id, fetch_archived=with_archived, | ||
from_all_courses=from_all_courses, | ||
from_all_courses=from_all_courses, exercise_tag=exercise_tag, | ||
) | ||
exercises = Exercise.as_dicts(db_exercises) | ||
solutions = ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import Iterable, Optional, Union | ||
|
||
from lms.lmsdb.models import Course, Exercise, ExerciseTag, Tag | ||
|
||
|
||
def get_exercises_of( | ||
course: Course, tag_name: str, | ||
orronai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> Union[Iterable['ExerciseTag'], 'ExerciseTag']: | ||
return ( | ||
ExerciseTag | ||
.select(ExerciseTag.exercise) | ||
.join(Tag) | ||
.where(Tag.text == tag_name, Tag.course == course) | ||
) | ||
|
||
|
||
def of_exercise( | ||
exercise_id: Optional[int] = None, course: Optional[int] = None, | ||
number: Optional[int] = None, | ||
orronai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> Optional[Union[Iterable['ExerciseTag'], 'ExerciseTag']]: | ||
if exercise_id is not None: | ||
return ExerciseTag.select().where(ExerciseTag.exercise == id) | ||
elif course is not None: | ||
orronai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tags = ( | ||
ExerciseTag | ||
.select() | ||
.join(Exercise) | ||
.where(Exercise.course == course) | ||
) | ||
if number is not None: | ||
tags = tags.where(Exercise.number == number) | ||
return tags | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I now think about changing this tablename to ExerciseTag, and the connection to ExerciseToTag, in order to allow easy future expansion of the tags to user. IDK what the right approach to a state where we have both Users and Exercises that use tags, but I have a strong hunch that they shouldn't share the same table.