def upload request Save the code from request and return HTML Returned

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def upload(request):
"""
Save the code from request and return HTML.
Returned styles info depends on style argument:
* 1 (default): prepend html code with styles in <style> tag
* 2: do not put styles info to response
Returns the dict in JSON format:
* html: html code
* url: absolute url of the dump
* text_url: absolute url of the text version of the dump
* css_link: URI of CSS styles for highlighting the html
"""
try:
style = int(request.POST['style'])
except (KeyError, ValueError):
style = API_STYLE_YES
else:
if not style in [API_STYLE_YES, API_STYLE_NO]:
style = API_STYLE_YES
code = request.POST.get('code', None)
if not code:
return {'error': 'Empty code field'}
lexer = request.POST.get('lexer', '')
comment = request.POST.get('comment', '')
obj = Code(code=code, lexer=lexer, comment=comment)
obj.save()
html = obj.code_rendered
if style == API_STYLE_YES:
html = u'<style type="text/css">%s</style>\n\n%s' % (generate_styles(), html)
return {'html': html,
'css_link': settings.DUMPZ_PYGMENTS_CSS_LINK,
'url': obj.get_absolute_url(),
'text_url': obj.get_absolute_text_url(),
}