Esempio n. 1
0
def ask_question(request, classroom):
    """Ask question for specific class

    Need to publish the question to rest of classroom
    """
    if request.method == 'POST':
        form = AskQuestionForm(data=request.POST)
        if form.is_valid():
            question = form.save(request.user, classroom)
            message = {"success": True}
            channel = "classroom_{0}".format(classroom.pk)
            pub_message = {
                "type": "new_question",
                "question": {
                    "pk": question.pk,
                    "subject": question.subject,
                    "content": question.content,
                    "student": question.student.username,
                    "up_vote_url": reverse("question_up_vote",
                                           args=[question.pk]),
                }
            }
            publish(channel, pub_message)
        else:
            message = {"error": form.errors}
            print message
            return HttpResponseBadRequest(message)
        return HttpResponse(json.dumps(message))
    return HttpResponseNotFound("Need to post")
Esempio n. 2
0
def ask_question(request, classroom):
    """Ask question for specific class

    Need to publish the question to rest of classroom
    """
    if request.method == 'POST':
        form = AskQuestionForm(data=request.POST)
        if form.is_valid():
            question = form.save(request.user, classroom)
            message = {"success": True}
            channel = "classroom_{0}".format(classroom.pk)
            pub_message = {
                "type": "new_question",
                "question": {
                    "pk": question.pk,
                    "subject": question.subject,
                    "content": question.content,
                    "student": question.student.username,
                    "up_vote_url": reverse(
                        "question_up_vote", args=[question.pk]
                    ),
                }
            }
            publish(channel, pub_message)
        else:
            message = {"error": form.errors}
            print message
            return HttpResponseBadRequest(message)
        return HttpResponse(json.dumps(message))
    return HttpResponseNotFound("Need to post")
Esempio n. 3
0
def ask(request):
    user = getAuthenticatedUser(request)

    if request.method == 'POST':
        form = AskQuestionForm(request.POST)
        if form.is_valid():
            question = Question.objects.create(
                user_ptr=user,
                title=form.cleaned_data.get('title'),
                text=form.cleaned_data.get('text'))
            tags = form.cleaned_data.get('tags').split(',')
            for tag in tags:
                if ' ' in tag:
                    tag = tag.replace(' ', '_')
                try:
                    t = Tag.objects.get(name=tag)
                except Tag.DoesNotExist:
                    t = Tag.objects.create(name=tag)
                    t.save()

                question.tags.add(t)

            question.save()
            return HttpResponseRedirect('/question/' + str(question.id))
    else:
        form = AskQuestionForm()

    context = {'User': user, 'form': form}
    return render(request, 'ask.html', context)
Esempio n. 4
0
def question_create(request):
    if request.method == 'POST':
        form = AskQuestionForm(request.POST, request.FILES)
        if form.is_valid():
            return HttpResponseRedirect(form.save().get_absolute_url())
    else:
        form = AskQuestionForm()
    return render_to_response('qna/create.html', {
        'form':form,
    }, context_instance=RequestContext(request))
Esempio n. 5
0
def question_create(request):
    if request.method == 'POST':
        form = AskQuestionForm(request.POST, request.FILES)
        if form.is_valid():
            return HttpResponseRedirect(form.save().get_absolute_url())
    else:
        form = AskQuestionForm()
    return render_to_response('qna/create.html', {
        'form': form,
    },
                              context_instance=RequestContext(request))
Esempio n. 6
0
def contact():
    messages = defaultdict(list)
    form = AskQuestionForm()

    if request.method == 'POST':
        form_data = request.form
        form = AskQuestionForm(form_data)

        if form.validate() and verify_recaptcha(request):
            with mail.connect() as conn:
                user_msg = Message(
                    subject='Stara Guta: your message has been sent.',
                    body=USER_MESSAGE_TEMPLATE.format(
                        username=form_data['name'], body=form_data['body']),
                    sender=
                    ADMIN_EMAIL,  # MAIL_DEFAULT_SENDER doesn't work for some reason
                    recipients=[form_data['email']],
                )
                admin_msg = Message(
                    subject='New question from Stara Guta website',
                    body=ADMIN_MESSAGE_TEMPLATE.format(
                        username=form_data['name'],
                        email=form_data['email'],
                        body=form_data['body']),
                    sender=
                    ADMIN_EMAIL,  # MAIL_DEFAULT_SENDER doesn't work for some reason
                    recipients=["*****@*****.**"],
                )
                conn.send(admin_msg)
                conn.send(user_msg)
            return redirect(url_for('question_sent'))

        if not form.validate():
            for field, errors in form.errors.items():
                messages['errors'].append("{}: {}".format(
                    ','.join([e for e in errors]), field))

        if not verify_recaptcha(request):
            messages['errors'].append("Подтвердите что Вы не робот ;)")

    return render_template('contact.html', form=form, messages=messages)