def get_highlight(request): if request.method == 'GET': l = int(request.GET['lang']) - 1 lang_name = LANGUAGE_CHOICES[l][1] code = highlight(request.GET['code'], lang_name) return JsonResponse(status=200, data={'code': code}) else: return JsonResponse(status=400, data={'message': "Use GET"})
def upload(request): if request.method == 'POST': form = FileForm(request.POST, request.FILES) if form.is_valid(): snippet = Snippet() name, lang_code = parse_filename(request.FILES['file'].name) code = request.FILES['file'].read().decode() snippet.language = lang_code highlighted = highlight(code, snippet.get_language_display()) return JsonResponse(status=200, data={ 'message': "ok", 'name': name, 'lang_code': lang_code, # 'lang_name': snippet.get_language_display(), 'raw': code, 'highlighted': highlighted }) else: return JsonResponse(status=400, data={'message': "Bad data"}) else: return JsonResponse(status=400, data={'message': "Use POST"})
def highlighted(self): return u.highlight(self.code, self.get_language_display())