示例#1
0
def oneliner_new(request, question_pk=None, oneliner_pk=None):
    params = _common_params(request)
    initial = _common_initial(request)
    params['next_url'] = initial['next_url']

    question = None
    oneliner0 = None

    if question_pk is not None:
        try:
            question = Question.objects.get(pk=question_pk)
            initial['summary'] = question.summary
        except:
            pass

    elif oneliner_pk is not None:
        try:
            oneliner0 = OneLiner.objects.get(pk=oneliner_pk)
            initial['summary'] = oneliner0.summary
        except:
            pass

    if request.method == 'POST':
        form = PostOneLinerForm(request.user, request.POST)
        if request.user.is_authenticated():
            if form.is_valid():
                new_oneliner = form.save()
                if new_oneliner.is_published:
                    tweet(new_oneliner)

                if question is not None:
                    question.add_answer(new_oneliner)
                    if new_oneliner.is_published:
                        send_oneliner_answer(question, new_oneliner)
                elif oneliner0 is not None:
                    oneliner0.add_alternative(new_oneliner)
                    if new_oneliner.is_published:
                        send_oneliner_alternative(oneliner0, new_oneliner)

                return redirect(oneliner, new_oneliner.pk)
            else:
                params['next_url'] = request.POST.get('next_url')
    else:
        form = PostOneLinerForm(request.user, initial=initial)

    params['form'] = form
    params['question'] = question
    params['oneliner'] = oneliner0

    return render_to_response('main/pages/oneliner_edit.html', params, context_instance=RequestContext(request))
示例#2
0
def oneliner_new(request, question_pk=None, oneliner_pk=None, cancel_url=None):
    params = _common_params(request)
    if not cancel_url:
        cancel_url = reverse(oneliners_newest)
    params["cancel_url"] = cancel_url

    question = None
    oneliner0 = None
    initial = {}

    if question_pk is not None:
        try:
            question = Question.objects.get(pk=question_pk)
            initial["summary"] = question.summary
        except Question.DoesNotExist:
            pass

    elif oneliner_pk is not None:
        try:
            oneliner0 = OneLiner.objects.get(pk=oneliner_pk)
            oneliner0.score = sum([x.value for x in oneliner0.vote_set.all()])
            initial["summary"] = oneliner0.summary
        except OneLiner.DoesNotExist:
            pass

    if request.method == "POST":
        form = PostOneLinerForm(request.user, request.POST)
        if request.user.is_authenticated():
            if form.is_valid():
                new_oneliner = form.save()
                if new_oneliner.is_published:
                    tweet(new_oneliner, format_canonical_url(request))

                if question is not None:
                    question.add_answer(new_oneliner)
                elif oneliner0 is not None:
                    oneliner0.add_alternative(new_oneliner)

                if new_oneliner.is_published:
                    return redirect(oneliners_newest)
                else:
                    return redirect(new_oneliner)
    else:
        form = PostOneLinerForm(request.user, initial=initial)

    params["form"] = form
    params["question"] = question
    params["oneliner"] = oneliner0

    return render_to_response("oneliners/pages/oneliner_edit.html", params, context_instance=RequestContext(request))
示例#3
0
def oneliner_new(request, question_pk=None, oneliner_pk=None, cancel_url=None):
    params = _common_params(request)
    if not cancel_url:
        cancel_url = reverse(oneliners_newest)
    params['cancel_url'] = cancel_url

    question = None
    oneliner0 = None
    initial = {}

    if question_pk is not None:
        try:
            question = Question.objects.get(pk=question_pk)
            initial['summary'] = question.summary
        except Question.DoesNotExist:
            pass

    elif oneliner_pk is not None:
        try:
            oneliner0 = OneLiner.objects.get(pk=oneliner_pk)
            oneliner0.score = sum([x.value for x in oneliner0.vote_set.all()])
            initial['summary'] = oneliner0.summary
        except OneLiner.DoesNotExist:
            pass

    if request.method == 'POST':
        form = PostOneLinerForm(request.user, request.POST)
        if request.user.is_authenticated():
            if form.is_valid():
                new_oneliner = form.save()
                if new_oneliner.is_published:
                    tweet(new_oneliner, format_canonical_url(request))

                if question is not None:
                    question.add_answer(new_oneliner)
                elif oneliner0 is not None:
                    oneliner0.add_alternative(new_oneliner)

                if new_oneliner.is_published:
                    return redirect(oneliners_newest)
                else:
                    return redirect(new_oneliner)
    else:
        form = PostOneLinerForm(request.user, initial=initial)

    params['form'] = form
    params['question'] = question
    params['oneliner'] = oneliner0

    return render_to_response('oneliners/pages/oneliner_edit.html',
                              params,
                              context_instance=RequestContext(request))
示例#4
0
def oneliner_new(request, oneliner_pk=None, cancel_url=None):
    params = _common_params(request)
    if not cancel_url:
        cancel_url = reverse(oneliners_newest)
    params['cancel_url'] = cancel_url

    oneliner0 = None
    initial = {}

    if oneliner_pk:
        try:
            oneliner0 = OneLiner.objects.get(pk=oneliner_pk)
            oneliner0.score = sum([x.value for x in oneliner0.vote_set.all()])
            initial['summary'] = oneliner0.summary
        except OneLiner.DoesNotExist:
            pass

    if request.method == 'POST':
        form = PostOneLinerForm(request.user, request.POST)
        if request.user.is_authenticated():
            if form.is_valid():
                new_oneliner = form.save()

                if oneliner0 is not None:
                    oneliner0.add_alternative(new_oneliner)

                if new_oneliner.is_published:
                    return redirect(oneliners_newest)
                else:
                    return redirect(new_oneliner)
    else:
        form = PostOneLinerForm(request.user, initial=initial)

    params['form'] = form
    params['oneliner'] = oneliner0

    return render(request, 'oneliners/pages/oneliner_edit.html', params)