upload ill
This commit is contained in:
@@ -1,3 +1,14 @@
|
||||
from django.contrib import admin
|
||||
from .models import *
|
||||
# Register your models here.
|
||||
|
||||
admin.site.register(User)
|
||||
admin.site.register(Doctor)
|
||||
admin.site.register(Patient)
|
||||
admin.site.register(Pictures)
|
||||
admin.site.register(Message)
|
||||
admin.site.register(History)
|
||||
admin.site.register(PictureTable)
|
||||
|
||||
|
||||
# Register your models here.
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
from time import time
|
||||
|
||||
from django.http import HttpResponseNotFound, HttpResponse, JsonResponse
|
||||
@@ -28,3 +30,50 @@ def picture(request):
|
||||
except Pictures.DoesNotExist:
|
||||
return HttpResponseNotFound()
|
||||
return HttpResponse(img.pic, content_type='image/png')
|
||||
|
||||
@csrf_exempt
|
||||
def uploadill(request):
|
||||
post_data = json.loads(request.body.decode('utf-8'))
|
||||
must_contains = ['name', 'age', 'gender', 'ill', 'info', 'wechat']
|
||||
for x in must_contains:
|
||||
if x not in post_data:
|
||||
return JsonResponse({"error": "no {}".format(x)})
|
||||
wechat = User.objects.get(openid=post_data['wechat'])
|
||||
try:
|
||||
patient = Patient.objects.get(wechat=wechat)
|
||||
except Patient.DoesNotExist:
|
||||
patient = Patient()
|
||||
patient.wechat = wechat
|
||||
wechat.role = 2
|
||||
wechat.save()
|
||||
|
||||
patient_fields = patient.json().keys()
|
||||
for x in patient_fields:
|
||||
if x in post_data and post_data[x] != getattr(patient, x):
|
||||
setattr(patient, x, post_data[x])
|
||||
patient.save()
|
||||
|
||||
history = History()
|
||||
history.patient = patient
|
||||
history.ill = post_data['ill']
|
||||
history.info = post_data['info']
|
||||
history.doctor = None
|
||||
history.diag_time = None
|
||||
history.send_time = datetime.now()
|
||||
history.rank = 0
|
||||
history.save()
|
||||
|
||||
if 'pics' in post_data:
|
||||
for pic in post_data['pics']:
|
||||
try:
|
||||
p = Pictures.objects.get(id=pic)
|
||||
except Pictures.DoesNotExist:
|
||||
return JsonResponse({'error': 'picture id {} does not exist.'.format(pic)})
|
||||
pt = PictureTable()
|
||||
pt.pic = p
|
||||
pt.history = history
|
||||
pt.save()
|
||||
|
||||
return JsonResponse({'success': True})
|
||||
|
||||
|
||||
|
||||
+22
-21
@@ -1,3 +1,4 @@
|
||||
import uuid as uuid
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
@@ -20,16 +21,16 @@ class User(models.Model):
|
||||
|
||||
|
||||
class Doctor(models.Model):
|
||||
id = models.IntegerField(primary_key=True, auto_created=True)
|
||||
uuid = models.UUIDField()
|
||||
id = models.AutoField(primary_key=True, auto_created=True)
|
||||
uuid = models.UUIDField(auto_created=True, default=uuid.uuid4)
|
||||
did = models.TextField(unique=True, db_index=True)
|
||||
name = models.TextField()
|
||||
gender = models.IntegerField(default=0) # 1: male, 2: female
|
||||
rank = models.IntegerField(default=1)
|
||||
credits = models.IntegerField(default=0)
|
||||
wechat = models.ForeignKey(User, on_delete='SET_NULL')
|
||||
wechat = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
|
||||
|
||||
def info(self):
|
||||
def json(self):
|
||||
d = {
|
||||
'id': self.id,
|
||||
'uuid': self.uuid,
|
||||
@@ -43,17 +44,17 @@ class Doctor(models.Model):
|
||||
|
||||
|
||||
class Patient(models.Model):
|
||||
id = models.IntegerField(primary_key=True, auto_created=True)
|
||||
uuid = models.UUIDField()
|
||||
id = models.AutoField(primary_key=True, auto_created=True)
|
||||
uuid = models.UUIDField(auto_created=True, default=uuid.uuid4)
|
||||
pid = models.TextField(unique=True, db_index=True)
|
||||
name = models.TextField()
|
||||
age = models.IntegerField()
|
||||
gender = models.IntegerField() # 1: male, 2: female
|
||||
wechat = models.ForeignKey(User, on_delete='SET_NULL')
|
||||
wechat = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
|
||||
location = models.TextField()
|
||||
phone = models.TextField(blank=True, null=True)
|
||||
|
||||
def info(self):
|
||||
def json(self):
|
||||
d = {
|
||||
'id': self.id,
|
||||
'uuid': self.uuid,
|
||||
@@ -73,8 +74,8 @@ class Pictures(models.Model):
|
||||
|
||||
|
||||
class Message(models.Model):
|
||||
id = models.IntegerField(primary_key=True, auto_created=True)
|
||||
uuid = models.UUIDField()
|
||||
id = models.AutoField(primary_key=True, auto_created=True)
|
||||
uuid = models.UUIDField(auto_created=True, default=uuid.uuid4)
|
||||
time = models.DateTimeField()
|
||||
sender = models.TextField(db_index=True)
|
||||
receiver = models.TextField(db_index=True)
|
||||
@@ -82,22 +83,22 @@ class Message(models.Model):
|
||||
|
||||
|
||||
class History(models.Model):
|
||||
id = models.IntegerField(primary_key=True, auto_created=True)
|
||||
patient = models.ForeignKey(Patient, on_delete='SET_NULL')
|
||||
id = models.AutoField(primary_key=True, auto_created=True)
|
||||
patient = models.ForeignKey(Patient, on_delete=models.SET_NULL, null=True)
|
||||
ill = models.TextField()
|
||||
state = models.TextField()
|
||||
doctor = models.ForeignKey(to=Doctor, on_delete='SET_NULL', null=True)
|
||||
diag_time = models.DateTimeField()
|
||||
info = models.TextField()
|
||||
doctor = models.ForeignKey(to=Doctor, on_delete=models.SET_NULL, null=True)
|
||||
diag_time = models.DateTimeField(null=True)
|
||||
send_time = models.DateTimeField()
|
||||
rank = models.IntegerField()
|
||||
|
||||
def info(self):
|
||||
def json(self):
|
||||
d = {
|
||||
'id': self.id,
|
||||
'ill': self.ill,
|
||||
'state': self.state,
|
||||
'doctor': self.doctor.info(),
|
||||
'patient': self.patient.info(),
|
||||
'info': self.info,
|
||||
'doctor': None if self.doctor is None else self.doctor.json(),
|
||||
'patient': self.patient.json(),
|
||||
'send_time': str(self.send_time.time()),
|
||||
'diag_time': str(self.diag_time.time()),
|
||||
'rank': self.rank
|
||||
@@ -106,5 +107,5 @@ class History(models.Model):
|
||||
|
||||
|
||||
class PictureTable(models.Model):
|
||||
pic = models.ForeignKey(Pictures, on_delete='SET_NULL')
|
||||
patient = models.ForeignKey(History, on_delete='SET_NULL')
|
||||
pic = models.ForeignKey(Pictures, on_delete=models.SET_NULL, null=True)
|
||||
history = models.ForeignKey(History, on_delete=models.SET_NULL, null=True)
|
||||
|
||||
+33
-2
@@ -1,3 +1,34 @@
|
||||
from django.test import TestCase
|
||||
import json
|
||||
import os
|
||||
from unittest import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
import django
|
||||
from django.test import Client
|
||||
|
||||
from .models import *
|
||||
import requests
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "website.settings")
|
||||
django.setup()
|
||||
url = 'http://127.0.0.1/hospital/'
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
|
||||
class Test(TestCase):
|
||||
def setUp(self):
|
||||
u = User()
|
||||
u.openid = 123
|
||||
u.save()
|
||||
|
||||
def test_uploadill(self):
|
||||
client = Client()
|
||||
req = client.post(url + 'ill', data={
|
||||
'pid': "患者编号xx001",
|
||||
'name': "张三",
|
||||
'age': 42,
|
||||
'gender': 1,
|
||||
'ill': "高血压",
|
||||
'wechat': "123",
|
||||
'info': "患者状态",
|
||||
'location': "沈阳市火石桥村1号",
|
||||
'pics': [101, 12, 23]
|
||||
}, content_type='application/json')
|
||||
self.assertEqual(req.content, b'123')
|
||||
|
||||
+2
-1
@@ -4,5 +4,6 @@ from hospital import views, func
|
||||
|
||||
urlpatterns = [
|
||||
path('index', views.index),
|
||||
path('picture', func.picture)
|
||||
path('picture', func.picture),
|
||||
path('ill', func.uploadill)
|
||||
]
|
||||
@@ -7,17 +7,11 @@
|
||||
<body>
|
||||
<h1>在线医疗系统</h1>
|
||||
<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
|
||||
|
||||
<fieldset>
|
||||
|
||||
<legend>上传文件</legend>
|
||||
|
||||
<input type="file" name="img" />
|
||||
|
||||
<input type="submit" value="上传"/>
|
||||
|
||||
</fieldset>
|
||||
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user