Work established
This commit is contained in:
parent
8f0a927c3b
commit
0ce5d07267
|
@ -1,2 +1,4 @@
|
|||
*.pyc
|
||||
*.pyo
|
||||
*sublime*
|
||||
python_environment/*
|
|
@ -1,5 +1,6 @@
|
|||
from django.conf.urls import patterns, url
|
||||
from . import views
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
#GET charts?userid=0&clientid=1
|
||||
#GET charts?userid=0&clientid=1&chartid=2
|
||||
|
@ -8,5 +9,5 @@ from . import views
|
|||
#POST charts?userid=0&clientid=1
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^charts$', views.doTheMagic),
|
||||
url(r'^charts$', csrf_exempt(views.doTheMagic)),
|
||||
)
|
||||
|
|
|
@ -1,14 +1,21 @@
|
|||
from django.http import HttpResponse
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from charts import models
|
||||
import json
|
||||
from datetime import datetime
|
||||
import time
|
||||
|
||||
def error(text):
|
||||
return HttpResponse(json.dumps({'status': 'error','message': text}))
|
||||
|
||||
@csrf_exempt
|
||||
def response(content):
|
||||
result = HttpResponse(content)
|
||||
result["Access-Control-Allow-Origin"] = "*"
|
||||
result["Access-Control-Allow-Methods"] = "GET, POST, DELETE, OPTIONS"
|
||||
return result
|
||||
|
||||
|
||||
def error(text):
|
||||
return response(json.dumps({'status': 'error','message': text}))
|
||||
|
||||
|
||||
def doTheMagic(request):
|
||||
clientId = request.GET.get('client', '')
|
||||
|
||||
|
@ -22,6 +29,9 @@ def doTheMagic(request):
|
|||
|
||||
chartId = request.GET.get('chart', '')
|
||||
|
||||
if request.method == 'OPTIONS':
|
||||
return respondToOptionsRequest(request.META)
|
||||
|
||||
if request.method == 'GET':
|
||||
if chartId == '':
|
||||
return getAllUserCharts(clientId, userId)
|
||||
|
@ -36,31 +46,34 @@ def doTheMagic(request):
|
|||
|
||||
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, content)
|
||||
return saveChart(clientId, userId, chartName, symbol, resoluion, content)
|
||||
else:
|
||||
return rewriteChart(clientId, userId, chartId, chartName, content)
|
||||
return rewriteChart(clientId, userId, chartId, chartName, symbol, resoluion, content)
|
||||
|
||||
else:
|
||||
return error('Wrong request')
|
||||
|
||||
|
||||
def response(content):
|
||||
result = HttpResponse(content)
|
||||
def respondToOptionsRequest(requestHeaders):
|
||||
result = response(json.dumps({'status': "ok"}))
|
||||
result["Access-Control-Allow-Headers"] = requestHeaders["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"]
|
||||
return result
|
||||
|
||||
|
||||
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())} , chartsList)
|
||||
result = map(lambda x : {'id': x.id, 'name': x.name, 'timestamp': time.mktime(x.lastModified.timetuple()), 'symbol': x.symbol, 'resolution': x.resolution} , chartsList)
|
||||
return response(json.dumps({'status': "ok", 'data': result}))
|
||||
|
||||
|
||||
def getChartContent(clientId, userId, chartId):
|
||||
try:
|
||||
chart = models.Chart.objects.get(ownerSource = clientId, ownerId = userId, id = chartId)
|
||||
result = json.dumps({'status': 'ok', 'id': chart.id, 'name': chart.name, 'timestamp': time.mktime(chart.lastModified.timetuple()), 'content': chart.content})
|
||||
result = json.dumps({'status': 'ok', 'data': { 'id': chart.id, 'name': chart.name, 'timestamp': time.mktime(chart.lastModified.timetuple()), 'content': chart.content}})
|
||||
return response(result)
|
||||
except:
|
||||
return error('Chart not found')
|
||||
|
@ -75,18 +88,29 @@ def removeChart(clientId, userId, chartId):
|
|||
return error('Chart not found')
|
||||
|
||||
|
||||
def saveChart(clientId, userId, chartName, content):
|
||||
newChart = models.Chart(ownerSource = clientId, ownerId = userId, name = chartName, content = content, lastModified = datetime.utcnow())
|
||||
def saveChart(clientId, userId, chartName, symbol, resolution, content):
|
||||
newChart = models.Chart(
|
||||
ownerSource = clientId,
|
||||
ownerId = userId,
|
||||
name = chartName,
|
||||
content = content,
|
||||
lastModified = datetime.utcnow(),
|
||||
symbol = symbol,
|
||||
resolution = resolution
|
||||
)
|
||||
|
||||
newChart.save()
|
||||
return response(json.dumps({'status': 'ok', 'id': newChart.id}))
|
||||
|
||||
|
||||
def rewriteChart(clientId, userId, chartId, chartName, content):
|
||||
def rewriteChart(clientId, userId, chartId, chartName, symbol, resoluion, content):
|
||||
try:
|
||||
chart = models.Chart.objects.get(ownerSource = clientId, ownerId = userId, id = chartId)
|
||||
chart.lastModified = datetime.utcnow()
|
||||
chart.content = content
|
||||
chart.name = chartName
|
||||
chart.symbol = symbol
|
||||
chart.resoluion = resoluion
|
||||
|
||||
chart.save()
|
||||
return response(json.dumps({'status': 'ok'}))
|
||||
|
|
|
@ -12,7 +12,7 @@ MANAGERS = ADMINS
|
|||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.postgresql_psycopg2',
|
||||
'NAME': 'tradingview',
|
||||
'NAME': 'charting_library_charts',
|
||||
'USER': 'postgres',
|
||||
'PASSWORD': '12345', ### Put your Postgres password here
|
||||
'HOST': 'localhost',
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('charts', '0002_auto_20141007_1601'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='chart',
|
||||
name='resolution',
|
||||
field=models.CharField(default='D', max_length=10),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='chart',
|
||||
name='symbol',
|
||||
field=models.CharField(default='AA', max_length=50),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='chart',
|
||||
name='lastModified',
|
||||
field=models.DateTimeField(),
|
||||
),
|
||||
]
|
|
@ -6,6 +6,8 @@ class Chart(models.Model):
|
|||
ownerSource = models.CharField(max_length=200)
|
||||
ownerId = models.CharField(max_length=200)
|
||||
name = models.CharField(max_length=200)
|
||||
symbol = models.CharField(max_length=50)
|
||||
resolution = models.CharField(max_length=10)
|
||||
lastModified = models.DateTimeField()
|
||||
content = JSONField()
|
||||
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
psycopg2
|
||||
jsonfield
|
Loading…
Reference in New Issue