Ejemplo n.º 1
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
Ejemplo n.º 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
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
 def add_olimpiad(self, data):
     if not check_all_parameters(data, ['student_id', 'olimpiad_id']):
         return json.dumps({"error": "Недостатньо данних"}), 400
     try:
         sql = "INSERT INTO compete (olimp_id, student_id) VALUES ('%s', '%s');" % (
             data['olimpiad_id'], data['student_id'])
         self.db.execute(sql)
         return json.dumps({"data": True}), 200
     except Exception as e:
         return get_error(e)
Ejemplo n.º 7
0
 def add_subject(self, data):
     print(data)
     if not check_all_parameters(data, ['student_id', 'sub_id']):
         return json.dumps({"error": "Недостатньо данних"}), 400
     try:
         sql = "INSERT INTO studying (subject_id, student_id) VALUES ('%s', '%s');" % (
             data['sub_id'], data['student_id'])
         self.db.execute(sql)
         return json.dumps({"data": True}), 200
     except Exception as e:
         return get_error(e)
Ejemplo n.º 8
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)
Ejemplo n.º 9
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)
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
 def login(self, data):
     # check all fields
     if not check_all_parameters(data, ['login', 'password']):
         return json.dumps({"error": "Недостатньо данних"}), 400
     # hash password
     data['password'] = get_hash(data['password'])
     try:
         sql = "SELECT * FROM pupils WHERE email='%s' AND password='******';" % (
             data['login'], data['password'])
         res = self.db.execute(sql)
         if len(res) < 1:
             return "no", 400
         return json.dumps({"id": res[0][0]}), 200
     except Exception as e:
         return get_error(e)
Ejemplo n.º 12
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)
Ejemplo n.º 13
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)
Ejemplo n.º 14
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)
Ejemplo n.º 15
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)
Ejemplo n.º 16
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)
Ejemplo n.º 17
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)
Ejemplo n.º 18
0
    def get_answer(self, data):
        try:
            if not check_all_parameters(data, ['pupil_id', 'task_id']):
                return json.dumps({"error": "Недостатньо данних"}), 400

            sql = "SELECT * FROM answers WHERE student_id='%s' AND task_id='%s';" % (
                data['pupil_id'], data['task_id'])
            res = self.db.execute(sql)
            if len(res) < 1:
                result = {"has": False}
            else:
                print(res)
                result = {
                    "has": True,
                    "id": res[0][0],
                    "text": res[0][1],
                    "hyperlink": "" if res[0][4] is None else res[0][4],
                    "response": "" if res[0][5] is None else res[0][5],
                    "mark": "" if res[0][6] is None else res[0][6],
                    "notes": "" if res[0][7] is None else res[0][7]
                }
            return json.dumps(result), 200
        except Exception as e:
            return get_error(e)