add commit_id but error log havent complete

This commit is contained in:
wrmswindmill 2018-08-14 09:03:07 +08:00
parent c03b7adecf
commit 8aea0454af
6 changed files with 1797 additions and 6 deletions

View File

@ -0,0 +1,19 @@
# Generated by Django 2.0.6 on 2018-08-14 08:37
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('operations', '0012_auto_20180808_1031'),
]
operations = [
migrations.AddField(
model_name='annotation',
name='commit_id',
field=models.CharField(default=None, max_length=255),
preserve_default=False,
),
]

View File

@ -65,6 +65,7 @@ class Annotation(models.Model):
is_latest = models.IntegerField(default=0, verbose_name='是最新的注释吗')
create_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
commit_id = models.CharField(max_length=255)
class Meta:
db_table = 'Annotation'
@ -239,7 +240,7 @@ class AnnotationComment(models.Model):
annotation = models.ForeignKey(Annotation, verbose_name='注释', related_name='annotation_comment',on_delete=models.CASCADE)
voteup = models.IntegerField(default=0, verbose_name='点赞数量')
votedown = models.IntegerField(default=0, verbose_name='点踩数量')
create_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)

View File

@ -36,15 +36,25 @@ class ShowAnnotationView(View):
策略例如只显示只显示自己的Annotation
分为多个策略吧
"""
def get_current_commitid_by_fileid(self,file_id):
try:
project_name = File.objects.get(id=file_id).project.name
return git.get_current_commitid_by_projectname(project_name)
except:
# 打BUG日志
return None
# 策略1获取所有用户的Annotation
def get_all_annotations(self,file_id,line_num):
annotations = Annotation.objects.filter(file_id=file_id, linenum=line_num,is_latest=1)
# 获取到当前commit_id对应的注释
commit_id = self.get_current_commitid_by_fileid(file_id)
annotations = Annotation.objects.filter(file_id=file_id, linenum=line_num,is_latest=1,commit_id=commit_id)
return annotations
# 策略2只获取用户自己的Annotation
def get_user_annotation(self,file_id,line_num,user_id):
annotations = Annotation.objects.filter(file_id=file_id, linenum=line_num,user_id=user_id,is_latest=1)
commit_id = self.get_current_commitid_by_fileid(file_id)
annotations = Annotation.objects.filter(file_id=file_id, linenum=line_num,user_id=user_id,is_latest=1,commit_id=commit_id)
return annotations
# FIXME
@ -54,7 +64,8 @@ class ShowAnnotationView(View):
# 首先获取当前组的所有用户id
user_ids = []
# 然后调用
annotations = Annotation.objects.filter(file_id=file_id, linenum=line_num,user_id__in=user_ids,is_latest=1)
commit_id = self.get_current_commitid_by_fileid(file_id)
annotations = Annotation.objects.filter(file_id=file_id, linenum=line_num,user_id__in=user_ids,is_latest=1,commit_id=commit_id)
return annotations
def post(self, request):
@ -239,6 +250,7 @@ class ShowMethodInfo(View):
html_str = render_to_string('projects/filesub/search-response.html', {'results': results, 'project_id': project.pk, })
return HttpResponse(json.dumps({"status": "success", "html_str":html_str}), content_type='application/json')
from utils import git
# FIXME
# 还没有获取用户id需要从request中获取用户id
class AddAnnotationView(View):
@ -267,12 +279,15 @@ class AddAnnotationView(View):
annotation.content = content
annotation.user = request.user
annotation.is_latest = 1
# 获取到当前的commit_id
project_name = File.objects.get(id=file_id).project.name
commit_id = git.get_current_commitid_by_projectname(project_name)
annotation.commit_id = commit_id
annotation.save()
# 因为没有办法得到annotation.id,所以只能先存后查了
annotation = Annotation.objects.get(file_id=file_id, linenum=linenum, user_id=request.user.id)
annotation.anno_id = annotation.pk
annotation.save()
return HttpResponse('{"status":"success","msg":"注释成功"}', content_type='application/json')
except Exception as e:
return HttpResponse('{"status":"fail","msg":"参数传递错误,注释失败"}', content_type='application/json')

View File

@ -18,7 +18,6 @@ class Language(models.Model):
def __str__(self):
return self.name
class Project(models.Model):
STATE_CHOICE = [
('1', '正常'),

26
apps/utils/git.py Normal file
View File

@ -0,0 +1,26 @@
from django.conf import settings
import os
import subprocess
LOCAL_SOURCE_PATH = settings.SOURCEPATH
# FIXME
# 添加日志
def get_current_commitid_by_projectname(project_name):
path = LOCAL_SOURCE_PATH+project_name
if os.path.exists(path):
os.chdir(path)
try:
cmd = "git rev-parse HEAD"
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
result = p.stdout.readline()
# 返回值为b''编码格式的转换为utf-8
return result[0:-1].decode(encoding='utf-8', errors='strict')
except:
# 可能有几个exception
# 第一个是Popen执行不成功
# 第二个是p.stdout返回值为None
# write into bug log
error_msg ="cant get the commit_id"

1731
nohup.out

File diff suppressed because it is too large Load Diff