Ejemplo n.º 1
0
def make_base_project(user):
    chain = new_chain()
    try:
        proj = create_project(
            id=chain.chain,
            uuid=user.uuid,
            last_application=None,
            owner=None,
            realname="system:" + user.uuid,
            homepage="",
            description=("system project for user " + user.username),
            end_date=(datetime.now() + relativedelta(years=100)),
            member_join_policy=CLOSED_POLICY,
            member_leave_policy=CLOSED_POLICY,
            limit_on_members_number=1,
            private=True,
            is_base=True)
    except IntegrityError as e:
        if 'uuid' in str(e):
            m = (("The impossible happened: "
                  "User UUID '%s' collides with an existing project. "
                  "To resolve the issue, delete the user "
                  "and create a new one.")
                 % user.uuid)
            logger.warning(m)
            raise ProjectConflict(m)
        raise
    user.base_project = proj
    user.save()
    return proj
Ejemplo n.º 2
0
def submit_application(owner=None,
                       name=None,
                       project_id=None,
                       homepage=None,
                       description=None,
                       start_date=None,
                       end_date=None,
                       member_join_policy=None,
                       member_leave_policy=None,
                       limit_on_members_number=None,
                       private=False,
                       comments=None,
                       resources=None,
                       request_user=None):

    project = None
    if project_id is not None:
        project = get_project_for_update(project_id)
        project_check_allowed(project, request_user, level=APPLICANT_LEVEL)
        if project.state not in Project.INITIALIZED_STATES:
            raise ProjectConflict("Cannot modify an uninitialized project.")

    policies = validate_resource_policies(resources)

    if name is not None:
        maxlen = ProjectApplication.MAX_NAME_LENGTH
        if len(name) > maxlen:
            raise ProjectBadRequest(
                "'name' value exceeds max length %s" % maxlen)
        if not valid_project_name(name):
            raise ProjectBadRequest("Project name should be in domain format")

    if member_join_policy is not None:
        if member_join_policy not in POLICIES:
            raise ProjectBadRequest("Invalid join policy")

    if member_leave_policy is not None:
        if member_leave_policy not in POLICIES:
            raise ProjectBadRequest("Invalid join policy")

    if homepage is not None:
        maxlen = ProjectApplication.MAX_HOMEPAGE_LENGTH
        if len(homepage) > maxlen:
            raise ProjectBadRequest(
                "'homepage' value exceeds max length %s" % maxlen)
    if description is not None:
        maxlen = MAX_TEXT_INPUT
        if len(description) > maxlen:
            raise ProjectBadRequest(
                "'description' value exceeds max length %s" % maxlen)
    if comments is not None:
        maxlen = MAX_TEXT_INPUT
        if len(comments) > maxlen:
            raise ProjectBadRequest(
                "'comments' value exceeds max length %s" % maxlen)
    if limit_on_members_number is not None:
        if not 0 <= limit_on_members_number <= MAX_BIGINT:
            raise ProjectBadRequest("max_members out of range")

    if start_date is not None:
        start_date = get_date(start_date, "start_date")
    if end_date is not None:
        end_date = get_date(end_date, "end_date")
        if end_date < datetime.now():
            raise ProjectBadRequest(
                "'end_date' must be in the future")

    force = request_user.is_project_admin()
    ok, limit = qh_add_pending_app(request_user, project, force)
    if not ok:
        m = _(astakos_messages.REACHED_PENDING_APPLICATION_LIMIT) % limit
        raise ProjectConflict(m)

    if project is None:
        chain = new_chain()
        project = create_project(
            id=chain.chain,
            owner=owner,
            realname=name,
            homepage=homepage,
            description=description,
            end_date=end_date,
            member_join_policy=member_join_policy,
            member_leave_policy=member_leave_policy,
            limit_on_members_number=limit_on_members_number,
            private=private)
        if policies is not None:
            set_project_resources(project, policies)
    elif project.is_base:
        if [x for x in [owner, name, homepage, description] if x is not None]:
            raise ProjectConflict(
                "Cannot modify fields 'owner', 'name', 'homepage', and "
                "'description' of a system project.")

    application = ProjectApplication.objects.create(
        applicant=request_user,
        chain=project,
        owner=owner,
        name=name,
        homepage=homepage,
        description=description,
        start_date=start_date,
        end_date=end_date,
        member_join_policy=member_join_policy,
        member_leave_policy=member_leave_policy,
        limit_on_members_number=limit_on_members_number,
        private=private,
        comments=comments)
    if policies is not None:
        set_application_resources(application, policies)

    project.last_application = application
    project.save()

    ProjectApplication.objects.\
        filter(chain=project, state=ProjectApplication.PENDING).\
        exclude(id=application.id).\
        update(state=ProjectApplication.REPLACED)

    logger.info("User %s submitted %s." %
                (request_user.log_display, application.log_display))
    action = "submit_new" if project_id is None else "submit_modification"
    project_notif.application_notify(application, action)
    return application