Beispiel #1
0
    def mutate(cls, root, info,
               name, division_id, school_id=None):
        user = get_current_user()
        if not user:
            raise GraphQLError("Invalid user!")
        if not school_id:
            school_id = user.school_id
        school_id = localize_id(school_id)
        division_id = localize_id(division_id)

        school = School.get_query(info).get(school_id)
        division = Division.get_query(info).get(division_id)
        if not school:
            raise GraphQLError("Invalid school ID!")
        if not division:
            raise GraphQLError("Invalid division ID")
        if not user.isSchoolAdmin(school):
            raise GraphQLError("Not an admin for the given school!")
        if not division:
            raise GraphQLError("Invalid division!")
        if division not in school.divisions:
            raise GraphQLError(
                "Your school does not have access to this division!"
                " Reach out to an admin to fix this.")
        # create the team
        new_team = TeamModel(name, school_id, division_id)
        db.session.add(new_team)
        db.session.commit()
        return CreateTeamMutation(team=new_team)
Beispiel #2
0
 def mutate(cls, root, info, id, **kwargs):
     query = Division.get_query(info)
     id = localize_id(id)
     divisionToModify = query.get(id)
     fields = clean_input(kwargs)
     update_model_with_dict(divisionToModify, fields)
     db.session.add(divisionToModify)
     db.session.commit()
     return UpdateDivisionMutation(division=divisionToModify)
Beispiel #3
0
 def mutate(cls, root, info, school_id, division_id):
     school = School.get_query(info).get(school_id)
     division = Division.get_query(info).get(division_id)
     if not (school and division):
         raise GraphQLError("One or more object IDs incorrect!")
     school.divisions.append(division)
     db.session.add(school)
     db.session.commit()
     return AddDivisionToSchoolMutation(school)
Beispiel #4
0
    def mutate(cls, root, info, studentInfo):
        user = get_current_user()
        print(studentInfo)
        studentInfo = clean_input(studentInfo)
        # at this point, IDs have been localized.
        if not studentInfo.get('school_id'):
            studentInfo['school_id'] = user.school_id
        if (user.school_id != studentInfo.get('school_id')
                and not user.isAdmin()) or not user.isApproved():
            raise GraphQLError("Not enough permissions!")

        school = School.get_query(info).get(
            localize_id(studentInfo.get('school_id'))
        )
        division = Division.get_query(info).get(
            localize_id(studentInfo.get('current_division_id'))
        )
        if not school:
            raise GraphQLError("Invalid school!")
        if not division:
            raise GraphQLError("Invalid division!")
        if not user.isSchoolAdmin(school):
            raise GraphQLError("Not an admin for this school!")
        if division not in school.divisions:
            raise GraphQLError("Invalid division for given school!")
        is_alternate = False
        if studentInfo.get('is_alternate'):
            if studentInfo.get('current_team_id'):
                raise GraphQLError(
                    "Cannot be on a team and an alternate at the same time!")
            alternate_limit = division.alternate_limit
            n = StudentDivisionAssociationModel.query.filter_by(
                division_id=division.id,
                is_alternate=True
            ).filter(
                StudentDivisionAssociationModel.student.has(
                    school_id=school.id
                )
            ).count()
            if (n >= alternate_limit):
                raise GraphQLError("Too many alternates already!")
            is_alternate = True
        studentInfo.pop('is_alternate')
        student = StudentModel(**studentInfo)
        db.session.add(student)
        db.session.commit()
        division_assoc = StudentDivisionAssociationModel(
            student_id=student.id,
            division_id=student.current_division_id,
            team_id=student.current_team_id,
            is_alternate=is_alternate,
        )
        db.session.add(division_assoc)
        db.session.commit()
        return CreateStudentMutation(student=student,
                                     id=student.id)
Beispiel #5
0
 def resolve_division_by_url(root, info, url, season_url=None):
     query = Division.get_query(info)
     divisions = query.filter_by(url=url)
     if season_url:
         season = Season.get_query(info).filter_by(
             season_url=season_url).first()
         if not season:
             raise GraphQLError("Invalid season!")
         divisions = divisions.filter_by(season_id=season.id)
     return divisions.first()
Beispiel #6
0
 def mutate(cls, root, info, id,
            name=None, school_id=None, division_id=None):
     user = get_current_user()
     if (school_id):
         school_id = localize_id(school_id)
     id = localize_id(id)
     if (division_id):
         division_id = localize_id(division_id)
     school = School.get_query(info).get(school_id)
     division = Division.get_query(info).get(division_id)
     # get the team
     team = Team.get_query(info).get(id)
     if not user:
         raise GraphQLError("Invalid user!")
     if not team:
         raise GraphQLError("Invalid team!")
     if school_id and not school:
         raise GraphQLError("Invalid school ID!")
     if division_id and not division:
         raise GraphQLError("Invalid division ID")
     if school_id and not user.isSchoolAdmin(school):
         raise GraphQLError("Not an admin for the given school!")
     if division_id and division not in school.divisions:
         raise GraphQLError(
             "Your school does not have access to this division!"
             " Reach out to an admin to fix this.")
     if team.school_id != school.id and not user.isAdmin():
         raise GraphQLError("You cannot modify another school's team!")
     if name:
         team.name = name
     if school_id:
         if user.isAdmin() or team.school_id == school_id:
             team.school_id = school_id
         else:
             raise GraphQLError("Cannot change a team\'s"
                                " school unless you are a coach")
     if division_id:
         team.division_id = division_id
     db.session.add(team)
     db.session.commit()
     return UpdateTeamMutation(team=team)
Beispiel #7
0
    def mutate(cls, root, info, studentInfo, id):
        user = get_current_user()
        query = Student.get_query(info)
        studentInfo = clean_input(studentInfo)
        id = localize_id(id)
        studentToModify = query.get(id)
        if not studentToModify:
            raise GraphQLError("Invalid student!")
        if 'school_id' not in studentInfo.keys():
            studentInfo['school_id'] = studentToModify.school_id
        # at this point, IDs have been localized.
        if (user.school_id != studentInfo.get('school_id')
                and not user.isAdmin()) or not user.isApproved():
            raise GraphQLError("Not enough permissions!")

        school = School.get_query(info).get(
            studentInfo.get('school_id')
        )
        division = Division.get_query(info).get(
            studentInfo.get('current_division_id')
        )
        if not school:
            raise GraphQLError("School not provided!")
        if not division:
            division = studentToModify.current_division
        if division not in school.divisions:
            raise GraphQLError("Invalid division for given school!")
        if not user.isSchoolAdmin(school):
            raise GraphQLError("Not an admin for this school!")
        is_alternate = False
        if studentInfo.get('is_alternate'):
            if studentInfo.get('current_team_id') or (
                (not studentInfo.get('current_team_id')) and
                    studentToModify.current_team):
                raise GraphQLError(
                    "An alternate cannot actively be on a team!")
            alternate_limit = division.alternate_limit
            n = StudentDivisionAssociationModel.query.filter_by(
                division_id=division.id,
                is_alternate=True
            ).filter(
                StudentDivisionAssociationModel.student.has(
                    school_id=school.id
                ),
                StudentDivisionAssociationModel.student_id != id
            ).count()
            if (n >= alternate_limit):
                raise GraphQLError("Too many alternates already!")
            is_alternate = True
        student = studentToModify
        if len(studentInfo) != 0:
            update_model_with_dict(studentToModify, studentInfo)
            db.session.commit()
        # confirmation
        studentToModify = query.get(id)
        student = studentToModify
        # you can only have one associated with current_division_id
        old_assoc = StudentDivisionAssociationModel.query.get({
                'student_id': student.id,
                'division_id': student.current_division_id})
        if not old_assoc:
            division_assoc = StudentDivisionAssociationModel(
                student_id=student.id,
                division_id=student.current_division_id,
                team_id=student.current_team_id,
                is_alternate=is_alternate,
            )
            db.session.add(division_assoc)
            db.session.commit()
        else:
            old_assoc.is_alternate = is_alternate
            db.session.add(old_assoc)
            db.session.commit()
        return UpdateStudentMutation(student=studentToModify,
                                     id=id)
Beispiel #8
0
 def resolve_division(root, info, id):
     query = Division.get_query(info)
     return query.get(localize_id(id))