Ejemplo n.º 1
0
def new_paste(request, id=None):
    text_form = file_form = link_form = None

    if id:
        try:
            paste = Paste.objects.get(pk=id)
        except Paste.DoesNotExist:
            return HttpResponseRedirect(url('pastes.new'))
        parent = paste.parent or paste
    else:
        parent = paste = None

    if request.method == 'POST':
        paste_type = int(request.POST.get('type', 0))
        if paste_type == PASTE_TYPE_TEXT:
            text_form = PasteTextForm(request.POST, prefix="text")
            text_form.is_active = True
            form = text_form
        elif paste_type == PASTE_TYPE_FILE:
            file_form = PasteFileForm(request.POST, request.FILES, prefix="file")
            file_form.is_active = True
            form = file_form
        elif paste_type == PASTE_TYPE_LINK:
            link_form = PasteLinkForm(request.POST, prefix="link")
            link_form.is_active = True
            form = link_form
        if form.is_valid():
            key = form.cleaned_data['slug']
            if not key:
                length = 5
                while 1:
                    key = Paste.generate_key(length)
                    try:
                        Paste.objects.get(pk=key)
                    except Paste.DoesNotExist:
                        break
                    length += 1

            new_paste = Paste(
                id=key,
                parent=parent,
                type=paste_type,
                status=form.cleaned_data.get('public') and 1 or 0,
                title=form.cleaned_data.get('title', None),
                ip=request.META['REMOTE_ADDR'],
                author=request.user.is_authenticated() and request.user or None,
                group=request.group,
            )
            if form is text_form:
                new_paste.text = text_form.cleaned_data['text']
                if not text_form.cleaned_data['syntax']:
                    # Automatic detection
                    try:
                        lexer = lexers.guess_lexer(new_paste.text)
                        syntax = Syntax.objects.filter(lexer=lexer.__class__.__name__)[0:1].get()
                    except (Syntax.DoesNotExist, ClassNotFound), exc:
                        syntax = Syntax.objects.get(name="Plaintext")
                    new_paste.syntax = syntax
                else:
                    new_paste.syntax = text_form.cleaned_data['syntax']
            elif form is link_form:
                new_paste.text = link_form.cleaned_data['text']
            elif form is file_form:
                content = form.cleaned_data['file'].read()
                filename = form.cleaned_data['file'].name
                if not new_paste.title:
                    new_paste.title = filename
                if IMAGE_TYPES.search(filename):
                    new_paste.type = PASTE_TYPE_IMAGE
                else:
                    syntax = None
                    if len(content) < 1024*25: # 10 kb
                        try:
                            lexer = lexers.guess_lexer_for_filename(filename, content)
                            syntax = Syntax.objects.filter(lexer=lexer.__class__.__name__)[0:1].get()
                        except (Syntax.DoesNotExist, ClassNotFound):
                            pass
                    if syntax:
                        new_paste.syntax = syntax
                        new_paste.type = PASTE_TYPE_TEXT
                        new_paste.text = content
                new_paste.file.save(filename, form.cleaned_data['file'])
                request.session.setdefault('pastes', []).append(str(new_paste.id))
                request.session.save()
            new_paste.save()
            return HttpResponseRedirect(new_paste.get_absolute_url())
Ejemplo n.º 2
0
        syntax = paste.syntax_id
    else:
        syntax = request.group.default_syntax_id

    initial = {
        'parent': parent,
        'public': False,
        'paste': paste,
        'syntax': syntax,
    }

    if text_form is None:
        text_initial = initial
        if paste and paste.type == PASTE_TYPE_TEXT:
            text_initial['text'] = paste.text
        text_form = PasteTextForm(initial=text_initial, prefix="text")
    if file_form is None:
        file_form = PasteFileForm(initial=initial, prefix="file")
    if link_form is None:
        link_form = PasteLinkForm(initial=initial, prefix="link")

    if request.method == 'GET':
        text_form.is_active = True
    
    if paste:
        PAGE = 'followup'
        title = 'Post Follow-up'
        form_url = paste.get_followup_url()
    else:
        PAGE = 'new'
        title = 'New Paste'