-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
609 additions
and
424 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
LearningAPI/migrations/0040_alter_capstonetimeline_date.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.2.8 on 2024-02-17 15:19 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("LearningAPI", "0039_add_project_stats_view"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="capstonetimeline", | ||
name="date", | ||
field=models.DateTimeField(auto_now=True), | ||
), | ||
] |
145 changes: 145 additions & 0 deletions
145
LearningAPI/migrations/0041_students_by_cohort_db_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# Generated by Django 4.2.8 on 2024-01-10 18:42 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("LearningAPI", "0040_alter_capstonetimeline_date"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunSQL( | ||
""" | ||
CREATE FUNCTION get_cohort_student_data(selected_cohort_id INT) | ||
RETURNS TABLE ( | ||
user_id INT, | ||
github_handle TEXT, | ||
extra_data TEXT, | ||
student_name TEXT, | ||
current_cohort TEXT, | ||
current_cohort_id INT, | ||
assessment_status_id INT, | ||
current_project_id INT, | ||
current_project_index INT, | ||
current_project_name TEXT, | ||
current_book_id INT, | ||
current_book_index INT, | ||
current_book_name TEXT, | ||
score INT, | ||
student_notes TEXT, | ||
capstone_proposals TEXT, | ||
project_duration DOUBLE PRECISION | ||
) AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
SELECT | ||
nu.user_id::int, | ||
nu.github_handle::text, | ||
social.extra_data::text, | ||
au."first_name" || ' ' || au."last_name" AS student_name, | ||
c.name::text AS current_cohort, | ||
c.id::int AS current_cohort_id, | ||
COALESCE(sa.status_id::int, 0) AS assessment_status_id, | ||
sp.project_id::int AS current_project_id, | ||
p.index::int AS current_project_index, | ||
p.name::text AS current_project_name, | ||
b.id::int AS current_book_id, | ||
b.index::int AS current_book_index, | ||
b.name::text AS current_book_name, | ||
COALESCE(lr.total_score, 0)::int AS score, | ||
COALESCE( | ||
json_agg( | ||
json_build_object( | ||
'note_id', sn.id, | ||
'note', sn.note, | ||
'created_on', sn.created_on | ||
) | ||
) | ||
)::text AS student_notes, | ||
COALESCE( | ||
( | ||
SELECT json_agg( | ||
json_build_object( | ||
'id', c."id", | ||
'status', ps.status, | ||
'proposal_url', c."proposal_url", | ||
'created_on', tl.date, | ||
'course_name', cr.name | ||
) | ||
) | ||
FROM "LearningAPI_capstone" c | ||
LEFT JOIN ( | ||
SELECT DISTINCT ON ( | ||
ct.capstone_id, c.course_id | ||
) * | ||
FROM "LearningAPI_capstonetimeline" ct | ||
JOIN "LearningAPI_capstone" c ON c.id = ct.capstone_id | ||
ORDER BY | ||
c.course_id, | ||
ct.capstone_id, | ||
date desc | ||
) tl ON tl.capstone_id = c.id | ||
LEFT JOIN "LearningAPI_proposalstatus" ps ON ps."id" = tl.status_id | ||
LEFT JOIN "LearningAPI_course" cr ON c.course_id = cr.id | ||
WHERE c."student_id" = nu."user_id" | ||
), '[]' | ||
)::text AS capstone_proposals, | ||
EXTRACT(YEAR FROM AGE(NOW(), sp.date_created)) * 365 + | ||
EXTRACT(MONTH FROM AGE(NOW(), sp.date_created)) * 30 + | ||
EXTRACT(DAY FROM AGE(NOW(), sp.date_created))::double precision AS project_duration | ||
FROM "LearningAPI_nssuser" nu | ||
JOIN "auth_user" au ON au."id" = nu."user_id" | ||
LEFT JOIN "LearningAPI_nssusercohort" nc ON nc."nss_user_id" = nu."id" | ||
LEFT JOIN "LearningAPI_cohort" c ON c."id" = nc."cohort_id" | ||
JOIN "LearningAPI_studentnote" sn ON sn."student_id" = nu."id" | ||
LEFT JOIN "socialaccount_socialaccount" social ON social.user_id = nu.id | ||
LEFT JOIN "LearningAPI_capstone" sc ON sc.student_id = nu."id" | ||
LEFT JOIN "LearningAPI_studentproject" sp | ||
ON sp."student_id" = nu."id" | ||
AND sp.id = ( | ||
SELECT id | ||
FROM "LearningAPI_studentproject" | ||
WHERE "student_id" = nu."id" | ||
ORDER BY id DESC | ||
LIMIT 1 | ||
) | ||
LEFT JOIN "LearningAPI_project" p ON p."id" = sp."project_id" | ||
LEFT JOIN "LearningAPI_book" b ON b."id" = p."book_id" | ||
LEFT JOIN "LearningAPI_assessment" la | ||
ON b.id = la.book_id | ||
LEFT JOIN "LearningAPI_studentassessment" sa | ||
ON sa."student_id" = nu."id" | ||
AND sa."date_created" = ( | ||
SELECT MAX("date_created") | ||
FROM "LearningAPI_studentassessment" | ||
WHERE "student_id" = nu."id" | ||
) | ||
AND sa.assessment_id = la.id | ||
LEFT JOIN ( | ||
SELECT lr."student_id", SUM(lw."weight") AS total_score | ||
FROM "LearningAPI_learningrecord" lr | ||
JOIN "LearningAPI_learningrecordentry" lre ON lre."record_id" = lr."id" | ||
JOIN "LearningAPI_learningweight" lw ON lw."id" = lr."weight_id" | ||
WHERE lr."achieved" = true | ||
GROUP BY lr."student_id" | ||
) lr ON lr."student_id" = nu."id" | ||
WHERE nc."cohort_id" = selected_cohort_id | ||
AND au.is_active = TRUE | ||
AND au.is_staff = FALSE | ||
GROUP BY nu.user_id, nu.github_handle, social.extra_data, | ||
student_name, current_cohort, current_cohort_id, assessment_status_id, | ||
current_project_id, current_project_index, current_project_name, | ||
project_duration, current_book_id, current_book_index, current_book_name, | ||
score | ||
ORDER BY b.index ASC, | ||
p.index ASC; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
""", | ||
"DROP FUNCTION IF EXISTS get_cohort_student_data(INT);" | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import os | ||
import requests | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
from LearningAPI.models.people import NssUser | ||
|
||
@api_view(['POST']) | ||
def notify(request): | ||
""" | ||
Sends a notification message to a Slack channel. | ||
Args: | ||
request (HttpRequest): The HTTP request object. | ||
Returns: | ||
Response: The HTTP response object with a success message. | ||
Raises: | ||
NssUser.DoesNotExist: If the NssUser object does not exist. | ||
AttributeError: If the user attribute is not present in the request's auth object. | ||
IndexError: If the assigned_cohorts queryset is empty. | ||
AttributeError: If the cohort attribute is not present in the first assigned_cohorts object. | ||
AttributeError: If the slack_channel attribute is not present in the cohort object. | ||
requests.exceptions.Timeout: If the request to the Slack API times out. | ||
""" | ||
|
||
student = NssUser.objects.get(user=request.auth.user) | ||
slack_channel = student.assigned_cohorts.order_by("-id").first().cohort.slack_channel | ||
|
||
message = request.data.get("message") | ||
|
||
headers = { | ||
"Content-Type": "application/x-www-form-urlencoded" | ||
} | ||
|
||
requests.post( | ||
"https://slack.com/api/chat.postMessage", | ||
data={ | ||
"text": message, | ||
"token": os.getenv("SLACK_BOT_TOKEN"), | ||
"channel": slack_channel | ||
}, | ||
headers=headers, | ||
timeout=10 | ||
) | ||
|
||
return Response({ 'message': 'Notification sent to Slack!'}, status=200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.