From 5a39b313c550a4ff1340b8c97f7326318c06d6a3 Mon Sep 17 00:00:00 2001 From: wrmswindmill Date: Thu, 30 Aug 2018 15:41:48 +0800 Subject: [PATCH 1/5] add Singleton --- apps/operations/views.py | 12 ++++++------ apps/projects/views.py | 5 +++-- apps/utils/models.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 apps/utils/models.py diff --git a/apps/operations/views.py b/apps/operations/views.py index f716c10..88d99fe 100644 --- a/apps/operations/views.py +++ b/apps/operations/views.py @@ -1167,6 +1167,8 @@ def getQuestionByFileId(): pass +from utils.models import projectTreeObj + class GetAddtabParasView(View): def post(self, request): url = request.POST.get('url','') @@ -1190,15 +1192,13 @@ class GetAddtabParasView(View): project = Project.objects.get(name=projectName) - from utils import get_project_tree - project_tree_obj = get_project_tree.ProjectTree() - project_tree = project_tree_obj.getHtml(settings.SOURCEPATH+project.path) + path_id_map = projectTreeObj.get_project_pathidmap_by_projectid(project.id) if path == "/": - first_tabs_jstree_id = project_tree_obj.path_id_map[""] + first_tabs_jstree_id = path_id_map[""] else: - first_tabs_jstree_id = project_tree_obj.path_id_map[path] + first_tabs_jstree_id = path_id_map[path] - return HttpResponse(json.dumps({"status": "success", "project_id": project.id,"path":path,"filename":filename,"project_jstree_map":project_tree_obj.path_id_map,"first_tabs_jstree_id":first_tabs_jstree_id}), content_type='application/json') + return HttpResponse(json.dumps({"status": "success", "project_id": project.id,"path":path,"filename":filename,"project_jstree_map":path_id_map,"first_tabs_jstree_id":first_tabs_jstree_id}), content_type='application/json') # 可能len(all_symbol)==3对于除了java外其他编程语言会有问题 def getMethodNum(project_path,file_path): diff --git a/apps/projects/views.py b/apps/projects/views.py index 199bc90..be4f3cd 100644 --- a/apps/projects/views.py +++ b/apps/projects/views.py @@ -12,6 +12,8 @@ from .forms import NewProjectForm from operations.models import Article, Annotation, Issue, QuestionAnswer,AnnotationStrategy from pure_pagination import Paginator, EmptyPage, PageNotAnInteger from utils import get_project_tree,scanner_project +from utils.models import projectTreeObj + logger = logging.getLogger('django') @@ -163,8 +165,7 @@ class ProjectInfoView(View): class ProjectSourceView(View): def get(self, request, name, path): project = Project.objects.filter(name=name).first() - project_tree_obj = get_project_tree.ProjectTree() - project_tree = project_tree_obj.getHtml(settings.SOURCEPATH+project.path) + project_tree = projectTreeObj.get_project_tree_by_projectid(project.id) return render(request, 'projects/source.html', locals()) diff --git a/apps/utils/models.py b/apps/utils/models.py new file mode 100644 index 0000000..2f50b33 --- /dev/null +++ b/apps/utils/models.py @@ -0,0 +1,33 @@ +from utils import get_project_tree,scanner_project +from django.conf import settings +from projects.models import Project + + +class ProjectTreeMap: + def __init__(self): + print("init") + self._tree_map = {} + + def get_project_tree_by_projectid(self,project_id): + if project_id in self._tree_map: + return self._tree_map[project_id]["tree"] + else: + obj = get_project_tree.ProjectTree() + project = Project.objects.get(id=project_id) + tree = obj.getHtml(settings.SOURCEPATH+project.path) + path_id_map = obj.path_id_map + self._tree_map[project_id]={"tree":tree,"path_id_map":path_id_map} + return self._tree_map[project_id]["tree"] + + def get_project_pathidmap_by_projectid(self,project_id): + if project_id in self._tree_map: + return self._tree_map[project_id]["path_id_map"] + else: + obj = get_project_tree.ProjectTree() + project = Project.objects.get(id=project_id) + tree = obj.getHtml(settings.SOURCEPATH+project.path) + path_id_map = obj.path_id_map + self._tree_map[project_id]={"tree":tree,"path_id_map":path_id_map} + return self._tree_map[project_id]["path_id_map"] + +projectTreeObj = ProjectTreeMap() \ No newline at end of file From 61559ef051d3ada65a6c478cb07cdb2717c0b7a6 Mon Sep 17 00:00:00 2001 From: wrmswindmill Date: Thu, 30 Aug 2018 16:06:16 +0800 Subject: [PATCH 2/5] modify --- apps/operations/views.py | 4 +- apps/utils/get_project_tree.py | 2 +- static/js/source.js | 4 +- templates/projects/filesub/annotation.html | 2 +- uwsgi.log | 560 +++++++++++++++++++++ 5 files changed, 568 insertions(+), 4 deletions(-) diff --git a/apps/operations/views.py b/apps/operations/views.py index 88d99fe..e265f04 100644 --- a/apps/operations/views.py +++ b/apps/operations/views.py @@ -122,6 +122,8 @@ class ShowAnnotationView(View): elif strategy.choice == 2: annotations = self.get_group_annotation(file_id, line_num, request.user.pk) + annotation_length = len(self.get_all_annotations(file_id,line_num)) + # all_anno_comment anno_comments = AnnotationComment.objects.filter(annotation__in=annotations) # anno_comments = sorted(anno_comments, key=lambda anno_comment: anno_comment.annotation_id) @@ -166,7 +168,7 @@ class ShowAnnotationView(View): # if self_annotation is not None: # annotations.filter(id=self_annotation.id).delete() - html_str = render_to_string('projects/filesub/annotation.html', {'linenum':line_num,'annos': annotations,"anno_comments":anno_comments,"self_annos":self_annotations,"allow_modify":allow_modify,"anno_vote_map":anno_vote_map,"comment_vote_map":comment_vote_map}) + html_str = render_to_string('projects/filesub/annotation.html', {'linenum':line_num,'annos': annotations,"anno_comments":anno_comments,"self_annos":self_annotations,"allow_modify":allow_modify,"anno_vote_map":anno_vote_map,"comment_vote_map":comment_vote_map,"annotation_length":annotation_length}) return HttpResponse(json.dumps({"status": "success","html_str":html_str}), content_type='application/json') # FIXME diff --git a/apps/utils/get_project_tree.py b/apps/utils/get_project_tree.py index 9d37bea..78aa602 100644 --- a/apps/utils/get_project_tree.py +++ b/apps/utils/get_project_tree.py @@ -77,7 +77,7 @@ class ProjectTree: def getHtml(self,path): - htmlstr = '
' + htmlstr = '