Esempio n. 1
0
def game_create(request):
    if request.method == 'POST':
        form = PartialGameForm(request.POST)
        if form.is_valid():
            center_loc = simplejson.loads(request.POST['locations'])[0]
            
            data = form.cleaned_data
            game = None
            if data['game_type'] == GAME_TYPES[0][0]:
                game = TreasureHuntGame()
            else:
                # note: this case shouldn't occur in normal use
                raise PermissionDenied('unable to create generic game')
            
            game.name = data['name']
            game.is_public = data['is_public']
            game.city = data['city']
            game.center_latitude = str(center_loc['lat'])
            game.center_longitude = str(center_loc['lon'])
            game.created_by = request.user
            game.created = datetime.now()
            game.save()
            a = ActivityStreamItem(actor=request.user,verb="created a new game",target=game,occurred=datetime.now())
            a.save()
            return HttpResponseRedirect(reverse('game_edit', args=(game.id,)))

    else:
        form = PartialGameForm()

    gi = pygeoip.GeoIP(LOCAL_ROOT_DIR + 'qr\\static\\GeoLiteCity.dat')
    client_address = request.META['REMOTE_ADDR'] 
    user_location = gi.record_by_addr(client_address)
    if client_address == '127.0.0.1':
    	latitude = '51.08'
    	longitude = '-114.08'
    else:
  		latitude = user_location['latitude']
  		longitude = user_location['longitude']
    
    gmap = Map('gmap', [(0,latitude,longitude,'')])
    gmap.center = (latitude,longitude)
    gmap.zoom = '5'
    
    context = RequestContext(request)
    context['form'] = form
    context['gmap_js'] = gmap.to_js()
    context = page_info(context)
    return render_to_response('games/create.html', context)