예제 #1
0
    def add_olimp(self, data):
        # check all fields
        if not check_all_parameters(data, ['title', 'discipline', 'teacher_id']):
            return json.dumps({"error": "Недостатньо данних"}), 400
        # check fields that can be NULL
        data['notes'] = check_for_null(data, 'notes')
        data['class_num'] = check_for_null(data, 'class_num')
        # generate code
        code = None
        while code is None:
            arr = [str(random.randint(0, 9)) for _ in range(10)]
            code = "".join(arr)
            res = self.db.execute("SELECT * FROM olimpiads WHERE olimp_id='%s';" % code)
            if len(res) > 0:
                code = None

        if not check_parameter(data, 'con_id'):
            sql = "INSERT INTO competition (name_id, ev_date, place, stage, notes) " \
                  "VALUES ('%s', '%s','%s', '%s', %s);" % (
                      data['name_id'], datetime.strptime(data['ev-date'], "%Y-%m-%dT%H:%M"),
                      data['place'], data['stage'], data['con_notes'])
            res0 = self.db.execute(sql)
            data['con_id'] = res0
        # try to add to db
        try:
            sql = "INSERT INTO olimpiads (olimp_id, title, teach_id, con_id, discipline, class_num, notes) " \
                  "VALUES ('%s', '%s','%s','%s','%s', %s, %s);" % (code, data['title'], data['teacher_id'],
                                                                   data['con_id'], data['discipline'],
                                                                   data['class_num'], data['notes'])
            self.db.execute(sql)
            return json.dumps({"code": code}), 200
        except Exception as e:
            return get_error(e)
예제 #2
0
    def register(self, data):
        # check all fields
        if not check_all_parameters(data, [
                'id', 'name', 'surname', 'email', 'school_id', 'password',
                'education'
        ]):
            return json.dumps({"error": "Недостатньо данних"}), 400
        if (not 'phd' in data):
            data['phd'] = False
        # check fields that can be NULL
        data['patronymic'] = check_for_null(data, 'patronymic')
        data['phone'] = check_for_null(data, 'phone')

        # hash password
        data['password'] = get_hash(data['password'])

        # try to add to db
        try:
            sql = "INSERT INTO teachers (teacher_id, name, surname, patronymic, phd, email, phone, school_id, education, password) " \
                  "VALUES ('%s', '%s','%s', %s, '%s', '%s', %s, '%s', '%s','%s');" % (
                      data['id'], data['name'], data['surname'],
                      data['patronymic'], data['phd'],
                      data['email'], data['phone'],
                      data['school_id'], data['education'],
                      data['password'])
            self.db.execute(sql)
        except Exception as e:
            return get_error(e, 1)
        return "ok", 201
예제 #3
0
    def register(self, data):
        # check all fields
        if not check_all_parameters(data, [
                'id', 'name', 'surname', 'school_id', 'password', 'email',
                'class'
        ]):
            return json.dumps({"error": "Недостатньо данних"}), 400

        # check fields that can be NULL

        data['patronymic'] = check_for_null(json, 'patronymic')
        data['phone'] = check_for_null(json, 'phone')
        data['birth_date'] = check_for_null(json, 'birth_date')

        # hash password
        data['password'] = get_hash(json['password'])

        # try to add to db
        try:
            sql = "INSERT INTO pupils (student_id,name, surname, patronymic, class, email, phone, birth_date, school_id, password) " \
                  "VALUES ('%s','%s', '%s', %s, '%s', '%s', %s, %s, '%s', '%s');" % (
                      data['id'], data['name'], data['surname'],
                      data['patronymic'], data['class'],
                      data['email'], data['phone'],
                      data['birth_date'], data['school_id'],
                      data['password'])
            self.db.execute(sql)
        except Exception as e:
            return get_error(e)
        return json.dumps({"data": True}), 201
예제 #4
0
    def add(self, data):
        # check all fields
        if not check_all_parameters(data, ['cityid', 'name', 'street', 'house', 'phone']):
            return json.dumps({"error": "Недостатньо данних"}), 400

        # check fields that can be NULL

        data['notes'] = check_for_null(data, 'notes')
        data['region'] = check_for_null(data, 'region')

        # generate school code
        code = None
        while code is None:
            arr = [str(random.randint(0, 9)) for _ in range(10)]
            code = "".join(arr)
            res = self.db.execute("SELECT code FROM schools WHERE code='%s';" % code)
            if len(res) > 0:
                code = None

        # try to add to db
        try:
            sql = "INSERT INTO schools (code, name, city, region, street, house_number, phone, notes) " \
                  "VALUES ('%s', '%s','%s', %s, '%s', '%s', '%s', %s);" % (code, data['name'],
                                                                           data['cityid'], data['region'],
                                                                           data['street'], data['house'],
                                                                           data['phone'], data['notes'])
            self.db.execute(sql)
            return json.dumps({"code": code}), 200
        except Exception as e:
            return get_error(e)
예제 #5
0
    def change_answer(self, data):
        data['response'] = check_for_null(data, 'response')
        data['mark'] = check_for_null(data, 'mark')

        try:
            sql = "UPDATE answers SET mark=%s, response=%s WHERE answer_id='%s';" % (
                data['mark'], data['response'], data['id'])
            self.db.execute(sql)
            return json.dumps({"data": True}), 200
        except Exception as e:
            return get_error(e)
예제 #6
0
    def edit(self, data):
        if not check_all_parameters(data, ['name', 'street', 'house', 'phone', 'code']):
            return json.dumps({"error": "Недостатньо данних"}), 400
        data['notes'] = check_for_null(data, 'notes')
        data['region'] = check_for_null(data, 'region')

        try:
            sql = "UPDATE schools SET name='%s',  region=%s, street='%s', house_number='%s', phone='%s', " \
                  "notes=%s WHERE code='%s';" % (data['name'], data['region'], data['street'], data['house'],
                                                 data['phone'], data['notes'], data['code'])
            self.db.execute(sql)
            return json.dumps({"data": True}), 200
        except Exception as e:
            return get_error(e)
예제 #7
0
 def get_all_pupils_learn(self, data):
     surname = check_for_null(data, 'surname')
     if surname == 'NULL':
         return json.dumps({"error": "Недостатньо данних"}), 400
     try:
         sql = "SELECT student_id, surname, name, email, class, school_id FROM pupils WHERE NOT EXISTS (SELECT * FROM studying AS A WHERE subject_id IN (SELECT " \
               "sub_id FROM subjects WHERE teacher_id IN (SELECT teacher_id FROM teachers WHERE surname=%s)) AND " \
               "NOT EXISTS (SELECT * FROM studying WHERE studying.student_id=pupils.student_id AND " \
               "A.subject_id=studying.subject_id));" % surname
         res = self.db.execute(sql)
         sql2 = "SELECT teacher_id FROM teachers WHERE surname=%s;" % surname
         res2 = self.db.execute(sql2)
         if len(res2) < 1:
             return json.dumps([]), 200
         result = []
         for pupil in res:
             result.append({
                 "id": pupil[0],
                 "name": pupil[1] + " " + pupil[2],
                 "email": pupil[3],
                 "class": pupil[4],
                 "school_id": pupil[5]
             })
         print(result)
         return json.dumps(result), 200
     except Exception as e:
         return get_error(e)
예제 #8
0
    def add(self, data):
        # check all fields
        if not check_all_parameters(data,
                                    ['title', 'class_num', 'teacher_id']):
            return json.dumps({"error": "Недостатньо данних"}), 400

        # check fields that can be NULL
        data['notes'] = check_for_null(data, 'notes')

        # generate school code
        code = None
        while code is None:
            arr = [str(random.randint(0, 9)) for _ in range(10)]
            code = "".join(arr)
            res = self.db.execute("SELECT * FROM subjects WHERE sub_id='%s';" %
                                  code)
            if len(res) > 0:
                code = None

        # try to add to db
        try:
            sql = "INSERT INTO subjects (sub_id, title, class_num, notes, teacher_id) " \
                  "VALUES ('%s', '%s','%s', %s, '%s');" % (code, data['title'], data['class_num'], data['notes'],
                                                           data['teacher_id'])
            self.db.execute(sql)
            return json.dumps({"code": code}), 200
        except Exception as e:
            return get_error(e)
예제 #9
0
    def edit_olimp(self, data):
        if not check_all_parameters(data, ['title', 'discipline']):
            return json.dumps({"error": "Недостатньо данних"}), 400
        # check fields that can be NULL
        data['notes'] = check_for_null(data, 'notes')
        data['class_num'] = check_for_null(data, 'class_num')

        # try to add to db
        try:
            sql = "UPDATE olimpiads SET title='%s', discipline='%s', class_num=%s, notes=%s" \
                  " WHERE olimp_id='%s';" % (
                      data['title'], data['discipline'], data['class_num'], data['notes'], data['id'])
            self.db.execute(sql)
            return json.dumps({"data": True}), 200
        except Exception as e:
            return get_error(e)
예제 #10
0
 def edit(self, data):
     if not check_all_parameters(data, ['title', 'id', 'class_num']):
         return json.dumps({"error": "Недостатньо данних"}), 400
     data['notes'] = check_for_null(data, 'notes')
     try:
         sql = "UPDATE subjects SET title='%s', class_num='%s', notes=%s WHERE sub_id='%s';" % (
             data['title'], data['class_num'], data['notes'], data['id'])
         self.db.execute(sql)
         return json.dumps({"data": True}), 200
     except Exception as e:
         return get_error(e)
예제 #11
0
 def add_additional_source(self, data):
     if not check_all_parameters(data, ['source_id', 'caption', 'content']):
         return json.dumps({"error": "Недостатньо данних"}), 400
     data['notes'] = check_for_null(data, 'notes')
     try:
         print(data)
         sql = "INSERT INTO additional_sources (caption, content, olimp_id, notes) VALUES ('%s','%s','%s',%s);" % \
               (data['caption'], data['content'], data['source_id'], data['notes'])
         id = self.db.execute(sql)
         for i in data['hyperlinks']:
             sql0 = "INSERT INTO additional_hyperlinks (hyperlink, source_id) VALUES ('%s', '%s');" % (i, id)
             self.db.execute(sql0)
         return json.dumps({"data": True}), 200
     except Exception as e:
         return get_error(e)
예제 #12
0
 def add_task(self, data):
     if not check_all_parameters(data, ['source_id', 'deadline', 'content', 'task_caption']):
         return json.dumps({"error": "Недостатньо данних"}), 400
     data['notes'] = check_for_null(data, 'notes')
     try:
         sql = "INSERT INTO competition_tasks (task_caption, content, notes, deadline, olimp_id) VALUES " \
               "('%s','%s',%s,'%s','%s');" % (data['task_caption'], data['content'], data['notes'],
                                              data['deadline'], data['source_id'])
         res = self.db.execute(sql)
         for i in data['hyperlinks']:
             sql0 = "INSERT INTO tasks_hyperlinks (link, task_id) VALUES ('%s', '%s');" % (i, res)
             self.db.execute(sql0)
         return json.dumps({"id": res}), 200
     except Exception as e:
         return get_error(e)
예제 #13
0
    def submit_answer(self, data):
        try:
            if not check_all_parameters(data, ['pupil_id', 'text', 'id']):
                return json.dumps({"error": "Недостатньо данних"}), 400

            data['hyperlink'] = check_for_null(data, 'hyperlink')

            sql = "INSERT INTO answers (text, student_id, task_id, hyperlink) VALUES ('%s', '%s', '%s', %s) " \
                  "ON DUPLICATE KEY UPDATE text='%s', hyperlink=%s;" % (data['text'], data['pupil_id'], data['id'],
                                                                        data['hyperlink'], data['text'],
                                                                        data['hyperlink'])
            self.db.execute(sql)
            return json.dumps({"data": True}), 200
        except Exception as e:
            return get_error(e)
예제 #14
0
 def edit_task(self, data):
     if not check_all_parameters(data, ['id', 'deadline', 'content', 'task_caption']):
         return json.dumps({"error": "Недостатньо данних"}), 400
     data['notes'] = check_for_null(data, 'notes')
     try:
         task_id = data['id']
         sql = "UPDATE competition_tasks SET task_caption='%s', content='%s', notes=%s, deadline='%s' WHERE " \
               "task_id='%s';" % (data['task_caption'], data['content'], data['notes'], data['deadline'], task_id)
         self.db.execute(sql)
         sql = "DELETE FROM tasks_hyperlinks WHERE task_id='%s';" % task_id
         self.db.execute(sql)
         for i in data['hyperlinks']:
             sql0 = "INSERT INTO tasks_hyperlinks (link, task_id) VALUES ('%s', '%s');" % (i, task_id)
             self.db.execute(sql0)
         return json.dumps({"id": task_id}), 200
     except Exception as e:
         return get_error(e)
예제 #15
0
 def add_hometask(self, data):
     if not check_all_parameters(data,
                                 ['title', 'id', 'content', 'deadline']):
         return json.dumps({"error": "Недостатньо данних"}), 400
     data['notes'] = check_for_null(data, 'notes')
     try:
         sql = "INSERT INTO hometasks (hw_title, content, deadline, notes, subject_id) VALUES ('%s','%s', '%s'," \
               " %s,'%s');" \
               % (data['title'], data['content'], datetime.strptime(data['deadline'], "%Y-%m-%dT%H:%M"),
                  data['notes'], data['id'])
         res = self.db.execute(sql)
         for link in data['hyperlinks']:
             sql = "INSERT INTO hometask_hyperlinks (hyperlink, homework_id) VALUES ('%s', '%s')" % (
                 link, res)
             self.db.execute(sql)
         return json.dumps({"hw_id": res}), 200
     except Exception as e:
         return get_error(e)
예제 #16
0
 def edit_hometask(self, data):
     if not check_all_parameters(data,
                                 ['hw_title', 'id', 'content', 'deadline']):
         return json.dumps({"error": "Недостатньо данних"}), 400
     data['notes'] = check_for_null(data, 'notes')
     try:
         sql = "UPDATE hometasks SET hw_title='%s', content='%s', deadline='%s', notes=%s WHERE hw_id='%s';" \
               % (data['hw_title'], data['content'], datetime.strptime(data['deadline'], "%Y-%m-%dT%H:%M"),
                  data['notes'], data['id'])
         self.db.execute(sql)
         sql = "DELETE FROM hometask_hyperlinks WHERE homework_id='%s';" % data[
             'id']
         self.db.execute(sql)
         for link in data['hyperlinks']:
             sql = "INSERT INTO hometask_hyperlinks (hyperlink, homework_id) VALUES ('%s', '%s'); " % (
                 link, data['id'])
             self.db.execute(sql)
         return json.dumps({"hw_id": data['id']}), 200
     except Exception as e:
         return get_error(e)