Exemplo n.º 1
0
    def add_exercise(self, req, identifier, name, description, partial, solution, include, num_rows):
        if not VALID_URL_NAME.match(identifier):
            raise BadRequest(
                "Exercise names must consist of a lowercase alphanumeric "
                "character followed by any number of lowercase alphanumerics, "
                "., +, - or _.")

        if req.store.find(Exercise, id=unicode(identifier)).one():
            raise BadRequest("An exercise with that URL name already exists.")

        new_exercise = Exercise()
        new_exercise.id = unicode(identifier)
        new_exercise.name = unicode(name)
        new_exercise.set_description(unicode(description))
        new_exercise.partial = unicode(partial)
        new_exercise.solution = unicode(solution)
        new_exercise.include = unicode(include)
        new_exercise.num_rows = int(num_rows)
        
        req.store.add(new_exercise)
        
        return {'result': 'ok'}
Exemplo n.º 2
0
def handle_create_group(req, fields):
    """Required cap: CAP_MANAGEGROUPS
    Creates a project group in a specific project set
    Required:
        projectsetid, groupnm
    Optional:
        nick
    Returns:
        groupid
    """
    # Get required fields
    projectsetid = fields.getfirst('projectsetid').value
    groupnm = fields.getfirst('groupnm').value

    if projectsetid is None or groupnm is None:
        raise BadRequest("Required: projectsetid, groupnm")
    groupnm = unicode(groupnm)
    try:
        projectsetid = int(projectsetid)
    except:
        raise BadRequest("projectsetid must be an integer")

    if not VALID_URL_NAME.match(groupnm):
        raise BadRequest(
            "Group names must consist of a lowercase alphanumeric character "
            "followed by any number of lowercase alphanumerics, ., +, - or _.")

    projectset = req.store.get(ivle.database.ProjectSet, projectsetid)
    if projectset is None:
        raise BadRequest("Invalid projectsetid")
    if not projectset.is_group:
        raise BadRequest("Not a group project set")
    if 'admin_groups' not in projectset.offering.get_permissions(
        req.user, req.config):
        raise Unauthorized()

    # Get optional fields
    nick = fields.getfirst('nick').value
    if nick is not None:
        nick = unicode(nick)

    group = ivle.database.ProjectGroup(name=groupnm,
                                       project_set=projectset,
                                       nick=nick,
                                       created_by=req.user,
                                       epoch=datetime.datetime.now())
    req.store.add(group)

    # Create the group repository
    # Yes, this is ugly, and it would be nice to just pass in the groupid,
    # but the object isn't visible to the extra transaction in
    # usrmgt-server until we commit, which we only do once the repo is
    # created.
    offering = group.project_set.offering

    args = {
        "subj_short_name": offering.subject.short_name,
        "year": offering.semester.year,
        "semester": offering.semester.url_name,
        "groupnm": group.name,
    }
    msg = {'create_group_repository': args}

    # Contact the usrmgt server
    try:
        usrmgt = chat.chat(req.config['usrmgt']['host'],
                           req.config['usrmgt']['port'],
                           msg,
                           req.config['usrmgt']['magic'],
                          )
    except ValueError, e:
        raise Exception("Could not understand usrmgt server response:" +
                        e.message)