Skip to content

Commit

Permalink
Added new notify function so client can send notication to Learning
Browse files Browse the repository at this point in the history
Platform channel
  • Loading branch information
stevebrownlee committed Feb 18, 2024
1 parent 766a684 commit 273705b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 279 deletions.
282 changes: 4 additions & 278 deletions LearningAPI.session.sql
Original file line number Diff line number Diff line change
@@ -1,283 +1,5 @@

SELECT *
FROM public."LearningAPI_cohort";


SELECT *
FROM public."LearningAPI_opportunity";


SELECT *
FROM public."LearningAPI_learningrecord";


SELECT *
FROM public."socialaccount_socialaccount";


SELECT *
FROM public."auth_user"
ORDER BY id DESC;


SELECT *
FROM public."LearningAPI_studentassessment";


SELECT *
FROM public."authtoken_token";


DELETE
FROM public."LearningAPI_cohortcourse";


SELECT *
FROM public."LearningAPI_course";


SELECT *
FROM public."LearningAPI_studenttag";


DELETE
FROM public."LearningAPI_studenttag";


SELECT *
FROM public."LearningAPI_cohort";


SELECT *
FROM public."LearningAPI_cohortcourse";


insert into public."LearningAPI_cohortcourse" (cohort_id,
course_id,
active)
values (3,
1,
FALSE);


insert into public."LearningAPI_cohortcourse" (cohort_id,
course_id,
active)
values (3,
3,
TRUE);


select *
from pg_catalog.pg_tables;


SELECT *
FROM public."LearningAPI_studentpersonality";


DELETE
FROM public."LearningAPI_cohort"
where id = 12;


DELETE
FROM public."LearningAPI_cohortcourse";


UPDATE public."LearningAPI_nssuser"
SET slack_handle = 'G08NYBJSY' ;


UPDATE public."LearningAPI_studentpersonality"
SET briggs_myers_type = 'ESTJ'
WHERE student_id = 56;


INSERT INTO public."LearningAPI_studentpersonality" (student_id,
briggs_myers_type,
bfi_extraversion,
bfi_agreeableness,
bfi_conscientiousness,
bfi_neuroticism,
bfi_openness)
VALUES (56,
'ENTJ-A',
69,
96,
73,
0,
94);


SELECT *
FROM public."LearningAPI_capstone";


SELECT *
FROM public."LearningAPI_proposalstatus";


INSERT INTO public."LearningAPI_capstonetimeline" (capstone_id,
status_id, date)
VALUES (3,
1,
'2022-11-04');


INSERT INTO public."LearningAPI_capstone" (proposal_url,
repo_url,
course_id,
student_id,
description)
VALUES ('http://www.claire.com',
'http://www.claire.com',
3,
56,
'Client side capstone proposal');


SELECT "LearningAPI_cohort"."id",
"LearningAPI_cohort"."name",
"LearningAPI_cohort"."slack_channel",
"LearningAPI_cohort"."start_date",
"LearningAPI_cohort"."end_date",
"LearningAPI_cohort"."break_start_date",
"LearningAPI_cohort"."break_end_date",
COUNT("LearningAPI_nssusercohort"."id") FILTER (
WHERE NOT "auth_user"."is_staff" ) AS "students",
COUNT("LearningAPI_nssusercohort"."id") FILTER (
WHERE "auth_user"."is_staff" ) AS "instructors"
FROM "LearningAPI_cohort"
LEFT OUTER JOIN "LearningAPI_nssusercohort" ON ("LearningAPI_cohort"."id" = "LearningAPI_nssusercohort"."cohort_id")
LEFT OUTER JOIN "LearningAPI_nssuser" ON ("LearningAPI_nssusercohort"."nss_user_id" = "LearningAPI_nssuser"."id")
LEFT OUTER JOIN "auth_user" ON ("LearningAPI_nssuser"."user_id" = "auth_user"."id")
GROUP BY "LearningAPI_cohort"."id" ;


SELECT "LearningAPI_capstone"."id",
"LearningAPI_capstone"."student_id",
"LearningAPI_capstone"."course_id",
"LearningAPI_capstone"."proposal_url",
"LearningAPI_capstone"."repo_url",
"LearningAPI_capstone"."description",
COUNT("LearningAPI_capstonetimeline"."id") AS "status_count",
COUNT("LearningAPI_capstonetimeline"."id") FILTER (
WHERE "LearningAPI_proposalstatus"."status" = Approved) AS "approved"
FROM "LearningAPI_capstone"
LEFT OUTER JOIN "LearningAPI_capstonetimeline" ON ("LearningAPI_capstone"."id" = "LearningAPI_capstonetimeline"."capstone_id")
LEFT OUTER JOIN "LearningAPI_proposalstatus" ON ("LearningAPI_capstonetimeline"."status_id" = "LearningAPI_proposalstatus"."id")
WHERE "LearningAPI_capstone"."student_id" = 74
GROUP BY "LearningAPI_capstone"."id" ;


select w.id,
w.label,
w.weight,
w.tier,
r.achieved,
r.student_id
from public."LearningAPI_learningweight" w
left outer join public."LearningAPI_learningrecord" r on r.weight_id = w.id
and r.student_id = 19
where r.achieved is NULL
order by w.tier;


SELECT "LearningAPI_book"."id",
"LearningAPI_book"."name",
"LearningAPI_book"."course_id",
"LearningAPI_book"."description",
"LearningAPI_book"."index"
FROM "LearningAPI_book"
ORDER BY "LearningAPI_book"."course_id" ASC,
"LearningAPI_book"."index" ASC ;


SELECT COUNT("LearningAPI_capstonetimeline"."id") AS "status_count",
COUNT("LearningAPI_capstonetimeline"."id") FILTER (
WHERE "LearningAPI_proposalstatus"."status" = 'Approved') AS approved,
COUNT("LearningAPI_capstonetimeline"."id") FILTER (
WHERE "LearningAPI_proposalstatus"."status" = 'MVP') AS mvp,
CASE
WHEN COUNT("LearningAPI_capstonetimeline"."id") = 0 THEN 'submitted'
WHEN (COUNT("LearningAPI_capstonetimeline"."id") > 0
AND COUNT("LearningAPI_capstonetimeline"."id") FILTER (
WHERE ("LearningAPI_proposalstatus"."status" = 'MVP')) = 1) THEN 'mvp'
WHEN (COUNT("LearningAPI_capstonetimeline"."id") > 0
AND COUNT("LearningAPI_capstonetimeline"."id") FILTER (
WHERE ("LearningAPI_proposalstatus"."status" = 'Approved')) = 0) THEN 'reviewed'
WHEN (COUNT("LearningAPI_capstonetimeline"."id") > 0
AND COUNT("LearningAPI_capstonetimeline"."id") FILTER (
WHERE ("LearningAPI_proposalstatus"."status" = 'Approved')) = 1) THEN 'approved'
ELSE 'unsubmitted'
END AS current_status
FROM "LearningAPI_capstone"
LEFT OUTER JOIN "LearningAPI_capstonetimeline" ON ("LearningAPI_capstone"."id" = "LearningAPI_capstonetimeline"."capstone_id")
LEFT OUTER JOIN "LearningAPI_proposalstatus" ON ("LearningAPI_capstonetimeline"."status_id" = "LearningAPI_proposalstatus"."id")
WHERE "LearningAPI_capstone"."student_id" = 291
GROUP BY "LearningAPI_capstone"."id" ;


SELECT *
FROM get_project_average_start_delay(1);


select *
from "LearningAPI_course";


SELECT book.name AS "BookName",
book.index AS "BookIndex",
project.name AS "ProjectName",
project.index AS "ProjectIndex",
AVG(student_project.date_created - cohort.start_date) AS "AverageStartDelay"
FROM "LearningAPI_studentproject" AS student_project
INNER JOIN "LearningAPI_project" AS project ON student_project.project_id = project.id
INNER JOIN "LearningAPI_book" AS book ON project.book_id = book.id
INNER JOIN "LearningAPI_nssusercohort" AS nssusercohort ON student_project.student_id = nssusercohort.nss_user_id
INNER JOIN "LearningAPI_cohort" AS cohort ON nssusercohort.cohort_id = cohort.id
INNER JOIN "LearningAPI_course" AS course ON book.course_id = course.id
WHERE course.id = 3
GROUP BY book.index,
book.name,
project.index,
project.name
ORDER BY book.index,
project.index ;




































Expand Down Expand Up @@ -432,6 +154,10 @@ $$ LANGUAGE plpgsql;










Expand Down
1 change: 1 addition & 0 deletions LearningAPI/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .student_view import StudentViewSet
from .auth import register_user
from .auth import login_user
from .notify import notify
from .course_view import CourseViewSet
from .book_view import BookViewSet
from .project_view import ProjectViewSet
Expand Down
2 changes: 1 addition & 1 deletion LearningAPI/views/capstone_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def create(self, request):
try:
Capstone.objects.get(student=student, course=course)

return Response({'message': 'You have already submittted a proposal for this course. If you made updates, just let your instructional team know via Slack'}, status=status.HTTP_400_BAD_REQUEST)
return Response({'message': 'You have already submitted a proposal for this course. If you made updates, just let your instructional team know via Slack'}, status=status.HTTP_400_BAD_REQUEST)
except Capstone.DoesNotExist:
proposal = Capstone()
proposal.course = course
Expand Down
47 changes: 47 additions & 0 deletions LearningAPI/views/notify.py
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)
1 change: 1 addition & 0 deletions LearningPlatform/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
path('records/entries/<int:entry_id>', views.LearningRecordViewSet.as_view({'delete': 'entries'}), name="entries"),

path('accounts', views.register_user),
path('notify', views.notify, name='notify'),
path('accounts/verify', rest_views.obtain_auth_token, name='api-token-auth'),

path('auth/', include('dj_rest_auth.urls')),
Expand Down

0 comments on commit 273705b

Please sign in to comment.