Add Drawing Templates functionality

This commit is contained in:
Marina Moshnogorskaya 2020-07-16 19:48:20 +03:00
parent 7dc769ab6a
commit 1417cb3878
5 changed files with 143 additions and 0 deletions

View File

@ -0,0 +1,83 @@
from django.http import HttpResponse
import json
import time
from datetime import datetime
from model import models
from api.v11 import common
def processRequest(request):
parsedRequest = common.parseRequest(request)
if parsedRequest['error'] is not None:
return parsedRequest['error']
if parsedRequest['response'] is not None:
return parsedRequest['response']
clientId = parsedRequest["clientId"]
userId = parsedRequest['userId']
name = request.GET.get('name', '')
tool = request.GET.get('tool', '')
if request.method == 'GET':
if name == '':
return getTemplates(clientId, userId, tool)
else:
return getTemplate(clientId, userId, tool, name)
elif request.method == 'POST':
content = request.POST.get('content', '')
return createOrUpdateTemplate(clientId, userId, name, tool, content)
elif request.method == 'DELETE':
if name == '':
return common.error('Wrong template id')
else:
return removeTemplate(clientId, userId, tool, name)
else:
return common.error('Wrong request')
#-----------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------
def getTemplates(clientId, userId, tool):
try:
items = models.DrawingTemplate.objects.defer('content').filter(ownerSource = clientId, ownerId = userId, tool = tool)
result = map(lambda x : x.name, items)
return common.response(json.dumps({'status': "ok", 'data': list(result)}))
except:
return common.error('Error loading Drawing Templates')
def getTemplate(clientId, userId, tool, name):
try:
item = models.DrawingTemplate.objects.get(ownerSource = clientId, ownerId = userId, tool = tool, name = name)
result = json.dumps({'status': 'ok', 'data': { 'name': item.name, 'content': item.content}})
return common.response(result)
except:
return common.error('Drawing Template not found')
def removeTemplate(clientId, userId, tool, name):
try:
item = models.DrawingTemplate.objects.get(ownerSource = clientId, ownerId = userId, tool = tool, name = name)
item.delete()
return common.response(json.dumps({'status': 'ok'}))
except:
return common.error('Drawing Template not found')
def createOrUpdateTemplate(clientId, userId, name, tool, content):
try:
newItem, created = models.DrawingTemplate.objects.get_or_create(ownerSource=clientId, ownerId=userId, name=name, tool=tool)
newItem.content = content
newItem.save()
return common.response(json.dumps({'status': 'ok'}))
except:
return common.error('Error updating Drawing Template')

View File

@ -1,5 +1,6 @@
from django.conf.urls import url
from api.v11 import studyTemplates
from api.v11 import drawingTemplates
from api.v11 import charts
from django.views.decorators.csrf import csrf_exempt
@ -7,4 +8,5 @@ from django.views.decorators.csrf import csrf_exempt
urlpatterns = [
url(r'^charts$', csrf_exempt(charts.processRequest)),
url(r'^study_templates$', csrf_exempt(studyTemplates.processRequest)),
url(r'^drawing_templates$', csrf_exempt(drawingTemplates.processRequest)),
]

View File

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2020-07-14 09:28
from __future__ import unicode_literals
from django.db import migrations, models
import jsonfield.fields
class Migration(migrations.Migration):
dependencies = [
('model', '0006_study_templates_indexes'),
]
operations = [
migrations.CreateModel(
name='DrawingTemplate',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('ownerSource', models.CharField(db_index=True, max_length=200)),
('ownerId', models.CharField(db_index=True, max_length=200)),
('name', models.CharField(max_length=200)),
('content', jsonfield.fields.JSONField()),
],
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 2.2.13 on 2020-07-15 19:25
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('model', '0007_drawingtemplate'),
]
operations = [
migrations.AddField(
model_name='drawingtemplate',
name='tool',
field=models.CharField(default='LineTool', max_length=200),
preserve_default=False,
),
]

View File

@ -29,3 +29,16 @@ class StudyTemplate(models.Model):
def setContent(self, _content):
self.content = _content
class DrawingTemplate(models.Model):
ownerSource = models.CharField(max_length=200, db_index=True)
ownerId = models.CharField(max_length=200, db_index=True)
name = models.CharField(max_length=200)
tool = models.CharField(max_length=200)
content = JSONField()
def __str__(self):
return self.ownerSource + ":" + self.ownerId
def setContent(self, _content):
self.content = _content