import graphene
from graphene import relay

from api.utils import get_object
from assignments.models import StudentSubmission
from assignments.schema.types import AssignmentNode, StudentSubmissionNode


class AssignmentsQuery(object):
    assignment = relay.Node.Field(AssignmentNode)
    assignments = graphene.List(AssignmentNode)



class StudentSubmissionQuery:
    student_submission = graphene.Field(StudentSubmissionNode, id=graphene.ID(required=True))

    def resolve_student_submission(root, info, *args, **kwargs):
        user = info.context.user
        id = kwargs.get('id')
        submission = get_object(StudentSubmission, id)
        if user == submission.student or (user.is_teacher() and submission.student in user.users_in_active_school_class()):
            return submission
        return None
