diff --git a/REQUIREMENTS.txt b/REQUIREMENTS.txt index 6e5370e..f5de22c 100644 --- a/REQUIREMENTS.txt +++ b/REQUIREMENTS.txt @@ -1,2 +1,3 @@ django djangorestframework +pyyaml diff --git a/compile_server/app/checker.py b/compile_server/app/checker.py new file mode 100644 index 0000000..675ab9c --- /dev/null +++ b/compile_server/app/checker.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +import json + +from rest_framework.response import Response +from rest_framework.decorators import api_view + +from compile_server.app.models import Resource, Example + + +@api_view(['POST']) +def check_program(request): + received_json = json.loads(request.body) + + matches = Example.objects.filter(name=received_json['example_name']) + + if not matches: + return Response() + + e = matches[0] + print received_json + print e + result = {'output': "bla\nbla\nbla"} + return Response(result) diff --git a/compile_server/app/management/commands/fill_examples.py b/compile_server/app/management/commands/fill_examples.py index 0cb46fa..c014bef 100644 --- a/compile_server/app/management/commands/fill_examples.py +++ b/compile_server/app/management/commands/fill_examples.py @@ -7,6 +7,9 @@ from django.core.management.base import BaseCommand from compile_server.app.models import Resource, Example +included_extensions = ['.ads', '.adb'] +# The extensions for the files that we want to show in the examples + class Command(BaseCommand): @@ -26,7 +29,7 @@ class Command(BaseCommand): Example.objects.all().delete() if options.get('dir', None): - d = options.get('dir')[0] + d = os.path.abspath(options.get('dir')[0]) # For now, consider all files in the directory to be part of the # example @@ -56,13 +59,15 @@ class Command(BaseCommand): resources = [] for file in glob.glob(os.path.join(d, '*')): - with open(file, 'rB') as f: - r = Resource(basename=os.path.basename(file), - contents=f.read()) - r.save() - resources.append(r) + if any([file.endswith(ext) for ext in included_extensions]): + with open(file, 'rB') as f: + r = Resource(basename=os.path.basename(file), + contents=f.read()) + r.save() + resources.append(r) e = Example(description=metadata['description'], + original_dir=d, name=metadata['name']) e.save() e.resources.add(*resources) diff --git a/compile_server/app/models.py b/compile_server/app/models.py index 1c11e82..8a5c33e 100644 --- a/compile_server/app/models.py +++ b/compile_server/app/models.py @@ -40,5 +40,8 @@ class Example(models.Model): # A description description = models.TextField() + # The directory which contains the original contents + original_dir = models.TextField() + # An example is a contains a set of resources resources = models.ManyToManyField(Resource) diff --git a/compile_server/app/serializers.py b/compile_server/app/serializers.py index 17a6614..c9390d3 100644 --- a/compile_server/app/serializers.py +++ b/compile_server/app/serializers.py @@ -1,6 +1,6 @@ from django.contrib.auth.models import User, Group from rest_framework import serializers -from compile_server.app.models import Resource +from compile_server.app.models import Resource, Example class UserSerializer(serializers.HyperlinkedModelSerializer): @@ -31,3 +31,9 @@ class ResourceSerializer(serializers.Serializer): instance.basename = validated_data.get('basename', instance.basename) instance.save() return instance + + +class ExampleSerializer(serializers.ModelSerializer): + class Meta: + model = Example + fields = ('name', 'description') diff --git a/compile_server/app/static/editors.js b/compile_server/app/static/editors.js index 2a642e4..cda006b 100644 --- a/compile_server/app/static/editors.js +++ b/compile_server/app/static/editors.js @@ -1,3 +1,37 @@ +// Launch a check on the given example editor +function query_check_result(example_name, editors, container) { + + files = [] + + editors.forEach(function(e){ + files.push({'basename': e.basename, + 'contents': e.getValue()}) + }) + + data = {"example_name": example_name, + "files": files} + + // request the examples + $.ajax({ + url: "/check_program/", + data: JSON.stringify(data), + type: "POST", + dataType : "json", + contentType: 'application/json; charset=UTF-8', + }) + .done(function( json ) { + alert(json) + }) + .fail(function( xhr, status, errorThrown ) { + // + alert( "could not run the example" ); + console.log( "Error: " + errorThrown ); + console.log( "Status: " + status ); + console.dir( xhr ); + }) +} + + // Fills a
with an editable representation of an example. // container: the
in question // example_name: the name of the example to load @@ -66,9 +100,10 @@ function fill_editor(container, example_name) { editor.session.setMode("ace/mode/ada"); // ... and their contents - editor.insert(resource.contents) + editor.setValue(resource.contents) + editor.gotoLine(1) editor.initial_contents = resource.contents - editor.filename = resource.basename + editor.basename = resource.basename // TODO: place the cursor at 1,1 @@ -84,13 +119,14 @@ function fill_editor(container, example_name) { reset_button.on('click', function (x){ reset_button.editors.forEach(function (x){ x.setValue(x.initial_contents) + x.gotoLine(1) }) }) check_button = $('