コード例 #1
0
ファイル: editmodule.py プロジェクト: daryltanzw/SqlLab
    def post(self, request, *args, **kwargs):
            edit_module_form = self.form_class(request.POST)
            class_id = self.kwargs['class_id']

            if edit_module_form.is_valid():
                if edit_module_form.has_changed():
                    class_name = request.POST['class_name']
                    semester = request.POST['semester']
                    facilitators = request.POST['facilitators']
                    try:
                        connection = get_db_connection()
                        with transaction.atomic():
                            module = Class.objects.get(classid=class_id)
                            queryset_module = Class.objects.filter(classid=class_id)
                            fields = ['class_name', 'semester', 'facilitators']
                            updatedValues = [class_name, semester, facilitators]
                            for field, updatedValue in zip(fields, updatedValues):
                                if getattr(module, field) != updatedValue:
                                    queryset_module.update(**{field: updatedValue})
                            connection.commit()

                    except ValueError as err:
                        connection.close()
                        raise err

                    return HttpResponseRedirect("../../module")

                else:
                    return HttpResponseRedirect("../../module")
            else:
                raise ValueError(edit_module_form.errors)
コード例 #2
0
def execute_formatted_query(fq):
    if not is_query_against_visible(fq):
        return "Table is not Queryable"

    result = sqlparse.parse(sqlparse.format(fq, reindent=True, keyword_case='upper'))[0]

    if is_select_query(result):
        try:
            conn = get_db_connection()
            toRet = ""
            with conn.cursor() as cursor:
                cursor.execute(fq)
                r = cursor.fetchmany(5)
                for tuple in r:
                    toRet += str(tuple) + '\n'

                return toRet

        except psycopg2.Error as e:
            error = e.pgerror
            pattern = re.compile('tid[1-9]+_')
            match = re.search(pattern, error)
            if match:
                s = str(match.group(0))
                return error.replace(s, "")
            return error

        except ValueError as e:
            return e.args.replace("tid", "")

        finally:
            conn.close()

    else:
        return "Only Select Queries are allowed"
コード例 #3
0
    def post(self, request, *args, **kwargs):
        create_test_form = self.form_class(request.POST, request.FILES)
        create_test_form.clean()
        cid = self.kwargs['class_id']
        classid = int(decryptData(cid))

        if create_test_form.is_valid():
            start_time = request.POST['start_time']
            end_time = request.POST['end_time']
            max_attempt = request.POST['max_attempt']
            test_name = request.POST['test_name']
            q_a_file = request.FILES['q_a_file_upload']
            data_file = request.FILES['data_file_upload']

            if q_a_file is None or data_file is None:
                raise ValueError("No Upload for Q&A file or Data Files")

            q_a_file_lines = q_a_file.read().splitlines()
            data_file_lines = data_file.read().splitlines()

            try:
                connection = get_db_connection()
                with transaction.atomic():
                    test_for_class_row = TestForClass(classid_id=classid,
                                                      max_attempt=max_attempt,
                                                      test_name=test_name,
                                                      start_time=start_time,
                                                      end_time=end_time)
                    test_for_class_row.save()
                    tid = test_for_class_row.tid

                    validate_q_a_file(q_a_file.name, q_a_file_lines)
                    cursor = connection.cursor()
                    processed_data_file_lines = append_to_relations(
                        tid, data_file_lines)
                    run_sql(cursor, processed_data_file_lines)
                    create_test_name_table(
                        cursor, test_name_table_format(tid, test_name),
                        q_a_file_lines)
                    connection.commit()

                    data_tbl_name_list = get_tbl_names(data_file_lines)
                    for name in data_tbl_name_list:
                        question_data_used_by_test_row = QuestionDataUsedByTest(
                            tid_id=tid, data_tbl_name=name)
                        question_data_used_by_test_row.save()

            except ValueError as err:
                raise err

            finally:
                connection.close()

            return HttpResponseRedirect("../test")

        else:
            raise ValueError(create_test_form.errors)
コード例 #4
0
ファイル: edittest.py プロジェクト: daryltanzw/SqlLab
    def post(self, request, *args, **kwargs):
        test_id = self.kwargs['test_id']
        tid = int(decryptData(test_id))
        class_id = encryptData(TestForClass.objects.get(tid=tid).classid_id)

        start_time = request.POST['start_time']
        end_time = request.POST['end_time']
        max_attempt = request.POST['max_attempt']
        data_tables = QuestionDataUsedByTest.objects.filter(tid_id=tid)

        current_time = datetime.datetime.fromtimestamp(
            time.time()).strftime('%Y-%m-%d %H:%M:%S')

        if end_time > current_time and start_time < end_time:
            try:
                connection = get_db_connection()
                with transaction.atomic():
                    test = TestForClass.objects.get(tid=tid)
                    queryset_test = TestForClass.objects.filter(tid=tid)
                    fields = ['start_time', 'end_time', 'max_attempt']
                    updatedValues = [start_time, end_time, max_attempt]

                    for field, updatedValue in zip(fields, updatedValues):
                        if getattr(test, field) != updatedValue:
                            queryset_test.update(**{field: updatedValue})

                    for table in data_tables:
                        result = request.POST.getlist(table.data_tbl_name)

                        with transaction.atomic():
                            is_visible = False
                            result = ''.join(result)
                            if result == 'on':
                                is_visible = True

                            QuestionDataUsedByTest.objects.filter(
                                tid_id=tid,
                                data_tbl_name=table.data_tbl_name).update(
                                    student_visibility=is_visible)

                    connection.commit()

            except ValueError as err:
                connection.close()
                raise err

            return HttpResponseRedirect("../../" + str(class_id) + "/test")

        else:
            raise ValueError(
                'Date cannot be in the past or start date cannot be before end date.'
            )
コード例 #5
0
    def test_valid_create_test_name_table(self):
        q_a_file_lines = [
            "Select all females, with score higher than 50\tselect * from students where gender = 'F' and grade "
            "> 50;\t5",
            'q2\ta2\t4', 'q3\ta3\t3']

        connection = get_db_connection()
        cursor = connection.cursor()
        tbl_name = "Midterm1"

        try:
            create_test_name_table(cursor, tbl_name, q_a_file_lines)
            connection.close()
        except ValueError as err:
            self.fail(err)
コード例 #6
0
ファイル: deletemodule.py プロジェクト: daryltanzw/SqlLab
    def get(self, request, *args, **kwargs):
        cid = self.kwargs['class_id']
        class_id = int(decryptData(cid))

        try:
            connection = get_db_connection()
            with transaction.atomic():
                module = Class.objects.get(classid=class_id)
                module.delete()
                connection.commit()

        except ValueError as err:
            connection.close()
            raise err

        return HttpResponseRedirect("../../module")
コード例 #7
0
ファイル: deletetest.py プロジェクト: daryltanzw/SqlLab
    def get(self, request, *args, **kwargs):
        test_id = self.kwargs['test_id']
        tid = int(decryptData(test_id))
        class_id = encryptData(TestForClass.objects.get(tid=tid).classid_id)

        try:
            connection = get_db_connection()
            with transaction.atomic():
                test = TestForClass.objects.get(tid=tid)
                test.delete()
                connection.commit()

        except ValueError as err:
            connection.close()
            raise err

        return HttpResponseRedirect("../../" + str(class_id) + "/test")
コード例 #8
0
    def test_invalid_create_test_name_table(self):
        raised = False
        q_a_file_lines = [
            "Select all females, with score higher than 50\tselect * from students where gender = 'F' and grade "
            "> 50;\t5",
            'q2\t4', 'q3\ta3\t3']

        connection = get_db_connection()
        cursor = connection.cursor()
        tbl_name = "Midterm1"

        try:
            create_test_name_table(cursor, tbl_name, q_a_file_lines)
            connection.close()
        except:
            connection.close()
            raised = True

        self.assertTrue(raised)
コード例 #9
0
def grade_formatted_query(student_query, teacher_query, mark):
    default_mark = 0

    try:
        conn = get_db_connection()
        fq1 = student_query.replace(";", "")
        fq2 = teacher_query.replace(";", "")

        with conn.cursor() as cursor:
            query = "( %s Except All %s) UNION ALL (%s EXCEPT All %s)" % (fq1, fq2, fq2, fq1)
            cursor.execute(query)
            r = cursor.fetchall()

            if len(r) != 0:
                mark = default_mark

    except:
        mark = default_mark

    finally:
        conn.close()
        return mark
コード例 #10
0
ファイル: taketest.py プロジェクト: daryltanzw/SqlLab
    def post(self, request, *args, **kwargs):
        submit_answer_form = self.form_class(request.POST)
        test_id = self.kwargs['test_id']
        tid = int(decryptData(test_id))
        class_id = encryptData(TestForClass.objects.get(tid=tid).classid_id)

        if submit_answer_form.is_valid():
            student_answer_list = request.POST.getlist('student_answer')
            student_email = request.user.email
            attempt_no = StudentAttemptsTest.objects.filter(
                tid_id=tid, student_email_id=student_email).count() + 1

            try:
                connection = get_db_connection()
                with transaction.atomic():
                    row, created = StudentAttemptsTest.objects.get_or_create(
                        attempt_no=attempt_no,
                        student_email_id=student_email,
                        tid_id=tid)
                    cursor = connection.cursor()
                    create_student_attempt_table(
                        cursor,
                        student_attempt_table_format(tid, student_email,
                                                     attempt_no), tid,
                        student_answer_list)
                    connection.commit()

            except ValueError as err:
                raise err
            finally:
                connection.close()

            return HttpResponseRedirect("../../" + str(class_id) + "/test")

        else:
            raise ValueError(submit_answer_form.errors)