Skip to content
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

Shivam Fixing Backend Issues #150

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ You will hear back within 48 hours from us via email.

### Install requirements

virtualenv env --python=python3.8
```
virtualenv env --python=python3.8
source env/bin/activate
Expand Down
1 change: 1 addition & 0 deletions core/apis/assignments/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .student import student_assignments_resources
from .teacher import teacher_assignments_resources
from .principal import principal_assignments_resources
41 changes: 41 additions & 0 deletions core/apis/assignments/principal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from flask import Blueprint, jsonify, request
from core import db
from core.apis import decorators
from core.apis.responses import APIResponse
from core.models.assignments import Assignment, AssignmentStateEnum, GradeEnum
from .schema import AssignmentSchema, AssignmentGradeSchema # Import your schema
from core.libs import assertions

principal_assignments_resources = Blueprint('principal_assignments_resources', __name__)

@principal_assignments_resources.route('/assignments', methods=['GET'], strict_slashes=False)
@decorators.authenticate_principal
def list_assignments(p):
"""Returns a list of all submitted and graded assignments."""
assignments = Assignment.get_assignments_by_principal()
assignments_dump = AssignmentSchema().dump(assignments, many=True)
return APIResponse.respond(data=assignments_dump)

@principal_assignments_resources.route('/assignments/grade', methods=['POST'], strict_slashes=False)
@decorators.accept_payload
@decorators.authenticate_principal
def grade_assignment(p, incoming_payload):
"""Grade an assignment"""
grade_assignment_payload = AssignmentGradeSchema().load(incoming_payload)

assignment = Assignment.get_by_id(grade_assignment_payload.id)
assertions.assert_found(assignment, 'No assignment with this id was found')

if assignment.state == AssignmentStateEnum.DRAFT:
return APIResponse.respond(
message='Assignment in draft state cannot be graded',
status_code=400
)

assignment.grade = GradeEnum(grade_assignment_payload.grade)
assignment.state = AssignmentStateEnum.GRADED
db.session.commit()

assignment_dump = AssignmentSchema().dump(assignment)
return APIResponse.respond(data=assignment_dump)

7 changes: 5 additions & 2 deletions core/apis/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@

class APIResponse(Response):
@classmethod
def respond(cls, data):
return make_response(jsonify(data=data))
def respond(cls, data=None, status_code=200, message=None):
response_data = {'data': data}
if message:
response_data['message'] = message
return make_response(jsonify(response_data), status_code)
10 changes: 10 additions & 0 deletions core/models/assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from core.models.teachers import Teacher
from core.models.students import Student
from sqlalchemy.types import Enum as BaseEnum
from sqlalchemy import or_


class GradeEnum(str, enum.Enum):
Expand Down Expand Up @@ -91,3 +92,12 @@ def get_assignments_by_student(cls, student_id):
@classmethod
def get_assignments_by_teacher(cls):
return cls.query.all()

@classmethod
def get_assignments_by_principal(cls):
return cls.filter(
or_(
cls.state == AssignmentStateEnum.SUBMITTED,
cls.state == AssignmentStateEnum.GRADED
)
).all()
7 changes: 6 additions & 1 deletion core/server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from flask import jsonify
from marshmallow.exceptions import ValidationError
from core import app
from core.apis.assignments import student_assignments_resources, teacher_assignments_resources
from core.apis.assignments import (
student_assignments_resources,
teacher_assignments_resources,
principal_assignments_resources
)
from core.libs import helpers
from core.libs.exceptions import FyleError
from werkzeug.exceptions import HTTPException
Expand All @@ -10,6 +14,7 @@

app.register_blueprint(student_assignments_resources, url_prefix='/student')
app.register_blueprint(teacher_assignments_resources, url_prefix='/teacher')
app.register_blueprint(principal_assignments_resources, url_prefix='/principal')


@app.route('/')
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
-- Write query to find the number of grade A's given by the teacher who has graded the most assignments
WITH TeacherGradingCount AS (
SELECT
teacher_id,
COUNT(*) AS total_assignments
FROM
assignments
GROUP BY
teacher_id
),
MaxGradingTeacher AS (
SELECT
teacher_id
FROM
TeacherGradingCount
ORDER BY
total_assignments DESC
LIMIT 1
)
SELECT
COUNT(*) AS grade_A_count
FROM
assignments
WHERE
grade = 'A'
AND teacher_id = (SELECT teacher_id FROM MaxGradingTeacher);