<commit_before>import json
from flask import request
from sqlalchemy import text
from sqlalchemy.exc import DatabaseError
from webargs import fields, missing
from webargs.flaskparser import parser, use_kwargs
from scuevals_api.models import Course, Quarter, Department, School
from scuevals_api import api, db
from flask_restful import Resource, abort


class Departments(Resource):
    args = {'university_id': fields.Integer()}

    @use_kwargs(args)
    def get(self, university_id):
        if university_id is missing:
            return {'error': 'missing university_id parameter'}

        departments = Department.query.join(Department.school).filter(School.university_id == university_id).all()

        return [
            {
                'id': department.id,
                'abbr': department.abbreviation,
                'school': department.school.abbreviation
            }
            for department in departments
        ]

    @use_kwargs(args)
    def post(self, university_id):
        if request.headers['Content-Type'] != 'application/json':
            return {'error': 'wrong mime type'}

        if 'departments' not in request.json or not isinstance(request.json['departments'], list):
            return {'error': 'invalid json format'}

        params = {'u_id': university_id, 'data': json.dumps(request.json)}

        try:
            result = db.engine.execute(text('select update_departments(:u_id, (:data)::jsonb)'), params)
        except DatabaseError:
            return {'error': 'invalid json format'}

        return {'result': 'success', 'updated_count': int(result.first()[0])}


class Courses(Resource):
    args = {'quarter_id': fields.Integer()}

    @use_kwargs(args)
    def get(self, quarter_id):

        if quarter_id is missing:
            courses = Course.query.all()
        else:
            courses = Course.query.join(Course.quarters).filter(Quarter.id == quarter_id).all()

        return [
            {
                'id': course.id,
                'department': course.department.abbreviation,
                'number': course.number,
                'title': course.title,
                'quarters': [quarter.id for quarter in course.quarters]
            }
            for course in courses
        ]


class Quarters(Resource):
    def get(self):<commit_msg>Fixed bug with departments api<commit_after>import json
import logging
from flask import request
from sqlalchemy import text
from sqlalchemy.exc import DatabaseError
from webargs import fields, missing
from webargs.flaskparser import parser, use_kwargs
from scuevals_api.models import Course, Quarter, Department, School
from scuevals_api import api, db
from flask_restful import Resource, abort


class Departments(Resource):
    args = {'university_id': fields.Integer()}

    @use_kwargs(args)
    def get(self, university_id):
        if university_id is missing:
            return {'error': 'missing university_id parameter'}

        departments = Department.query.join(Department.school).filter(School.university_id == university_id).all()

        return [
            {
                'id': department.id,
                'abbr': department.abbreviation,
                'school': department.school.abbreviation
            }
            for department in departments
        ]

    @use_kwargs(args)
    def post(self, university_id):
        if request.headers['Content-Type'] != 'application/json':
            return {'error': 'wrong mime type'}

        if 'departments' not in request.json or not isinstance(request.json['departments'], list):
            return {'error': 'invalid json format'}

        if university_id is missing:
            return {'error': 'missing university_id parameter'}

        params = {'u_id': university_id, 'data': json.dumps(request.json)}

        try:
            result = db.engine.execute(text('select update_departments(:u_id, (:data)::jsonb)'), params)
        except DatabaseError as e:
            logging.error('failed to update departments: ' + str(e))
            return {'error': 'database error'}

        return {'result': 'success', 'updated_count': int(result.first()[0])}


class Courses(Resource):
    args = {'quarter_id': fields.Integer()}

    @use_kwargs(args)
    def get(self, quarter_id):

        if quarter_id is missing:
            courses = Course.query.all()
        else:
            courses = Course.query.join(Course.quarters).filter(Quarter.id == quarter_id).all()

        return [
            {
                'id': course.id,
                'department': course.department.abbreviation,
                'number': course.number,
                'title': course.title,
                'quarters': [quarter.id for quarter in course.quarters]
            }
            for course in courses
        ]


class Quarters(Resource):
    def get(self):
{'fraction_non_alphanumeric': 0.10955806783144913, 'fraction_numerical': 0.00041109969167523125, 'mean_word_length': 2.48068669527897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5068692', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1269, 'n_words': 348}