Exemple #1
0
 def post(self, request, pk=None):
     question = get_object_or_404(Question, id=pk, owner=self.request.user)
     form = QuestionForm(request.POST, instance=question)
     if not form.is_valid():
         ctx = {'form': form}
         return render(request, self.template, ctx)
     question = form.save(commit=False)
     question.save()
     return redirect(self.success_url)
Exemple #2
0
def post_question(request, entity_slug=None, slug=None):
    profile = request.user.profile
    if not entity_slug:
        entity = profile.locality
    else:
        entity = Entity.objects.get(slug=entity_slug)
        if entity != profile.locality:
            return HttpResponseForbidden(
                _("You can only post questions in your own locality"))

    q = slug and get_object_or_404(Question, unislug=slug, entity=entity)

    if request.method == "POST":
        form = QuestionForm(request.user, request.POST)
        if form.is_valid():
            ''' carefull when changing a question's history '''
            if not q:
                try:
                    q = Question.objects.get(
                        author=request.user,
                        subject=form.cleaned_data['subject'])
                except:
                    pass
            question = form.save(commit=False)
            if q:
                if q.author != request.user:
                    return HttpResponseForibdden(
                        _("You can only edit your own questions."))
                if q.answers.count():
                    return HttpResponseForbidden(
                        _("Question has been answered, editing disabled."))
                question.id = q.id
                question.created_at = q.created_at

            question.author = request.user
            question.save()
            form.save_m2m()
            if form.cleaned_data.get('facebook_publish', False):
                publish_question_to_facebook.delay(question)
            return HttpResponseRedirect(question.get_absolute_url())
    else:
        if q:
            form = QuestionForm(request.user, instance=q)
        else:
            form = QuestionForm(request.user, initial={'entity': entity})

    becoming_editor = not profile.is_editor and\
                      Profile.objects.need_editors(entity)
    context = RequestContext(
        request, {
            "form": form,
            "entity": entity,
            "max_length_q_subject": MAX_LENGTH_Q_SUBJECT,
            "slug": slug,
            "becoming_editor": becoming_editor,
        })
    return render(request, "qa/post_question.html", context)
Exemple #3
0
 def post(self, request, pk=None):
     form = QuestionForm(request.POST)
     if not form.is_valid():
         ctx = {'form': form}
         return render(request, self.template, ctx)
     # Add owner to the model before saving
     question = form.save(commit=False)
     question.owner = self.request.user
     question.save()
     return redirect(self.success_url)
Exemple #4
0
def question_edit(request, question_id):
    q = None
    if question_id:
        q = get_object_or_404(Question, pk=question_id)
    if request.method == 'POST':
        form = QuestionForm(request.POST, instance=q)
        if form.is_valid():
            form.save()
            if '_addanother' in request.POST:
                return HttpResponseRedirect(reverse('question_add'))
            else:
                return HttpResponseRedirect(reverse('questions'))
    else:
        form = QuestionForm(instance=q)

    return render(request, 'question_form.html', { 'form': form,
                                                   'action': 'edit' })
Exemple #5
0
def post_question(request, entity_id=None, slug=None):
    if request.user.is_anonymous():
        messages.error(request, _('Sorry but only connected users can post questions'))
        return HttpResponseRedirect(settings.LOGIN_URL)

    profile = request.user.profile

    if entity_id:
        entity = Entity.objects.get(pk=entity_id)
        if entity != profile.locality:
            messages.warning(request, _('Sorry, you may only post questions in your locality') +
                "\n" +
                _('Before posting a new question, please check if it already exists in this page'))
            return HttpResponseRedirect(reverse('local_home',
                                        kwargs={'entity_id': profile.locality.id,}))

    entity = profile.locality

    q = slug and get_object_or_404(Question, unislug=slug, entity=entity)

    if request.method == "POST":
        form = QuestionForm(request.user, request.POST, instance=q)
        if form.is_valid():
            ''' carefull when changing a question's history '''
            if not q:
                try:
                    q = Question.objects.get(author=request.user, subject=form.cleaned_data['subject'])
                except:
                    pass
            question = form.save(commit=False)
            if q:
                if q.author != request.user:
                    return HttpResponseForibdden(_("You can only edit your own questions."))
                if q.answers.count():
                    return HttpResponseForbidden(_("Question has been answered, editing disabled."))
                question.id = q.id
                question.created_at = q.created_at

            question.author = request.user
            question.save()
            form.save_m2m()
            if form.cleaned_data.get('facebook_publish', False):
                publish_question_to_facebook.delay(question)
            follow(request.user, question)
            return HttpResponseRedirect(question.get_absolute_url())
    else:
        if q:
            form = QuestionForm(request.user, instance=q)
        else:
            form = QuestionForm(request.user, initial={'entity': entity})

    becoming_editor = not profile.is_editor and\
                      Profile.objects.need_editors(entity)
    context = RequestContext(request, {"form": form,
                                       "entity": entity,
                                       "max_length_q_subject": MAX_LENGTH_Q_SUBJECT,
                                       "slug": slug,
                                       "becoming_editor": becoming_editor,
                                      })
    return render(request, "qa/post_question.html", context)
Exemple #6
0
def post_question(request, entity_slug=None, slug=None):
    profile = request.user.profile
    if not entity_slug:
        entity = profile.locality
    else:
        entity = Entity.objects.get(slug=entity_slug)
        if entity != profile.locality:
            return HttpResponseForbidden(_("You can only post questions in your own locality"))

    q = slug and get_object_or_404(Question, unislug=slug, entity=entity)

    if request.method == "POST":
        form = QuestionForm(request.user, request.POST)
        if form.is_valid():
            ''' carefull when changing a question's history '''
            if not q:
                try:
                    q = Question.objects.get(author=request.user, subject=form.cleaned_data['subject'])
                except:
                    pass
            question = form.save(commit=False)
            if q:
                if q.author != request.user:
                    return HttpResponseForibdden(_("You can only edit your own questions."))
                if q.answers.count():
                    return HttpResponseForbidden(_("Question has been answered, editing disabled."))
                question.id = q.id
                question.created_at = q.created_at

            question.author = request.user
            question.save()
            form.save_m2m()
            if form.cleaned_data.get('facebook_publish', False):
                publish_question_to_facebook.delay(question)
            return HttpResponseRedirect(question.get_absolute_url())
    else:
        if q:
            form = QuestionForm(request.user, instance=q)
        else:
            form = QuestionForm(request.user, initial={'entity': entity})

    becoming_editor = not profile.is_editor and\
                      Profile.objects.need_editors(entity)
    context = RequestContext(request, {"form": form,
                                       "entity": entity,
                                       "max_length_q_subject": MAX_LENGTH_Q_SUBJECT,
                                       "slug": slug,
                                       "becoming_editor": becoming_editor,
                                      })
    return render(request, "qa/post_question.html", context)
Exemple #7
0
def question_add(request):
    if request.method == 'POST':
        form = QuestionForm(request.POST)
        if form.is_valid():
            form.save()
            if '_addanother' in request.POST:
                form = QuestionForm()
            else:
                return HttpResponseRedirect(reverse('questions'))
    else:
        form = QuestionForm()

    return render(request, 'question_form.html', { 'form': form,
                                                   'action': 'add' })
Exemple #8
0
def post_question(request, entity_slug, slug=None):
    entity = Entity.objects.get(slug=entity_slug)
    if slug:
        q = get_object_or_404(Question, unislug=slug, entity=entity)

    if request.method == "POST":
        form = QuestionForm(request.POST)
        if form.is_valid():
            if slug:
                if q.author != request.user:
                    return HttpResponseForibdden(
                        _("You can only edit your own questions."))
                if q.answers.count():
                    return HttpResponseForbidden(
                        _("Question has been answered, editing disabled."))
                question = q
                question.subject = form.cleaned_data.get('subject', "")
                question.save()
            else:
                question = form.save(commit=False)
                question.author = request.user
                question.entity = entity
                question.save()
                form.save_m2m()
            return HttpResponseRedirect(question.get_absolute_url())
    else:
        if slug:
            subject = q.subject
        else:
            subject = ""
        form = QuestionForm(initial={'entity': entity, 'subject': subject})

    context = RequestContext(
        request, {
            "form": form,
            "entity": entity,
            "max_length_q_subject": MAX_LENGTH_Q_SUBJECT,
            "slug": slug,
        })
    return render(request, "qa/post_question.html", context)
Exemple #9
0
def post_question(request, entity_slug):
    entity = Entity.objects.get(slug=entity_slug)
    if request.method == "POST":
        form = QuestionForm(request.POST)
        if form.is_valid():
            question = form.save(commit=False)
            question.author = request.user
            question.entity = entity
            question.save()
            form.save_m2m()
            return HttpResponseRedirect(question.get_absolute_url())
    else:
        form = QuestionForm()

    context = RequestContext(request, {"form": form,
                    "entity": entity,
                    "max_length_q_subject": MAX_LENGTH_Q_SUBJECT,
                    })
    return render(request, "qa/post_question.html", context)
Exemple #10
0
def post_question(request, entity_slug, slug=None):
    entity = Entity.objects.get(slug=entity_slug)
    if slug:
        q = get_object_or_404(Question, unislug=slug, entity=entity)

    if request.method == "POST":
        form = QuestionForm(request.POST)
        if form.is_valid():
            if slug:
                if q.author != request.user:
                    return HttpResponseForibdden(_("You can only edit your own questions."))
                if q.answers.count():
                    return HttpResponseForbidden(_("Question has been answered, editing disabled."))
                question = q
                question.subject = form.cleaned_data.get('subject', "")
                question.save()
            else:
                question = form.save(commit=False)
                question.author = request.user
                question.entity = entity
                question.save()
                form.save_m2m()
            return HttpResponseRedirect(question.get_absolute_url())
    else:
        if slug:
            subject = q.subject
        else:
            subject = ""
        form = QuestionForm(initial={'entity': entity, 'subject': subject})

    context = RequestContext(request, {"form": form,
                                       "entity": entity,
                                       "max_length_q_subject": MAX_LENGTH_Q_SUBJECT,
                                       "slug": slug,
    })
    return render(request, "qa/post_question.html", context)
Exemple #11
0
def post_question(request, entity_id=None, slug=None):
    if request.user.is_anonymous():
        messages.error(request,
                       _('Sorry but only connected users can post questions'))
        return HttpResponseRedirect(settings.LOGIN_URL)

    profile = request.user.profile

    if entity_id:
        entity = Entity.objects.get(pk=entity_id)
        if entity != profile.locality:
            messages.warning(
                request,
                _('Sorry, you may only post questions in your locality') +
                "\n" +
                _('Before posting a new question, please check if it already exists in this page'
                  ))
            return HttpResponseRedirect(
                reverse('local_home',
                        kwargs={
                            'entity_id': profile.locality.id,
                        }))

    entity = profile.locality

    q = slug and get_object_or_404(Question, unislug=slug, entity=entity)

    if request.method == "POST":
        form = QuestionForm(request.user, request.POST, instance=q)
        if form.is_valid():
            ''' carefull when changing a question's history '''
            if not q:
                try:
                    q = Question.objects.get(
                        author=request.user,
                        subject=form.cleaned_data['subject'])
                except:
                    pass
            question = form.save(commit=False)
            if q:
                if q.author != request.user:
                    return HttpResponseForibdden(
                        _("You can only edit your own questions."))
                if q.answers.count():
                    return HttpResponseForbidden(
                        _("Question has been answered, editing disabled."))
                question.id = q.id
                question.created_at = q.created_at

            question.author = request.user
            question.save()
            form.save_m2m()
            if form.cleaned_data.get('facebook_publish', False):
                publish_question_to_facebook.delay(question)
            follow(request.user, question)
            return HttpResponseRedirect(question.get_absolute_url())
    else:
        if q:
            form = QuestionForm(request.user, instance=q)
        else:
            form = QuestionForm(request.user, initial={'entity': entity})

    becoming_editor = not profile.is_editor and\
                      Profile.objects.need_editors(entity)
    context = RequestContext(
        request, {
            "form": form,
            "entity": entity,
            "max_length_q_subject": MAX_LENGTH_Q_SUBJECT,
            "slug": slug,
            "becoming_editor": becoming_editor,
        })
    return render(request, "qa/post_question.html", context)
Exemple #12
0
 def get(self, request, pk):
     question = get_object_or_404(Question, id=pk, owner=self.request.user)
     form = QuestionForm(instance=question)
     ctx = {'form': form}
     return render(request, self.template, ctx)
Exemple #13
0
 def get(self, request, pk=None):
     form = QuestionForm()
     ctx = {'form': form}
     return render(request, self.template, ctx)