Beispiel #1
0
def delete_subprojects(project, subprojects):
    Annotation.objects(
        project=project,
        subproject__in=subprojects,
    ).delete()
    project = Project.objects(project_name=project).first()
    project.subprojects = [
        sp for sp in project.subprojects if sp not in subprojects
    ]
    project.save()
Beispiel #2
0
def delete_subproject(project, subproject):
    Annotation.objects(
        project=project,
        subproject=subproject,
    ).delete()
    project = Project.objects(project_name=project).first()
    project.subprojects = [
        sp for sp in project.subprojects if sp != subproject
    ]
    project.save()
Beispiel #3
0
def add_users_to_project(usernames, project):
    project = Project.objects(project_name=project).first()
    for username in usernames:
        user = User.objects(username=username).first()
        if project.project_name not in user.projects:
            user.projects.append(project.project_name)
            user.save()
        if username not in project.usernames:
            project.usernames.append(username)
            project.save()
Beispiel #4
0
def get_project_by_name(project_name):
    return Project.objects(project_name=project_name).first()
Beispiel #5
0
def get_project_names():
    return [p.project_name for p in Project.objects()]
Beispiel #6
0
def get_project_by_url_name(project_name_url):
    return Project.objects(
        project_name=urllib.parse.unquote_plus(project_name_url))
Beispiel #7
0
def create_project(project_name, start_date, end_date):
    project = Project(project_name=project_name,
                      start_date=start_date,
                      end_date=end_date)
    project.save()
Beispiel #8
0
def _load_json_to_database(filepath, upload: Upload):
    with open(filepath) as fh:
        data = json.load(fh)
    try:
        jsonschema.validate(data, get_schema())
    except Exception as e:
        print(e)
        return False

    # setup connections
    connections = setup_connections(*data['connections'])

    # get project
    try:
        project = Project.objects(project_name=data['project']).first()
    except Exception as e:
        raise ValueError(f'Project does not exist: {data["project"]}, {e}')
    if not project:
        raise ValueError(
            f'Project does not exist: {data["project"]}. Please create project first.'
        )

    subproject_name = data.get('subproject', f'{project.name}_{uuid.uuid4()}')
    project.subprojects.append(subproject_name)
    project.save()
    labels = data.get('labels', None)
    highlights = data.get('highlights', list())
    documents = data.get('documents', [])
    document_index = 0
    start_index = 0
    end_index = None
    n_users = len(data['annotation']['annotators'])
    for user_index, user in enumerate(data['annotation']['annotators']):
        if user['name'] not in project.usernames:
            raise ValueError(
                f'User not part of project: {user["name"]}, {project.name}')
        add_documents_to_user(
            upload,
            project.name,
            subproject_name,
            user['name'],
            user.get('documents', []),
            connections,
            labels=labels,
            end_date=project.end_date,
            highlights=highlights,
        )
        add_documents_to_user(upload,
                              project.name,
                              subproject_name,
                              user['name'],
                              data.get('irr_documents', []),
                              connections,
                              labels=labels,
                              end_date=project.end_date,
                              highlights=highlights)
        if n_users == user_index + 1:  # last user
            end_index = None
        elif 'number' in user:
            start_index = document_index
            end_index = document_index + user['number']
            document_index = end_index
        elif 'percent' in user:
            increment = round(user['percent'] * len(documents))
            start_index = document_index
            end_index = document_index + increment
            document_index = end_index
        add_documents_to_user(upload,
                              project.name,
                              subproject_name,
                              user['name'],
                              documents[start_index:end_index],
                              connections,
                              labels=labels,
                              end_date=project.end_date,
                              highlights=highlights)
    return True