Exemplo n.º 1
0
 def get(self, request, slug):
     if self.context:
         return self.context
     else:
         self.context = self.model.__name__.lower()
     obj = go404(self.model, slug__iexact=slug)
     return render(request, self.template, context={self.context: obj})
Exemplo n.º 2
0
def hint_upvote(request, pk):
    hint = go404(Hint, pk=pk)
    upvote, created = HintUpvote.objects.get_or_create(user=request.user, hint=hint)
    if created:
        hint.upvotes += 1
        hint.save()
        messages.info(request, "upvoted #{}".format(pk))
    else:
        messages.warning(request, "You've upvoted this already!")
    return redirect(hint.problem)
Exemplo n.º 3
0
def code_upvote(request, pk):
    code = go404(Code, pk=pk)
    upvote, created = Upvote.objects.get_or_create(user=request.user, code=code)
    if created:
        code.upvotes += 1
        code.save()
        messages.info(request, "upvoted #{}".format(pk))
    else:
        messages.warning(request, "You've upvoted this already!")
    return redirect(code.problem)
Exemplo n.º 4
0
def hint_upvote(request, pk):
    hint = go404(Hint, pk=pk)
    upvote, created = HintUpvote.objects.get_or_create(user=request.user,
                                                       hint=hint)
    if created:
        hint.upvotes += 1
        hint.save()
        messages.info(request, "upvoted #{}".format(pk))
    else:
        messages.warning(request, "You've upvoted this already!")
    return redirect(hint.problem)
Exemplo n.º 5
0
def code_upvote(request, pk):
    code = go404(Code, pk=pk)
    upvote, created = Upvote.objects.get_or_create(user=request.user,
                                                   code=code)
    if created:
        code.upvotes += 1
        code.save()
        messages.info(request, "upvoted #{}".format(pk))
    else:
        messages.warning(request, "You've upvoted this already!")
    return redirect(code.problem)
Exemplo n.º 6
0
def problem_share(request, oj_id):
    problem = go404(Problem, oj_id=oj_id)
    try:
        thecodeitself = oj.fetch_ac_code(request.user.username, request.session['password'], oj_id)
    except oj.YouNoAc:
        messages.warning(request, "You have not AC'd this problem.")
        return redirect(problem)
    code, created = Code.objects.get_or_create(problem=problem, user=request.user)
    code.text = thecodeitself
    code.save()
    messages.info(request, 'Successfully shared code of problem {}'.format(oj_id))
    return redirect(problem)
Exemplo n.º 7
0
def hint_share(request, oj_id):
    problem = go404(Problem, oj_id=oj_id)
    if not Code.objects.filter(problem=problem, user=request.user).exists():
        messages.warning(request, "You cannot share a hint until you share your AC code")
        return redirect(problem)
    form = HintForm(request.POST)
    if form.is_valid():
        hint, created = Hint.objects.get_or_create(user=request.user, problem=problem)
        hint.text = form.cleaned_data['hint']
        hint.save()
    else:
        messages.error(request, 'invalid input.')
    return redirect(problem)
Exemplo n.º 8
0
def vote(request, question_id):
    question = go404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST["choice"])
    except (KeyError, Choice.DoesNotExist):
        return render(
            request,
            "polls/detail.html",
            {"question": question, "error_message": "You didn't select a choice."},
        )
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
Exemplo n.º 9
0
def problem_share(request, oj_id):
    problem = go404(Problem, oj_id=oj_id)
    try:
        thecodeitself = oj.fetch_ac_code(request.user.username,
                                         request.session['password'], oj_id)
    except oj.YouNoAc:
        messages.warning(request, "You have not AC'd this problem.")
        return redirect(problem)
    code, created = Code.objects.get_or_create(problem=problem,
                                               user=request.user)
    code.text = thecodeitself
    code.save()
    messages.info(request,
                  'Successfully shared code of problem {}'.format(oj_id))
    return redirect(problem)
Exemplo n.º 10
0
def hint_share(request, oj_id):
    problem = go404(Problem, oj_id=oj_id)
    if not Code.objects.filter(problem=problem, user=request.user).exists():
        messages.warning(
            request, "You cannot share a hint until you share your AC code")
        return redirect(problem)
    form = HintForm(request.POST)
    if form.is_valid():
        hint, created = Hint.objects.get_or_create(user=request.user,
                                                   problem=problem)
        hint.text = form.cleaned_data['hint']
        hint.save()
    else:
        messages.error(request, 'invalid input.')
    return redirect(problem)
Exemplo n.º 11
0
def profile(request, pk):
    # XXX
    # PLEASE AWARE PERFORMANCE ISSUES HERE
    user = go404(User, pk=pk)
    user_code = Code.objects.filter(user=user)
    user_hint = Hint.objects.filter(user=user)
    shares = user_code.count()
    share_upvotes = Upvote.objects.filter(code__in=user_code).count()
    hints = user_hint.count()
    hint_upvotes = HintUpvote.objects.filter(hint__in=user_hint).count()
    return render(
        request, 'profile.html', {
            'user': user,
            'shares': shares,
            'share_upvotes': share_upvotes,
            'hints': hints,
            'hint_upvotes': hint_upvotes,
            'sum': shares + share_upvotes + hints + hint_upvotes
        })
Exemplo n.º 12
0
def profile(request, pk):
    # XXX
    # PLEASE AWARE PERFORMANCE ISSUES HERE
    user = go404(User, pk=pk)
    user_code = Code.objects.filter(user=user)
    user_hint = Hint.objects.filter(user=user)
    shares = user_code.count()
    share_upvotes = Upvote.objects.filter(code__in=user_code).count()
    hints = user_hint.count()
    hint_upvotes = HintUpvote.objects.filter(hint__in=user_hint).count()
    return render(
        request,
        "profile.html",
        {
            "user": user,
            "shares": shares,
            "share_upvotes": share_upvotes,
            "hints": hints,
            "hint_upvotes": hint_upvotes,
            "sum": shares + share_upvotes + hints + hint_upvotes,
        },
    )