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']
    templateName = request.GET.get('template', '')

    if request.method == 'GET':
        if templateName == '':
            return getAllTemplatesList(clientId, userId)
        else:
            return getTemplate(clientId, userId, templateName)

    elif request.method == 'DELETE':
        if templateName == '':
            return common.error('Wrong template id')
        else:
            return removeTemplate(clientId, userId, templateName)

    elif request.method == 'POST':
        templateName = request.POST.get('name')
        content = request.POST.get('content')
        return createOrUpdateTemplate(clientId, userId, templateName, content)

    else:
        return common.error('Wrong request')
Exemple #2
0
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')
Exemple #3
0
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')
Exemple #4
0
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')
Exemple #5
0
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')
Exemple #6
0
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')
Exemple #7
0
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']
    chartId = request.GET.get('chart', '')

    if request.method == 'GET':
        if chartId == '':
            return getAllUserCharts(clientId, userId)
        else:
            return getChartContent(clientId, userId, chartId)

    elif request.method == 'DELETE':
        if chartId == '':
            return common.error('Wrong chart id')
        else:
            return removeChart(clientId, userId, chartId)

    elif request.method == 'POST':
        chartName = request.POST.get('name')
        symbol = request.POST.get('symbol')
        resoluion = request.POST.get('resolution')
        content = request.POST.get('content')
        if chartId == '':
            return saveChart(clientId, userId, chartName, symbol, resoluion,
                             content)
        else:
            return rewriteChart(clientId, userId, chartId, chartName, symbol,
                                resoluion, content)

    else:
        return common.error('Wrong request')
Exemple #8
0
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')
Exemple #9
0
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')
Exemple #10
0
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')
Exemple #11
0
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')
Exemple #12
0
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']
    chartId = request.GET.get('chart', '')

    if request.method == 'GET':
        if chartId == '':
            return getAllUserCharts(clientId, userId)
        else:
            return getChartContent(clientId, userId, chartId)

    elif request.method == 'DELETE':
        if chartId == '':
            return common.error('Wrong chart id')
        else:
            return removeChart(clientId, userId, chartId)

    elif request.method == 'POST':
        chartName = request.POST.get('name')
        symbol = request.POST.get('symbol')
        resolution = request.POST.get('resolution')
        content = request.POST.get('content')

        if '|' in chartName:
            splittedName, receivedName = chartName.split('|')
            symbol = request.POST.get('symbol')
            resolution = request.POST.get('resolution')
            content = request.POST.get('content')
            sharedUserId = None
            sharedChartId = None
            sharedChartName = None
            if receivedName in userList:
                sharedUserId = userList[receivedName]
            if not (userId == sharedUserId):
                sharedName = useridList[userId]
                sharedChartName = "{}-{}".format(splittedName, sharedName)
            if chartId == '':
                sharedContent = content.replace(chartName, sharedChartName)
                sharedChartId = saveChart(clientId, sharedUserId,
                                          sharedChartName, symbol, resolution,
                                          sharedContent, [], [], True)
                return saveChart(clientId, userId, chartName, symbol,
                                 resolution, content, [sharedUserId],
                                 [sharedChartId], False)
            else:
                return rewriteChart(clientId, userId, chartId, chartName,
                                    symbol, resolution, content, sharedUserId,
                                    sharedChartId, sharedChartName)
        else:
            if chartId == '':
                return saveChart(clientId, userId, chartName, symbol,
                                 resolution, content, [], [], False)
            else:
                return rewriteChart(clientId, userId, chartId, chartName,
                                    symbol, resolution, content, None, None,
                                    None)

    else:
        return common.error('Wrong request')