<commit_before>from typing import Dict

from aiohttp import web
from aiohttp.web_request import Request
from aiohttp.web_response import Response
from aiohttp_jinja2 import template
from bleach import clean

from db_helper import get_project_name
from permissions import is_user


@template('project_edit.jinja2')
async def project_edit(request: Request) -> Dict:
    """
    Edit a project.
    This view should only be allowed if the project group is not read only and the logged
    in user is the owner of the project.

    :param request:
    :return:
    """
    session = request.app["session"]
    project_name = request.match_info["project_name"]
    project = get_project_name(session, project_name)
    if project is None:
        return web.Response(status=404)
    if not is_user(request.cookies, project.supervisor):
        return web.Response(status=403)
    if project.group.read_only:
        return web.Response(status=403)
    programmes = request.app["misc_config"]["programmes"]
    return {"project": project,
            "label": "Save and Update",
            "show_delete_button": True,
            "programmes": programmes}


async def on_submit(request: Request) -> Response:
    """
    Update a project in the database.
    Redirect the user to the new location for the project if the title is changed
    This view should only be allowed if the project group is not read only and the logged

    :param request:
    :return:
    """
    session = request.app["session"]
    project_name = request.match_info["project_name"]
    project = get_project_name(session, project_name)
    if not is_user(request.cookies, project.supervisor):
        return web.Response(status=403)
    if project.group.read_only:
        return web.Response(status=403)
    post = await request.post()
    try:
        project.programmes = "|".join(post.getall("programmes"))
    except KeyError:
        project.programmes = ""
    project.title = post["title"]
    project.is_wetlab = post["options"] in ("wetlab", "both")
    project.is_computational = post["options"] in ("computational", "both")
    project.small_info = post["authors"]
    project.abstract = clean(post["message"])
    session.commit()
    return web.Response(status=200, text=f"../{project.title}/edit")


async def on_delete(request: Request) -> Response:
    session = request.app["session"]
    project_name = request.match_info["project_name"]
    project = get_project_name(session, project_name)
    if not is_user(request.cookies, project.supervisor):
        return web.Response(status=403)
    if project.group.read_only:
        return web.Response(status=403)
    session.delete(project)
    session.commit()
    return web.Response(status=200, text=f"/")<commit_msg>Fix cleaning of html<commit_after>from typing import Dict

from aiohttp import web
from aiohttp.web_request import Request
from aiohttp.web_response import Response
from aiohttp_jinja2 import template
from bleach import clean

from db_helper import get_project_name
from permissions import is_user


@template('project_edit.jinja2')
async def project_edit(request: Request) -> Dict:
    """
    Edit a project.
    This view should only be allowed if the project group is not read only and the logged
    in user is the owner of the project.

    :param request:
    :return:
    """
    session = request.app["session"]
    project_name = request.match_info["project_name"]
    project = get_project_name(session, project_name)
    if project is None:
        return web.Response(status=404)
    if not is_user(request.cookies, project.supervisor):
        return web.Response(status=403)
    if project.group.read_only:
        return web.Response(status=403)
    programmes = request.app["misc_config"]["programmes"]
    return {"project": project,
            "label": "Save and Update",
            "show_delete_button": True,
            "programmes": programmes}


async def on_submit(request: Request) -> Response:
    """
    Update a project in the database.
    Redirect the user to the new location for the project if the title is changed
    This view should only be allowed if the project group is not read only and the logged

    :param request:
    :return:
    """
    session = request.app["session"]
    project_name = request.match_info["project_name"]
    project = get_project_name(session, project_name)
    if not is_user(request.cookies, project.supervisor):
        return web.Response(status=403)
    if project.group.read_only:
        return web.Response(status=403)
    post = await request.post()
    try:
        project.programmes = "|".join(post.getall("programmes"))
    except KeyError:
        project.programmes = ""
    project.title = post["title"]
    project.is_wetlab = post["options"] in ("wetlab", "both")
    project.is_computational = post["options"] in ("computational", "both")
    project.small_info = post["authors"]
    cleaned = clean(post["message"],
                    tags=['a', 'b', 'blockquote', 'code', 'em', 'i', 'li', 'ol', 'strong', 'ul', 'font', 'div', 'u', 'pre', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'br', 'sub', 'sup'],
                    attributes=['align', 'size', 'face', 'href', 'title', 'target'])
    project.abstract = cleaned
    session.commit()
    return web.Response(status=200, text=f"../{project.title}/edit")


async def on_delete(request: Request) -> Response:
    session = request.app["session"]
    project_name = request.match_info["project_name"]
    project = get_project_name(session, project_name)
    if not is_user(request.cookies, project.supervisor):
        return web.Response(status=403)
    if project.group.read_only:
        return web.Response(status=403)
    session.delete(project)
    session.commit()
    return web.Response(status=200, text=f"/")
{'fraction_non_alphanumeric': 0.11529209621993128, 'fraction_numerical': 0.010996563573883162, 'mean_word_length': 3.5229215229215227, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7211675', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1690, 'n_words': 482}