From 0ce5d0726746282d519b8d712fe21161f4c31556 Mon Sep 17 00:00:00 2001 From: rlagutin Date: Thu, 9 Oct 2014 18:20:35 +0400 Subject: [PATCH] Work established --- .gitignore | 4 +- api/v10/urls.py | 3 +- api/v10/views.py | 50 +++++++++++++++----- charting_library_charts/settings.py | 2 +- charts/migrations/0003_auto_20141008_1252.py | 31 ++++++++++++ charts/models.py | 2 + requirements.txt | 2 + 7 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 charts/migrations/0003_auto_20141008_1252.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 7c621b4..feffed4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.pyc -*sublime* \ No newline at end of file +*.pyo +*sublime* +python_environment/* \ No newline at end of file diff --git a/api/v10/urls.py b/api/v10/urls.py index 8b4ae8d..388115c 100644 --- a/api/v10/urls.py +++ b/api/v10/urls.py @@ -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)), ) diff --git a/api/v10/views.py b/api/v10/views.py index ac3794d..8d22295 100644 --- a/api/v10/views.py +++ b/api/v10/views.py @@ -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'})) diff --git a/charting_library_charts/settings.py b/charting_library_charts/settings.py index 17b6ae0..d725822 100644 --- a/charting_library_charts/settings.py +++ b/charting_library_charts/settings.py @@ -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', diff --git a/charts/migrations/0003_auto_20141008_1252.py b/charts/migrations/0003_auto_20141008_1252.py new file mode 100644 index 0000000..9b88df0 --- /dev/null +++ b/charts/migrations/0003_auto_20141008_1252.py @@ -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(), + ), + ] diff --git a/charts/models.py b/charts/models.py index 29bd998..8b28fd6 100644 --- a/charts/models.py +++ b/charts/models.py @@ -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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4ffe5a4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +psycopg2 +jsonfield \ No newline at end of file