def removeTemplate(clientId, userId, name): try: item = models.StudyTemplate.objects.get(ownerSource = clientId, ownerId = userId, name = name) item.delete() return common.response(json.dumps({'status': 'ok'})) except: return common.error('StudyTemplate not found')
def createOrUpdateTemplate(clientId, userId, name, content): newItem, created = models.StudyTemplate.objects.get_or_create(ownerSource=clientId, ownerId=userId, name=name) newItem.content = content newItem.save() return common.response(json.dumps({'status': 'ok'}))
def getTemplate(clientId, userId, name): try: item = models.StudyTemplate.objects.get(ownerSource = clientId, ownerId = userId, name = name) result = json.dumps({'status': 'ok', 'data': { 'name': item.name, 'content': item.content}}) return common.response(result) except: return common.error('StudyTemplate not found')
def removeChart(clientId, userId, chartId): try: chart = models.Chart.objects.get(ownerSource=clientId, ownerId=userId, id=chartId) chart.delete() return common.response(json.dumps({'status': 'ok'})) except: return common.error('Chart 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')
def saveChart(clientId, userId, chartName, symbol, resolution, content): newChart = models.Chart(ownerSource=clientId, ownerId=userId, name=chartName, content=content, lastModified=datetime.now(), symbol=symbol, resolution=resolution) newChart.save() return common.response(json.dumps({'status': 'ok', 'id': newChart.id}))
def getAllUserCharts(clientId, userId): chartsList = models.Chart.objects.filter(ownerSource=clientId, ownerId=userId) result = map( lambda x: { 'id': x.id, 'name': x.name, 'timestamp': time.mktime(x.lastModified.timetuple()), 'symbol': x.symbol, 'resolution': x.resolution }, chartsList) return common.response(json.dumps({'status': "ok", 'data': list(result)}))
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 rewriteChart(clientId, userId, chartId, chartName, symbol, resolution, content): try: chart = models.Chart.objects.get(ownerSource=clientId, ownerId=userId, id=chartId) chart.lastModified = datetime.now() chart.content = content chart.name = chartName chart.symbol = symbol chart.resolution = resolution chart.save() return common.response(json.dumps({'status': 'ok'})) except: return common.error('Chart not found')
def removeChart(clientId, userId, chartId): try: chart = models.Chart.objects.get(ownerSource=clientId, ownerId=userId, id=chartId) if len(chart.sharedUserId) > 0 and len(chart.sharedChartId) > 0: for childUserId, childChartId in zip(chart.sharedUserId, chart.sharedChartId): chartChild = models.Chart.objects.get(ownerSource=clientId, ownerId=childUserId, id=childChartId) chartChild.delete() chart.delete() return common.response(json.dumps({'status': 'ok'})) except: return common.error('Chart not found')
def getChartContent(clientId, userId, chartId): try: chart = models.Chart.objects.get(ownerSource=clientId, ownerId=userId, id=chartId) result = json.dumps({ 'status': 'ok', 'data': { 'id': chart.id, 'name': chart.name, 'timestamp': time.mktime(chart.lastModified.timetuple()), 'content': chart.content } }) return common.response(result) except: return common.error('Chart not found')
def rewriteChart(clientId, userId, chartId, chartName, symbol, resolution, content, sharedUserId=None, sharedChartId=None, sharedChartName=None): try: chart = models.Chart.objects.get(ownerSource=clientId, ownerId=userId, id=chartId) chart.lastModified = datetime.now() chart.content = content chart.name = chartName chart.symbol = symbol chart.resolution = resolution if (sharedUserId and sharedChartId) and ( sharedUserId not in chart.sharedUserId) and (sharedChartId not in chart.sharedChartId): chart.sharedUserId.append(sharedUserId) chart.sharedChartId.append(sharedChartId) if sharedChartName and len(chart.sharedUserId) > 0 and len( chart.sharedChartId) > 0: for childUserId, childChartId in zip(chart.sharedUserId, chart.sharedChartId): chartChild = models.Chart.objects.get(ownerSource=clientId, ownerId=childUserId, id=childChartId) sharedContent = content.replace(chartName, sharedChartName) chartChild.lastModified = datetime.now() chartChild.content = sharedContent chartChild.name = sharedChartName chartChild.save() chart.save() return common.response(json.dumps({'status': 'ok'})) except: return common.error('Chart not found')
def getAllTemplatesList(clientId, userId): items = models.StudyTemplate.objects.defer('content').filter( ownerSource=clientId, ownerId=userId) result = map(lambda x: {'name': x.name}, items) return common.response(json.dumps({'status': "ok", 'data': list(result)}))