upfront / upfront / views.py

from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from pyramid.security import Allow, Everyone, Deny
from pyramid.security import remember
from pyramid.url import resource_url
from upfront.game import UpFront

from upfront.models import Root, Users, User, Games, OpenGame

@view_config(context=User, renderer='upfront:templates/users/games.mako', name='games')
def my_games(context, request):
    opengames = dict()
    games = dict()
    finished = dict()
    for id, game in context.games.iteritems():
        if isinstance(game, OpenGame):
            opengames[id] = game
        elif game.state == 'finished':
            finished[id] = game
        else:
            games[id] = game
    return dict(opengames=opengames, games=games, finished=finished)

@view_config(context=OpenGame, renderer='upfront:templates/game.mako', name='join')
def join_game(context, request):
    game = UpFront(context.player1, context.player1nation, request.user, request.subpath[0])
    id = int(context.__name__)
    games = context.__parent__
    del games.openGames[id]
    games.games[id] = game
    context.player1.games[id] = game
    request.user.games[id] = game 
    return dict(game=game) 

@view_config(context=OpenGame, name='delete', permission='delete-game')
@view_config(context=UpFront, name='delete', permission='delete-game')
def delete_game(context, request):
    id = int(context.__name__)
    if isinstance(context, OpenGame):
        del context.__parent__.openGames[id]

    del context.player1.games[id]
    del context.__parent__.games[id]        
    return HTTPFound(location=resource_url(context.__parent__, request, 'open'))


@view_config(context=Games, renderer='upfront:templates/opengames.mako', name='open')
def open_games(context, request):
    games = context.openGames
    return dict(games=games)

@view_config(context=Games, renderer='upfront:templates/newgame.mako', permission='create-game', name='new')
def new_game(context, request):
    if 'form.submitted' in request.params:
        context.addOpenGame(request.user, request.params['nation'])
        return HTTPFound(location = resource_url(context, request, 'open'))

    return dict(url=resource_url(context, request, 'new'))

@view_config(context=Root, renderer='upfront:templates/index.mako')
@view_config(context=HTTPNotFound, renderer='upfront:templates/index.mako')
def my_view(context, request):
    message = None
    if 'message' in request.session:
        message = request.session['message']
        del request.session['message']

    if isinstance(context, HTTPNotFound):
        message = 'AWOL - The page you requested is missing'
    return dict(section='home', message=message)

@view_config(context=User, renderer='upfront:templates/users/user.mako', permission='view-user-details')
def user_view(context, request):
    return dict(user=context)

@view_config(context=User, renderer='upfront:templates/users/delete.mako', name='delete',
    permission='delete-user')
def del_user(context, request):
    root = request.root
    del root['users'][context.name.lower()]
    message = '"%s" deleted' % (context.name)
    request.session['message'] = message

    return HTTPFound(location = resource_url(root, request, ''))

@view_config(context=Users, renderer='upfront:templates/users/list.mako', permission='view-user-details')
def user_list(context, request):
    return dict(users=context)

@view_config(context=Users, renderer='upfront:templates/users/create.mako', name='create')
def create_user(context, request):
    users = request.root['users']
    message = ''
    username = ''
    password = ''

    if 'form.submitted' in request.params:
        username = request.params['username']
        password = request.params['password']

        if users.has_key(username.lower()):
            message = 'User already exists'
        else:
            user = User(username, password) 
            user.__acl__ = [(Allow, username.lower(), 'view-user-details')] 
            user.__parent__ = users

            users.addUser(user)

            headers = remember(request, username.lower())
            return HTTPFound(location = resource_url(user, request, ''), headers = headers)

    return dict(
        url = resource_url(context, request, 'create'),
        message = message,
        username = username,
        password = password,
        section = 'join'
        )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.