Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
el-agua committed Sep 1, 2024
1 parent 4605ae0 commit 1cdf8fa
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 187 deletions.
34 changes: 29 additions & 5 deletions backend/courses/migrations/0064_auto_20240120_1914.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
# Generated by Django 3.2.23 on 2024-01-21 00:14

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('courses', '0063_auto_20231212_1750'),
("courses", "0063_auto_20231212_1750"),
]

operations = [
migrations.AlterField(
model_name='section',
name='activity',
field=models.CharField(choices=[('', 'Undefined'), ('CLN', 'Clinic'), ('CRT', 'Clinical Rotation'), ('DAB', 'Dissertation Abroad'), ('DIS', 'Dissertation'), ('DPC', 'Doctoral Program Exchange'), ('FLD', 'Field Work'), ('HYB', 'Hybrid'), ('IND', 'Independent Study'), ('LAB', 'Lab'), ('LEC', 'Lecture'), ('MST', 'Masters Thesis'), ('ONL', 'Online'), ('PRC', 'Practicum'), ('REC', 'Recitation'), ('SEM', 'Seminar'), ('SRT', 'Senior Thesis'), ('STU', 'Studio')], db_index=True, help_text='The section activity, e.g. `LEC` for CIS-120-001 (2020A). Options and meanings: <table width=100%><tr><td>""</td><td>"Undefined"</td></tr><tr><td>"CLN"</td><td>"Clinic"</td></tr><tr><td>"CRT"</td><td>"Clinical Rotation"</td></tr><tr><td>"DAB"</td><td>"Dissertation Abroad"</td></tr><tr><td>"DIS"</td><td>"Dissertation"</td></tr><tr><td>"DPC"</td><td>"Doctoral Program Exchange"</td></tr><tr><td>"FLD"</td><td>"Field Work"</td></tr><tr><td>"HYB"</td><td>"Hybrid"</td></tr><tr><td>"IND"</td><td>"Independent Study"</td></tr><tr><td>"LAB"</td><td>"Lab"</td></tr><tr><td>"LEC"</td><td>"Lecture"</td></tr><tr><td>"MST"</td><td>"Masters Thesis"</td></tr><tr><td>"ONL"</td><td>"Online"</td></tr><tr><td>"PRC"</td><td>"Practicum"</td></tr><tr><td>"REC"</td><td>"Recitation"</td></tr><tr><td>"SEM"</td><td>"Seminar"</td></tr><tr><td>"SRT"</td><td>"Senior Thesis"</td></tr><tr><td>"STU"</td><td>"Studio"</td></tr></table>', max_length=50),
model_name="section",
name="activity",
field=models.CharField(
choices=[
("", "Undefined"),
("CLN", "Clinic"),
("CRT", "Clinical Rotation"),
("DAB", "Dissertation Abroad"),
("DIS", "Dissertation"),
("DPC", "Doctoral Program Exchange"),
("FLD", "Field Work"),
("HYB", "Hybrid"),
("IND", "Independent Study"),
("LAB", "Lab"),
("LEC", "Lecture"),
("MST", "Masters Thesis"),
("ONL", "Online"),
("PRC", "Practicum"),
("REC", "Recitation"),
("SEM", "Seminar"),
("SRT", "Senior Thesis"),
("STU", "Studio"),
],
db_index=True,
help_text='The section activity, e.g. `LEC` for CIS-120-001 (2020A). Options and meanings: <table width=100%><tr><td>""</td><td>"Undefined"</td></tr><tr><td>"CLN"</td><td>"Clinic"</td></tr><tr><td>"CRT"</td><td>"Clinical Rotation"</td></tr><tr><td>"DAB"</td><td>"Dissertation Abroad"</td></tr><tr><td>"DIS"</td><td>"Dissertation"</td></tr><tr><td>"DPC"</td><td>"Doctoral Program Exchange"</td></tr><tr><td>"FLD"</td><td>"Field Work"</td></tr><tr><td>"HYB"</td><td>"Hybrid"</td></tr><tr><td>"IND"</td><td>"Independent Study"</td></tr><tr><td>"LAB"</td><td>"Lab"</td></tr><tr><td>"LEC"</td><td>"Lecture"</td></tr><tr><td>"MST"</td><td>"Masters Thesis"</td></tr><tr><td>"ONL"</td><td>"Online"</td></tr><tr><td>"PRC"</td><td>"Practicum"</td></tr><tr><td>"REC"</td><td>"Recitation"</td></tr><tr><td>"SEM"</td><td>"Seminar"</td></tr><tr><td>"SRT"</td><td>"Senior Thesis"</td></tr><tr><td>"STU"</td><td>"Studio"</td></tr></table>',
max_length=50,
),
)
]
7 changes: 3 additions & 4 deletions backend/courses/migrations/0066_merge_20240426_0627.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
class Migration(migrations.Migration):

dependencies = [
('courses', '0064_auto_20240120_1914'),
('courses', '0065_topic_historical_probabilities_fall_and_more'),
("courses", "0064_auto_20240120_1914"),
("courses", "0065_topic_historical_probabilities_fall_and_more"),
]

operations = [
]
operations = []
78 changes: 64 additions & 14 deletions backend/courses/migrations/0067_comment.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,82 @@
# Generated by Django 3.2.23 on 2024-04-26 10:27

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('courses', '0066_merge_20240426_0627'),
("courses", "0066_merge_20240426_0627"),
]

operations = [
migrations.CreateModel(
name='Comment',
name="Comment",
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('modified_at', models.DateTimeField(auto_now=True)),
('path', models.TextField(db_index=True)),
('author', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='comments', to=settings.AUTH_USER_MODEL)),
('base', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='courses.comment')),
('downvotes', models.ManyToManyField(help_text='The number of downvotes a comment gets.', related_name='downvotes', to=settings.AUTH_USER_MODEL)),
('parent', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='courses.comment')),
('section', models.ForeignKey(help_text='\nThe section with which a comment is associated. Section was chosen instead of topics for\nhosting comments because topics are SOFT STATE and are recomputed regularly.\n', null=True, on_delete=django.db.models.deletion.CASCADE, to='courses.section')),
('upvotes', models.ManyToManyField(help_text='The number of upvotes a comment gets.', related_name='upvotes', to=settings.AUTH_USER_MODEL)),
(
"id",
models.AutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("text", models.TextField()),
("created_at", models.DateTimeField(auto_now_add=True)),
("modified_at", models.DateTimeField(auto_now=True)),
("path", models.TextField(db_index=True)),
(
"author",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="comments",
to=settings.AUTH_USER_MODEL,
),
),
(
"base",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="courses.comment",
),
),
(
"downvotes",
models.ManyToManyField(
help_text="The number of downvotes a comment gets.",
related_name="downvotes",
to=settings.AUTH_USER_MODEL,
),
),
(
"parent",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="children",
to="courses.comment",
),
),
(
"section",
models.ForeignKey(
help_text="\nThe section with which a comment is associated. Section was chosen instead of topics for\nhosting comments because topics are SOFT STATE and are recomputed regularly.\n",
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="courses.section",
),
),
(
"upvotes",
models.ManyToManyField(
help_text="The number of upvotes a comment gets.",
related_name="upvotes",
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
29 changes: 12 additions & 17 deletions backend/courses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from PennCourses.settings.base import FIRST_BANNER_SEM, PRE_NGSS_PERMIT_REQ_RESTRICTION_CODES
from review.annotations import review_averages


User = get_user_model()


Expand Down Expand Up @@ -1540,6 +1541,7 @@ def __str__(self):
f"Friendship(Sender: {self.sender}, Recipient: {self.recipient}, Status: {self.status})"
)


class Comment(models.Model):
"""
A single comment associated with a topic to be displayed on PCR. Comments support replies
Expand All @@ -1555,20 +1557,15 @@ class Comment(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
author = models.ForeignKey(
get_user_model(),
on_delete = models.SET_NULL,
null=True,
related_name="comments"
get_user_model(), on_delete=models.SET_NULL, null=True, related_name="comments"
)
upvotes = models.ManyToManyField(
get_user_model(),
related_name="upvotes",
help_text="The number of upvotes a comment gets."
get_user_model(), related_name="upvotes", help_text="The number of upvotes a comment gets."
)
downvotes = models.ManyToManyField(
get_user_model(),
related_name="downvotes",
help_text="The number of downvotes a comment gets."
help_text="The number of downvotes a comment gets.",
)
section = models.ForeignKey(
Section,
Expand All @@ -1579,25 +1576,22 @@ class Comment(models.Model):
hosting comments because topics are SOFT STATE and are recomputed regularly.
"""
),
null=True
null=True,
)

base = models.ForeignKey(
"self",
on_delete=models.SET_NULL, # redundant due to special deletion conditions
on_delete=models.SET_NULL, # redundant due to special deletion conditions
null=True,
)
parent = models.ForeignKey(
"self",
on_delete=models.SET_NULL, # similarly redundant
null=True,
related_name="children"
"self", on_delete=models.SET_NULL, null=True, related_name="children" # similarly redundant
)
path = models.TextField(db_index=True)

def level(self):
return len(self.path.split('.'))
return len(self.path.split("."))

def delete(self, **kwargs):
if Comment.objects.filter(parent_id=self).exists():
self.text = "This comment has been removed."
Expand All @@ -1607,5 +1601,6 @@ def delete(self, **kwargs):
self.save()
else:
super().delete(**kwargs)

def __str__(self):
return f"{self.author}: {self.text}"
return f"{self.author}: {self.text}"
40 changes: 35 additions & 5 deletions backend/courses/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from courses.models import (
Attribute,
Comment,
Course,
Friendship,
Instructor,
Expand All @@ -14,7 +15,6 @@
Section,
StatusUpdate,
UserProfile,
Comment
)
from plan.management.commands.recommendcourses import cosine_similarity

Expand Down Expand Up @@ -477,6 +477,7 @@ class FriendshipRequestSerializer(serializers.Serializer):
def to_representation(self, instance):
return super().to_representation(instance)


class CommentSerializer(serializers.ModelSerializer):
author_name = serializers.CharField(source="author.username", read_only=True)
votes = serializers.SerializerMethodField()
Expand All @@ -485,19 +486,33 @@ class CommentSerializer(serializers.ModelSerializer):
parent = serializers.SerializerMethodField()

def get_votes(self, obj):
return len(obj.upvotes.values_list('id')) - len(obj.downvotes.values_list('id'))
return len(obj.upvotes.values_list("id")) - len(obj.downvotes.values_list("id"))

def get_base(self, obj):
if obj.base is None:
return None
return obj.base.id

def get_parent(self, obj):
if obj.parent is None:
return None
return obj.parent.id

class Meta:
model = Comment
fields = ['id', 'text', 'created_at', 'modified_at', 'author_name', 'votes', 'section', 'base', 'parent', 'path']
fields = [
"id",
"text",
"created_at",
"modified_at",
"author_name",
"votes",
"section",
"base",
"parent",
"path",
]


class CommentListSerializer(serializers.ModelSerializer):
author_name = serializers.CharField(source="author.username", read_only=True)
Expand All @@ -509,16 +524,31 @@ class CommentListSerializer(serializers.ModelSerializer):
user_downvoted = serializers.BooleanField()

def get_votes(self, obj):
return len(obj.upvotes.values_list('id')) - len(obj.downvotes.values_list('id'))
return len(obj.upvotes.values_list("id")) - len(obj.downvotes.values_list("id"))

def get_base(self, obj):
if obj.base is None:
return None
return obj.base.id

def get_parent(self, obj):
if obj.parent is None:
return None
return obj.parent.id

class Meta:
model = Comment
fields = ['id', 'text', 'created_at', 'modified_at', 'author_name', 'votes', 'section', 'base', 'parent', 'path', 'user_upvoted', 'user_downvoted']
fields = [
"id",
"text",
"created_at",
"modified_at",
"author_name",
"votes",
"section",
"base",
"parent",
"path",
"user_upvoted",
"user_downvoted",
]
14 changes: 9 additions & 5 deletions backend/courses/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,30 +723,34 @@ def get_semesters(semesters: str = None) -> list[str]:
raise ValueError(f"Provided semester {s} was not found in the db.")
return sorted(semesters)


def get_section_from_course_instructor_semester(course_code, professors, semester):
"""
Attempts to return a course section that matches the given parameters.
ValueError is raised if the section does not exist.
"""

course_candidate = Course.objects.filter(full_code=course_code).first()
if not course_candidate:
raise ValueError(f"No course exists with code ({course_code})")
course_topic = course_candidate.topic
if not course_topic:
raise ValueError(f"No topic exists for course with code ({course_code})")
course_topic_parent = course_topic.most_recent.full_code
sections = Section.objects.prefetch_related('instructors').filter(
course__topic__most_recent__full_code = course_topic_parent,
course__semester = semester
sections = Section.objects.prefetch_related("instructors").filter(
course__topic__most_recent__full_code=course_topic_parent, course__semester=semester
)
professors_query = Q(instructors__name=professors[0])
for professor in professors[1:]:
professors_query &= Q(instructors__name=professor)
matching_sections = sections.filter(professors_query).distinct()
if matching_sections.count() == 1:
return matching_sections.first()
raise ValueError(f"No section exists with course code ({course_code}), professor ({professors[0]}), semester ({semester})")
raise ValueError(
f"""No section exists with course code ({course_code}), professor ({professors[0]}),
semester ({semester})"""
)


def historical_semester_probability(current_semester: str, semesters: list[str]):
"""
Expand Down
6 changes: 3 additions & 3 deletions backend/review/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from django.db import models
from django.db.models import Avg, Q
from django.contrib.auth import get_user_model
from textwrap import dedent


class Review(models.Model):
"""
Expand Down Expand Up @@ -119,4 +119,4 @@ class Meta:
unique_together = (("review", "field"),)

def __str__(self):
return f"#{self.review.pk} - {self.field}: {self.average}"
return f"#{self.review.pk} - {self.field}: {self.average}"
2 changes: 1 addition & 1 deletion backend/review/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class ReviewSerializer(serializers.ModelSerializer):

class Meta:
model = Review
fields = ("section", "instructor")
fields = ("section", "instructor")
Loading

0 comments on commit 1cdf8fa

Please sign in to comment.