def account_post(request): _ = request.translate redirect = HTTPSeeOther(location=request.route_url('account')) profiles_limit = 10 profile_name = request.POST.get('profilename') profile_delete = request.POST.get('delete') if profile_name: p = Profile() if not p.validate_name(profile_name): request.messages.error(_('Invalid name.')) return redirect # Check if the name is already used used = DBSession.query(Profile).filter_by(uid=request.user.id) \ .filter_by(name=profile_name).first() if used: request.messages.error(_('Name already used.')) return redirect # Check if this user's under the profile number limit profiles_count = DBSession.query(func.count(Profile.id)) \ .filter_by(uid=request.user.id).scalar() if profiles_count > profiles_limit: request.messages.error(_('You have too many profiles.')) return redirect p.name = profile_name p.uid = request.user.id DBSession.add(p) DBSession.flush() return HTTPSeeOther( location=request.route_url('account_profiles_edit', id=p.id)) if profile_delete: try: profile_delete = int(profile_delete) except ValueError: return redirect p = DBSession.query(Profile) \ .filter_by(id=int(profile_delete)) \ .filter(Profile.name != '') \ .filter_by(uid=request.user.id) \ .first() if not p: request.messages.error(_('Unknown profile.')) return redirect DBSession.delete(p) return redirect
def account_post(request): _ = request.translate redirect = HTTPSeeOther(location=request.route_url('account')) profiles_limit = 10 profile_name = request.POST.get('profilename') profile_delete = request.POST.get('delete') if profile_name: p = Profile() if not p.validate_name(profile_name): request.messages.error(_('Invalid name.')) return redirect # Check if the name is already used used = DBSession.query(Profile).filter_by(uid=request.user.id) \ .filter_by(name=profile_name).first() if used: request.messages.error(_('Name already used.')) return redirect # Check if this user's under the profile number limit profiles_count = DBSession.query(func.count(Profile.id)) \ .filter_by(uid=request.user.id).scalar() if profiles_count > profiles_limit: request.messages.error(_('You have too many profiles.')) return redirect p.name = profile_name p.uid = request.user.id DBSession.add(p) DBSession.flush() return HTTPSeeOther(location=request.route_url('account_profiles_edit', id=p.id)) if profile_delete: try: profile_delete = int(profile_delete) except ValueError: return redirect p = DBSession.query(Profile) \ .filter_by(id=int(profile_delete)) \ .filter(Profile.name != '') \ .filter_by(uid=request.user.id) \ .first() if not p: request.messages.error(_('Unknown profile.')) return redirect DBSession.delete(p) return redirect
def account_post(request): # TODO: Fix that. split in two functions or something. errors = [] try: if 'profilename' in request.POST: p = Profile() p.validate_name(request.POST['profilename']) or \ errors.append('Invalid name.') assert not errors name_used = DBSession.query(Profile) \ .filter_by(uid=request.user.id, name=request.POST['profilename']) \ .first() if name_used: errors.append('Name already used.') profiles_count = DBSession.query(func.count(Profile.id)) \ .filter_by(uid=request.user.id).scalar() if profiles_count > 10: errors.append('You have too many profiles.') assert not errors p.name = request.POST['profilename'] p.askpw = 'askpw' in request.POST and request.POST['askpw'] == '1' p.uid = request.user.id if not p.askpw: p.password = random_access_token() DBSession.add(p) DBSession.flush() return account(request) if 'profiledelete' in request.POST: p = DBSession.query(Profile) \ .filter_by(id=int(request.POST['profiledelete'])) \ .filter_by(uid=request.user.id) \ .first() assert p or errors.append('Unknown profile.') DBSession.delete(p) DBSession.flush() return account(request) u = request.user if request.POST['password'] != '': u.validate_password(request.POST['password']) or \ errors.append('Invalid password.') if request.POST['password'] != request.POST['password2']: errors.append('Both passwords do not match.') if request.POST['email'] != '': u.validate_email(request.POST['email']) or \ errors.append('Invalid email address.') assert not errors new_email = request.POST.get('email') if new_email and new_email != request.user.email: c = DBSession.query(func.count(User.id).label('ec')) \ .filter_by(email=new_email).first() if c.ec > 0: errors.append('E-mail address already registered.') assert not errors if request.POST['password'] != '': u.set_password(request.POST['password']) if request.POST['email'] != '': u.email = request.POST['email'] request.session.flash(('info', 'Saved!')) DBSession.flush() except KeyError: return HTTPBadRequest() except AssertionError: for error in errors: request.session.flash(('error', error)) return account(request)
def account_post(request): _ = request.translate # TODO: Fix that. split in two functions or something. errors = [] try: if 'profilename' in request.POST: p = Profile() p.validate_name(request.POST['profilename']) or \ errors.append(_('Invalid name.')) assert not errors name_used = DBSession.query(Profile) \ .filter_by(uid=request.user.id, name=request.POST['profilename']) \ .first() if name_used: errors.append(_('Name already used.')) profiles_count = DBSession.query(func.count(Profile.id)) \ .filter_by(uid=request.user.id).scalar() if profiles_count > 10: errors.append(_('You have too many profiles.')) assert not errors p.name = request.POST['profilename'] p.askpw = 'askpw' in request.POST and request.POST['askpw'] == '1' p.uid = request.user.id if not p.askpw: p.password = random_access_token() DBSession.add(p) DBSession.flush() return account(request) if 'profiledelete' in request.POST: p = DBSession.query(Profile) \ .filter_by(id=int(request.POST['profiledelete'])) \ .filter_by(uid=request.user.id) \ .first() assert p or errors.append(_('Unknown profile.')) DBSession.delete(p) DBSession.flush() return account(request) u = request.user if request.POST['password'] != '': u.validate_password(request.POST['password']) or \ errors.append(_('Invalid password.')) if request.POST['password'] != request.POST['password2']: errors.append(_('Both passwords do not match.')) if request.POST['email'] != '': u.validate_email(request.POST['email']) or \ errors.append(_('Invalid email address.')) assert not errors new_email = request.POST.get('email') if new_email and new_email != request.user.email: c = DBSession.query(func.count(User.id).label('ec')) \ .filter_by(email=new_email).first() if c.ec > 0: errors.append(_('E-mail address already registered.')) assert not errors if request.POST['password'] != '': u.set_password(request.POST['password']) if request.POST['email'] != '': u.email = request.POST['email'] request.messages.info(_('Saved!')) DBSession.flush() except KeyError: return HTTPBadRequest() except AssertionError: for error in errors: request.session.flash(('error', error)) return account(request)