Quiz

Форк
0
/
views.py 
98 строк · 2.7 Кб
1
from django.contrib import messages
2
from django.contrib.auth.decorators import login_required
3
from django.shortcuts import render, get_object_or_404
4
from django.views.generic import DetailView
5
from authapp.models import AuthUser
6
from games.models import Game
7

8

9
def index(request):
10
    pass
11

12

13
@login_required
14
def user_list(request):
15
    users = AuthUser.objects.all()
16
    context = {
17
        'users': users,
18
    }
19
    return render(request, 'user_profile/user_list.html', context)
20

21

22
@login_required
23
def view_friends(request):
24
    user = AuthUser.objects.get(username=request.user)
25
    friends = user.friends.all()
26
    context = {
27
        'friends': friends,
28
    }
29
    return render(request, 'user_profile/friends.html', context)
30

31

32
class UserDetailView(DetailView):
33
    model = AuthUser
34
    template_name = 'user_profile/profile.html'
35

36
    def get_context_data(self, **kwargs):
37
        context = super().get_context_data(**kwargs)
38
        context['title'] = AuthUser.objects.get(pk=self.kwargs['pk'])
39

40
        return context
41

42

43
@login_required
44
def manage_friends(request):
45
    user = request.user
46
    query = request.GET.get('q')
47
    action = request.POST.get('action')
48
    friend_username = request.POST.get('friend_username')
49

50
    if query:
51
        results = AuthUser.objects.filter(username__icontains=query).exclude(username=user)
52
    else:
53
        results = None
54

55
    if action == 'add':
56
        friend = get_object_or_404(AuthUser, username=friend_username)
57
        if friend == request.user:
58
            messages.error(request, "Вы не можете добавить себя в друзья")
59
        else:
60
            user.friends.add(friend)
61
    elif action == 'remove':
62
        friend = get_object_or_404(user.friends, username=friend_username)
63
        user.friends.remove(friend)
64

65
    context = {
66
        'user': user,
67
        'results': results,
68
    }
69
    return render(request, 'user_profile/manage_friends.html', context)
70

71

72
# @login_required
73
# def my_games(request):
74
#     user = request.user
75
#     objs = Game.objects.filter(players=user).order_by('-started')  # all() == filter(players=user).order_by('-started')
76
#     games = [x for x in objs if str(user.pk) in x.players]
77
#     context = {
78
#         'user': user,
79
#         'games': games,
80
#     }
81
#     return render(request, 'user_profile/profile.html', context)
82

83

84
def leaderboard(request):
85
    players = AuthUser.objects.order_by('-level', '-current_experience')[:5]
86
    return render(request, 'user_profile/leaderboard.html', {'players': players})
87

88

89
@login_required
90
def my_games(request):
91
    user = request.user
92
    obj = Game.objects.all()
93
    games = [x for x in obj if str(user.pk) in x.players]
94
    context = {
95
        'user': user,
96
        'games': games,
97
    }
98
    return render(request, 'user_profile/games_list.html', context)
99

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.