def _to_python(self, value, state): db = DBSession() user = db.query(User.id).filter(User.name == value).first() if user: raise formencode.Invalid("That username is already taken", value, state) return value
def _authenticated_user(request): user_name = request.matchdict['user_name'] db = DBSession() user = db.query(User).filter( User.name == user_name).first() if not user or user.id != authenticated_userid(request): return None return user
def authentication_callback(userid, request): db = DBSession() user = db.query(User).filter(User.id==userid).first() if user: request.remote_user = user.name return [('group:%s' % group.name) for group in user.groups] else: request.remote_user = None
def _to_python(self, value, state): db = DBSession() game = db.query(Game.name).filter( Game.user_id == state.user_id).filter(Game.name == value).first() if game: raise formencode.Invalid("A game with that name already exists", value, state) return value
def _initTestingDB(): from sqlalchemy import create_engine from mudwyrm.models import initialize_sql, DBSession, transaction from mudwyrm.models.auth import User, Group from sqlalchemy.exc import IntegrityError initialize_sql(create_engine('sqlite://')) users_group = Group('users') DBSession.add(users_group) user = User(name='foo', password='******', email='*****@*****.**', groups=[users_group]) DBSession.add(user) transaction.commit() return DBSession
def edit(request): name = request.matchdict['game'] user_id = authenticated_userid(request) db = DBSession() game = db.query(Game).filter( Game.user_id == user_id).filter(Game.name == name).first() if not game: return HTTPNotFound(request.translate("A game named %s doesn't exist" % name)) form = Form(request, EditGameSchema, obj=game) if form.validate(): form.bind(game, exclude=['name']) transaction.commit() return HTTPFound(location=request.route_url('games')) return dict(form=FormRenderer(form))
def play(request): user_id = authenticated_userid(request) if user_id is None: return HTTPForbidden() username = request.remote_user gamename = request.matchdict['game'] db = DBSession() game = db.query(Game.name).filter(Game.user_id == user_id).filter( Game.name == gamename).first() if not game: return HTTPNotFound(request.translate("That game doesn't exist.")) return render_to_response( 'mudwyrm_users:%s/%s/ui/templates/index.mako' % (username, gamename), {'user_files_url': '/user/%s/%s/' % (username, gamename)}, request)
def login(request): came_from = request.params.get('came_from', '/') auth_failed = False form = Form(request, LoginSchema) if form.validate(): db = DBSession() user = db.query(User).filter(User.name == form.data['name']).first() if user and user.validate_password(form.data['password']): return HTTPFound(location=came_from, headers=remember(request, user.id)) auth_failed = True return dict( auth_failed = auth_failed, form = FormRenderer(form) )
def add(request): user_id = authenticated_userid(request) form = Form(request, AddGameSchema, state=State(user_id=user_id), defaults=dict(port=23)) if form.validate(): game = form.bind(Game()) game.user_id = user_id db = DBSession() db.add(game) db.flush() transaction.commit() #template_dir = os.path.join(os.path.dirname(__file__), # '_game_session_template') #session_dir = os.path.join(self.users_dir, metauser.name, session.name) #if not os.path.exists(session_dir): # shutil.copytree(template_dir, session_dir) return HTTPFound(location=request.route_url('games')) return dict(form=FormRenderer(form))
def register(request): db = DBSession() form = Form(request, RegistrationSchema) if form.validate(): user = form.bind(User(), exclude=['password_confirmation']) group = db.query(Group).filter(Group.name == 'users').one() user.groups.append(group) db.add(user) db.flush() transaction.commit() return HTTPFound(location=request.route_url('root')) return dict(form=FormRenderer(form))
def test_success(self): from mudwyrm.views.auth import register from mudwyrm.models import DBSession from mudwyrm.models.auth import User, Group from pyramid.httpexceptions import HTTPFound request = testing.DummyRequest( post = dict( name = 'user', password = '******', password_confirmation = 'password', email = '*****@*****.**' ) ) request.route_url = lambda r: '/' if r == 'root' else None result = register(request) self.assertIsInstance(result, HTTPFound) self.assertEquals(result.location, request.route_url('root')) user = DBSession.query(User).filter(User.name == 'user').first() self.assertIsNotNone(user) self.assertEquals(user.name, 'user') self.assertTrue(user.validate_password('password')) self.assertEquals(user.email, '*****@*****.**') self.assertEquals(user.groups[0].name, 'users')
def games(request): db = DBSession() user_id = authenticated_userid(request) games = db.query(Game.name).filter(Game.user_id == user_id).all() games = map(lambda g: g[0], games) return dict(games=games)