Пример #1
0
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"})
Пример #2
0
def show(request, snippet_id=None, fork=False):
    active_page = None
    my_snippets = None
    if request.user.is_authenticated():
        my_snippets = request.user.snippet_set.filter(public=1).order_by('name')

    try:
        snippet = Snippet.objects.get(id=snippet_id)
        mine = snippet.author == request.user
        title = "{0} by {1}".format(snippet.name, snippet.author.username if snippet.author
                                    else "Anonymous")
        if fork:
            active_page = 'create'
            title = "Fork " + title
            fork = Snippet()
            fork.name = snippet.name + "-fork"
            if request.user.is_authenticated():
                fork.author = request.user
            fork.public = 1
            fork.code = snippet.code
            fork.language = snippet.language
            snippet = fork
            mine = True
    except Snippet.DoesNotExist:
        title = "Create snippet"
        active_page = 'create'
        snippet = None
        mine = True
    return render_to_response('snippetalk/snippet.html',
                              {
                                  'snippet':     snippet,
                                  'mine':        mine,
                                  'langs':       LANG_JS,
                                  'plain_text':  PLAIN_TEXT,
                                  'title':       title,
                                  'active_page': active_page,
                                  'my_snippets': my_snippets
                              },
                              RequestContext(request))