def post(self): registerData = { 'username': request.json['username'], 'password': request.json['password'], 'email': request.json['email'] } user = User.objects(username=registerData['username']).first() userEmail = User.objects(email=registerData['email']).first() if user: return abort( 403, description='Użytkownik o podanej nazwie już istnieje!') elif userEmail: return abort( 403, description= 'Konto o podanym adresie email już istnieje! prosimy o podanie innego.' ) newUser = User(username=registerData['username'], password=bcrypt.generate_password_hash( registerData['password']), email=registerData['email']).save() token = generate_confirmation_token(newUser.email) send_email( newUser.email, 'Aktywacja Konta', render_template('auth/activate.html', confirm_url=url_for('auth.confirm_account', token=token, _external=True))) return jsonify( message= 'Twoje konto zostało pomyślnie utworzone! Na adres e-mail została wysłana wiadomość z linkiem aktywacyjnym - prosimy aktywować konto.' )
def register(): if current_user.is_authenticated: return redirect(url_for('home.home_page')) form = RegistrationForm() if form.validate_on_submit(): user = User.objects(username=form.username.data).first() userEmail = User.objects(email=form.email.data).first() if user: flash('Podana nazwa użytkownika jest już zajęta!', 'danger') elif userEmail: return flash('Konto o podanym adresie email już istnieje! prosimy o podanie innego.', 'danger') newUser = User( username=form.username.data, email=form.email.data, password=bcrypt.generate_password_hash( form.password.data)).save() token = generate_confirmation_token(newUser.email) send_email( newUser.email, 'Aktywacja Konta', render_template( 'auth/activate.html', confirm_url=url_for( 'auth.confirm_account', token=token, _external=True))) flash('Twoje konto zostało pomyślnie utworzone! Na podany adres e-mail wyslaliśmy wiadomość z linkiem aktywacyjnym. Prosimy aktywować swoje konto aby mieć dostęp do pełnej wersji strony', 'success') return redirect(url_for('home.home_page')) return render_template('/auth/register.html', form=form)
def post(self): user = UserModel.objects(username='******').first() if not request.json: return abort( 403, description= 'Brakujące argumenty, prosze wypełnić wszystkie pola.') kidData = [ request.json['name'], request.json['age'], request.json['height'], request.json['favorite_colour'], request.json['shoe_size'] ] for k in kidData: if not k: return abort( 403, description= 'Brakujące argumenty, prosze wypełnić wszystkie pola.') if not kidData[1].isdigit() or not kidData[2].isdigit( ) or not kidData[4].isdigit(): return jsonify( message='Podane wartości muszą być typu liczbowego!') user.kids.append( Kid(name=kidData[0], age=kidData[1], height=kidData[2], favorite_colour=kidData[3], shoe_size=kidData[4])) user.save() return jsonify(message='Dziecko zostało dodane pomyślnie')
def forget_password(): if current_user.is_authenticated: return redirect(url_for('home.home_page')) else: form = ResetPasswordForm() if form.validate_on_submit(): user = User.objects(email=form.email.data).first() if user.last_change + timedelta(minutes=30) <= datetime.now(): if user: token = generate_confirmation_token(user.email) send_email( user.email, 'Zmiana hasła', render_template( 'auth/activate.html', confirm_url=url_for( 'auth.reset_password', token=token, _external=True))) flash( 'Na podany adres email zostały wysłane dalesze instrukcje dotyczące zmiany hasła!', 'success') else: flash( 'Do podanego adresu email nie zostało przypisane żadne konto!', 'danger') else: flash( 'Hasło można zresetować po upływie 30minut od ostatniej zmiany!', 'warning') return render_template('/auth/forgetPassword.html', form=form)
def reset_password(token): if current_user.is_authenticated: return redirect(url_for('home.home_page')) form = ResetPasswordChangeForm() if form.validate_on_submit(): try: email = confirm_token(token) except BaseException: flash( 'Link resetujący hasło wygasł, lub jest nieprawidłowy!', 'danger') user = User.objects(email=email).first_or_404() if user.last_change + timedelta(minutes=30) <= datetime.now(): user.update(password=bcrypt.generate_password_hash(form.password.data)) user.update(last_password_change=datetime.now()) flash( 'Twoje hasło zostało pomyślnie zresetowane!, teraz możesz się zalogować.', 'success') return redirect(url_for('auth.login')) else: flash( 'Hasło można zresetować po upływie 30minut od ostatniej zmiany!', 'warning') return redirect(url_for('home.home_page')) return render_template('/auth/resetPassword.html', form=form)
def post(self): if not request.json: return abort( 403, description= 'Brakujące argumenty, prosze wypełnić wszystkie pola.') loginData = { 'username': request.json['username'], 'password': request.json['password'] } if not loginData['username'] or not loginData['password']: return abort( 403, description= 'Brakujące argumenty, prosze wypełnić wszystkie pola.') user = User.objects(username=loginData['username']).first() if user and bcrypt.check_password_hash(user.password, loginData['password']): login_user(user) else: return abort( 403, description= 'Dane logowania są niepoprawne! Spradz poprawność wprowadzonych danych!' ) return jsonify(message='Zostałeś pomyślnie zalogowany do serwisu!')
def get(self): user = User.objects(username='******').first() login_user(user) if not current_user.is_authenticated: return abort(403) if current_user.confirmed: return abort(403) user = User.objects(username=current_user.username).first_or_404() token = generate_confirmation_token(user.email) send_email( user.email, 'Aktywacja Konta', render_template('auth/activate.html', confirm_url=url_for('auth.confirm_account', token=token, _external=True))) return jsonify( message='Na twoj adres email został wysłany link potwierdzający!')
def get(self): return jsonify(kids=[ dict(name=k.name, age=k.age, heigth=k.height, favorite_colour=k.favorite_colour, shoe_size=k.shoe_size) for k in UserModel.objects(username='******').first().kids ])
def confirm_account(token): try: email = confirm_token(token) except BaseException: flash('Link potwierdzający jest nieprawidłowy lub wygasł!.', 'danger') user = User.objects(email=email).first_or_404() if user.confirmed: flash('Twoje konto zostało już wcześniej potwierdzone!', 'info') else: user.update(confirmed=True) flash('Aktywacja konta przebiegła pomyślnie. Dziękujemy!', 'success') return redirect(url_for('home.home_page'))
def post(self): user = UserModel.objects(username='******').first() kidData = [request.json['name'], request.json['age'], request.json['height'], request.json['favorite_colour'], request.json['shoe_size']] for k in kidData: if not k: return jsonify(message='Brakujące parametry') if not kidData[1].isdigit() or not kidData[2].isdigit() or not kidData[4].isdigit(): return jsonify(message='Podane wartości muszą być typu liczbowego!') user.kids.append(Kid(name=kidData[0], age=kidData[1], height=kidData[2], favorite_colour=kidData[3],shoe_size=kidData[4])) user.save() return jsonify(message='Dziecko zostało dodane pomyślnie')
def post(self, confirmToken): try: emailp = confirm_token(confirmToken) except BaseException: return abort( 403, description='Token aktywujący wygasł, lub jest niepoprawny!') # if not emailp: return abort(403, description='Coś poszło nie tak, skontaktuj sie z administratorem serwisu!') user = User.objects(username='******').first_or_404() if user.confirmed: return abort(403, description='To konto zostało już aktywowane.') else: user.update(confirmed=True) return jsonify( message='Konto zostało pomyślnie aktywowane! Dziękujemy')
def validate_email(self, email): mail = User.objects(email=email.data).first() if mail: raise ValidationError( message='Podany adres jest już w naszej bazie!')
def validate_username(self, username): user = User.objects(username=username.data).first() if user: raise ValidationError( 'Użytkownik o podanej nazwie jest już zarejestrowany!')
def current_user_profile(): if current_user.is_authenticated: user = User.objects(username=current_user.username).first() else: return 404 return render_template('profile/profile.html', user=user)
def user_profile(username): return render_template('profile/profile.html', user=User.objects(username=username).first_or_404())