Skip to content

Commit

Permalink
Merge branch 'add_validated_tree'
Browse files Browse the repository at this point in the history
  • Loading branch information
khansadaoudi committed Sep 29, 2023
2 parents cc02010 + 5e76ce5 commit bc7563c
Show file tree
Hide file tree
Showing 20 changed files with 484 additions and 407 deletions.
19 changes: 17 additions & 2 deletions app/db_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
print('mode must be prod or dev')
exit()

if args.version != 'add_github' and args.version != 'add_constructicon' and args.version != 'all' and args.version != 'new_roles':
if args.version != 'add_github' and args.version != 'add_constructicon' and args.version != 'all' and args.version != 'new_roles' and args.version != 'add_validated_tree':
print('version must be add_github, add_constructicon or all')
exit()

Expand Down Expand Up @@ -49,6 +49,18 @@ def migrate_add_constructicon(engine):
def migrate_new_roles(engine):
with engine.connect() as connection:
connection.execute('UPDATE projectaccess SET access_level = 3 WHERE projectaccess.access_level == 2')


def migrate_add_validated_tree(engine):
with engine.connect() as connection:
connection.execute('ALTER TABLE projects RENAME COLUMN exercise_mode To blind_annotation_mode')
connection.execute('ALTER TABLE projects ADD config STRING')
connection.execute('ALTER TABLE projects ADD language STRING')
connection.execute('ALTER TABLE projects DROP show_all_trees')
connection.execute('ALTER TABLE exerciselevel RENAME COLUMN exercise_level TO blind_annotation_level')
connection.execute('ALTER TABLE exerciselevel RENAME TO blindannotationlevel')
connection.execute('CREATE TABLE user_tags (id INTEGER NOT NULL, user_id VARCHAR(256), tags JSONB, PRIMARY KEY(id), FOREIGN KEY(user_id) REFERENCES users(id))')


if args.version == 'add_github':
migrate_add_github(engine)
Expand All @@ -61,4 +73,7 @@ def migrate_new_roles(engine):
migrate_add_constructicon(engine)

if args.version == 'new_roles':
migrate_new_roles(engine)
migrate_new_roles(engine)

if args.version == 'add_validated_tree':
migrate_add_validated_tree(engine)
13 changes: 9 additions & 4 deletions app/github/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
from typing import List

from flask import abort
from flask_login import current_user

from app import db
from app.config import Config
from app.projects.service import ProjectService
from app.utils.grew_utils import GrewService, grew_request , SampleExportService
from app.user.service import UserService
import app.samples.service as SampleService

from .model import GithubRepository, GithubCommitStatus
Expand All @@ -30,7 +32,7 @@ def get_github_synchronized_repository(project_id):
@staticmethod
def synchronize_github_repository(user_id, project_id, repository_name, branch, sha):

github_repository ={
github_repository = {
"project_id": project_id,
"user_id": user_id,
"repository_name": repository_name,
Expand Down Expand Up @@ -220,8 +222,7 @@ def delete_file_from_github(access_token, project_name, full_name, sample_name):
def delete_sample_from_project(project_name, sample_name):
project = ProjectService.get_by_name(project_name)
GrewService.delete_sample(project_name, sample_name)
SampleService.SampleRoleService.delete_by_sample_name(project.id, sample_name)
SampleService.SampleExerciseLevelService.delete_by_sample_name(project.id, sample_name)
SampleService.SampleBlindAnnotationLevelService.delete_by_sample_name(project.id, sample_name)


class GithubService:
Expand Down Expand Up @@ -334,7 +335,11 @@ def create_tree(access_token, full_name, updated_samples, project_name, username
tree = []
sample_names, sample_content_files = GrewService.get_samples_with_string_contents(project_name, updated_samples)
for sample_name, sample in zip(sample_names,sample_content_files):
content = sample.get(username)
if (username == 'validated'):
owner_username = UserService.get_by_id(current_user.id).username
content = GrewService.get_validated_trees_filled_up_with_owner_trees(project_name, sample_name, owner_username)
else:
content = sample.get(username)
sha = GithubService.create_blob_for_updated_file(access_token, full_name, content)
blob = {"path": sample_name+".conllu", "mode":"100644", "type":"blob", "sha": sha}
tree.append(blob)
Expand Down
42 changes: 36 additions & 6 deletions app/grew/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,38 @@ def post(self, project_name: str):
class SearchResource(Resource):
"Search"
def post(self, project_name: str):

parser = reqparse.RequestParser()
parser.add_argument(name="pattern", type=str)
parser.add_argument(name="userType", type=str)
args = parser.parse_args()

pattern = args.get("pattern")
user_type = args.get("userType")
trees_type = args.get("userType")

user_type = 'all' if trees_type == 'pending' else trees_type

reply = GrewService.search_pattern_in_graphs(project_name, pattern, user_type)

if reply["status"] != "OK":
abort(400)

trees = {}
for m in reply["data"]:
if m["user_id"] == "":
abort(409)
conll = m["conll"]
trees = formatTrees_new(m, trees, conll)
return trees

search_results = {}
if trees_type == "pending":
for sample_name, sample_results in trees.items():
search_results[sample_name] = {
sent_id: result for sent_id, result in sample_results.items() if 'validated' not in result["conlls"].keys()
}
else:
search_results = trees

return search_results


@api.route("/<string:project_name>/sample/<string:sample_name>/search")
Expand All @@ -98,8 +111,15 @@ def post(self, project_name: str, sample_name: str):
args = parser.parse_args()

pattern = args.get("pattern")
user_type = args.get("userType")
trees_type = args.get("userType")

user_type = 'all' if trees_type == 'pending' else trees_type

reply = GrewService.search_pattern_in_graphs(project_name, pattern, user_type)

if reply["status"] != "OK":
abort(400)

trees = {}
for m in reply["data"]:
if m["sample_id"] != sample_name:
Expand All @@ -108,7 +128,16 @@ def post(self, project_name: str, sample_name: str):
abort(409)
conll = m["conll"]
trees = formatTrees_new(m, trees, conll)
return trees

search_results = {}
if trees_type == 'pending':
search_results[sample_name] = {
sent_id: result for sent_id, result in trees[sample_name].items() if 'validated' not in result["conlls"].keys()
}
else:
search_results = trees

return search_results

@api.route("/<string:project_name>/try-package")
class TryPackageResource(Resource):
Expand Down Expand Up @@ -158,6 +187,8 @@ def post(self, project_name):
user_ids = { "one": [current_user.username, "__last__"] }
elif tableType=='recent':
user_ids = { "one": ["__last__"] }
elif tableType=='validated':
user_ids = { "one": ["validated"]}
elif tableType=='all':
user_ids = "all"
reply = grew_request(
Expand Down Expand Up @@ -267,7 +298,6 @@ def post_process_diffs(grew_search_results, other_users, features):
matches[other_user_id] = list_matches
conlls[other_user_id] = grew_search_results[sample_name][sent_id]["conlls"][other_user_id]
if len(conlls) > 0 :
print('test')
post_processed_results[sample_name][sent_id] = {
"sentence": grew_search_results[sample_name][sent_id]["sentence"],
"sample_name": sample_name,
Expand Down
8 changes: 5 additions & 3 deletions app/lexicon/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ def post(self, project_name: str):
lexicon_type=args.get("lexiconType")
prune=args.get("prune")

if lexicon_type=='user':
if lexicon_type == 'user':
user_ids = { "one": [current_user.username] }
elif lexicon_type=='user_recent':
elif lexicon_type == 'user_recent':
user_ids = { "one": [current_user.username, "__last__"] }
elif lexicon_type=='recent':
elif lexicon_type == 'recent':
user_ids = { "one": ["__last__"] }
elif lexicon_type == 'validated':
user_ids = { "one": ["validated"] }
elif lexicon_type=='all':
user_ids = "all"

Expand Down
10 changes: 6 additions & 4 deletions app/projects/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ def post(self) -> Project:
parser = reqparse.RequestParser()
parser.add_argument(name="projectName", type=str)
parser.add_argument(name="description", type=str)
parser.add_argument(name="showAllTrees", type=bool)
parser.add_argument(name="exerciseMode", type=bool)
parser.add_argument(name="blindAnnotationMode", type=bool)
parser.add_argument(name="visibility", type=int)
parser.add_argument(name="config", type=str)
parser.add_argument(name="language", type=str)
args = parser.parse_args()

# Sanitize the project name to correspond to Grew folders requirements
Expand All @@ -115,10 +116,11 @@ def post(self) -> Project:
new_project_attrs: ProjectInterface = {
"project_name": projectName,
"description": args.description,
"show_all_trees": args.showAllTrees,
"exercise_mode": args.exerciseMode,
"blind_annotation_mode": args.blindAnnotationMode,
"visibility": args.visibility,
"freezed": False,
"config": args.config,
"language": args.language
}

# KK : TODO : put all grew request in a seperated file and add error catching
Expand Down
5 changes: 3 additions & 2 deletions app/projects/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ class ProjectInterface(TypedDict, total=False):
description: str
image: Any
visibility: int
show_all_trees: bool
exercise_mode: bool
blind_annotation_mode: bool
freezed: bool
config: str
language: str


class ProjectExtendedInterface(ProjectInterface, total=False):
Expand Down
5 changes: 3 additions & 2 deletions app/projects/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class Project(db.Model, BaseM):
description = Column(String(256))
image = Column(String(256), nullable=True)
visibility = Column(Integer)
show_all_trees = Column(Boolean, default=True)
exercise_mode = Column(Boolean, default=False)
blind_annotation_mode = Column(Boolean, default=False)
diff_mode = Column(Boolean, default=False)
diff_user_id = Column(String(256), nullable=True)
freezed = Column(Boolean, default=False)
config = Column(String(256), nullable=True)
language = Column(String(256), nullable=True)

feature = db.relationship("ProjectFeature", cascade="all,delete", backref="projects")
meta_feature = db.relationship("ProjectMetaFeature", cascade="all,delete", backref="projects")
Expand Down
10 changes: 6 additions & 4 deletions app/projects/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ class ProjectSchema(Schema):
description = fields.String(attribute="description")
image = BlobImageField(attribute="image")
visibility = fields.Integer(attribute="visibility")
showAllTrees = fields.Boolean(attribute="show_all_trees")
exerciseMode = fields.Boolean(attribute="exercise_mode")
blindAnnotationMode = fields.Boolean(attribute="blind_annotation_mode")
freezed = fields.Boolean(attribute="freezed")
config = fields.String(attribute="config")
language = fields.String(attribute="language")


class ProjectExtendedSchema(ProjectSchema):
Expand Down Expand Up @@ -58,9 +59,10 @@ class ProjectSchemaCamel(Schema):
description = fields.String(attribute="description")
image = BlobImageField(attribute="image")
visibility = fields.Integer(attribute="visibility")
showAllTrees = fields.Boolean(attribute="show_all_trees")
exerciseMode = fields.Boolean(attribute="exercise_mode")
blindAnnotationMode = fields.Boolean(attribute="blind_annotation_mode")
freezed = fields.Boolean(attribute="freezed")
config = fields.String(attribute="config")
language = fields.String(attribute="language")
diffMode = fields.Boolean(attribute="diff_mode")
diffUserId = fields.String(attribute="diff_user_id")

Expand Down
1 change: 0 additions & 1 deletion app/projects/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from .interface import ProjectExtendedInterface, ProjectInterface
from .model import Project, ProjectAccess, ProjectFeature, ProjectMetaFeature, DefaultUserTrees
from ..utils.grew_utils import grew_request
from ..samples.model import SampleRole


class ProjectService:
Expand Down
2 changes: 2 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def register_routes(api, app, root="api"):
from app.projects import register_routes as attach_projects
from app.samples import register_routes as attach_samples
from app.trees import register_routes as attach_trees
from app.tags import register_routes as attach_tags
from app.lexicon import register_routes as attach_lexicon
from app.constructicon import register_routes as attach_constructicon
from app.grew import register_routes as attach_grew
Expand All @@ -15,6 +16,7 @@ def register_routes(api, app, root="api"):
attach_projects(api, app, root)
attach_samples(api, app, root)
attach_trees(api, app, root)
attach_tags(api, app, root)
attach_lexicon(api, app, root)
attach_constructicon(api, app, root)
attach_grew(api, app, root)
Expand Down
Loading

0 comments on commit bc7563c

Please sign in to comment.