Пример #1
0
def new_test():
    form = TestForm()
    if form.validate_on_submit():
        test = Test(
            test_name=form.test_name.data,
            num_mc=form.num_mc.data,
            mc_answers = int(form.mc_answers.data),
            num_or=form.num_or.data,
            #or_points = int(form.or_points.data),
            num_students=form.num_students.data,
            #test_data = defaultGrid(form.num_mc.data, int(form.mc_answers.data), form.num_or.data, form.or_points.data,form.num_students.data),
            mc_data = mcDetails(form.num_mc.data, int(form.mc_answers.data)),
            or_data = orDetails(form.num_mc.data,form.num_or.data),
            student_data = studentDetails(form.num_students.data),
            added_by=session['email']
        )
        try:
            test.put()
            test_id = test.key.id()
            test = Test.get_by_id(test_id)
            mc_data = json.dumps(test.mc_data)
            or_data = json.dumps(test.or_data)
            student_data = json.dumps(test.student_data)
            flash(u'Test %s successfully saved.' % test_id, 'success')
            return render_template('test_details.html', test = Test.get_by_id(test_id), test_id = test_id, mc_data = mc_data, or_data = or_data, student_data = student_data)
        except CapabilityDisabledError:
            flash(u'App Engine Datastore is currently in read-only mode.', 'info')
            return redirect(url_for('list_tests'))
    return redirect(url_for('list_tests'))
Пример #2
0
def new_test():
    form = TestForm()
    if form.validate_on_submit():
        test = Test(
            test_name=form.test_name.data,
            num_mc=form.num_mc.data,
            mc_answers=int(form.mc_answers.data),
            num_or=form.num_or.data,
            #or_points = int(form.or_points.data),
            num_students=form.num_students.data,
            #test_data = defaultGrid(form.num_mc.data, int(form.mc_answers.data), form.num_or.data, form.or_points.data,form.num_students.data),
            mc_data=mcDetails(form.num_mc.data, int(form.mc_answers.data)),
            or_data=orDetails(form.num_mc.data, form.num_or.data),
            student_data=studentDetails(form.num_students.data),
            added_by=session['email'])
        try:
            test.put()
            test_id = test.key.id()
            test = Test.get_by_id(test_id)
            mc_data = json.dumps(test.mc_data)
            or_data = json.dumps(test.or_data)
            student_data = json.dumps(test.student_data)
            flash(u'Test %s successfully saved.' % test_id, 'success')
            return render_template('test_details.html',
                                   test=Test.get_by_id(test_id),
                                   test_id=test_id,
                                   mc_data=mc_data,
                                   or_data=or_data,
                                   student_data=student_data)
        except CapabilityDisabledError:
            flash(u'App Engine Datastore is currently in read-only mode.',
                  'info')
            return redirect(url_for('list_tests'))
    return redirect(url_for('list_tests'))
Пример #3
0
 def execute(id):
     test = Test.get_by_key_name(testName)
     returnValue = None
     if not test:
         test = Test(id=id, name=testName, key_name=testName)
         returnValue = test
     if branch.key() not in test.branches:
         test.branches.append(branch.key())
     if platform.key() not in test.platforms:
         test.platforms.append(platform.key())
     test.put()
     return returnValue
Пример #4
0
    def post(self):
        user = users.get_current_user()
        entity = Entity.query( Entity.id == user.user_id() ).get()
        test_query = Test.query( ancestor = ndb.Key('Entity', entity.id ) )
        test_query = test_query.filter( Test.id == self.request.get( 'id' ) ).fetch()

        if len(test_query) > 0:
            test = test_query[0]
            test.modified = datetime.datetime.now()
        else:
            test = Test( parent = ndb.Key('Entity', entity.id ) )
            test.created = datetime.datetime.now()
            test.times_taken = 0
            test.total_score = 0
            test.num_marked = 0
            test.average_rating = 0
            test.open = True
            test.author_id = user.user_id()

        test.title = self.request.get( 'title' )
        test.description = self.request.get( 'description' )
        test.group = self.request.get( 'group' )
        test.level = int(self.request.get( 'level' ))

        # Define rules for what is and isn't a valid group
        try:
            assert re.match('^[a-z0-9_]{2,16}$', self.request.get( 'group' )) is not None
        except:
            # If the group is invalid, try again
            template_values = get_template_values( self )
            template_values['error'] = """There was an error with the group entered. Please ensure it uses only
                                       lowercase letters, numbers, and underscores."""
            template_values['user_groups'] = set(
                itertools.chain( entity.test_groups, default_groups )
            )
            template_values['user_levels'] = json.dumps( get_grouped_marks( ndb.Key( "Entity", entity.id ) ))
            template_values = add_test_to_template(template_values, test)
            path = os.path.join( os.path.dirname(__file__), os.path.join( template_dir, 'create.html' ) )
            self.response.out.write( template.render( path, template_values ))
            return

        # Define rules for what is and isn't a valid level for the user to be posting in.
        user_level = get_user_group_level( get_grouped_marks( entity.id ), test.group )
        max_test_query = Test.query( Test.group == test.group ).order(-Test.level).fetch()
        print max_test_query

        try:
            max_test_level = max_test_query[0].level / 2
        except IndexError:
            max_test_level = 0

        if user_level < max_test_level or user_level < test.level:
            # User level is not high enough.
            template_values = get_template_values( self )
            if user_level < max_test_level:
                template_values['error'] = "You must be at least level %d in %s to create a test." \
                                           "You are only level %d." \
                                           % ( math.floor(max_test_level), test.group, user_level)
            elif user_level < test.level:
                template_values['error'] = """You must be at least level %d in %s to create a level %d test .""" \
                                       % ( test.level, test.group, test.level)
            template_values['user_groups'] = set( itertools.chain( entity.test_groups, default_groups ) )
            template_values['user_levels'] = json.dumps( get_grouped_marks( entity_id=entity.id ) )
            template_values = add_test_to_template(template_values, test)
            path = os.path.join( os.path.dirname(__file__), os.path.join( template_dir, 'create.html' ) )
            self.response.out.write( template.render( path, template_values ))
            return


        # Create an id and save the test if the group is valid
        #test.id = str( test.put().id() )
        test.id = ''.join(random.choice(string.digits+string.ascii_lowercase) for x in range(20))
        test.put()

        # Keep track of which test groups a user has used
        if test.group not in entity.test_groups:
            entity.test_groups.append(test.group)
            entity.put()

        # Add/Alter this test's Document in the search index
        test_as_document = search.Document(
                doc_id = test.id,
                fields = [
                    search.AtomField( name="group", value=test.group ),
                    search.TextField( name="title", value=test.title ),
                    search.NumberField( name="times_taken", value=test.times_taken ),
                    search.DateField( name="date", value=test.created ),
                    search.NumberField( name="level", value=test.level ),
                    search.NumberField( name="rating", value=test.average_rating ),
                ]
            )
        try:
            index = search.Index(name="tests")
            index.put( test_as_document )
        except search.Error:
            logging.info("Index put failed")

        self.redirect('/t/%s' % test.id )
        return
Пример #5
0
    def post(self):
        user = users.get_current_user()
        entity = Entity.query(Entity.id == user.user_id()).get()
        test_query = Test.query(ancestor=ndb.Key('Entity', entity.id))
        test_query = test_query.filter(
            Test.id == self.request.get('id')).fetch()

        if len(test_query) > 0:
            test = test_query[0]
            test.modified = datetime.datetime.now()
        else:
            test = Test(parent=ndb.Key('Entity', entity.id))
            test.created = datetime.datetime.now()
            test.times_taken = 0
            test.total_score = 0
            test.num_marked = 0
            test.average_rating = 0
            test.open = True
            test.author_id = user.user_id()

        test.title = self.request.get('title')
        test.description = self.request.get('description')
        test.group = self.request.get('group')
        test.level = int(self.request.get('level'))

        # Define rules for what is and isn't a valid group
        try:
            assert re.match('^[a-z0-9_]{2,16}$',
                            self.request.get('group')) is not None
        except:
            # If the group is invalid, try again
            template_values = get_template_values(self)
            template_values[
                'error'] = """There was an error with the group entered. Please ensure it uses only
                                       lowercase letters, numbers, and underscores."""
            template_values['user_groups'] = set(
                itertools.chain(entity.test_groups, default_groups))
            template_values['user_levels'] = json.dumps(
                get_grouped_marks(ndb.Key("Entity", entity.id)))
            template_values = add_test_to_template(template_values, test)
            path = os.path.join(os.path.dirname(__file__),
                                os.path.join(template_dir, 'create.html'))
            self.response.out.write(template.render(path, template_values))
            return

        # Define rules for what is and isn't a valid level for the user to be posting in.
        user_level = get_user_group_level(get_grouped_marks(entity.id),
                                          test.group)
        max_test_query = Test.query(
            Test.group == test.group).order(-Test.level).fetch()
        print max_test_query

        try:
            max_test_level = max_test_query[0].level / 2
        except IndexError:
            max_test_level = 0

        if user_level < max_test_level or user_level < test.level:
            # User level is not high enough.
            template_values = get_template_values(self)
            if user_level < max_test_level:
                template_values['error'] = "You must be at least level %d in %s to create a test." \
                                           "You are only level %d." \
                                           % ( math.floor(max_test_level), test.group, user_level)
            elif user_level < test.level:
                template_values['error'] = """You must be at least level %d in %s to create a level %d test .""" \
                                       % ( test.level, test.group, test.level)
            template_values['user_groups'] = set(
                itertools.chain(entity.test_groups, default_groups))
            template_values['user_levels'] = json.dumps(
                get_grouped_marks(entity_id=entity.id))
            template_values = add_test_to_template(template_values, test)
            path = os.path.join(os.path.dirname(__file__),
                                os.path.join(template_dir, 'create.html'))
            self.response.out.write(template.render(path, template_values))
            return

        # Create an id and save the test if the group is valid
        test.id = str(test.put().id())
        test.put()

        # Keep track of which test groups a user has used
        if test.group not in entity.test_groups:
            entity.test_groups.append(test.group)
            entity.put()

        # Add/Alter this test's Document in the search index
        test_as_document = search.Document(
            doc_id=test.id,
            fields=[
                search.AtomField(name="group", value=test.group),
                search.TextField(name="title", value=test.title),
                search.NumberField(name="times_taken", value=test.times_taken),
                search.DateField(name="date", value=test.created),
                search.NumberField(name="level", value=test.level),
                search.NumberField(name="rating", value=test.average_rating),
            ])
        try:
            index = search.Index(name="tests")
            index.put(test_as_document)
        except search.Error:
            logging.info("Index put failed")

        self.redirect('/t/%s' % test.id)
        return