diff --git a/apps/operations/views.py b/apps/operations/views.py index 8a3dc2b..033127a 100644 --- a/apps/operations/views.py +++ b/apps/operations/views.py @@ -211,30 +211,33 @@ class ShowIssueQuestionView(View): else: return HttpResponse(json.dumps({"status": "success", "html_str": html_str})) +def get_navigation_content(project_path,file_path): + navigation_url = settings.OPENGROK_NAVIGATION_URL + navigation_url = navigation_url +project_path + file_path + response = requests.get(navigation_url).text + project_id = Project.objects.get(name=project_path) + file_id = File.objects.get(project_id=project_id,path=file_path).pk + response = response.replace("]],[","]]|[") + if response: + all_symbols = [] + for symbol in response.split("|"): + symbol = json.loads(symbol) + del symbol[1] + all_symbols.append(symbol) + return all_symbols,file_id + class ShowNavigationView(View): """ 获取当前文件的Structure,也就是Package,Class,Method信息。 """ def post(self, request): - navigation_url = settings.OPENGROK_NAVIGATION_URL - project_path = request.POST.get('project_path', '') - file_path = request.POST.get('file_path', '') - - navigation_url = navigation_url +project_path + file_path - response = requests.get(navigation_url).text - project_id = Project.objects.get(name=project_path) - file_id = File.objects.get(project_id=project_id,path=file_path).pk - response = response.replace("]],[","]]|[") - if response: - all_symbols = [] - for symbol in response.split("|"): - try: - symbol = json.loads(symbol) - del symbol[1] - all_symbols.append(symbol) - except: - return HttpResponse(json.dumps({"status": "failed", "msg": 'null'}), content_type='application/json') + try: + project_path = request.POST.get('project_path', '') + file_path = request.POST.get('file_path', '') + all_symbols,file_id = get_navigation_content(project_path,file_path) return HttpResponse(json.dumps({"status": "success", "msg": all_symbols,"file_id":file_id}), content_type='application/json') + except: + return HttpResponse(json.dumps({"status": "failed", "msg": 'null'}), content_type='application/json') else: return HttpResponse(json.dumps({"status": "failed", "msg": 'null'}), content_type='application/json') @@ -342,7 +345,7 @@ def get_currentfile_comment_anno(file,anno_content): def get_currentfile_language(file): if file.name.endswith(".py"): return "python" - elif file.name.endswith(".html") or file.name.endswith(".xml") : + elif file.name.endswith(".html"): return "html" elif file.name.endswith(".css"): return "css" @@ -356,7 +359,8 @@ def get_currentfile_language(file): return "markdown" elif file.name.endswith(".go"): return "go" - + else: + return "txt" @@ -888,6 +892,9 @@ def get_code(request,project_id,path): project = Project.objects.filter(id=project_id).first() file = File.objects.filter(path=path, project_id=project_id).first() + file.views += 1 + file.save() + fileid = file.pk annos = Annotation.objects.filter(file=file).values('linenum').annotate(nums=Count('linenum')) @@ -989,6 +996,37 @@ def get_project_info(project_id): html_str = render_to_string('projects/filesub/dir_info.html', locals()) return html_str +def get_files_method_lines(file_id): + file = File.objects.get(id=file_id) + file_path = file.path + project_path = file.project.name + lines=[] + try: + all_symbols,_ =get_navigation_content(project_path,file_path) + for i in range(len(all_symbols)): + symbol = all_symbols[i] + if symbol[0]=='Method' or symbol[0]=='Function': + for j in range(len(symbol[1])): + lines.append(symbol[1][j][1]) + return lines + except: + return lines + +def get_annoted_method_num(file_ids): + # 获取annoted_method + # 以file为基本单位 + # 1.获取method对应的行 + # 2.获取该行是否有注释 + annoted_method_num=0 + for i in range(len(file_ids)): + file_id = file_ids[i] + method_lines = get_files_method_lines(file_id) + for j in range(len(method_lines)): + current_line = method_lines[j] + if Annotation.objects.filter(linenum=current_line) is not None: + annoted_method_num = annoted_method_num + 1 + return annoted_method_num + # 调用api接口 def get_dir_info(project_id,path): # 获取当前文件的注释总数以及问题总数 @@ -1009,9 +1047,10 @@ def get_dir_info(project_id,path): fileid_questionnum = Question.objects.filter(file_id__in=file_ids).values('file_id').annotate(question_num=Count('file_id')) usersum = len(annos.values('user_id').annotate(user_num=Count('user_id'))) + annoted_method_num= get_annoted_method_num(file_ids) + anno_sum,issue_sum,question_sum=0,0,0 anno_filenum=0 - for i in range(len(fileid_annonum)): anno_filenum +=1 current_file_id = fileid_annonum[i]['file_id'] @@ -1028,9 +1067,12 @@ def get_dir_info(project_id,path): for i in range(len(fileid_questionnum)): question_sum += fileid_questionnum[i]['question_num'] + issue_question_sum = issue_sum + question_sum + anno_num = len(Annotation.objects.filter(file_id=file_id)) question_num = len(Question.objects.filter(file_id=file_id)) + current_file_summary = FileSummary.objects.get(project_id=project_id,current_path=path) line_sum = current_file_summary.line_num method_sum = current_file_summary.method_num @@ -1105,9 +1147,49 @@ def getFileLineNum(project_path,file_path): class RightView(View): - def get_file_right_content(self,project_path,file_path,file_id): - # 获取方法个数 + def get_dir_right_content(self,file_id): + isdir = True + file = File.objects.get(id=file_id) + project_id = file.project.id + path = file.path + anno_issue_summarys = FileSummary.objects.filter(project_id=project_id,parent_path=path) + #当前文件夹下所有的file_ids以及fileid与filename的映射 + file_ids = [] + for summary in anno_issue_summarys: + file_ids.append(summary.file_id) + + anno_num,issue_num,question_num=0,0,0 + + annos = Annotation.objects.filter(file_id__in=file_ids) + fileid_annonum = annos.values('file_id').annotate(anno_num=Count('file_id')) + for i in range(len(fileid_annonum)): + anno_num += fileid_annonum[i]['anno_num'] + + fileid_issuenum = Issue.objects.filter(file_id__in=file_ids).values('file_id').annotate(issue_num=Count('file_id')) + for i in range(len(fileid_issuenum)): + issue_num += fileid_issuenum[i]['issue_num'] + + fileid_questionnum = Question.objects.filter(file_id__in=file_ids).values('file_id').annotate(question_num=Count('file_id')) + for i in range(len(fileid_questionnum)): + question_num += fileid_questionnum[i]['question_num'] + + view_num = 0 + for i in range(len(file_ids)): + view_num += File.objects.get(id=file_ids[i]).views + + issue_question_num = issue_num + question_num + + current_file_summary = FileSummary.objects.get(project_id=project_id,current_path=path) + filelinenum = current_file_summary.line_num + create_time = file.create_time + file_num = current_file_summary.file_num + html_str = render_to_string('projects/filesub/file-right.html', locals()) + return html_str + + def get_file_right_content(self,project_path,file_path,file_id): + isdir = False + # 获取方法个数 method_num = getMethodNum(project_path,file_path); if method_num == -1: return "" @@ -1123,8 +1205,17 @@ class RightView(View): issue_num = len(Issue.objects.filter(file_id=file_id)) except: issue_num = 0 + try: + question_num = len(Question.objects.filter(file_id=file_id)) + except: + question_num = 0 + issue_question_num = issue_num + question_num + # 获得观看数 - view_num = 0 + file = File.objects.get(id=file_id) + view_num = file.views + create_time = file.create_time + # 观看数应该放在File_id中 html_str = render_to_string('projects/filesub/file-right.html', locals()) return html_str @@ -1137,6 +1228,11 @@ class RightView(View): project_path = Project.objects.get(id=project_id).path file = File.objects.get(project_id=project_id,path=file_path) - html_str = self.get_file_right_content(project_path,file_path,file.pk) + isDir = (len(FileSummary.objects.filter(project_id=project_id,parent_path=file_path))>0) + if isDir: + html_str = self.get_dir_right_content(file.id) + else: + html_str = self.get_file_right_content(project_path,file_path,file.pk) + return HttpResponse(json.dumps({"status": "success", "html_str": html_str}), content_type='application/json') \ No newline at end of file diff --git a/templates/projects/filesub/dir_info.html b/templates/projects/filesub/dir_info.html index eb33c6d..cc259ab 100644 --- a/templates/projects/filesub/dir_info.html +++ b/templates/projects/filesub/dir_info.html @@ -33,39 +33,40 @@
File Summary
+ {% if isdir %} +Module Summary
+ {% else %} +File Summary
+ {% endif %}@@ -31,21 +35,27 @@
Created: - 2018/06/06 + {{create_time}}