diff options
-rw-r--r-- | requirements.txt | 2 | ||||
-rw-r--r-- | soc/settings.py | 5 | ||||
-rw-r--r-- | soc/urls.py | 5 | ||||
-rw-r--r-- | static/website/css/main.css | 12 | ||||
-rw-r--r-- | static/website/js/cloud.js | 134 | ||||
-rw-r--r-- | static/website/templates/ajax-books.html | 3 | ||||
-rw-r--r-- | static/website/templates/ajax-chapters.html | 1 | ||||
-rw-r--r-- | static/website/templates/ajax-examples.html | 1 | ||||
-rw-r--r-- | static/website/templates/ajax-execute.html | 4 | ||||
-rw-r--r-- | static/website/templates/index.html | 42 | ||||
-rw-r--r-- | website/ajax.py | 82 | ||||
-rw-r--r-- | website/helpers.py | 4 | ||||
-rw-r--r-- | website/urls.py | 7 | ||||
-rw-r--r-- | website/views.py | 72 |
14 files changed, 177 insertions, 197 deletions
diff --git a/requirements.txt b/requirements.txt index 387c763..163942c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,6 @@ Django==1.5 MySQL-python==1.2.4 argparse==1.2.1 distribute==0.7.3 +django-dajax==0.9.2 +django-dajaxice==0.6 wsgiref==0.1.2 diff --git a/soc/settings.py b/soc/settings.py index 8873a44..28fc1ad 100644 --- a/soc/settings.py +++ b/soc/settings.py @@ -97,6 +97,7 @@ STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', # 'django.contrib.staticfiles.finders.DefaultStorageFinder', + 'dajaxice.finders.DajaxiceFinder', ) # Make this unique, and don't share it with anybody. @@ -106,7 +107,7 @@ SECRET_KEY = 'gv)nipm-2ik)r!$l$ssmin$*i&ns@htgf3d)q5vx$y-4amzrn%' TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', -# 'django.template.loaders.eggs.Loader', + 'django.template.loaders.eggs.Loader', ) MIDDLEWARE_CLASSES = ( @@ -142,6 +143,8 @@ INSTALLED_APPS = ( # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', + 'dajaxice', + 'dajax', 'website', ) diff --git a/soc/urls.py b/soc/urls.py index 5e14fd8..db3954d 100644 --- a/soc/urls.py +++ b/soc/urls.py @@ -1,4 +1,6 @@ from django.conf.urls import patterns, include, url +from dajaxice.core import dajaxice_autodiscover, dajaxice_config +dajaxice_autodiscover() # Uncomment the next two lines to enable the admin: # from django.contrib import admin @@ -15,4 +17,7 @@ urlpatterns = patterns('', # Uncomment the next line to enable the admin: # url(r'^admin/', include(admin.site.urls)), + + # Dajaxice urls + url(dajaxice_config.dajaxice_url, include('dajaxice.urls')), ) diff --git a/static/website/css/main.css b/static/website/css/main.css index 4b09e93..fddd0aa 100644 --- a/static/website/css/main.css +++ b/static/website/css/main.css @@ -21,7 +21,9 @@ body, html { width: 100%; } #topbar { - min-height: 50px; + height: 50px; + overflow: hidden; + margin-bottom: 10px; background: #4E3419; color: #ffffff; font-family: monospace; @@ -44,7 +46,7 @@ body, html { margin-right: 15px; } #navlinks ul li { - margin: 15px 15px 15px 15px; + margin: 16px 15px 15px 15px; float: left; } #navlinks ul li a{ @@ -57,7 +59,7 @@ body, html { margin: 5px 0 0 0; } #selectors { - margin: 10px 0 0 15px; + margin: 0 15px 0 15px; } #selectors label { display: inline-block; @@ -135,3 +137,7 @@ body, html { background: #ffffff; box-shadow: 0 0 10px #ffffff; } +a.extra-link { + float: right; + color: #aaaaaa; +} diff --git a/static/website/js/cloud.js b/static/website/js/cloud.js index 523cea0..7bd501c 100644 --- a/static/website/js/cloud.js +++ b/static/website/js/cloud.js @@ -1,4 +1,32 @@ $(document).ready(function() { + + var editor = CodeMirror.fromTextArea(document.getElementById("code"), { + lineNumbers: true, + lineWrapping: true, + theme: "default", + extraKeys: { + "F11": function(cm) { + cm.setOption("fullScreen", !cm.getOption("fullScreen")); + }, + "Esc": function(cm) { + if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); + } + } + }); + + var result = CodeMirror.fromTextArea(document.getElementById("result"), { + lineWrapping: true, + theme: "default", + extraKeys: { + "F11": function(cm) { + cm.setOption("fullScreen", !cm.getOption("fullScreen")); + }, + "Esc": function(cm) { + if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); + } + } + }); + /* Code Mirror Controls */ $fullscreen_code = $("#fullscreen-code"); $toggle_code = $("#toggle-code"); @@ -7,6 +35,7 @@ $(document).ready(function() { editor.setOption("fullScreen", !editor.getOption("fullScreen")); editor.focus(); }); + $toggle_code.click(function() { if(editor.getOption("theme") == "monokai") { editor.setOption("theme", "default"); @@ -22,6 +51,7 @@ $(document).ready(function() { result.setOption("fullScreen", !result.getOption("fullScreen")); result.focus(); }); + $toggle_result.click(function() { if(result.getOption("theme") == "monokai") { result.setOption("theme", "default"); @@ -32,101 +62,55 @@ $(document).ready(function() { /* * Selectors function - * Write the queries using live + * Write the queries using .on() */ - $("#categories").change(function() { + $(document).on("change", "#categories", function() { $("#books-wrapper").html(""); $("#chapters-wrapper").html(""); $("#examples-wrapper").html(""); - - $.ajax({ - url: "/ajax-books/", - type: "POST", - data: { - category_id: $(this).val() - }, - dataType: "html", - success: function(data) { - $("#books-wrapper").html(data); - } - }); + Dajaxice.website.books(Dajax.process, {category_id: $(this).val()}); }); - $(document).on("change", "#books", function(){ + $(document).on("change", "#books", function() { $("#chapters-wrapper").html(""); $("#examples-wrapper").html(""); - - $.ajax({ - url: "/ajax-chapters/", - type: "POST", - data: { - book_id: $("#books").val() - }, - dataType: "html", - success: function(data) { - $("#chapters-wrapper").html(data); - } - }); + Dajaxice.website.chapters(Dajax.process, {book_id: $(this).val()}); }); - $(document).on("change", "#chapters", function(){ + $(document).on("change", "#chapters", function() { $("#examples-wrapper").html(""); - $.ajax({ - url: "/ajax-examples/", - type: "POST", - data: { - chapter_id: $("#chapters").val() - }, - dataType: "html", - success: function(data) { - $("#examples-wrapper").html(data); - } - }); + Dajaxice.website.examples(Dajax.process, {chapter_id: $(this).val()}); }); - $(document).on("change", "#examples", function(){ - $.ajax({ - url: "/ajax-code/", - type: "POST", - data: { - example_id: $("#examples").val() - }, - dataType: "html", - success: function(data) { - editor.setValue(data); - } - }); + $(document).on("change", "#examples", function() { + Dajaxice.website.code(function(data) { + editor.setValue(data.code); + }, {example_id: $(this).val()}); }); /* Execute the code */ $lightbox_wrapper = $("#lightbox-me-wrapper"); $lightbox = $("#lightbox-me"); - $("#execute").click(function() { - var csrfmiddlewaretoken = $("[name='csrfmiddlewaretoken']").val(); - var code = editor.getValue(); + $(document).on("click", "#execute", function() { $("body").css("cursor", "wait"); - $.ajax({ - url:"/ajax-execute/", - type: "POST", - data: { - csrfmiddlewaretoken: csrfmiddlewaretoken, - code: code, - book_id: $("#books").val(), - chapter_id: $("#chapters").val(), - example_id: $("#examples").val() - }, - dataType: "text", - success: function(data) { - $("body").css("cursor", "auto"); - $data = $(data); - var output = $data.find("#output").html(); - var plot = $data.find("#plot").html(); - result.setValue(output); - if(plot) { - $lightbox.html(plot); - $lightbox_wrapper.lightbox_me({centered: true}); - } + Dajaxice.website.execute(function(data) { + $("body").css("cursor", "auto"); + result.setValue(data.output); + if(data.plot_path) { + $plot = $("<img>"); + $plot.attr({ + src: data.plot_path, + width: 400 + }); + $lightbox.html($plot); + $lightbox_wrapper.lightbox_me({centered: true}); } + }, { + token: $("[name='csrfmiddlewaretoken']").val(), + code: editor.getValue(), + book_id: $("#books").val() || 0, + chapter_id: $("#chapters").val() || 0, + example_id: $("#examples").val() || 0 }); }); }); diff --git a/static/website/templates/ajax-books.html b/static/website/templates/ajax-books.html index a7eba3e..cf12559 100644 --- a/static/website/templates/ajax-books.html +++ b/static/website/templates/ajax-books.html @@ -3,7 +3,8 @@ <select id="books"> <option value="">Select Book</option> {% for book in books %} - <option value="{{ book.id }}">{{ book.book }}</option> + <option value="{{ book.id }}">{{ book.book }} (by {{ book.author }})</option> {% endfor %} </select> {% endif %} +<a href="#" class="extra-link" id="download-book">Download Book</a> diff --git a/static/website/templates/ajax-chapters.html b/static/website/templates/ajax-chapters.html index e9ef640..5f39ba5 100644 --- a/static/website/templates/ajax-chapters.html +++ b/static/website/templates/ajax-chapters.html @@ -7,3 +7,4 @@ {% endfor %} </select> {% endif %} +<a href="#" class="extra-link" id="download-chapter">Download Chapter</a> diff --git a/static/website/templates/ajax-examples.html b/static/website/templates/ajax-examples.html index c69e5c7..f95bf7a 100644 --- a/static/website/templates/ajax-examples.html +++ b/static/website/templates/ajax-examples.html @@ -7,3 +7,4 @@ {% endfor %} </select> {% endif %} +<a href="#" class="extra-link" id="download-example">Download Example</a> diff --git a/static/website/templates/ajax-execute.html b/static/website/templates/ajax-execute.html deleted file mode 100644 index cb7ac66..0000000 --- a/static/website/templates/ajax-execute.html +++ /dev/null @@ -1,4 +0,0 @@ -<div> - <span id="output">{{ output }}</span> - <span id="plot">{% if plot_path %}<img src="{{ plot_path }}" style="width:400px;">{% endif %}</span> -</div> diff --git a/static/website/templates/index.html b/static/website/templates/index.html index bc06ea3..50dee60 100644 --- a/static/website/templates/index.html +++ b/static/website/templates/index.html @@ -1,4 +1,5 @@ {% load static %} +{% load dajaxice_templatetags %} <html> <head> <title>Scilab on Cloud</title> @@ -7,6 +8,7 @@ <link href="{% static 'website/css/fullscreen.css'%}" rel="stylesheet"/> <link href="{% static 'website/css/monokai.css' %}" rel="stylesheet"/> <link href="{% static 'website/css/main.css' %}" rel="stylesheet"/> + {% dajaxice_js_import %} </head> <body> <div id="header-wrapper"> @@ -24,11 +26,11 @@ <li><a href="#">Contact Us</a></li> <img id="scilab-logo" src="{% static 'website/images/scilab-logo.png'%}" width="100px"> </ul> + <div class="clearfix"></div> </div> <!-- /#navlinks --> </div> <!-- /#topbar --> </div> <!-- /#header-inner --> </div> <!-- /#header-wrapper --> - <div id="content-wrapper"> <div id="content-inner"> <div id="selectors"> @@ -49,7 +51,8 @@ <option value="6">Signal Processing</option> <option value="4">Thermodynamics</option> <option value="13">Others</option> - </select> + </select> + <a href="#" class="extra-link" id="contributor">Contributor</a> </div> <div id="books-wrapper"></div> <div id="chapters-wrapper"></div> @@ -103,43 +106,18 @@ </div> <!-- /#content-inner --> </div> <!-- /#content-wrapper --> - <div id="lightbox-me-wrapper"> - <div id="lightbox-me"></div> - </div> <!-- /#lightbox-me-wrapper --> + <div id="lightbox-me-wrapper"> + <div id="lightbox-me"></div> + </div> <!-- /#lightbox-me-wrapper --> + <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> + <script src="{% static '/static/dajax/jquery.dajax.core.js' %}"></script> <script src="{% static 'website/js/codemirror.js'%}"></script> <script src="{% static 'website/js/javascript.js'%}"></script> <script src="{% static 'website/js/fullscreen.js'%}"></script> <script src="{% static 'website/js/placeholder.js'%}"></script> <script src="{% static 'website/js/lightbox_me.js'%}"></script> <script src="{% static 'website/js/cloud.js'%}"></script> - <script> - var editor = CodeMirror.fromTextArea(document.getElementById("code"), { - lineNumbers: true, - lineWrapping: true, - theme: "default", - extraKeys: { - "F11": function(cm) { - cm.setOption("fullScreen", !cm.getOption("fullScreen")); - }, - "Esc": function(cm) { - if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); - } - } - }); - var result = CodeMirror.fromTextArea(document.getElementById("result"), { - lineWrapping: true, - theme: "default", - extraKeys: { - "F11": function(cm) { - cm.setOption("fullScreen", !cm.getOption("fullScreen")); - }, - "Esc": function(cm) { - if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); - } - } - }); - </script> {% csrf_token %} </body> </html> diff --git a/website/ajax.py b/website/ajax.py new file mode 100644 index 0000000..a7a1c3c --- /dev/null +++ b/website/ajax.py @@ -0,0 +1,82 @@ +from dajax.core import Dajax +from django.utils import simplejson +from dajaxice.decorators import dajaxice_register + +from django.http import HttpResponse, HttpResponseRedirect +from django.shortcuts import render +from django.template.loader import render_to_string +from django.core.context_processors import csrf +from django.views.decorators.csrf import csrf_exempt, csrf_protect +from django.db.models import Q + +from website.helpers import scilab_run +from website.models import TextbookCompanionPreference,\ + TextbookCompanionProposal, TextbookCompanionChapter,\ + TextbookCompanionExample, TextbookCompanionExampleFiles,\ + TextbookCompanionExampleDependency, TextbookCompanionDependencyFiles + +@dajaxice_register +def books(request, category_id): + dajax = Dajax() + context = {} + if category_id: + ids = TextbookCompanionProposal.objects.using('scilab')\ + .filter(proposal_status=3).values('id') + + books = TextbookCompanionPreference.objects.using('scilab')\ + .filter(category=category_id).filter(approval_status=1)\ + .filter(proposal_id__in=ids).order_by('book') + + context = { + 'books': books + } + books = render_to_string('website/templates/ajax-books.html', context) + dajax.assign('#books-wrapper', 'innerHTML', books) + return dajax.json() + +@dajaxice_register +def chapters(request, book_id): + dajax = Dajax() + context = {} + if book_id: + chapters = TextbookCompanionChapter.objects.using('scilab')\ + .filter(preference_id=book_id).order_by('number') + + context = { + 'chapters': chapters + } + chapters = render_to_string('website/templates/ajax-chapters.html', context) + dajax.assign('#chapters-wrapper', 'innerHTML', chapters) + return dajax.json() + +@dajaxice_register +def examples(request, chapter_id): + dajax = Dajax() + context = {} + if chapter_id: + examples = TextbookCompanionExample.objects.using('scilab')\ + .filter(chapter_id=chapter_id).order_by('number') + + context = { + 'examples': examples + } + examples = render_to_string('website/templates/ajax-examples.html', context) + dajax.assign('#examples-wrapper', 'innerHTML', examples) + return dajax.json() + +@dajaxice_register +def code(request, example_id): + example = TextbookCompanionExampleFiles.objects.using('scilab')\ + .get(example_id=example_id, filetype='S') + example_path = '/var/www/scilab_in/uploads/' + example.filepath + f = open(example_path) + code = f.read() + f.close() + return simplejson.dumps({'code': code}) + +@dajaxice_register +def execute(request, token, code, book_id, chapter_id, example_id): + dependency_exists = TextbookCompanionExampleDependency.objects.using('scilab')\ + .filter(example_id=example_id).exists() + data = scilab_run(code, token, book_id, dependency_exists) + return simplejson.dumps(data) diff --git a/website/helpers.py b/website/helpers.py index be8165b..7659d43 100644 --- a/website/helpers.py +++ b/website/helpers.py @@ -61,6 +61,6 @@ def scilab_run(code, token, book_id, dependency_exists): return data def trim(output): - #for future use - output = re.sub(r'\n \n \n', '\n', output) + output = [line for line in output.split('\n') if line.strip() != ''] + output = '\n'.join(output) return output diff --git a/website/urls.py b/website/urls.py index b4d782a..f9140f8 100644 --- a/website/urls.py +++ b/website/urls.py @@ -2,11 +2,4 @@ from django.conf.urls import patterns, include, url urlpatterns = patterns('', url(r'^$', 'website.views.index', name='index'), - - # Ajax urls - url(r'^ajax-books/$', 'website.views.ajax_books', name='ajax_books'), - url(r'^ajax-chapters/$', 'website.views.ajax_chapters', name='ajax_chapters'), - url(r'^ajax-examples/$', 'website.views.ajax_examples', name='ajax_examples'), - url(r'^ajax-code/$', 'website.views.ajax_code', name='ajax_code'), - url(r'^ajax-execute/$', 'website.views.ajax_execute', name='ajax_execute'), ) diff --git a/website/views.py b/website/views.py index 7d1a3a3..0c8d049 100644 --- a/website/views.py +++ b/website/views.py @@ -14,75 +14,3 @@ def index(request): context = {} context.update(csrf(request)) return render(request, 'website/templates/index.html', context) - -@csrf_exempt -def ajax_books(request): - context = {} - if request.method == 'POST': - category_id = request.POST['category_id'] - if category_id: - ids = TextbookCompanionProposal.objects.using('scilab')\ - .filter(proposal_status=3).values('id') - - books = TextbookCompanionPreference.objects.using('scilab')\ - .filter(category=category_id).filter(approval_status=1)\ - .filter(proposal_id__in=ids).order_by('book') - - context = { - 'books': books - } - return render(request, 'website/templates/ajax-books.html', context) - -@csrf_exempt -def ajax_chapters(request): - context = {} - if request.method == "POST": - book_id = request.POST['book_id'] - if book_id: - chapters = TextbookCompanionChapter.objects.using('scilab')\ - .filter(preference_id=book_id).order_by('number') - - context = { - 'chapters': chapters - } - return render(request, 'website/templates/ajax-chapters.html', context) - -@csrf_exempt -def ajax_examples(request): - context = {} - if request.method == "POST": - chapter_id = request.POST['chapter_id'] - if chapter_id: - examples = TextbookCompanionExample.objects.using('scilab')\ - .filter(chapter_id=chapter_id).order_by('number') - - context = { - 'examples': examples - } - return render(request, 'website/templates/ajax-examples.html', context) - -@csrf_exempt -def ajax_code(request): - if request.method == "POST": - example_id = request.POST['example_id'] - example = TextbookCompanionExampleFiles.objects.using('scilab')\ - .get(example_id=example_id, filetype='S') - - example_path = '/var/www/scilab_in/uploads/' + example.filepath - - f = open(example_path) - code = f.readlines() - f.close() - return HttpResponse(code) - -def ajax_execute(request): - if request.method == "POST": - code = request.POST['code'] - book_id = request.POST.get('book_id', None) - chapter_id = request.POST.get('chapter_id ', None) - example_id = request.POST.get('example_id', None) - token = request.POST['csrfmiddlewaretoken'] - dependency_exists = TextbookCompanionExampleDependency.objects.using('scilab')\ - .filter(example_id=example_id).exists() - data = scilab_run(code, token, book_id, dependency_exists) - return render(request, 'website/templates/ajax-execute.html', data) |