Skip to content

Commit

Permalink
完成实体的添加、修改、删除、查询以及任务创建功能, close #59, #55, #56
Browse files Browse the repository at this point in the history
  • Loading branch information
Pupil59 committed May 19, 2020
1 parent 926ac48 commit d768d5e
Show file tree
Hide file tree
Showing 11 changed files with 381 additions and 135 deletions.
183 changes: 105 additions & 78 deletions textMark/.idea/workspace.xml

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions textMark/common/migrations/0002_auto_20200511_2155.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 3.0.5 on 2020-05-11 13:55

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('common', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AddField(
model_name='project',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='entity',
name='project',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='common.Project'),
),
]
Binary file modified textMark/db.sqlite3
Binary file not shown.
1 change: 1 addition & 0 deletions textMark/project/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def modifyentity(request):
'ret': 1,
'msg': f'实体名在项目中已经存在,添加者为{username}'
})

entity.name = newdata['name']

# 注意,一定要执行save才能将修改信息保存到数据库
Expand Down
2 changes: 1 addition & 1 deletion textMark/project/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def modifyrelation(request):
newdata = request.params['newdata']

try:
relation = Entity.objects.get(id=rid)
relation = Relation.objects.get(id=rid)
except Entity.DoesNotExist:
return {
'ret': 1,
Expand Down
131 changes: 82 additions & 49 deletions textMark/project/tests_entity.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,83 @@
from rest_framework.test import APIClient, APITestCase
from rest_framework.test import APIClient, APITestCase
from common.models import Entity
from common.models import Project
from register.models import big_user
from django.db.models import Count
import json

class entityTests(APITestCase):

class entityTests(APITestCase):
def setUp(self):
self.client = APIClient()
self.assertEqual(big_user.objects.count(), 0)
# 注册
response = self.client.post(
path= '/user/register/',
path='/user/register/',
data=
{
'user_id': '3100',
'password': '123456',
'user_name': 'dz'
}
)

self.assertEqual(response.status_code, 200)
response_content = json.loads(response.content)
self.assertEqual(big_user.objects.count(), 1)
self.assertEqual(response_content['msg'], '创建成功')
data= {
'action':'add_project',
self.assertEqual(response_content['msg'], '创建成功')

data = {
'action': 'add_project',
'data': {
'name':'project1'
'name': 'project1'
}
}

response = self.client.post(
path='/user/projects',
data=json.dumps(data),
content_type= 'application/json'
content_type='application/json'
)
self.assertEqual(response.status_code, 200)
self.assertEqual(Project.objects.count(), 1)
project_id = (json.loads(response.content))['id']


data={

data = {
'project_id': project_id
}
response = self.client.post(
path= '/api/project/pro_session',
path='/api/project/pro_session',
data=json.dumps(data),
content_type= 'application/json'
content_type='application/json'
)

def test_add_entity_duplicate(self):
# 添加实体1
self.assertEqual(Entity.objects.count(), 0)
data= {
'action':'add_entity',
data = {
'action': 'add_entity',
'data': {
'name':'entity1'
'name': 'entity1'
}
}
response = self.client.post(
path='/api/project/entities',
data= json.dumps(data),
content_type= 'application/json'
data=json.dumps(data),
content_type='application/json'
)
self.assertEqual(Entity.objects.count(), 1)

# 添加实体2
data= {
'action':'add_entity',
data = {
'action': 'add_entity',
'data': {
'name':'entity2'
'name': 'entity2'
}
}
response = self.client.post(
path='/api/project/entities',
data= json.dumps(data),
content_type= 'application/json'
data=json.dumps(data),
content_type='application/json'
)
self.assertEqual(Entity.objects.count(), 2)

Expand All @@ -87,81 +86,115 @@ def test_add_entity_duplicate(self):
path='/api/project/entities',
data=
{
'action':'list_entity'
'action': 'list_entity'
}
)

response_content = json.loads(response.content)
self.assertEqual(len(response_content['retlist']), 2)
self.assertEqual(len(response_content['retlist']), 2)

# 添加实体--重名
data= {
'action':'add_entity',
data = {
'action': 'add_entity',
'data': {
'name':'entity2'
'name': 'entity2'
}
}
response = self.client.post(
path='/api/project/entities',
data= json.dumps(data),
content_type= 'application/json'
data=json.dumps(data),
content_type='application/json'
)
self.assertEqual(Entity.objects.count(), 2)

def test_del_entity(self):
# 添加实体1
self.assertEqual(Entity.objects.count(), 0)
data= {
'action':'add_entity',
data = {
'action': 'add_entity',
'data': {
'name':'entity1'
'name': 'entity1'
}
}
response = self.client.post(
path='/api/project/entities',
data= json.dumps(data),
content_type= 'application/json'
data=json.dumps(data),
content_type='application/json'
)
self.assertEqual(Entity.objects.count(), 1)
entity_id_1 = (json.loads(response.content))['id']

# 添加实体2
data= {
'action':'add_entity',
data = {
'action': 'add_entity',
'data': {
'name':'entity2'
'name': 'entity2'
}
}
response = self.client.post(
path='/api/project/entities',
data= json.dumps(data),
content_type= 'application/json'
data=json.dumps(data),
content_type='application/json'
)
self.assertEqual(Entity.objects.count(), 2)
entity_id_2 = (json.loads(response.content))['id']

# 删除实体1
data= {
data = {
'action': 'del_entity',
'id': entity_id_1
}
self.client.post(
path='/api/project/entities',
data= json.dumps(data),
content_type= 'application/json'
data=json.dumps(data),
content_type='application/json'
)
self.assertEqual(Entity.objects.count(), 1)
self.assertEqual((json.loads(response.content))['ret'], 0)

# 删除实体2
data= {
data = {
'action': 'del_entity',
'id': entity_id_2
}
self.client.post(
path='/api/project/entities',
data= json.dumps(data),
content_type= 'application/json'
data=json.dumps(data),
content_type='application/json'
)
self.assertEqual(Entity.objects.count(), 0)
self.assertEqual((json.loads(response.content))['ret'], 0)

def test_modify_entity(self):
self.assertEqual(Entity.objects.count(), 0)
# 添加实体1
data = {
'action': 'add_entity',
'data': {
'name': 'entity1'
}
}
response = self.client.post(
path='/api/project/entities',
data=json.dumps(data),
content_type='application/json'
)
self.assertEqual(Entity.objects.count(), 1)
entity_id_1 = (json.loads(response.content))['id']

# 修改实体1
data = {
'action': 'modify_entity',
'id': entity_id_1,
'newdata': {
'name': '实体2'
}
}
self.client.post(
path='/api/project/entities',
data=json.dumps(data),
content_type='application/json'
)
self.assertEqual(Entity.objects.count(), 1)
self.assertEqual(Entity.objects.get(id=entity_id_1).name, '实体2')
self.assertEqual((json.loads(response.content))['ret'], 0)
79 changes: 79 additions & 0 deletions textMark/project/text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# from django.http import JsonResponse
# import json
# from django.core.files.storage import default_storage
# from django.core.files.base import ContentFile
#
# from common.models import Text
#
#
# def dispatcher(request):
# # 将请求参数统一放入request 的 params 属性中,方便后续处理
# if request.user.is_authenticated():
# return JsonResponse({
# 'ret': 302,
# 'msg': '未登录'},
# status=302)
#
# # GET请求 参数在url中,同过request 对象的 GET属性获取
# if request.method == 'GET':
# request.params = request.GET
#
# # POST/PUT/DELETE 请求 参数 从 request 对象的 body 属性中获取
# elif request.method in ['POST', 'DELETE']:
# # 根据接口,POST/PUT/DELETE 请求的消息体都是 json格式
# request.params = json.loads(request.body)
#
# # 根据不同的action分派给不同的函数进行处理
# action = request.params['action']
# if action == 'list_text':
# return listtexts(request)
# elif action == 'add_text':
# return addtext(request)
# elif action == 'del_text':
# return deltext(request)
#
# else:
# return JsonResponse({'ret': 1, 'msg': '不支持该类型http请求'})
#
#
# def listtexts(request):
# # 返回一个 QuerySet 对象 ,包含所有的表记录
# pid = request.params['project_id']
# qs = Text.objects.filter(project_id=pid).values('id', 'name', 'text')
#
# # 将 QuerySet 对象 转化为 list 类型
# retlist = list(qs)
#
# return JsonResponse({'ret': 0, 'retlist': retlist})
#
#
# def addtext(request):
# info = request.params['data']
#
# text = request.Files.get('text')
#
# # path = default_storage.save('')
#
# record = Text.objects.create(name=text.name,
# text=text,
# project_id=info['project_id'])
#
# return JsonResponse({'ret': 0, 'id': record.id})
#
#
# def deltext(request):
# textid = request.params['id']
#
# try:
# # 根据 id 从数据库中找到相应的客户记录
# text = Text.objects.get(id=textid)
# except text.DoesNotExist:
# return {
# 'ret': 1,
# 'msg': f'id 为`{textid}`的文本不存在'
# }
#
# # delete 方法就将该记录从数据库中删除了
# text.delete()
#
# return JsonResponse({'ret': 0})
4 changes: 4 additions & 0 deletions textMark/register/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

class big_user(AbstractUser):
name = models.CharField(max_length=40)
friends = set()
friend_apply = set()
project_invite = set()
fri_pro = set()



Expand Down
Loading

0 comments on commit d768d5e

Please sign in to comment.