def clean_currentpassword(self): currentPassword = self.cleaned_data.get('currentpassword') if not currentPassword: raise forms.ValidationError(strLang()['error_change_password_empty_field']) else: if not self.request.user.check_password(currentPassword): raise forms.ValidationError(strLang()['error_current_password_incorrect']) return currentPassword
def clean(self): cleaned_data = super(RegisterForm, self).clean() password = self.cleaned_data.get('password') password2 = self.cleaned_data.get('password2') if password != password2: self._errors["password"] = self.error_class([strLang()['error_register_passwords_differents']]) self._errors["password2"] = self.error_class([strLang()['error_register_passwords_differents']]) return cleaned_data
def clean(self): cleaned_data = super(RecoverNewPasswordForm, self).clean() password = self.cleaned_data.get('newpassword') password2 = self.cleaned_data.get('newpassword2') if password != password2: self._errors["newpassword"] = self.error_class([strLang()['error_change_password_differents']]) self._errors["newpassword2"] = self.error_class([strLang()['error_change_password_differents']]) return cleaned_data
def clean(self): cleaned_data = super(LoginForm, self).clean() username = self.cleaned_data.get('username') password = self.cleaned_data.get('password') user = authenticate(username=username, password=password) if user is not None: if not user.is_active: raise forms.ValidationError(strLang()['error_login_failed']) else: raise forms.ValidationError(strLang()['error_login_failed']) return cleaned_data
def mylogin(request): # Load strings language to template login.html try: lang = strLang() except: lang = '' if request.method == 'POST': # If the form has been submitted... loginForm = LoginForm(request.POST) # A form bound to the POST data if loginForm.is_valid(): # All validation rules pass user = authenticate(username=request.POST['username'], password=request.POST['password']) login(request, user) return HttpResponseRedirect('/my-tournaments/') #return HttpResponseRedirect('/') else: registerForm = RegisterForm() return render_to_response('login.html', { 'loginForm': loginForm, 'registerForm': registerForm, 'lang': lang, }) else: loginForm = LoginForm() # An unbound form registerForm = RegisterForm() return render_to_response('login.html', { 'loginForm': loginForm, 'registerForm': registerForm, 'lang': lang, })
def clean_name(self): name = self.cleaned_data.get('name') try: tournament = Tournament.objects.get(name=name) except Tournament.DoesNotExist: return name raise forms.ValidationError(strLang()['error_new_tournament_name_exists'])
def clean_username(self): username = self.cleaned_data.get('username') try: user = User.objects.get(username=username) except User.DoesNotExist: return username raise forms.ValidationError(strLang()['error_register_username_exists'])
def ActiveTournaments(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/mobile') #activeTournaments = Tournament.objects.filter(Q(finish_date__gte = datetime.datetime.now())).order_by('-start_date') activeTournaments = Tournament.objects.filter(start_date__lte = datetime.date.today(), finish_date__gte = datetime.date.today()).order_by('-start_date') tournamentCategories = [] for t in activeTournaments: categories = t.categories.all() c = {} c['tid'] = t.pk c['categories'] = categories tournamentCategories.append(c) # Load strings language to template mytournaments.html try: lang = strLang() except: lang = '' return render_to_response('mobile/active-tournaments.html', { 'activeTournaments': activeTournaments, 'tournamentCategories': tournamentCategories, 'lang': lang, })
def clean_email(self): email = self.cleaned_data.get('email') try: email = User.objects.get(email=email) except User.DoesNotExist: return email raise forms.ValidationError(strLang()['error_register_email_exists'])
def NextTournaments(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/mobile') nextTournaments = Tournament.objects.filter(start_date__gt = datetime.date.today()) tournamentCategories = [] for t in nextTournaments: categories = t.categories.all() c = {} c['tid'] = t.pk c['categories'] = categories tournamentCategories.append(c) # Load strings language to template mytournaments.html try: lang = strLang() except: lang = '' return render_to_response('mobile/next-tournaments.html', { 'nextTournaments': nextTournaments, 'tournamentCategories': tournamentCategories, 'lang': lang, })
def LostGames(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/') # Select all won games lostGames = Game.objects.filter(Q(player1 = request.user) | Q(player2 = request.user), ~Q(winner = request.user), Q(log=True)).order_by('-start_time') template = 'custom/lost-games.html' if request.mobile: template = 'mobile/lost-games.html' # Load strings language to template mynextgames.html try: lang = strLang() except: lang = '' up = UserProfile.objects.get(user=request.user) timezone = up.timezone return render_to_response(template, { 'user_me': request.user, 'timezone': timezone, 'lang': lang, 'lostGames': lostGames, })
def MyTournaments(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/mobile') dateNow = datetime.datetime.now() myTournaments = Tournament.objects.filter(players = request.user).order_by('-start_date') tournamentCategories = [] for t in myTournaments: categories = t.categories.all() c = {} c['tid'] = t.pk c['categories'] = categories tournamentCategories.append(c) # Load strings language to template mytournaments.html try: lang = strLang() except: lang = '' return render_to_response('mobile/my-tournaments.html', { 'myTournaments': myTournaments, 'tournamentCategories': tournamentCategories, 'lang': lang, })
def NextTournaments(request): if not request.user.is_authenticated(): return HttpResponseRedirect("/") nextTournaments = Tournament.objects.filter(start_date__gt=datetime.date.today()) nextTournaments_format = [] for t in nextTournaments: nextTournament = { "days_per_round": t.days_per_round, "start_date": t.start_date, "finish_date": t.finish_date, "rounds": t.rounds, "name": t.name, "pk": t.pk, } categories = " - " for c in t.categories.all(): categories += c.name + " - " nextTournament["categories"] = categories nextTournaments_format.append(nextTournament) # tournamentCategories = [] # # for t in nextTournaments: # categories = t.categories.all() # c = {} # c['tid'] = t.pk # c['categories'] = categories # tournamentCategories.append(c) # Load strings language to template mytournaments.html try: lang = strLang() except: lang = "" template = get_template("custom/next-tournaments.html") if request.mobile: template = get_template("mobile/next-tournaments.html") return HttpResponse( template.render( RequestContext( request, { "user_me": request.user, "nextTournaments": nextTournaments_format, #'tournamentCategories': tournamentCategories, "lang": lang, }, ) ) )
def clean_username(self): username = self.cleaned_data.get('username') try: user = User.objects.get(username=username) if user: return username except User.DoesNotExist: raise forms.ValidationError(strLang()['error_recover_user_unknown'])
def clean_email(self): email = self.cleaned_data.get('email') try: user = User.objects.get(email=email) if user: return email except User.DoesNotExist: raise forms.ValidationError(strLang()['error_recover_email_unknown'])
def ActiveTournaments(request): if not request.user.is_authenticated(): return HttpResponseRedirect("/") # activeTournaments = Tournament.objects.filter(Q(finish_date__gte = datetime.datetime.now())).order_by('-start_date') activeTournaments = Tournament.objects.filter( start_date__lte=datetime.date.today(), finish_date__gte=datetime.date.today() ).order_by("-start_date") activeTournaments_format = [] for t in activeTournaments: activeTournament = { "days_per_round": t.days_per_round, "start_date": t.start_date, "finish_date": t.finish_date, "rounds": t.rounds, "name": t.name, "pk": t.pk, } categories = " - " for c in t.categories.all(): categories += c.name + " - " activeTournament["categories"] = categories activeTournaments_format.append(activeTournament) # tournamentCategories = [] # for t in activeTournaments: # categories = t.categories.all() # c = {} # c['tid'] = t.pk # c['categories'] = categories # tournamentCategories.append(c) # Load strings language to template mytournaments.html try: lang = strLang() except: lang = "" template = "custom/active-tournaments.html" if request.mobile: template = "mobile/active-tournaments.html" return render_to_response( template, { "user_me": request.user, "activeTournaments": activeTournaments_format, #'tournamentCategories': tournamentCategories, "lang": lang, }, )
def recoverAccount(request): if request.user.is_authenticated(): return HttpResponseRedirect('/my-tournaments/') try: lang = strLang() except: lang = '' # Info about user user_me = request.user if request.method == 'POST': recoverUserForm = RecoverUserForm(request.POST, request=request) # A form bound to the POST data if recoverUserForm.is_valid(): user = User.objects.get(username=request.POST['username'], email=request.POST['email']) rkey = "".join(random.sample(string.letters+string.digits, 16)) recoverUser = RecoverUser(user=user,code=rkey) recoverUser.save() # We show a notification user try: box = setBox('success_recover_account_email_sent') except: box = '' recoverUserForm = RecoverUserForm() notify_user(user, 'recover_user', recoverUser) return render_to_response('recoveruser.html', { 'user_me': user_me, 'recoverUserForm': recoverUserForm, 'box': box, 'lang': lang, }) else: return render_to_response('recoveruser.html', { 'user_me': user_me, 'recoverUserForm': recoverUserForm, 'lang': lang, }) else: # If get request, generate a new form recoverUserForm = RecoverUserForm() # Must we show a notification user? try: if request.GET['status']: box = setBox(request.GET['status']) except: box = '' return render_to_response('recoveruser.html', { 'user_me': user_me, 'recoverUserForm': recoverUserForm, 'lang': lang, 'box': box, })
def clean(self): cleaned_data = super(RecoverUserForm, self).clean() username = self.cleaned_data.get('username') email = self.cleaned_data.get('email') userName = '' userEmail = '' try: userName = User.objects.get(username=username) userEmail = User.objects.get(email=email) if userName == userEmail and userName != '' and userEmail != '': return cleaned_data except User.DoesNotExist: if userName: self._errors["email"] = self.error_class([strLang()['error_recover_email_unknown']]) if userEmail: self._errors["username"] = self.error_class([strLang()['error_recover_user_unknown']]) return cleaned_data
def ranking(request): # SIDEBAR INFO if request.user.is_authenticated(): myTournaments = myActiveTournaments(request) myAdmnTournaments = myAdminTournaments(request) todayGames = myTodayGames(request) nextGames = myNextGames(request) user_me = UserProfile.objects.get(user=request.user) pendingQR = myAdminPendingQuestionReviews(request.user) else: myTournaments = '' myAdmnTournaments = '' todayGames = '' nextGames = '' user_me = request.user pendingQR = 0 if request.user.has_perm('fleq.quizbowl.add_tournament'): admin_user = True else: admin_user = False # Load strings language to template newquestionreview.html try: lang = strLang() except: lang = '' # Select all stats by users users = UserProfile.objects.all().order_by('-winner_games') ranking = [] for user in users: u = {} u['user'] = user u['total_games'] = user.winner_games + user.loser_games u['winner_games'] = user.winner_games u['loser_games'] = user.loser_games if user.winner_games + user.loser_games == 0: u['winner_total_ratio'] = 0 else: u['winner_total_ratio'] = (user.winner_games/(user.winner_games + user.loser_games)) ranking.append(u) return render_to_response('ranking.html', { 'user_me': user_me, 'myTournaments': myTournaments, 'myAdminTournaments': myAdmnTournaments, 'todayGames': todayGames, 'nextGames': nextGames, 'lang': lang, 'ranking': ranking, 'admin_user': admin_user, 'pendingQR': pendingQR, })
def MyTournaments(request): if not request.user.is_authenticated(): return HttpResponseRedirect("/") dateNow = datetime.datetime.now() myTournaments = Tournament.objects.filter(players=request.user).order_by("-start_date") myTournaments_format = [] for t in myTournaments: myTournament = { "days_per_round": t.days_per_round, "start_date": t.start_date, "finish_date": t.finish_date, "rounds": t.rounds, "name": t.name, "pk": t.pk, } categories = " - " for c in t.categories.all(): categories += c.name + " - " myTournament["categories"] = categories myTournaments_format.append(myTournament) # tournamentCategories = [] # # for t in myTournaments: # categories = t.categories.all() # c = {} # c['tid'] = int(t.pk) # c['categories'] = categories # tournamentCategories.append(c) # Load strings language to template mytournaments.html try: lang = strLang() except: lang = "" template = "custom/my-tournaments.html" if request.mobile: template = "mobile/my-tournaments.html" return render_to_response( template, { "user_me": request.user, "myTournaments": myTournaments_format, #'tournamentCategories': tournamentCategories, "lang": lang, }, )
def myQuestionReviews(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/register') # Load strings language to template questionreviews.html try: lang = strLang() except: lang = '' if request.user.has_perm('fleq.quizbowl.add_tournament'): admin_user = True else: admin_user = False # SIDEBAR AND MENU INFO myTournaments = myActiveTournaments(request) myAdmnTournaments = myAdminTournaments(request) todayGames = myTodayGames(request) nextGames = myNextGames(request) user_me = UserProfile.objects.get(user=request.user) pendingQR = myAdminPendingQuestionReviews(request.user) # My personal question reviews myQuestionReviews = Question_review.objects.filter(player = request.user).order_by('-pk') # Admin question reviews myAdminQuestionReviews = [] allMyAdminTournaments = Tournament.objects.filter(admin=user_me.user) if allMyAdminTournaments: # All reviews in my Tournaments for tournament in allMyAdminTournaments: questionReviews = Question_review.objects.filter(game__round__tournament = tournament).order_by('-pk') for qr in questionReviews: myAdminQuestionReviews.append(qr) # Must we show a notification user? try: if request.GET['status']: box = setBox(request.GET['status']) except: box = '' return render_to_response('myquestionreviews.html', { 'user_me': user_me, 'myQuestionReviews': myQuestionReviews, 'myAdminQuestionReviews': myAdminQuestionReviews, 'lang': lang, 'box': box, 'myTournaments': myTournaments, 'myAdminTournaments': myAdmnTournaments, 'todayGames': todayGames, 'nextGames': nextGames, 'pendingQR': pendingQR, 'admin_user': admin_user, })
def questionReview(request,qrid): if not request.user.is_authenticated(): return HttpResponseRedirect('/register') if request.user.has_perm('fleq.quizbowl.add_tournament'): admin_user = True else: admin_user = False # Check if exists try: questionReview = Question_review.objects.get(pk = qrid) except: questionReview = '' # SIDEBAR INFO myTournaments = myActiveTournaments(request) myAdmnTournaments = myAdminTournaments(request) todayGames = myTodayGames(request) nextGames = myNextGames(request) user_me = UserProfile.objects.get(user=request.user) pendingQR = myAdminPendingQuestionReviews(request.user) # Load strings language to template questionreviews.html try: lang = strLang() except: lang = '' # Only for admin reply if request.method == "POST" and request.POST['resolution']: questionReview.resolution = request.POST['resolution'] questionReview.save() notify_user(questionReview.player, 'review_closed', questionReview) return HttpResponseRedirect('/question-review/' + qrid + '?status=success_resolution') else: # Must we show a notification user? try: if request.GET['status']: box = setBox(request.GET['status']) except: box = '' return render_to_response('questionreview.html', { 'user_me': user_me, 'questionReview': questionReview, 'lang': lang, 'box': box, 'myTournaments': myTournaments, 'myAdminTournaments': myAdmnTournaments, 'todayGames': todayGames, 'nextGames': nextGames, 'admin_user': admin_user, 'pendingQR': pendingQR, })
def recoverAccountNewPassword(request, ruid): try: lang = strLang() except: lang = '' ruid = ruid.replace("#", "") if request.method == 'POST': newPasswordForm = RecoverNewPasswordForm(request.POST, request=request) # A form bound to the POST data if newPasswordForm.is_valid(): try: recoverUser = RecoverUser.objects.get(code=ruid) user = recoverUser.user user.set_password(request.POST['newpassword']) user.save() # Delete all requests from user to change password allRecoverUser = RecoverUser.objects.filter(user=user) for recoverUser in allRecoverUser: recoverUser.delete() except RecoverUser.DoesNotExist: return HttpResponseRedirect('/recover-account/?status=error_unknown_code') return HttpResponseRedirect('/register/?status=success_change_password') else: return render_to_response('recovernewpassword.html', { 'newPasswordForm': newPasswordForm, 'lang': lang, 'ruid': ruid, }) else: # If get request, generate a new form newPasswordForm = RecoverNewPasswordForm() try: recoverUser = RecoverUser.objects.get(code=ruid) except RecoverUser.DoesNotExist: return HttpResponseRedirect('/recover-account/?status=error_unknown_code') # Must we show a notification user? try: if request.GET['status']: box = setBox(request.GET['status']) except: box = '' return render_to_response('recovernewpassword.html', { 'newPasswordForm': newPasswordForm, 'lang': lang, 'ruid': ruid, })
def MyTournaments(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/') dateNow = datetime.datetime.now() myTournaments = Tournament.objects.filter(players = request.user).order_by('-start_date') myTournaments_format = [] for t in myTournaments: myTournament = {'days_per_round': t.days_per_round, 'start_date': t.start_date, 'finish_date': t.finish_date, 'rounds': t.rounds, 'name': t.name, 'pk': t.pk} categories = " - " for c in t.categories.all(): categories += c.name + " - " myTournament['categories'] = categories myTournaments_format.append(myTournament) # tournamentCategories = [] # # for t in myTournaments: # categories = t.categories.all() # c = {} # c['tid'] = int(t.pk) # c['categories'] = categories # tournamentCategories.append(c) # Load strings language to template mytournaments.html try: lang = strLang() except: lang = '' template = 'custom/my-tournaments.html' if request.mobile: template = 'mobile/my-tournaments.html' return render_to_response(template, { 'user_me': request.user, 'myTournaments': myTournaments_format, #'tournamentCategories': tournamentCategories, 'lang': lang, })
def ActiveTournaments(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/') #activeTournaments = Tournament.objects.filter(Q(finish_date__gte = datetime.datetime.now())).order_by('-start_date') activeTournaments = Tournament.objects.filter(start_date__lte = datetime.date.today(), finish_date__gte = datetime.date.today()).order_by('-start_date') activeTournaments_format = [] for t in activeTournaments: activeTournament = {'days_per_round': t.days_per_round, 'start_date': t.start_date, 'finish_date': t.finish_date, 'rounds': t.rounds, 'name': t.name, 'pk': t.pk} categories = " - " for c in t.categories.all(): categories += c.name + " - " activeTournament['categories'] = categories activeTournaments_format.append(activeTournament) # tournamentCategories = [] # for t in activeTournaments: # categories = t.categories.all() # c = {} # c['tid'] = t.pk # c['categories'] = categories # tournamentCategories.append(c) # Load strings language to template mytournaments.html try: lang = strLang() except: lang = '' template = 'custom/active-tournaments.html' if request.mobile: template = 'mobile/active-tournaments.html' return render_to_response(template, { 'user_me': request.user, 'activeTournaments': activeTournaments_format, #'tournamentCategories': tournamentCategories, 'lang': lang, })
def NextTournaments(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/') nextTournaments = Tournament.objects.filter(start_date__gt = datetime.date.today()) nextTournaments_format = [] for t in nextTournaments: nextTournament = {'days_per_round': t.days_per_round, 'start_date': t.start_date, 'finish_date': t.finish_date, 'rounds': t.rounds, 'name': t.name, 'pk': t.pk} categories = " - " for c in t.categories.all(): categories += c.name + " - " nextTournament['categories'] = categories nextTournaments_format.append(nextTournament) # tournamentCategories = [] # # for t in nextTournaments: # categories = t.categories.all() # c = {} # c['tid'] = t.pk # c['categories'] = categories # tournamentCategories.append(c) # Load strings language to template mytournaments.html try: lang = strLang() except: lang = '' template = get_template('custom/next-tournaments.html') if request.mobile: template = get_template('mobile/next-tournaments.html') return HttpResponse(template.render(RequestContext(request, { 'user_me': request.user, 'nextTournaments': nextTournaments_format, #'tournamentCategories': tournamentCategories, 'lang': lang, })))
def lostGames(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/register') # Select all won games lostGames = Game.objects.filter(Q(player1 = request.user) | Q(player2 = request.user), ~Q(winner = request.user)).order_by('-start_time') # SIDEBAR INFO myTournaments = myActiveTournaments(request) myAdmnTournaments = myAdminTournaments(request) todayGames = myTodayGames(request) nextGames = myNextGames(request) pendingQR = myAdminPendingQuestionReviews(request.user) # Info about user user_me = UserProfile.objects.get(user=request.user) # Load strings language to template mynextgames.html try: lang = strLang() except: lang = '' # Must we show a notification user? try: if request.GET['status']: box = setBox(request.GET['status']) except: box = '' if request.user.has_perm('fleq.quizbowl.add_tournament'): admin_user = True else: admin_user = False return render_to_response('lostgames.html', { 'user_me': user_me, 'box': box, 'lang': lang, 'myTournaments': myTournaments, 'myAdminTournaments': myAdmnTournaments, 'todayGames': todayGames, 'nextGames': nextGames, 'lostGames': lostGames, 'admin_user': admin_user, 'pendingQR': pendingQR, })
def NextGames(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/') # Games haven't been played and user has joined #myFutureGames = Game.objects.filter(Q(log__isnull = False), Q(start_time__gte = datetime.datetime.now()), # Q(player1 = request.user) | Q(player2 = request.user)).order_by('start_time') myFutureGames = Game.objects.filter(Q(log = False), Q(player1 = request.user) | Q(player2 = request.user)).order_by('start_time') dates = [] for game in myFutureGames: if not game.start_time_committed and (datetime.datetime.now() + datetime.timedelta(minutes=120)) > game.start_time: game.start_time_committed = True game.save() if not game.start_time.date() in dates: dates.append(game.start_time.date()) # Load strings language to template mynextgames.html try: lang = strLang() except: lang = '' template ='custom/next-games.html' # template = get_template('custom/next-games.html') if request.mobile: template = 'mobile/next-games.html' # template = get_template('mobile/next-games.html') up= UserProfile.objects.get(user=request.user) timezone = up.timezone return render_to_response(template, { 'user_me': request.user, 'timezone': timezone, 'myNextGames': myFutureGames, 'dates': dates, 'lang': lang, })
def home(request): if request.mobile: return HttpResponseRedirect('mobile') # Load strings language to template index.html try: lang = strLang() except: lang = '' if request.user.has_perm('fleq.quizbowl.add_tournament'): admin_user = True else: admin_user = False # SIDEBAR INFO if request.user.is_active: myTournaments = myActiveTournaments(request) myAdmnTournaments = myAdminTournaments(request) todayGames = myTodayGames(request) nextGames = myNextGames(request) # Info about user user_me = UserProfile.objects.get(user=request.user) pendingQR = myAdminPendingQuestionReviews(request.user) else: myTournaments = "" myAdmnTournaments = "" todayGames = "" nextGames = "" user_me = request.user pendingQR = 0 return render_to_response('custom/index.html', { 'user_me': user_me, 'lang': lang, 'myTournaments': myTournaments, 'myAdminTournaments': myAdmnTournaments, 'todayGames': todayGames, 'nextGames': nextGames, 'admin_user': admin_user, 'pendingQR': pendingQR, })
def Signin(request): # Load strings language to template login.html try: lang = strLang() except: lang = '' if request.method == 'POST': registerForm = RegisterForm(request.POST) if registerForm.is_valid(): user = User.objects.create_user(request.POST['username'], request.POST['email'], request.POST['password']) user.is_staff = False user.first_name = request.POST['first_name'] user.last_name = request.POST['last_name'] userProfile = UserProfile(user=user) user.save() userProfile.save() user = authenticate(username=request.POST['username'], password=request.POST['password']) login(request, user) return render_to_response('mobile/welcome.html', {}) else: return render_to_response('mobile/signin.html', { 'user_me': request.user, 'registerForm': registerForm, 'lang': lang, }) else: # If get request, generate a new form registerForm = RegisterForm() # An unbound form return render_to_response('mobile/signin.html', { 'user_me': request.user, 'registerForm': registerForm, 'lang': lang, })