def list_directories(request): """ List of all directories. Photos are segregated in directories, e.g.: Bialjam2016. """ template = loader.get_template('gallery/directories_list.html') directory_list = Directory.objects.all() paginator = Paginator(directory_list, 6) # Show 6 directories per page if len(directory_list) == 0: messages.error(request, _('No photos to show')) else: page = request.GET.get('page') try: directories = paginator.page(page) except PageNotAnInteger: directories = paginator.page(1) except EmptyPage: directories = paginator.page(paginator.num_pages) context = { 'directories': directories } return CustomHttpResponse.send(template, context, request) return CustomHttpResponse.send(template, {}, request)
def add_new_game(request): """ Adding new game """ template = loader.get_template('game/add_game.html') if request.method == 'POST': form = GameForm(request.user, request.POST) if form.is_valid(): game = form.save(commit=False) game.year = datetime.datetime.now().year game.team = request.user.profile.team if 'game_image' in request.FILES: game.image = request.FILES['game_image'] if request.POST.get('description_game'): game.description = request.POST.get('description_game') game.save() else: messages.error(request, _("Fill all fields")) return CustomHttpResponse.send(template, {}, request) return redirect('/games/team-games') else: if request.user.profile.team is None: return redirect('/team/new') form = GameForm(request.user) context = {'form': form} return CustomHttpResponse.send(template, context, request)
def remove_game(request, id): """ Remove game, which belong to user team """ template = loader.get_template('game/remove_game.html') context = {} if request.method == 'POST': Game.objects.filter(pk=id).delete() return redirect('/games/team-games') else: try: game = Game.objects.get(pk=id) except Game.DoesNotExist: messages.error(request, _("This game does not exist")) return CustomHttpResponse.send(template, context, request) user_team = request.user.profile.team if game.team.id is not user_team.id: messages.error(request, _("You can't remove this game, it's not belong to your team")) else: context = { 'game': game } return CustomHttpResponse.send(template, context, request)
def post_detail(request, id): """ Show detail chosen post """ template = loader.get_template('post/post_detail.html') try: post = Post.objects.get(pk=id) except Post.DoesNotExist: messages.error(request, _('Post does not exist')) return CustomHttpResponse.send(template, {}, request) context = {'post': post} return CustomHttpResponse.send(template, context, request)
def post_list(request): """ Return list of all posts. """ template = loader.get_template('post/post_list.html') posts = Post.objects.all().order_by('created_at') if posts is None: messages.error(request, _('No posts')) return CustomHttpResponse.send(template, {}, request) context = {'posts': posts} return CustomHttpResponse.send(template, context, request)
def team_games(request): """ Show all games, which belong to auth user """ template = loader.get_template('game/team_games.html') team = request.user.profile.team if team: games = Game.objects.filter(team=team).order_by('name') paginator = Paginator(games, 6) page = request.GET.get('page') try: games = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. games = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. games = paginator.page(paginator.num_pages) context = { 'team_games': games, 'team_name': team.name } return CustomHttpResponse.send(template, context, request) else: return redirect('/team/new')
def users_list(request): """ Show list of all users """ all_users = [] users = User.objects.all() for user in users: profile = Profile.objects.get(user=user) team = '' location = '' if profile.team is not None: team = profile.team.name if profile.location is not None: location = profile.location user_info = { 'id': user.id, 'username': user.username, 'first_name': user.first_name, 'last_name': user.last_name, 'team': team, 'image': profile.image, 'location': location } all_users.append(user_info) template = loader.get_template('account/users_list.html') context = { 'users_list': all_users, } return CustomHttpResponse.send(template, context, request)
def team_list(request): """ Show sorted list of all teams """ template = loader.get_template('team/team_list.html') teams_list = Team.objects.all().order_by('name') if not request.user.is_authenticated: team = None else: team = request.user.profile.team paginator = Paginator(teams_list, 6) page = request.GET.get('page') try: teams = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. teams = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. teams = paginator.page(paginator.num_pages) context = {'teams': teams, 'team': team} return CustomHttpResponse.send(template, context, request)
def join_team(request): """ Send message for chosen team with request to join """ template = loader.get_template('team/join_team.html') if request.method == 'POST': form = UserRequestForm(request.POST) user = request.user if form.is_valid(): for team in form.cleaned_data['teams']: request_user_team_obj = RequestUserTeam( team=team, user=user, message=request.POST.get('message')) request_user_team_obj.save() messages.success(request, _('Your requests have been sent.')) else: team = request.user.profile.team if team is None: form = UserRequestForm() else: return redirect( '/') # User which has a team can not see this template context = {'form': form} return CustomHttpResponse.send(template, context, request)
def game_details(request, id): """ Show game details """ template = loader.get_template('game/game_details.html') context = {} try: game = Game.objects.get(pk=id) except Game.DoesNotExist: messages.error(request, _("This game does not exist")) return CustomHttpResponse.send(template, context, request) context = { 'game': game } return CustomHttpResponse.send(template, context, request)
def vip_jury(request): """ List of vips. It's possible to choose: jury, lectures or both. """ template = loader.get_template('vip/vip.html') vip_list = Vip.objects.filter( guest_as__in=['JU', 'LE', 'BO']).order_by('name') context = {'vip_list': vip_list, 'title': _("Jury:")} return CustomHttpResponse.send(template, context, request)
def my_team(request): """ Show and edit user's team details with ability to leave the team """ template = loader.get_template('team/my_team.html') team = request.user.profile.team if team is not None: team_members = User.objects.filter(profile__team=team) context = { 'team_name': team.name, 'team_members': team_members, 'team_logo': team.logo, 'team_info': team.information } if request.POST.get('save'): if request.POST.get('new_name') != '': new_name = request.POST.get('new_name') team.name = new_name if 'logo_image' in request.FILES: team.logo = request.FILES['logo_image'] new_info = request.POST.get('new_info') team.information = new_info team.save() context['team_name'] = team.name context['team_info'] = team.information context['team_logo'] = team.logo if request.POST.get('save_name'): new_name = request.POST.get('new_name') team.name = new_name team.save() context['team_name'] = team.name if request.POST.get('save_info'): new_info = request.POST.get('new_info') team.information = new_info team.save() context['team_info'] = team.information if request.POST.get('save_logo'): team.logo = request.FILES['logo_image'] team.save() context['team_logo'] = team.logo if request.POST.get('leave_team'): request.user.profile.team = None request.user.profile.save() context = None return redirect('/') return CustomHttpResponse.send(template, context, request) else: return redirect('/team/new')
def view_information(request): """ Return information about contest """ try: info = Information.objects.last() except Information.DoesNotExist: messages.error(request, _('No information')) info = "" template = loader.get_template('information/information.html') context = {'info': info} return CustomHttpResponse.send(template, context, request)
def request_user(request): """ Return all user's requests to join user team with ability to add or remove request """ template = loader.get_template('team/request_user.html') team = request.user.profile.team if team is not None: team_name = team.name request_users = RequestUserTeam.objects.filter(team=team) context = {'team_name': team_name, 'request_users': request_users} if request.POST.get("add"): # Check quantity of users in team team_members = User.objects.filter(profile__team=team) if len(team_members) == 4: context['info'] = 'You have full team' return CustomHttpResponse.send(template, context, request) # Add user user_username = request.POST.get("add") new_member = User.objects.get(username=user_username) new_member.profile.team = team new_member.save() Team.request_users.through.objects.filter( user_id=new_member.id).delete() messages.success(request, _('User has been added.')) if request.POST.get("remove"): user_username = request.POST.get("remove") member = User.objects.get(username=user_username) Team.request_users.through.objects.filter( user_id=member.id).delete() return CustomHttpResponse.send(template, context, request) else: return CustomHttpResponse.send(template, None, request)
def remove_user(request): """ User remove his account """ if request.method == 'POST': rem = User.objects.get(username=request.user.username) if rem is not None: rem.is_active = False rem.save() return redirect('/home') template = loader.get_template('account/remove-user.html') return CustomHttpResponse.send(template, {}, request)
def vip_details(request, id): """ Vip details with information about lessons """ template = loader.get_template('vip/vip_details.html') try: vip = Vip.objects.get(id=id) lessons = Lesson.objects.filter(lecturer_id=id) context = {'vip': vip, 'lessons': lessons} except Vip.DoesNotExist: context = None return CustomHttpResponse.send(template, context, request)
def change_password(request): """ Changing password """ if request.method == 'POST': form = PasswordChangeForm(user=request.user, data=request.POST) if form.is_valid(): form.save() update_session_auth_hash(request, form.user) return redirect('/accounts/details') else: form = PasswordChangeForm(user=request.user) template = loader.get_template('account/change-password.html') context = {'form': form} return CustomHttpResponse.send(template, context, request)
def new(request): """ Create new team, it's possible to create one team with 4 members """ template = loader.get_template('team/new.html') if request.method == 'POST': form = TeamForm(request.user, request.POST) if form.is_valid(): team = form.save(commit=False) team.year = datetime.datetime.now().year if 'logo_image' in request.FILES: team.logo = request.FILES['logo_image'] if request.POST.get('team_info'): team.information = request.POST.get('team_info') team.save() # assign team to all members request.user.profile.team = team request.user.save() if form.cleaned_data['member2'] is not '': member2 = User.objects.get(pk=form.cleaned_data['member2']) member2.profile.team = team member2.save() if form.cleaned_data['member3'] is not '': member3 = User.objects.get(pk=form.cleaned_data['member3']) member3.profile.team = team member3.save() if form.cleaned_data['member4'] is not '': member4 = User.objects.get(pk=form.cleaned_data['member4']) member4.profile.team = team member4.save() messages.success(request, _('Your team has been created.')) else: if request.user.profile.team is not None: return redirect('/team/my-team') form = TeamForm(request.user) context = {'form': form} return CustomHttpResponse.send(template, context, request)
def users_details(request, pk): """ Show user details """ try: user = User.objects.get(pk=pk) except User.DoesNotExist: messages.error(request, 'Requested user does not exist!') return render( request, 'account/user_details.html', ) profile = Profile.objects.get(user=user) team = '' location = '' bio = '' if profile.team is not None: team = profile.team.name if profile.location is not None: location = profile.location if profile.bio is not None: bio = profile.bio user = { 'id': user.id, 'username': user.username, 'first_name': user.first_name, 'last_name': user.last_name, 'team': team, 'image': profile.image, 'location': location, 'bio': bio } template = loader.get_template('account/user_details.html') context = { 'user': user, } return CustomHttpResponse.send(template, context, request)
def team_details(request, id): """ Show chosen team details """ template = loader.get_template('team/details.html') try: team = Team.objects.get(pk=id) team_members = User.objects.filter(profile__team=team) context = { 'team_name': team.name, 'team_info': team.information, 'team_logo': team.logo, 'team_members': team_members, 'days': Information.getDaysToContest() } except Team.DoesNotExist: context = None return CustomHttpResponse.send(template, context, request)
def signup(request): """ Signup """ if request.method == 'POST': form = SignUpForm(request.POST) if form.is_valid(): user = form.save() user.refresh_from_db() user.profile.birth_date = form.cleaned_data.get('birth_date') user.save() raw_password = form.cleaned_data.get('password1') user = authenticate(username=user.username, password=raw_password) login(request, user) messages.success(request, _('Your account has been created.')) else: form = SignUpForm() template = loader.get_template('account/signup.html') context = {'form': form} return CustomHttpResponse.send(template, context, request)
def update_profile(request): """ Update profile """ profile = request.user.profile user_avatar = profile.image if request.POST.get('save_avatar') and 'avatar_image' in request.FILES: profile.image = request.FILES['avatar_image'] profile.save() user_avatar = profile.image if request.POST.get('save_fields'): user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, request.FILES, instance=profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() else: messages.error(request, _('Please correct the error below.')) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) template = loader.get_template('account/details.html') context = { 'user_form': user_form, 'profile_form': profile_form, 'user_avatar': user_avatar, } return CustomHttpResponse.send(template, context, request)
def games_list(request): """ Show all games """ template = loader.get_template('game/games_list.html') games = Game.objects.order_by('name') paginator = Paginator(games, 6) page = request.GET.get('page') try: games = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. games = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. games = paginator.page(paginator.num_pages) context = { 'games': games, } return CustomHttpResponse.send(template, context, request)
def list_photos(request, id): """ List of all photos in chosen directory """ template = loader.get_template('gallery/photos_list.html') directory = Directory.objects.get(id=id) photo_list = Image.objects.filter(directory=directory) paginator = Paginator(photo_list, 6) # Show 6 photos per page page = request.GET.get('page') try: photos = paginator.page(page) except PageNotAnInteger: photos = paginator.page(1) except EmptyPage: photos = paginator.page(paginator.num_pages) context = { 'photos': photos } return CustomHttpResponse.send(template, context, request)