Exemplo n.º 1
0
    def post(self, request):
        form = SignupForm(request.POST, request.FILES)
        if form.is_valid():
            # Creating user and profile
            try:
                with transaction.atomic():
                    new_user = User.objects.create(
                        username=form.cleaned_data['login'],
                        password=make_password(form.cleaned_data['password']),
                        email=form.cleaned_data['email'],
                        avatar=form.cleaned_data['avatar'],
                        reg_date=datetime.datetime.now())
                    if new_user:
                        MailSender().send(new_user.email,
                                          'sign_up',
                                          context={"login": new_user.username})
                return HttpResponseRedirect('/signup/done/')
            except Exception as error:
                message = 'Error while adding new user: '******'Error while adding new user, check fields '

        return render(request, "index/signup.html", {
            "form": form,
            "message": message,
            "trends": Trend.get_trends()
        })
Exemplo n.º 2
0
    def get(self, request):
        form = SignupForm()

        return render(request, "index/signup.html", {
            "form": form,
            "trends": Trend.get_trends()
        })
Exemplo n.º 3
0
    def get(self, request):
        quest_list = Question.objects.order_by('-pub_date')
        paginator = Paginator(quest_list, 20)
        page = request.GET.get('page')
        questions = paginator.get_page(page)

        return render(request, "question/index.html", {
            "questions": questions,
            "page": page,
            "trends": Trend.get_trends()
        })
Exemplo n.º 4
0
 def post(self, request):
     form = AskForm(request.POST)
     if form.is_valid():
         new_question_id = Question.add_question(form.cleaned_data,
                                                 request.user)
         return HttpResponseRedirect('/question/' + str(new_question_id) +
                                     '/')
     else:
         message = 'Error while adding'
         return render(request, "question/ask.html", {
             "form": form,
             "message": message,
             "trends": Trend.get_trends()
         })
Exemplo n.º 5
0
    def get(self, request):
        query = request.GET.get('q')
        quest_list = Question.objects.filter(
            Q(heading__icontains=query)
            | Q(content__icontains=query)).order_by('votes', '-pub_date')[:20]
        paginator = Paginator(quest_list, 20)
        page = request.GET.get('page')
        questions = paginator.get_page(page)

        return render(request, "question/search.html", {
            "questions": questions,
            "query": query,
            "trends": Trend.get_trends()
        })
Exemplo n.º 6
0
 def post(self, request):
     user_form = UserForm(request.POST,
                          request.FILES,
                          instance=request.user)
     if user_form.is_valid():
         with transaction.atomic():
             user_form.save()
         message = 'User settings successfully updated'
     else:
         message = 'Error while updating user'
     return render(
         request, "index/settings.html", {
             "user_form": user_form,
             "message": message,
             "trends": Trend.get_trends()
         })
Exemplo n.º 7
0
    def get(self, request, id):
        form = AnswerForm()
        quest = Question.objects.get(id=id)
        quest.active_user_vote = quest.active_vote(request.user.id)
        answers_set = Answer.get_answers(quest, request.user.id)

        page = request.GET.get('page')
        paginator = self.get_paginator(answers_set, 30)
        answers = paginator.get_page(page)

        return render(
            request, "question/question.html", {
                "trends": Trend.get_trends(),
                "form": form,
                "question": quest,
                "answers": answers,
            })
Exemplo n.º 8
0
 def post(self, request, id):
     if request.user.is_authenticated:
         form = AnswerForm(request.POST)
         if form.is_valid():
             quest_author_email = Answer.add(id, form.cleaned_data,
                                             request.user)
             url, link = make_question_url(id)
             Mailer.send(quest_author_email,
                         'new_answer',
                         context={"link": link})
             return HttpResponseRedirect(url)
         else:
             message = 'Error while adding'
             return render(
                 request, "question/question.html", {
                     "form": form,
                     "id": id,
                     "message": message,
                     "trends": Trend.get_trends()
                 })
Exemplo n.º 9
0
 def post(self, request, id):
     if request.user.is_authenticated:
         form = AnswerForm(request.POST)
         if form.is_valid():
             quest_author_email = Answer.add(id, form.cleaned_data,
                                             request.user)
             url = request.headers["Origin"] + '/question/%s/' % id
             link = '<a href="{}">{}</a>'.format(url, url)
             MailSender.send(quest_author_email,
                             'new_answer',
                             context={"link": link})
             return HttpResponseRedirect(url)
         else:
             message = 'Error while adding'
             return render(
                 request, "question/question.html", {
                     "form": form,
                     "id": id,
                     "message": message,
                     "trends": Trend.get_trends()
                 })
Exemplo n.º 10
0
    def get(self, request, tag):
        tag = str(tag).strip()
        if Tag.objects.filter(name=tag).exists():
            tag_elem = Tag.objects.get(name=tag)
            quest_list = Question.objects.filter(tags=tag_elem).order_by(
                'votes', '-pub_date')[:20]
            paginator = Paginator(quest_list, 20)
            page = request.GET.get('page')

            try:
                questions = paginator.get_page(page)
            except PageNotAnInteger:
                questions = paginator.get_page(1)
            except EmptyPage:
                questions = paginator.get_page(page)

        return render(
            request, "question/search.html", {
                "questions": questions,
                "query": "tag:{}".format(tag),
                "trends": Trend.get_trends()
            })
Exemplo n.º 11
0
    def get(self, request):
        page = request.GET.get('page')
        sort = request.GET.get('sort')
        if sort == 'date':
            sort_1 = '-pub_date'
            sort_2 = '-votes'
        else:
            sort_1 = '-votes'
            sort_2 = '-pub_date'
        quest_list = Question.objects.order_by(sort_1, sort_2)
        paginator = Paginator(quest_list, 20)

        try:
            questions = paginator.get_page(page)
        except PageNotAnInteger:
            questions = paginator.get_page(1)
        except EmptyPage:
            questions = paginator.get_page(page)

        return render(request, "question/list.html", {
            "questions": questions,
            "trends": Trend.get_trends()
        })
Exemplo n.º 12
0
 def get(self, request):
     form = AskForm()
     return render(request, "question/ask.html", {
         "trends": Trend.get_trends(),
         "form": form
     })
Exemplo n.º 13
0
 def get(self, request):
     return render(request, "index/settings.html",
                   {"trends": Trend.get_trends()})
Exemplo n.º 14
0
 def get(self, request):
     return render(request, "index/signup_done.html",
                   {"trends": Trend.get_trends()})
Exemplo n.º 15
0
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, re_path
from django.conf.urls import url, include
from django.contrib.auth import views as auth_views

from user.views import SignupView, SignupDoneView, UserSettingsView
from question.models import Trend
from question.views import IndexView, AskView, QuestionView, QuestionListView, VoteView, BestAnswerView, SearchView, TagView

urlpatterns = [
    path(
        'login/',
        auth_views.LoginView.as_view(
            extra_context={"trends": Trend.get_trends()})),
    path('logout/', auth_views.LogoutView.as_view()),
    path('signup/', SignupView.as_view()),
    path('signup/done/', SignupDoneView.as_view()),
    path('settings/', UserSettingsView.as_view()),
    path('ask/', AskView.as_view()),
    path('question/<int:id>/', QuestionView.as_view(), name="question"),
    path('question/vote/', VoteView.as_view(), name="question_vote"),
    path('question/best/', BestAnswerView.as_view(), name="question_best"),
    path('question/list/', QuestionListView.as_view(), name="question_vote"),
    path('search/', SearchView.as_view(), name="question_search"),
    path('tag/<str:tag>/', TagView.as_view(), name="question_tag"),
    path('admin/', admin.site.urls),
    url(r'^api/', include(('api.urls', 'api'), namespace='api')),
    path('', IndexView.as_view()),
]