def upload_view request Save the code from request and return HTML Acc

 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def upload_view(request):
"""
Save the code from request and return HTML.
Accepted arguments: code, lexer, format, style, numbers
Argument "lexer" is equal to "text" by default.
Type of formatting depends on "format" argument:
* html (default): generate HTML tags
* bbcode: generate bbcode
Returned styles info depends on "style" argument (makes sense only for html format):
* 'no' (default): do not put styles info to response
* 'prepend': prepend html code with styles in <style> tag
* 'inline': do not use classes, put inline styles inside the rendered html
Line numeration depends on "numbers" argument (makes sense only for html format):
* true (default): generate line numbers
* false: do not generate line numbers
Return the dict in JSON format:
* code: rendered 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
"""
request.POST = request.GET
style = request.POST.get('style')
if not style in ['prepend', 'no', 'inline']:
style = 'no'
numbers = request.POST.get('numbers', '1') == '1'
format = request.POST.get('format')
if not format in ['html', 'bbcode']:
format = 'html'
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()
inline_styles = style == 'inline' and True or False
out = util.render(lexer, code, inline_styles=inline_styles, numbers=numbers, format=format)
if format == 'html' and style == 'prepend':
out = u'<style type="text/css">%s</style>\n\n%s' % (util.generate_styles(), out)
return {'code': out,
'css_link': settings.DUMPZ_PYGMENTS_CSS_LINK,
'url': obj.get_absolute_url(),
'text_url': obj.get_absolute_text_url(),
}
# the simplest way to allow upload_help view get access to docstring of upload_view
upload = ajax(upload_view)