Example #1
0
def project_new_post(auth, **kwargs):
    user = auth.user

    title = strip_html(request.json.get('title'))
    template = request.json.get('template')
    description = strip_html(request.json.get('description'))
    title = title.strip()

    if not title or len(title) > 200:
        raise HTTPError(http.BAD_REQUEST)

    if template:
        original_node = Node.load(template)
        changes = {
            'title': title
        }

        if description:
            changes['description'] = description

        project = original_node.use_as_template(
            auth=auth,
            changes={
                template: changes
            })

    else:
        project = new_node('project', title, user, description)

    return {
        'projectUrl': project.url
    }, http.CREATED
Example #2
0
def project_new_post(auth, **kwargs):
    user = auth.user

    data = request.get_json()
    title = strip_html(data.get('title'))
    title = title.strip()
    category = data.get('category', 'project')
    template = data.get('template')
    description = strip_html(data.get('description'))

    if not title or len(title) > 200:
        raise HTTPError(http.BAD_REQUEST)

    if template:
        original_node = Node.load(template)
        changes = {
            'title': title,
            'category': category,
            'template_node': original_node,
        }

        if description:
            changes['description'] = description

        project = original_node.use_as_template(auth=auth,
                                                changes={
                                                    template: changes,
                                                })

    else:
        project = new_node(category, title, user, description)

    return {'projectUrl': project.url}, http.CREATED
Example #3
0
def project_new_node(auth, node, **kwargs):
    form = NewNodeForm(request.form)
    user = auth.user
    if form.validate():
        try:
            new_component = new_node(
                title=strip_html(form.title.data),
                user=user,
                category=form.category.data,
                parent=node,
            )
        except ValidationValueError as e:
            raise HTTPError(http.BAD_REQUEST,
                            data=dict(message_long=e.message))

        message = (
            'Your component was created successfully. You can keep working on the component page below, '
            'or return to the <u><a href="{url}">project page</a></u>.'
        ).format(url=node.url)
        status.push_status_message(message, kind='info', trust=True)

        return {
            'status': 'success',
        }, 201, None, new_component.url
    else:
        # TODO: This function doesn't seem to exist anymore?
        status.push_errors_to_status(form.errors)
    raise HTTPError(http.BAD_REQUEST, redirect_url=node.url)
Example #4
0
def project_new_node(auth, node, **kwargs):
    form = NewNodeForm(request.form)
    user = auth.user
    if form.validate():
        try:
            new_component = new_node(
                title=strip_html(form.title.data),
                user=user,
                category=form.category.data,
                parent=node,
            )
        except ValidationValueError as e:
            raise HTTPError(
                http.BAD_REQUEST,
                data=dict(message_long=e.message)
            )

        message = (
            'Your component was created successfully. You can keep working on the component page below, '
            'or return to the <u><a href="{url}">project page</a></u>.'
        ).format(url=node.url)
        status.push_status_message(message, kind='info', trust=True)

        return {
            'status': 'success',
        }, 201, None, new_component.url
    else:
        # TODO: This function doesn't seem to exist anymore?
        status.push_errors_to_status(form.errors)
    raise HTTPError(http.BAD_REQUEST, redirect_url=node.url)
Example #5
0
def project_new_post(auth, **kwargs):
    user = auth.user

    title = strip_html(request.json.get('title'))
    template = request.json.get('template')
    description = strip_html(request.json.get('description'))
    title = title.strip()

    if not title or len(title) > 200:
        raise HTTPError(http.BAD_REQUEST)

    if template:
        original_node = Node.load(template)
        changes = {
            'title': title
        }

        if description:
            changes['description'] = description

        project = original_node.use_as_template(
            auth=auth,
            changes={
                template: changes
            })

    else:
        project = new_node('project', title, user, description)

    return {
        'projectUrl': project.url
    }, http.CREATED
Example #6
0
def project_new_post(auth, **kwargs):
    user = auth.user

    data = request.get_json()
    title = strip_html(data.get("title"))
    title = title.strip()
    category = data.get("category", "project")
    template = data.get("template")
    description = strip_html(data.get("description"))
    new_project = {}

    if template:
        original_node = Node.load(template)
        changes = {"title": title, "category": category, "template_node": original_node}

        if description:
            changes["description"] = description

        project = original_node.use_as_template(auth=auth, changes={template: changes})

    else:
        try:
            project = new_node(category, title, user, description)
        except ValidationValueError as e:
            raise HTTPError(http.BAD_REQUEST, data=dict(message_long=e.message))
        new_project = _view_project(project, auth)
    return {"projectUrl": project.url, "newNode": new_project["node"] if new_project else None}, http.CREATED
Example #7
0
def project_new_node(auth, node, **kwargs):
    form = NewNodeForm(request.form)
    user = auth.user
    if form.validate():
        try:
            new_component = new_node(
                title=strip_html(form.title.data),
                user=user,
                category=form.category.data,
                parent=node,
            )
        except ValidationError as e:
            raise HTTPError(http.BAD_REQUEST,
                            data=dict(message_long=e.message))
        redirect_url = node.url
        message = (
            'Your component was created successfully. You can keep working on the project page below, '
            'or go to the new <u><a href={component_url}>component</a></u>.'
        ).format(component_url=new_component.url)
        if form.inherit_contributors.data and node.has_permission(user, WRITE):
            for contributor in node.contributors:
                perm = CREATOR_PERMISSIONS if contributor._id == user._id else node.get_permissions(
                    contributor)
                if contributor._id == user._id and not contributor.is_registered:
                    new_component.add_unregistered_contributor(
                        fullname=contributor.fullname,
                        email=contributor.email,
                        permissions=perm,
                        auth=auth,
                        existing_user=contributor)
                else:
                    new_component.add_contributor(contributor,
                                                  permissions=perm,
                                                  auth=auth)

            new_component.save()
            redirect_url = new_component.url + 'contributors/'
            message = (
                'Your component was created successfully. You can edit the contributor permissions below, '
                'work on your <u><a href={component_url}>component</a></u> or return to the <u> '
                '<a href="{project_url}">project page</a></u>.').format(
                    component_url=new_component.url, project_url=node.url)
        status.push_status_message(message, kind='info', trust=True)

        return {
            'status': 'success',
        }, 201, None, redirect_url
    else:
        # TODO: This function doesn't seem to exist anymore?
        status.push_errors_to_status(form.errors)
    raise HTTPError(http.BAD_REQUEST, redirect_url=node.url)
Example #8
0
def get_or_create_node(title, user):
    """Get or create node by title and creating user.

    :param str title: Node title
    :param User user: User creating node
    :return: Tuple of (node, created)
    """
    try:
        node = Node.find_one(
            Q('title', 'iexact', title)
            & Q('contributors', 'eq', user))
        return node, False
    except ModularOdmException:
        node = new_node('project', title, user)
        return node, True
Example #9
0
def project_new_node(auth, node, **kwargs):
    form = NewNodeForm(request.form)
    user = auth.user
    if form.validate():
        try:
            new_component = new_node(
                title=strip_html(form.title.data),
                user=user,
                category=form.category.data,
                parent=node,
            )
        except ValidationError as e:
            raise HTTPError(
                http.BAD_REQUEST,
                data=dict(message_long=e.message)
            )
        redirect_url = node.url
        message = (
            'Your component was created successfully. You can keep working on the project page below, '
            'or go to the new <u><a href={component_url}>component</a></u>.'
        ).format(component_url=new_component.url)
        if form.inherit_contributors.data and node.has_permission(user, WRITE):
            for contributor in node.contributors:
                perm = CREATOR_PERMISSIONS if contributor._id == user._id else node.get_permissions(contributor)
                if contributor._id == user._id and not contributor.is_registered:
                    new_component.add_unregistered_contributor(
                        fullname=contributor.fullname, email=contributor.email,
                        permissions=perm, auth=auth, existing_user=contributor
                    )
                else:
                    new_component.add_contributor(contributor, permissions=perm, auth=auth)

            new_component.save()
            redirect_url = new_component.url + 'contributors/'
            message = (
                'Your component was created successfully. You can edit the contributor permissions below, '
                'work on your <u><a href={component_url}>component</a></u> or return to the <u> '
                '<a href="{project_url}">project page</a></u>.'
            ).format(component_url=new_component.url, project_url=node.url)
        status.push_status_message(message, kind='info', trust=True)

        return {
            'status': 'success',
        }, 201, None, redirect_url
    else:
        # TODO: This function doesn't seem to exist anymore?
        status.push_errors_to_status(form.errors)
    raise HTTPError(http.BAD_REQUEST, redirect_url=node.url)
Example #10
0
def get_or_create_node(title, user):
    """Get or create node by title and creating user.

    :param str title: Node title
    :param User user: User creating node
    :return: Tuple of (node, created)
    """
    try:
        node = Node.find_one(
            Q('title', 'iexact', title)
            & Q('contributors', 'eq', user._id)
        )
        return node, False
    except ModularOdmException:
        node = new_node('project', title, user)
        return node, True
Example #11
0
def project_new_post(auth, **kwargs):
    user = auth.user

    data = request.get_json()
    title = strip_html(data.get('title'))
    title = title.strip()
    category = data.get('category', 'project')
    template = data.get('template')
    description = strip_html(data.get('description'))
    new_project = {}

    if template:
        original_node = Node.load(template)
        changes = {
            'title': title,
            'category': category,
            'template_node': original_node,
        }

        if description:
            changes['description'] = description

        project = original_node.use_as_template(
            auth=auth,
            changes={
                template: changes,
            }
        )

    else:
        try:
            project = new_node(category, title, user, description)
        except ValidationError as e:
            raise HTTPError(
                http.BAD_REQUEST,
                data=dict(message_long=e.message)
            )
        new_project = _view_project(project, auth)
    return {
        'projectUrl': project.url,
        'newNode': new_project['node'] if new_project else None
    }, http.CREATED
Example #12
0
def project_new_post(auth, **kwargs):
    user = auth.user

    data = request.get_json()
    title = strip_html(data.get('title'))
    title = title.strip()
    category = data.get('category', 'project')
    template = data.get('template')
    description = strip_html(data.get('description'))
    new_project = {}

    if template:
        original_node = Node.load(template)
        changes = {
            'title': title,
            'category': category,
            'template_node': original_node,
        }

        if description:
            changes['description'] = description

        project = original_node.use_as_template(
            auth=auth,
            changes={
                template: changes,
            }
        )

    else:
        try:
            project = new_node(category, title, user, description)
        except ValidationValueError as e:
            raise HTTPError(
                http.BAD_REQUEST,
                data=dict(message_long=e.message)
            )
        new_project = _view_project(project, auth)
    return {
        'projectUrl': project.url,
        'newNode': new_project['node'] if new_project else None
    }, http.CREATED
Example #13
0
def project_new_node(auth, node, **kwargs):
    form = NewNodeForm(request.form)
    user = auth.user
    if form.validate():
        try:
            new_component = new_node(
                title=strip_html(form.title.data),
                user=user,
                category=form.category.data,
                parent=node,
            )
        except ValidationValueError as e:
            raise HTTPError(http.BAD_REQUEST,
                            data=dict(message_long=e.message))
        redirect_url = new_component.url
        message = (
            'Your component was created successfully. You can keep working on the component page below, '
            'or return to the <u><a href="{url}">project page</a></u>.'
        ).format(url=node.url)
        if form.inherit_contributors.data and node.has_permission(user, ADMIN):
            for contributor in node.contributors:
                new_component.add_contributor(
                    contributor,
                    permissions=node.get_permissions(contributor),
                    auth=auth)
            new_component.save()
            redirect_url = redirect_url + 'contributors/'
            message = (
                'Your component was created successfully. You can edit the contributor permissions below, '
                'work on your <u><a href=' + new_component.url +
                '>component</a></u> or return to the <u><a href="{url}">project page</a></u>.'
            ).format(url=node.url)
        status.push_status_message(message, kind='info', trust=True)

        return {
            'status': 'success',
        }, 201, None, redirect_url
    else:
        # TODO: This function doesn't seem to exist anymore?
        status.push_errors_to_status(form.errors)
    raise HTTPError(http.BAD_REQUEST, redirect_url=node.url)
Example #14
0
def project_new_node(auth, node, **kwargs):
    form = NewNodeForm(request.form)
    user = auth.user
    if form.validate():
        try:
            new_component = new_node(
                title=strip_html(form.title.data),
                user=user,
                category=form.category.data,
                parent=node,
            )
        except ValidationValueError as e:
            raise HTTPError(
                http.BAD_REQUEST,
                data=dict(message_long=e.message)
            )
        redirect_url = new_component.url
        message = (
            'Your component was created successfully. You can keep working on the component page below, '
            'or return to the <u><a href="{url}">project page</a></u>.'
        ).format(url=node.url)
        if form.inherit_contributors.data and node.has_permission(user, ADMIN):
            for contributor in node.contributors:
                new_component.add_contributor(contributor, permissions=node.get_permissions(contributor), auth=auth)
            new_component.save()
            redirect_url = redirect_url + 'contributors/'
            message = (
                'Your component was created successfully. You can edit the contributor permissions below, '
                'work on your <u><a href=' + new_component.url +
                '>component</a></u> or return to the <u><a href="{url}">project page</a></u>.'
            ).format(url=node.url)
        status.push_status_message(message, kind='info', trust=True)

        return {
            'status': 'success',
        }, 201, None, redirect_url
    else:
        # TODO: This function doesn't seem to exist anymore?
        status.push_errors_to_status(form.errors)
    raise HTTPError(http.BAD_REQUEST, redirect_url=node.url)
Example #15
0
def project_new_node(auth, node, **kwargs):
    form = NewNodeForm(request.form)
    user = auth.user
    if form.validate():
        node = new_node(
            title=strip_html(form.title.data),
            user=user,
            category=form.category.data,
            parent=node,
        )
        message = (
            'Your component was created successfully. You can keep working on the component page below, '
            'or return to the <u><a href="{url}">Project Page</a></u>.'
        ).format(url=node.url)
        status.push_status_message(message, 'info')

        return {
            'status': 'success',
        }, 201, None, node.url
    else:
        status.push_errors_to_status(form.errors)
    raise HTTPError(http.BAD_REQUEST, redirect_url=node.url)
Example #16
0
def project_new_node(auth, node, **kwargs):
    form = NewNodeForm(request.form)
    user = auth.user
    if form.validate():
        node = new_node(
            title=strip_html(form.title.data),
            user=user,
            category=form.category.data,
            parent=node,
        )
        message = (
            'Your component was created successfully. You can keep working on the component page below, '
            'or return to the <u><a href="{url}">Project Page</a></u>.'
        ).format(url=node.url)
        status.push_status_message(message, 'info')

        return {
            'status': 'success',
        }, 201, None, node.url
    else:
        status.push_errors_to_status(form.errors)
    raise HTTPError(http.BAD_REQUEST, redirect_url=node.url)
Example #17
0
def project_new_post(auth, **kwargs):
    user = auth.user

    data = request.get_json()
    title = strip_html(data.get('title'))
    title = title.strip()
    category = data.get('category', 'project')
    template = data.get('template')
    description = strip_html(data.get('description'))

    if not title or len(title) > 200:
        raise HTTPError(http.BAD_REQUEST)

    if template:
        original_node = Node.load(template)
        changes = {
            'title': title,
            'category': category,
            'template_node': original_node,
        }

        if description:
            changes['description'] = description

        project = original_node.use_as_template(
            auth=auth,
            changes={
                template: changes,
            }
        )

    else:
        project = new_node(category, title, user, description)

    return {
        'projectUrl': project.url
    }, http.CREATED
Example #18
0
def add_poster_by_email(conf, recipient, address, fullname, subject,
                        message, attachments, tags=None, system_tags=None,
                        is_spam=False):

    # Fail if no attachments
    if not attachments:
        send_mail(
            address,
            CONFERENCE_FAILED,
            fullname=fullname,
        )
        return

    # Use address as name if name missing
    fullname = fullname or address.split('@')[0]

    created = []

    user, user_created = get_or_create_user(fullname, address, is_spam)

    if user_created:
        created.append(user)
        set_password_url = web_url_for(
            'reset_password',
            verification_key=user.verification_key,
        )
    else:
        set_password_url = None

    auth = Auth(user=user)

    # Find or create node
    node = Node.find(Q('title', 'iexact', subject))
    node = node[0] if node.count() else None
    if node is None or not node.is_contributor(user):
        node = new_node('project', subject, user)
        created.append(node)

    # Add admin to project
    if conf.admins:
        for admin in conf.admins:
            node.add_contributor(
                contributor=admin,
                visible=False,
                log=False,
                save=True
            )

    # Make public if confident that this is not spam and projects made public
    if is_spam:
        logger.warn(
            'Possible spam detected in email modification of '
            'user {0} / node {1}'.format(
                user._id, node._id,
            )
        )
    elif conf.public_projects:
        node.set_privacy('public', auth=auth)

    # Add body
    node.update_node_wiki('home', sanitize(message), auth)

    # Add tags
    presentation_type = 'talk' if 'talk' in recipient else 'poster'

    tags = tags or []
    tags.append(presentation_type)
    for tag in tags:
        node.add_tag(tag, auth=auth)

    # Add system tags
    system_tags = system_tags or []
    system_tags.append(presentation_type)
    system_tags.append('emailed')
    if is_spam:
        system_tags.append('spam')
    for tag in system_tags:
        if tag not in node.system_tags:
            node.system_tags.append(tag)

    # Save changes
    node.save()

    from website.addons.osfstorage import utils as storage_utils

    # Add files
    for attachment in attachments:
        name, content, content_type, size = prepare_file(attachment)
        upload_url = storage_utils.get_upload_url(node, user, size, content_type, name)
        requests.put(
            upload_url,
            data=content,
            headers={'Content-Type': content_type},
        )

    download_url = node.web_url_for(
        'osf_storage_view_file',
        path=attachments[0].filename,
        action='download',
    )

    # Add mail record
    mail_record = MailRecord(
        data=request_to_data(),
        records=created,
    )
    mail_record.save()

    # Send confirmation email
    send_mail(
        address,
        CONFERENCE_SUBMITTED,
        conf_full_name=conf.name,
        conf_view_url=urlparse.urljoin(
            settings.DOMAIN, os.path.join('view', conf.endpoint)
        ),
        fullname=fullname,
        user_created=user_created,
        set_password_url=set_password_url,
        profile_url=user.absolute_url,
        node_url=urlparse.urljoin(settings.DOMAIN, node.url),
        file_url=urlparse.urljoin(settings.DOMAIN, download_url),
        presentation_type=presentation_type,
        is_spam=is_spam,
    )
Example #19
0
def add_poster_by_email(conf,
                        recipient,
                        address,
                        fullname,
                        subject,
                        message,
                        attachments,
                        tags=None,
                        system_tags=None,
                        is_spam=False):

    # Fail if no attachments
    if not attachments:
        send_mail(
            address,
            CONFERENCE_FAILED,
            fullname=fullname,
        )
        return

    # Use address as name if name missing
    fullname = fullname or address.split('@')[0]

    created = []

    user, user_created = get_or_create_user(fullname, address, is_spam)

    if user_created:
        created.append(user)
        set_password_url = web_url_for(
            'reset_password',
            verification_key=user.verification_key,
        )
    else:
        set_password_url = None

    auth = Auth(user=user)

    # Find or create node
    node = Node.find(Q('title', 'iexact', subject))
    node = node[0] if node.count() else None
    if node is None or not node.is_contributor(user):
        node = new_node('project', subject, user)
        created.append(node)

    # Add admin to project
    if conf.admins:
        for admin in conf.admins:
            node.add_contributor(contributor=admin,
                                 visible=False,
                                 log=False,
                                 save=True)

    # Make public if confident that this is not spam and projects made public
    if is_spam:
        logger.warn('Possible spam detected in email modification of '
                    'user {0} / node {1}'.format(
                        user._id,
                        node._id,
                    ))
    elif conf.public_projects:
        node.set_privacy('public', auth=auth)

    # Add body
    node.update_node_wiki('home', sanitize(message), auth)

    # Add tags
    presentation_type = 'talk' if 'talk' in recipient else 'poster'

    tags = tags or []
    tags.append(presentation_type)
    for tag in tags:
        node.add_tag(tag, auth=auth)

    # Add system tags
    system_tags = system_tags or []
    system_tags.append(presentation_type)
    system_tags.append('emailed')
    if is_spam:
        system_tags.append('spam')
    for tag in system_tags:
        if tag not in node.system_tags:
            node.system_tags.append(tag)

    # Save changes
    node.save()

    from website.addons.osfstorage import utils as storage_utils

    # Add files
    for attachment in attachments:
        name, content, content_type, size = prepare_file(attachment)
        upload_url = storage_utils.get_upload_url(node, user, size,
                                                  content_type, name)
        requests.put(
            upload_url,
            data=content,
            headers={'Content-Type': content_type},
        )

    download_url = node.web_url_for(
        'osf_storage_view_file',
        path=attachments[0].filename,
        action='download',
    )

    # Add mail record
    mail_record = MailRecord(
        data=request_to_data(),
        records=created,
    )
    mail_record.save()

    # Send confirmation email
    send_mail(
        address,
        CONFERENCE_SUBMITTED,
        conf_full_name=conf.name,
        conf_view_url=urlparse.urljoin(settings.DOMAIN,
                                       os.path.join('view', conf.endpoint)),
        fullname=fullname,
        user_created=user_created,
        set_password_url=set_password_url,
        profile_url=user.absolute_url,
        node_url=urlparse.urljoin(settings.DOMAIN, node.url),
        file_url=urlparse.urljoin(settings.DOMAIN, download_url),
        presentation_type=presentation_type,
        is_spam=is_spam,
    )