diff options
-rw-r--r-- | static/tmp/a.sci | 2 | ||||
-rw-r--r-- | static/tmp/mLnhjbU1jr3BCrLv8tMJai6ElivgI1dJ.sci | 48 | ||||
-rw-r--r-- | static/tmp/out.txt | 74 | ||||
-rw-r--r-- | static/website/css/main.css | 2 | ||||
-rw-r--r-- | static/website/js/cloud.js | 104 | ||||
-rw-r--r-- | static/website/js/placeholder.js | 48 | ||||
-rw-r--r-- | static/website/templates/ajax-books.html | 9 | ||||
-rw-r--r-- | static/website/templates/ajax-chapters.html | 9 | ||||
-rw-r--r-- | static/website/templates/ajax-examples.html | 9 | ||||
-rw-r--r-- | static/website/templates/index.html | 83 | ||||
-rw-r--r-- | website/helpers.py | 96 | ||||
-rw-r--r-- | website/models.py | 69 | ||||
-rw-r--r-- | website/urls.py | 6 | ||||
-rw-r--r-- | website/views.py | 65 |
14 files changed, 588 insertions, 36 deletions
diff --git a/static/tmp/a.sci b/static/tmp/a.sci new file mode 100644 index 0000000..ac1be22 --- /dev/null +++ b/static/tmp/a.sci @@ -0,0 +1,2 @@ +exec('/home/cheese/cloud/soc/static/tmp/mLnhjbU1jr3BCrLv8tMJai6ElivgI1dJ.sci') +quit diff --git a/static/tmp/mLnhjbU1jr3BCrLv8tMJai6ElivgI1dJ.sci b/static/tmp/mLnhjbU1jr3BCrLv8tMJai6ElivgI1dJ.sci new file mode 100644 index 0000000..f635a71 --- /dev/null +++ b/static/tmp/mLnhjbU1jr3BCrLv8tMJai6ElivgI1dJ.sci @@ -0,0 +1,48 @@ +lines(0) + +disp("Example 2.1") +printf("\n") +printf("Given") +disp("Resistance used is 4 ohm") +disp("Current flow is i=2.5*sin(w*t)") +disp("Angular frequency(w)=500 rad/s") + +R=4; +iamp=2.5;w=500; +t=0:0.001:0.012566 +i=2.5*sin(w*t) + + +Vamp=iamp*R; +printf("v=%d*sin(%d*t)(V)\n",Vamp,w) + +pamp=iamp*iamp*R; +printf("p=%d(sin(%d*t))^2(W)\n",pamp,w) +p=pamp*sin(w*t)^2; + +//On integrating p with respect to t +W=25*(t/2-sin(2*w*t)/(4*w)) + +function p=f(t),p=pamp*sin(w*t)^2,endfunction +w1=intg(0,2*%pi/w,f); + + +subplot(221) +plot(t,i) +xs2jpg(gcf(), "/home/cheese/cloud/soc/static/tmp/1387919154.jpg"); +xtitle ('i vs wt','wt','i '); + +subplot(222) +plot(t,p) +xs2jpg(gcf(), "/home/cheese/cloud/soc/static/tmp/1387919155.jpg"); +xtitle ('p vs wt','wt','p '); + + + +subplot(223) +plot(t,W) +xs2jpg(gcf(), "/home/cheese/cloud/soc/static/tmp/1387919156.jpg"); +xtitle ('w vs wt','wt','w '); + + +exit diff --git a/static/tmp/out.txt b/static/tmp/out.txt new file mode 100644 index 0000000..26c869c --- /dev/null +++ b/static/tmp/out.txt @@ -0,0 +1,74 @@ + + + +-->lines(0) + + +-->disp("Example 2.1") + + Example 2.1 + +-->printf("\n") + + +-->printf("Given") +Given +-->disp("Resistance used is 4 ohm") + + Resistance used is 4 ohm + +-->disp("Current flow is i=2.5*sin(w*t)") + + Current flow is i=2.5*sin(w*t) + +-->disp("Angular frequency(w)=500 rad/s") + + Angular frequency(w)=500 rad/s + + +-->R=4; + +-->iamp=2.5;w=500; + +-->t=0:0.001:0.012566 + t = + + 0. 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01 0.011 0.012 + +-->i=2.5*sin(w*t) + i = + + 0. 1.1985638 2.1036775 2.4937375 2.2732436 1.4961804 0.3528000 - 0.8769581 - 1.8920062 - 2.4438253 - 2.3973107 - 1.7638508 - 0.6985387 + + + +-->Vamp=iamp*R; + +-->printf("v=%d*sin(%d*t)(V)\n",Vamp,w) +v=10*sin(500*t)(V) + + +-->pamp=iamp*iamp*R; + +-->printf("p=%d(sin(%d*t))^2(W)\n",pamp,w) +p=25(sin(500*t))^2(W) + +-->p=pamp*sin(w*t)^2; + + +-->//On integrating p with respect to t + +-->W=25*(t/2-sin(2*w*t)/(4*w)) + W = + + 0. 0.0019816 0.0136338 0.035736 0.0594600 0.0744866 0.0784927 0.0792877 0.0876330 0.1073485 0.1318003 0.1499999 0.1567072 + + +-->funcion p=f(t),p=pamp*sin(w*t)^2,endfunction +funcion p=f(t),p=pamp*sin(w*t)^2,endfunction + !--error 4 +Undefined variable: funcion +at line 26 of exec file called by : +exec('/home/cheese/cloud/soc/static/tmp/mLnhjbU1jr3BCrLv8tMJai6ElivgI1dJ.sci') + + diff --git a/static/website/css/main.css b/static/website/css/main.css index 802aa5f..8f95975 100644 --- a/static/website/css/main.css +++ b/static/website/css/main.css @@ -5,6 +5,8 @@ select { display: inline; margin-bottom: 15px; + min-width: 220px; + width: auto; } .CodeMirror { height: 99%; diff --git a/static/website/js/cloud.js b/static/website/js/cloud.js new file mode 100644 index 0000000..1b64532 --- /dev/null +++ b/static/website/js/cloud.js @@ -0,0 +1,104 @@ +$(document).ready(function() { + /* Code Mirror Controls */ + $fullscreen_code = $("#fullscreen-code"); + $toggle_code = $("#toggle-code"); + + $fullscreen_code.click(function(e) { + editor.setOption("fullScreen", !editor.getOption("fullScreen")); + editor.focus(); + }); + $toggle_code.click(function() { + if(editor.getOption("theme") == "monokai") { + editor.setOption("theme", "default"); + } else{ + editor.setOption("theme", "monokai"); + } + }); + + $fullscreen_result = $("#fullscreen-result"); + $toggle_result = $("#toggle-result"); + + $fullscreen_result.click(function(e) { + result.setOption("fullScreen", !result.getOption("fullScreen")); + result.focus(); + }); + $toggle_result.click(function() { + if(result.getOption("theme") == "monokai") { + result.setOption("theme", "default"); + } else{ + result.setOption("theme", "monokai"); + } + }); + + /* + * Selectors function + * Write the queries using live + */ + $("#categories").change(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); + } + }); + }); + + $(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); + } + }); + }); + + $(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); + } + }); + }); + + /* Execute the code */ + $("#execute").click(function() { + var csrfmiddlewaretoken = $("[name='csrfmiddlewaretoken']").val(); + var code = editor.getValue(); + $.ajax({ + url:"/ajax-execute/", + type: "POST", + data: { + csrfmiddlewaretoken: csrfmiddlewaretoken, + code: code + }, + dataType: "text", + success: function(data) { + result.setValue(data); + } + }); + }); +}); diff --git a/static/website/js/placeholder.js b/static/website/js/placeholder.js new file mode 100644 index 0000000..748afe7 --- /dev/null +++ b/static/website/js/placeholder.js @@ -0,0 +1,48 @@ +(function() { + CodeMirror.defineOption("placeholder", "", function(cm, val, old) { + var prev = old && old != CodeMirror.Init; + if (val && !prev) { + cm.on("blur", onBlur); + cm.on("change", onChange); + onChange(cm); + } else if (!val && prev) { + cm.off("blur", onBlur); + cm.off("change", onChange); + clearPlaceholder(cm); + var wrapper = cm.getWrapperElement(); + wrapper.className = wrapper.className.replace(" CodeMirror-empty", ""); + } + + if (val && !cm.hasFocus()) onBlur(cm); + }); + + function clearPlaceholder(cm) { + if (cm.state.placeholder) { + cm.state.placeholder.parentNode.removeChild(cm.state.placeholder); + cm.state.placeholder = null; + } + } + function setPlaceholder(cm) { + clearPlaceholder(cm); + var elt = cm.state.placeholder = document.createElement("pre"); + elt.style.cssText = "height: 0; overflow: visible"; + elt.className = "CodeMirror-placeholder"; + elt.appendChild(document.createTextNode(cm.getOption("placeholder"))); + cm.display.lineSpace.insertBefore(elt, cm.display.lineSpace.firstChild); + } + + function onBlur(cm) { + if (isEmpty(cm)) setPlaceholder(cm); + } + function onChange(cm) { + var wrapper = cm.getWrapperElement(), empty = isEmpty(cm); + wrapper.className = wrapper.className.replace(" CodeMirror-empty", "") + (empty ? " CodeMirror-empty" : ""); + + if (empty) setPlaceholder(cm); + else clearPlaceholder(cm); + } + + function isEmpty(cm) { + return (cm.lineCount() === 1) && (cm.getLine(0) === ""); + } +})(); diff --git a/static/website/templates/ajax-books.html b/static/website/templates/ajax-books.html new file mode 100644 index 0000000..a7eba3e --- /dev/null +++ b/static/website/templates/ajax-books.html @@ -0,0 +1,9 @@ +{% if books %} +<label>Book: </label> +<select id="books"> + <option value="">Select Book</option> +{% for book in books %} + <option value="{{ book.id }}">{{ book.book }}</option> +{% endfor %} +</select> +{% endif %} diff --git a/static/website/templates/ajax-chapters.html b/static/website/templates/ajax-chapters.html new file mode 100644 index 0000000..e9ef640 --- /dev/null +++ b/static/website/templates/ajax-chapters.html @@ -0,0 +1,9 @@ +{% if chapters %} +<label>Chapter: </label> +<select id="chapters"> + <option value="">Select Chapter</option> +{% for chapter in chapters %} + <option value="{{ chapter.id }}">{{ chapter.number }} - {{ chapter.name }}</option> +{% endfor %} +</select> +{% endif %} diff --git a/static/website/templates/ajax-examples.html b/static/website/templates/ajax-examples.html new file mode 100644 index 0000000..c69e5c7 --- /dev/null +++ b/static/website/templates/ajax-examples.html @@ -0,0 +1,9 @@ +{% if examples %} +<label>Example: </label> +<select id="examples"> + <option value="">Select Example</option> +{% for example in examples %} + <option value="{{ example.id }}">{{ example.number }} - {{ example.caption }}</option> +{% endfor %} +</select> +{% endif %} diff --git a/static/website/templates/index.html b/static/website/templates/index.html index 85f0ce3..a4b200b 100644 --- a/static/website/templates/index.html +++ b/static/website/templates/index.html @@ -34,43 +34,59 @@ <div id="selectors"> <div id="category-wrapper"> <label>Category: </label> - <select><option>Select Category</option></select> - </div> - <div id="book-wrapper"> - <label>Book: </label> - <select><option>Select Book</option></select> - </div> - <div id="chapter-wrapper"> - <label>Chapter: </label> - <select><option>Select Chapter</option></select> - </div> - <div id="example-wrapper"> - <label>Example: </label> - <select><option>Select Example</option></select> + <select id="categories"> + <option value="">Select Category</option> + <option value="10">Analog Electronics</option> + <option value="3">Chemical Engineering</option> + <option value="12">Computer Programming</option> + <option value="2">Control Theory & Control Systems</option> + <option value="7">Digital Communications</option> + <option value="11">Digital Electronics</option> + <option value="8">Electrical Technology</option> + <option value="1">Fluid Mechanics</option> + <option value="9">Mathematics & Pure Science</option> + <option value="5">Mechanical Engineering</option> + <option value="6">Signal Processing</option> + <option value="4">Thermodynamics</option> + <option value="13">Others</option> + </select> </div> + <div id="books-wrapper"></div> + <div id="chapters-wrapper"></div> + <div id="examples-wrapper"></div> </div> <div id="content"> <div id="input"> <label> Scilab Code <span id="controls"> - <a href="#" id="toggle"> + <a href="#" id="toggle-code"> <img src="{% static 'website/images/icon-toggle.png'%}"> </a> - <a href="#" id="fullscreen"> + <a href="#" id="fullscreen-code"> <img src="{% static 'website/images/icon-fullscreen.png'%}"> </a> </span> <!-- /#controls --> </label> <form> - <textarea id="code"></textarea> + <textarea id="code" placeholder="Write a new code or select existing from above category..."></textarea> </form> </div><!-- /#input --> <a id="execute" class="button">Execute</a> <div id="output"> - <label>Result</label> - <textarea></textarea> + <label> + Result + <span id="controls"> + <a href="#" id="toggle-result"> + <img src="{% static 'website/images/icon-toggle.png'%}"> + </a> + <a href="#" id="fullscreen-result"> + <img src="{% static 'website/images/icon-fullscreen.png'%}"> + </a> + </span> <!-- /#controls --> + </label> + <textarea id="result"></textarea> </div> <!-- /#output --> <a id="bug" class="button">Report bug / Give Feedback</a> @@ -88,9 +104,11 @@ </div> <!-- /#content-wrapper --> <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> + <script src="{% static 'website/js/cloud.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> var editor = CodeMirror.fromTextArea(document.getElementById("code"), { lineNumbers: true, @@ -105,23 +123,20 @@ } } }); - - $(document).ready(function() { - $fullscreen = $("#fullscreen"); - $toggle = $("#toggle"); - - $fullscreen.click(function(e) { - editor.setOption("fullScreen", !editor.getOption("fullScreen")); - editor.focus(); - }); - $toggle.click(function() { - if(editor.getOption("theme") == "monokai") { - editor.setOption("theme", "default"); - } else{ - editor.setOption("theme", "monokai"); - } - }); + 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/helpers.py b/website/helpers.py new file mode 100644 index 0000000..651a886 --- /dev/null +++ b/website/helpers.py @@ -0,0 +1,96 @@ +import os +import re +import time +import subprocess + +from soc.settings import PROJECT_DIR + +def scilab_run(code, token): + plot_exists = False + #Finding the plot and appending xs2jpg function + p = re.compile(r'.*plot.*\(.*,.*,*\).*\n') + if p.search(code): + plot_exists = True + code = code + '\n' + temp = code + code = '' + start, end, count = 0, 0, 0 + current_time = time.time() + plot_path = [] + for (count, match) in enumerate(p.finditer(temp)): + print "count==================",count + plot_path.append( + PROJECT_DIR + '/static/tmp/{0}.jpg'.format(int(current_time) + count) + ) + end = match.end() + code += temp[start:end] + code += 'xs2jpg(gcf(), "{0}");\n'.format(plot_path[-1]) + start = end + code += temp[end:] + + # Create a file for the current request + canvas_off = """ + function turnCanvasOff() + m = getscilabmode(); + if (m=="STD"|m=="NW") then + previousDisp = disp; + prot = funcprot(); + funcprot(0); + deff("disp(str)", ""); + usecanvas(%f); + disp = previousDisp; + funcprot(prot); + end + endfunction + turnCanvasOff(); + clear turnCanvasOff; + """ + + file_path = PROJECT_DIR + '/static/tmp/' + token + '.sci' + + f = open(file_path, "w") + f.write("mode(-1);\nlines(0);\nmode(-1);\ntry\nmode(2);\n") + f.write('lines(0)\n') + f.write(canvas_off) + f.write(code) + f.write('\nexit') + f.write("\nmode(-1);\nexit();\ncatch\nmode(-1);\ndisp(lasterror());\nexit();") + f.close() + + if plot_exists: + cmd = 'export DISPLAY=:99 && ' + cmd += 'xvfb-run -a cat {0}'.format(file_path) + ' | scilab-adv-cli -nw -nb' + print "############################" + print cmd + else: + cmd = 'cat {0}'.format(file_path) + ' | scilab-adv-cli -nw' + + output = subprocess.Popen( + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True + ).communicate()[0] + #os.remove(file_path) + output = trim(output) + return output + +def trim(output): + atoms_banner = """ + Start Image Processing Tool 2 + Load macros + Load help + + SIVP - Scilab Image and Video Processing Toolbox + Load macros + Load gateways + Load help + Load demos + + + + Start identification + + Load macros + + Load help + """ + output = output.replace(atoms_banner, '') + return output diff --git a/website/models.py b/website/models.py index 71a8362..76d07b7 100644 --- a/website/models.py +++ b/website/models.py @@ -1,3 +1,70 @@ from django.db import models -# Create your models here. + +""" +Models from Scilab Database created using inspectdb +Use it with the "scilab" database eg:using("scilab") +These models are used only for django orm reference. +""" +class TextbookCompanionPreference(models.Model): + id = models.IntegerField(unique=True, primary_key=True) + proposal_id = models.IntegerField() + pref_number = models.IntegerField() + book = models.CharField(max_length=100L) + author = models.CharField(max_length=100L) + isbn = models.CharField(max_length=25L) + publisher = models.CharField(max_length=50L) + edition = models.CharField(max_length=2L) + year = models.IntegerField() + category = models.IntegerField() + approval_status = models.IntegerField() + cloud_pref_err_status = models.IntegerField() + class Meta: + db_table = 'textbook_companion_preference' + +class TextbookCompanionProposal(models.Model): + id = models.IntegerField(unique=True, primary_key=True) + uid = models.IntegerField() + approver_uid = models.IntegerField() + full_name = models.CharField(max_length=50L) + mobile = models.CharField(max_length=15L) + gender = models.CharField(max_length=10L) + how_project = models.CharField(max_length=50L) + course = models.CharField(max_length=50L) + branch = models.CharField(max_length=50L) + university = models.CharField(max_length=100L) + faculty = models.CharField(max_length=100L) + reviewer = models.CharField(max_length=100L) + completion_date = models.IntegerField() + creation_date = models.IntegerField() + approval_date = models.IntegerField() + proposal_status = models.IntegerField() + message = models.TextField() + scilab_version = models.CharField(max_length=20L) + operating_system = models.CharField(max_length=20L) + teacher_email = models.CharField(max_length=20L) + class Meta: + db_table = 'textbook_companion_proposal' + +class TextbookCompanionChapter(models.Model): + id = models.IntegerField(primary_key=True) + preference_id = models.IntegerField() + number = models.IntegerField() + name = models.CharField(max_length=255L) + cloud_chapter_err_status = models.CharField(max_length=255L) + class Meta: + db_table = 'textbook_companion_chapter' + +class TextbookCompanionExample(models.Model): + id = models.IntegerField(primary_key=True) + chapter_id = models.IntegerField() + approver_uid = models.IntegerField() + number = models.CharField(max_length=10L) + caption = models.CharField(max_length=255L) + approval_date = models.IntegerField() + approval_status = models.IntegerField() + timestamp = models.IntegerField() + cloud_err_status = models.IntegerField() + class Meta: + db_table = 'textbook_companion_example' + diff --git a/website/urls.py b/website/urls.py index f9140f8..8f6b630 100644 --- a/website/urls.py +++ b/website/urls.py @@ -2,4 +2,10 @@ 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-execute/$', 'website.views.ajax_execute', name='ajax_execute'), ) diff --git a/website/views.py b/website/views.py index bede851..a26ae91 100644 --- a/website/views.py +++ b/website/views.py @@ -1,5 +1,68 @@ from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render +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 def index(request): - return render(request, 'website/templates/index.html') + 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) + +def ajax_execute(request): + if request.method == "POST": + code = request.POST['code'] + token = request.POST['csrfmiddlewaretoken'] + result = scilab_run(code, token) + return HttpResponse(result) |