Exemplo n.º 1
0
def user(username):
    page = request.args.get('page', 1, type=int)
    user = User.query.filter_by(username=username).first_or_404()
    if current_user.username == username:
        return redirect(
            url_for('index'))  # Need to be changed on 'Profile' page
    form = AskForm()
    if form.validate_on_submit():
        q = Question(
            body=form.body.data,
            anonymous=form.is_anonymous.data,
            sender_id=current_user.id,
            receiver_id=User.query.filter_by(username=username).first().id,
            answered=False)
        db.session.add(q)
        db.session.commit()
        flash('Your question was sent!')
        return (redirect('/user/' + username))
    questions = user.received.filter_by(answered=True).order_by(-Question.answer_timestamp)\
                .paginate(page, app.config['QUESTIONS_PER_PAGE'], False)
    next_url = url_for('user', username=username, page=questions.next_num) \
        if questions.has_next else None
    prev_url = url_for('user', username=username, page=questions.prev_num) \
        if questions.has_prev else None
    return render_template('user.html',
                           user=user,
                           questions=questions.items,
                           page=user.username,
                           form=form,
                           next_url=next_url,
                           prev_url=prev_url)
Exemplo n.º 2
0
def ask():
    getUser()
    form = AskForm()
    if session['qCount'] >= 5:
        return redirect('/')
    if form.validate_on_submit():
        q = Question(id=str(uuid.uuid4()),body=request.form['question'],optionOne=request.form['answerOne'],optionTwo=request.form['answerTwo'])
        q.author=db.session.query(User).filter_by(ip=session['user']).first()
        session['qCount']+=1
        db.session.add(q)
        db.session.commit()
        return redirect('/')
    return render_template('ask.html',form=form)
Exemplo n.º 3
0
def ask(request):
    if request.method == 'GET':
        form = AskForm()
    else:
        form = AskForm(data=request.POST)
        question = form.save(commit=False)
        question.author = request.user.author
        form.instance.user = Author.objects.get(user=request.user)
        question.identificator = Question.objects.all().count() + 1  # TODO
        question.save()
        tags_ = form.cleaned_data.get("tags")
        question.tags.add(*tags_)
        question.save()
        return redirect(reverse('askcats_question', kwargs={'no': question.identificator}))
    ctx = {'form': form}
    return render(request, 'ask.html', ctx)
Exemplo n.º 4
0
def ask(request):
    form = AskForm(request.POST)

    if request.POST:
        if form.is_valid():
            tags = form.cleaned_data.get('tags')
            if tags:
                tags = tags.split()
                Tag.objects.create_from_list(tags)

            question = Question.objects.create(
                title=form.cleaned_data.get('title'),
                text=form.cleaned_data.get('text'),
                author=request.user.profile)
            question.add_tags(tags)
            return redirect(reverse('question', args=[question.id]))

    context['form'] = form
    return render(request, 'ask.html', context)
Exemplo n.º 5
0
 def post(self, request):
     form = AskForm(request.POST)
     if not form.is_valid():
         return render(request, 'ask.html', {'form': form})
     new_question = form.save(commit=False)
     new_question.author = request.user.user
     new_question.save()
     form.save_m2m()
     return HttpResponseRedirect(
         reverse('question', args=(new_question.id, )))
Exemplo n.º 6
0
def ask(request):
    tags = Tag.objects.count_popular()
    if request.method == "POST":
        form = AskForm(request.POST)
        if form.is_valid():
            q = form.save(request.user)
            return HttpResponseRedirect(
                reverse('question', kwargs={'question_id': q.id}))
    else:
        form = AskForm()
    return render(request, 'ask.html', {'tags': tags, 'form': form})
Exemplo n.º 7
0
def new_question(request):
    if request.method == 'GET':
        form = AskForm()
    elif request.method == 'POST':
        form = AskForm(data=request.POST)
        if form.is_valid():
            question = form.save(commit=False)
            question.author = request.user.profile
            question.save()
            tags = form.cleaned_data.get('tags').split()
            return redirect(reverse('question', kwargs={'qid': question.id}))
    return render(request, 'new_question.html', {
        'form': form,
    })
Exemplo n.º 8
0
def ask(request):
    if request.method == "GET":
        form = AskForm()
    if request.method == "POST":
        form = AskForm(data=request.POST)
        if form.is_valid():
            ask_question = form.save(commit=False)
            ask_question.author = Users.objects.get(user=request.user.pk)
            ask_question.save()
            tags = form.cleaned_data['tags'].split(' ')
            for one_tag in tags:
                customer, created = Tags.objects.get_or_create(title=one_tag)
                ask_question.tags.add(customer)

            return redirect(reverse('question', kwargs={'id':
                                                        ask_question.pk}))
    ctx = {'form': form}
    return render(request, 'question.html', ctx)
Exemplo n.º 9
0
 def get(self, request):
     form = AskForm()
     return render(request, 'ask.html', {'form': form})