Skip to content

Commit bf1c764

Browse files
authored
Merge pull request #1959 from JdeRobot/1826-manager-add-functionality-to-the-manager-to-store-the-style-evaluation-in-elasticsearch-when-appropriate
Evaluate style on RADI webserver
2 parents 180f7ec + 2754b96 commit bf1c764

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

academy/settings.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
'webpack_loader', # Integrate React webpack bundles with Django
4646
'rest_framework', # Django Rest Framework
4747
'academy.academy_rest_api.apps.AcademyRestApiConfig', # Academy rest api application
48-
'react_frontend.apps.ReactFrontendConfig' # React frontend application
48+
'react_frontend.apps.ReactFrontendConfig', # React frontend application
49+
'corsheaders',
4950
]
5051

5152
MIDDLEWARE = [
@@ -56,6 +57,7 @@
5657
'django.contrib.auth.middleware.AuthenticationMiddleware',
5758
'django.contrib.messages.middleware.MessageMiddleware',
5859
'django.middleware.clickjacking.XFrameOptionsMiddleware',
60+
'corsheaders.middleware.CorsMiddleware',
5961
]
6062

6163
ROOT_URLCONF = 'academy.urls'
@@ -153,4 +155,29 @@
153155
'POLL_INTERVAL': 0.1,
154156
'IGNORE': [r'.+\.hot-update.js', r'.+\.map'],
155157
}
156-
}
158+
}
159+
160+
161+
CSRF_TRUSTED_ORIGINS = [
162+
"http://localhost:7000",
163+
"http://127.0.0.1:7000",
164+
]
165+
166+
CORS_ALLOWED_ORIGINS = [
167+
"http://localhost:7000",
168+
"http://127.0.0.1:7000",
169+
]
170+
171+
CORS_ALLOW_HEADERS = (
172+
'accept',
173+
'accept-encoding',
174+
'authorization',
175+
'content-type',
176+
'dnt',
177+
'origin',
178+
'user-agent',
179+
'x-csrftoken',
180+
'x-requested-with',
181+
)
182+
183+
CORS_ALLOW_CREDENTIALS = True

exercises/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
path('', views.index, name='index'),
77
path("exercises/<slug:exercise_id>/", views.load_exercise, name="load_exercise"),
88
path("exercise/request/<slug:exercise_id>", views.request_code, name="request_code"),
9+
path('evaluate_style/', views.evaluate_style, name="evaluate_style"),
910
]

exercises/views.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
from django.conf import settings
44
from .models import Exercise
55
import ast
6+
import sys
67
import json
8+
import tempfile
9+
from pylint import epylint as lint
10+
from django.views.decorators.csrf import csrf_exempt
711

812
#TODO: Too many hardcoded strings, review
913
def index(request):
@@ -30,3 +34,50 @@ def request_code(request, exercise_id):
3034
if difficulty != None:
3135
print('EXERCISE: ', exercise_id, 'DIFFICULTY: ', difficulty)
3236
return HttpResponse(data, content_type="text/plain")
37+
38+
@csrf_exempt
39+
def evaluate_style(request):
40+
print("1º")
41+
try:
42+
python_code = get_python_code(request)
43+
code_file = tempfile.NamedTemporaryFile(delete=False)
44+
code_file.write(python_code.encode())
45+
code_file.seek(0)
46+
options = code_file.name + ' --enable=similarities' + " --disable=C0114,C0116"
47+
(stdout, stderr) = lint.py_run(options, return_std=True)
48+
code_file.seek(0)
49+
code_file.close()
50+
result = stdout.getvalue()
51+
name = code_file.name.split('/')[-1]
52+
result = result[(result.find(name) + len(name) - 1):]
53+
result = result.replace(code_file.name, 'mycode')
54+
result = result[result.find('\n'):]
55+
init_index = result.find('rated at ')
56+
score = -1
57+
if init_index != -1:
58+
init_index += len('rated at ')
59+
final_index = result.find('/10', init_index)
60+
score = round(float(result[init_index:final_index]) * 10, 2)
61+
response = HttpResponse(result+"\n"+str(score), content_type="text/plain")
62+
return response
63+
except Exception as ex:
64+
print("2º")
65+
print(ex)
66+
response = HttpResponse("Error", content_type="text/plain")
67+
return response
68+
69+
70+
71+
72+
def get_python_code(request):
73+
python_code = request.GET.get('python_code', None)
74+
if not python_code:
75+
body_unicode = request.body.decode('utf-8')
76+
body_unicode = body_unicode[0:18] + body_unicode[18: len(body_unicode) - 2].replace('"',
77+
"'") + body_unicode[-2:]
78+
body = json.loads(body_unicode, strict=False)
79+
python_code = body['python_code']
80+
python_code = python_code.lstrip('\\').lstrip('"')
81+
python_code = python_code.replace('\\n', '\n')
82+
python_code = python_code.replace('\\"', '"').replace("\\'", "'")
83+
return python_code

scripts/Dockerfile.base

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ RUN apt-get update && apt-get -y --quiet --no-install-recommends install \
267267
&& rm -rf /var/lib/apt/lists/{apt,dpkg,cache,log} /tmp/* /var/tmp/*
268268

269269
# websocket server dependency
270-
RUN pip3 install websocket_server posix-ipc django djangorestframework==3.13.1 django-webpack-loader==1.5.0
270+
RUN pip3 install websocket_server posix-ipc django djangorestframework==3.13.1 django-webpack-loader==1.5.0 django-cors-headers==3.14.0
271271
RUN python3.8 -m pip install websockets asyncio
272272

273273
# RAM dependencies

scripts/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dill==0.3.5.1
2929
distro==1.4.0
3030
distro-info===0.23ubuntu1
3131
Django==4.1.7
32+
django-cors-headers==3.14.0
3233
django-webpack-loader==1.5.0
3334
djangorestframework==3.13.1
3435
docutils==0.16

0 commit comments

Comments
 (0)