Quiz
98 строк · 2.7 Кб
1from django.contrib import messages2from django.contrib.auth.decorators import login_required3from django.shortcuts import render, get_object_or_4044from django.views.generic import DetailView5from authapp.models import AuthUser6from games.models import Game7
8
9def index(request):10pass11
12
13@login_required
14def user_list(request):15users = AuthUser.objects.all()16context = {17'users': users,18}19return render(request, 'user_profile/user_list.html', context)20
21
22@login_required
23def view_friends(request):24user = AuthUser.objects.get(username=request.user)25friends = user.friends.all()26context = {27'friends': friends,28}29return render(request, 'user_profile/friends.html', context)30
31
32class UserDetailView(DetailView):33model = AuthUser34template_name = 'user_profile/profile.html'35
36def get_context_data(self, **kwargs):37context = super().get_context_data(**kwargs)38context['title'] = AuthUser.objects.get(pk=self.kwargs['pk'])39
40return context41
42
43@login_required
44def manage_friends(request):45user = request.user46query = request.GET.get('q')47action = request.POST.get('action')48friend_username = request.POST.get('friend_username')49
50if query:51results = AuthUser.objects.filter(username__icontains=query).exclude(username=user)52else:53results = None54
55if action == 'add':56friend = get_object_or_404(AuthUser, username=friend_username)57if friend == request.user:58messages.error(request, "Вы не можете добавить себя в друзья")59else:60user.friends.add(friend)61elif action == 'remove':62friend = get_object_or_404(user.friends, username=friend_username)63user.friends.remove(friend)64
65context = {66'user': user,67'results': results,68}69return 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
84def leaderboard(request):85players = AuthUser.objects.order_by('-level', '-current_experience')[:5]86return render(request, 'user_profile/leaderboard.html', {'players': players})87
88
89@login_required
90def my_games(request):91user = request.user92obj = Game.objects.all()93games = [x for x in obj if str(user.pk) in x.players]94context = {95'user': user,96'games': games,97}98return render(request, 'user_profile/games_list.html', context)99