|
7 | 7 |
|
8 | 8 | from opaque_keys.edx.keys import CourseKey |
9 | 9 | from rest_framework import status |
| 10 | +from rest_framework.response import Response |
10 | 11 | from rest_framework.generics import GenericAPIView |
11 | 12 |
|
12 | 13 | from common.djangoapps.student.auth import has_course_author_access |
13 | 14 | from openedx.core.djangoapps.util.forms import to_bool |
14 | 15 | from openedx.core.lib.api.view_utils import DeveloperErrorViewMixin, view_auth_classes |
15 | 16 | from openedx.core.lib.cache_utils import request_cached |
16 | 17 | from xmodule.modulestore.django import modulestore # lint-amnesty, pylint: disable=wrong-import-order |
17 | | - |
| 18 | +from calc.preview import latex_preview |
| 19 | +import pyparsing |
18 | 20 |
|
19 | 21 | @view_auth_classes() |
20 | 22 | class BaseCourseView(DeveloperErrorViewMixin, GenericAPIView): |
@@ -135,3 +137,17 @@ def _wrapper_view(self, request, course_id, *args, **kwargs): |
135 | 137 | ) |
136 | 138 | return view(self, request, course_key, *args, **kwargs) |
137 | 139 | return _wrapper_view |
| 140 | + |
| 141 | +class NumericalInputValidationView(GenericAPIView): |
| 142 | + |
| 143 | + def post(self, request): |
| 144 | + result = {'preview': '', |
| 145 | + 'is_valid': True, |
| 146 | + 'error': ''} |
| 147 | + try: |
| 148 | + result['preview'] = latex_preview(request.data.get('formula')) |
| 149 | + except pyparsing.ParseException: |
| 150 | + result["error"] = "Sorry, couldn't parse formula" |
| 151 | + result['is_valid'] = False |
| 152 | + return Response(result, status=400) |
| 153 | + return Response(result) |
0 commit comments