Пример #1
0
def ProgGenerator(number, test_templates, offset):
    ret = {}
    prog_query = ProgEntry.query(ancestor=generic_key("programming", "prog_dstore"))
    test_cases = prog_query.fetch()
    if number > len(test_cases):
        number = len(test_cases)
    cnt = 0
    # for test_case in test_cases:
    while cnt < number:
        test_case = choice(test_cases)
        test_cases.remove(test_case)
        test_dict = {}
        test_pType = base64.decodestring(test_case.test_pType).split(",")
        test_pName = base64.decodestring(test_case.test_pName).split(",")
        test_pVal = base64.decodestring(test_case.test_pVal).split(",")
        for i in range(0, len(test_pType)):
            test_pType[i] = base64.decodestring(test_pType[i])
            test_pName[i] = base64.decodestring(test_pName[i])
            test_pVal[i] = base64.decodestring(test_pVal[i])
            if test_pType[i] == "words" or test_pType[i] == "numbers":
                test_dict[test_pName[i]] = choice(test_pVal[i].split("/"))
            elif test_pType[i] == "ranges":
                tmpRange = test_pVal[i].split(":")
                test_dict[test_pName[i]] = str(randint(int(tmpRange[0]), int(tmpRange[1])))
            else:
                continue
        ret[cnt + offset] = base64.decodestring(test_case.test_text) % test_dict
        cnt += 1
        if cnt >= number:
            break
    return ret
Пример #2
0
    def get(self):
        user = users.get_current_user()
        if not user:
            self.redirect(users.create_login_url(self.request.uri))
            return
        if not users.is_current_user_admin():
            template = JINJA_ENVIRONMENT.get_template("html/access.html")
            self.response.write(template.render({"header": gTestHeader}))
            return
        test_cases = {}
        for key in test_templates:
            if key == "generic":
                continue
            tmp_dstore = test_templates[key]["datastore"]
            tmp_query = tmp_dstore["entry"].query(ancestor=generic_key(tmp_dstore["dstore"], tmp_dstore["dname"]))
            tmp_tests = tmp_query.fetch()
            test_cases[key] = {}
            test_cases[key]["fullname"] = test_templates[key]["fullname"]
            for tmp_test in tmp_tests:
                # test_cases[key][tmp_test.key.id()] = tmp_test.test_text
                try:
                    tmpStr = base64.decodestring(tmp_test.test_text)
                    test_cases[key][tmp_test.key.id()] = tmpStr.encode("utf-8")
                except:
                    test_cases[key][tmp_test.key.id()] = tmp_test.test_text

        template_values = {"header": gTestHeader, "test_cases": test_cases, "test_vars": []}
        template = JINJA_ENVIRONMENT.get_template("html/edit.html")
        self.response.write(template.render(template_values))
Пример #3
0
def GetNumber(test_templates, key):
    dStore = test_templates[key]['datastore']
    entry = dStore['entry']
    generic_query = entry.query(
        ancestor=generic_key(dStore['dstore'], dStore['dname']))
    test_cases = generic_query.fetch()
    return len(test_cases)
Пример #4
0
def CinterpHandler(req, test_templates):
    text=req.request.get('text')
    testid=req.request.get('id')
    dStore = test_templates['cinterp']['datastore']
    if len(testid) > 0:
        tmpEntry = CinterpEntry.get_by_id(int(testid), 
                                        generic_key(dStore['dstore'], 
                                                    dStore['dname']))
    else:
        tmpEntry = CinterpEntry(parent=generic_key(dStore['dstore'], dStore['dname']))
    tmpEntry.test_text = text
    try:
        tmpEntry.put()
        req.response.write("Test case successfully added to the database.")
    except:
        req.response.write("ERROR: Failed to add test case to the database.")
Пример #5
0
def DocGenerator(number, test_templates, offset):
    ret = {}
    dStore = test_templates['doc']['datastore']
    doc_query = DocEntry.query(
        ancestor=generic_key(dStore['dstore'], dStore['dname']))
    test_cases = doc_query.fetch()
    if number > len(test_cases):
        number = len(test_cases)
    cnt = 0
    #for test_case in test_cases:
    while cnt < number:
        test_case = choice( test_cases)
        test_cases.remove(test_case)
        tmpStr = base64.decodestring(test_case.test_text)
        test_questions = base64.decodestring(test_case.test_questions).split(",")
        qLen = len(test_questions)
        if qLen > 3:
            qLen = 3
        for cnt2 in range(0, qLen):
            cnt2 = cnt2
            tmpQuestion = choice (test_questions)
            tmpStr = tmpStr + "\n\n\t" + base64.decodestring( tmpQuestion )
            test_questions.remove(tmpQuestion)
        ret[cnt+offset] = tmpStr
        cnt += 1
        if cnt >= number:
            break
    return ret
Пример #6
0
def ProgHandler(req, test_templates):
    text = req.request.get("text")
    types = req.request.get("types")
    names = req.request.get("names")
    values = req.request.get("values")
    testid = req.request.get("id")
    dStore = test_templates["prog"]["datastore"]
    if len(testid) > 0:
        tmpEntry = ProgEntry.get_by_id(int(testid), generic_key(dStore["dstore"], dStore["dname"]))
    else:
        tmpEntry = ProgEntry(parent=generic_key(dStore["dstore"], dStore["dname"]))
    tmpEntry.test_text = text
    tmpEntry.test_pType = types
    tmpEntry.test_pName = names
    tmpEntry.test_pVal = values
    try:
        tmpEntry.put()
        req.response.write("Test case successfully added to the database.")
    except:
        req.response.write("ERROR: Failed to add test case to the database.")
Пример #7
0
def CinterpParams(test_id, test_templates):
    if test_id == -1:
        return {}
    tmp_dstore = test_templates['cinterp']['datastore']
    tmpEntity = tmp_dstore['entry'].get_by_id(int(test_id), 
                                          generic_key(tmp_dstore['dstore'],
                                                      tmp_dstore['dname']))
    params = {}
    params['text'] = base64.decodestring(tmpEntity.test_text)
    params['pid'] = test_id
    return params
Пример #8
0
def QuestionParams(test_id, test_templates):
    dStore = test_templates['question']['datastore']
    ferror_query = QuestionEntry.query(
        ancestor=generic_key(dStore['dstore'], dStore['dname']))
    test_cases = ferror_query.fetch()
    params = {}
    params['questionroups'] = []
    for tmpEntity in test_cases:
        test_label = base64.decodestring(tmpEntity.test_label)
        if test_label not in params['questionroups']:
            params['questionroups'].append(test_label)
        if test_id != -1 and tmpEntity.key.id() == int(test_id):
            params['text'] = base64.decodestring(tmpEntity.test_text)
            params['label'] = test_label
            params['pid'] = test_id
    return params
Пример #9
0
def DocParams(test_id, test_templates):
    if test_id == -1:
        return {}
    tmp_dstore = test_templates['doc']['datastore']
    tmpEntity = tmp_dstore['entry'].get_by_id(int(test_id), 
                                          generic_key(tmp_dstore['dstore'],
                                                      tmp_dstore['dname']))
    params = {}
    params['text'] = base64.decodestring(tmpEntity.test_text)
    params['pid'] = test_id
    test_questions = base64.decodestring( tmpEntity.test_questions ).split(",")
    params['params'] = {}
    for i in range(0, len(test_questions)):
        params["params"][i] = {}
        params["params"][i]['question'] = base64.decodestring( test_questions[i] )
    return params
Пример #10
0
def ProgParams(test_id, test_templates):
    if test_id == -1:
        return {}
    tmp_dstore = test_templates["prog"]["datastore"]
    tmpEntity = tmp_dstore["entry"].get_by_id(int(test_id), generic_key(tmp_dstore["dstore"], tmp_dstore["dname"]))
    params = {}
    params["text"] = base64.decodestring(tmpEntity.test_text)
    params["pid"] = test_id
    test_pType = base64.decodestring(tmpEntity.test_pType).split(",")
    test_pName = base64.decodestring(tmpEntity.test_pName).split(",")
    test_pVal = base64.decodestring(tmpEntity.test_pVal).split(",")
    params["params"] = {}
    for i in range(0, len(test_pName)):
        params["params"][i] = {}
        params["params"][i]["type"] = base64.decodestring(test_pType[i])
        params["params"][i]["name"] = base64.decodestring(test_pName[i])
        params["params"][i]["val"] = base64.decodestring(test_pVal[i])
    return params
Пример #11
0
def QuestionGenerator(number, test_templates, offset):
    ret = {}
    dStore = test_templates['question']['datastore']
    ferror_query = QuestionEntry.query(
        ancestor=generic_key(dStore['dstore'], dStore['dname']))
    test_cases = ferror_query.fetch()
    if number > len(test_cases):
        number = len(test_cases)
    cnt = 0
    #for test_case in test_cases:
    while cnt < number:
        test_case = choice( test_cases)
        test_cases.remove(test_case)
        ret[cnt+offset] = base64.decodestring(test_case.test_text)
        cnt += 1
        if cnt >= number:
            break
    return ret
Пример #12
0
def CinterpGenerator(number, test_templates, offset):
    ret = {}
    dStore = test_templates['cinterp']['datastore']
    Cinterp_query = CinterpEntry.query(
        ancestor=generic_key(dStore['dstore'], dStore['dname']))
    test_cases = Cinterp_query.fetch()
    if number > len(test_cases):
        number = len(test_cases)
    cnt = 0
    #for test_case in test_cases:
    while cnt < number:
        test_case = choice( test_cases)
        test_cases.remove(test_case)
        ret[cnt+offset] = "What does this program do?:\n\n"+base64.decodestring(test_case.test_text)
        cnt += 1
        if cnt >= number:
            break
    return ret
Пример #13
0
def FerrorGenerator(number, test_templates, offset):
    ret = {}
    dStore = test_templates['ferror']['datastore']
    ferror_query = FerrorEntry.query(
        ancestor=generic_key(dStore['dstore'], dStore['dname']))
    test_cases = ferror_query.fetch()
    if number > len(test_cases):
        number = len(test_cases)
    cnt = 0
    #for test_case in test_cases:
    while cnt < number:
        test_case = choice( test_cases)
        test_cases.remove(test_case)
        ret[cnt+offset] = "Check if there are any errors and fix them:\n\n"+base64.decodestring(test_case.test_text)
        cnt += 1
        if cnt >= number:
            break
    return ret
Пример #14
0
 def post(self):
     if not check_user(self):
         return
     ajax_type = self.request.get("ajax_type")
     if ajax_type == "test_type":
         self.handle_testType()
     elif ajax_type == "test_add":
         test_type = self.request.get("test")
         test_templates[test_type]["handler"](self, test_templates)
     elif ajax_type == "test_del":
         test_id = self.request.get("id")
         test_typ = self.request.get("typ")
         tmp_dstore = test_templates[test_typ]["datastore"]
         tmpEntity = tmp_dstore["entry"].get_by_id(
             int(test_id), generic_key(tmp_dstore["dstore"], tmp_dstore["dname"])
         )
         tmpEntity.key.delete()
         self.redirect("/edit.html")
     elif ajax_type == "test_mod":
         test_id = self.request.get("id")
         test_typ = self.request.get("typ")
         template_values = test_templates[test_typ]["params"](test_id, test_templates)
         template = JINJA_ENVIRONMENT.get_template(test_templates[test_typ]["template"])
         self.response.write(template.render(template_values))