Beispiel #1
0
def get_post(request, post_id):
    try:
        if request.method == 'POST':
            form = CommentsForm(request.POST)
            if form.is_valid():
                st = "/post/" + str(post_id) + '/'
                form.save()
                return HttpResponseRedirect(st)
        else:
            form = CommentsForm()
        # выбираем конкретный пост, pk - primary key
        post = Game.objects.get(pk=post_id)
        post.show_times = post.show_times + 1
        post.save()
        comments = Comment.objects.filter(
            game=post_id).order_by("date").reverse()
        images = Screenshot.objects.filter(game=post_id)
        post_type = ContentType.objects.get_for_model(post)
        rating = Rating.objects.filter(content_type=post_type.id,
                                       object_id=post_id)
        print(len(rating))
        sum = 0.0
        votes = len(rating)
        if votes != 0:
            for rate in rating:
                sum = sum + rate.mark
            sum = sum / len(rating)

        count = len(images)
        images_list = list(images)
        first_img_link = ""
        if len(images_list) > 0:
            first_img_link = images_list[0].img
            images_list.pop(0)
        else:
            first_img_link = "screenshots/default.jpg"
        lists = []
        for i in range(0, count):
            lists.append(i)
    except Game.DoesNotExist:
        # если такого поста нет, то генерируем 404
        raise Http404
    # отрисовываем
    return render(
        request, 'single.html', {
            "title": post.title,
            "description": post.description,
            "comments": comments,
            "id": post.id,
            "images": images_list,
            "first_img_link": first_img_link,
            "count": lists,
            "rating": str(sum),
            "votes": votes,
            'form': form,
            'show_times': post.show_times
        })
Beispiel #2
0
def post_comment(request):
    if request.method == "POST":
        print(request.user)
        print(request.POST)
        print(request.user.id)
        form = CommentsForm(request.POST)
        if form.is_valid() and request.POST["id"] != "":
            # text_ = request.POST["text"]
            print("==========")
            print(request.POST["to_user_id"])
            print(request.POST["answer_to_comment_id"])
            if request.POST["to_user_id"] != "" and request.POST[
                    "answer_to_comment_id"] != "":
                to_user_id = MyUser.objects.get(
                    id=int(request.POST["to_user_id"]))
                print(to_user_id.email)
                answer_to = Comment.objects.get(
                    id=int(request.POST["answer_to_comment_id"]))
                print(answer_to)
                mes = "http://playnoch.cloudapp.net/post/" + str(
                    request.POST["id"]) + "/"
                print("==========")
                # send_mail("На ваш комментарий <<" + answer_to.text + ">>", mes, '*****@*****.**',[to_user_id.email], fail_silently=False)
                ###########
                subject, from_email, to = 'Ёу, ' + to_user_id.username, '*****@*****.**', to_user_id.email
                text_content = 'This is an important message.'
                html_content = "<div style=\'font-size:30px; color: red;\'>NOCH</div><pre>На твой комментарий, <a href=\'" + mes + "\'>ответил</a>  " + request.user.username + "</pre>"
                msg = EmailMultiAlternatives(subject, text_content, from_email,
                                             [to])
                msg.attach_alternative(html_content, "text/html")
                msg.send()
                #########

            game_id_ = request.POST["id"]

            # g = Game.objects.get(id=int(game_id_))
            # u = MyUser.objects.get(id=request.user.id)
            # e = Comment(text = text_)
            # g.comment_set.add(e)
            # u.comment_set.add(e)
            new_comment = form.save()
            g = Game.objects.get(id=int(game_id_))
            u = MyUser.objects.get(id=request.user.id)
            g.comment_set.add(new_comment)
            u.comment_set.add(new_comment)
            # comments = Comment.objects.filter(game=int(game_id_))
            # #print(comments)

            # objectQuerySet = Comment.objects.filter(game=int(game_id_))
            # data = serializers.serialize('json', objectQuerySet, fields=('user','text','date'))
            # data2 = json.loads(data)
            # for d in data2:
            #     d["fields"]["user"] = MyUser.objects.get(id=int(d["fields"]["user"])).username
            #     #print(d)
            # data = serializers.serialize('json', data2, fields=('user','text','date'))

            # print(data)
            # response = data

            profile_photo = ProfilePhoto.objects.filter(user=request.user.id)
            print(profile_photo)
            photo_src = "media/profile.png"
            print("2==============")
            print(profile_photo)
            if len(profile_photo) > 0:
                print("in")
                photo_src = profile_photo[0].img_src

            print("3==============")
            response = {
                'user': request.user.username,
                'date': new_comment.date.strftime('%Y-%m-%d %H:%M'),
                'text': new_comment.text,
                'profile_photo': photo_src,
                'status': 'OK',
            }
            print("4==============")
            print(response)
            print("valid")
        else:
            response = {
                'msg': 'lel',
                'status': 'bad',
            }
            print("invalid")
        return HttpResponse(json.dumps(response),
                            content_type="application/json")