Пример #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)
Пример #2
0
def game_edit(request, game_id):
    
    # get the game
    game = get_object_or_404(Game, pk=game_id)
    
    # only the game's creator can edit the locations
    if request.user != game.created_by:
        return HttpResponseForbidden('Cannot access: not game creator')
    
    locations = game.location_set.all().order_by('id')
    
    error_msgs = []
    if request.method == 'POST':
        # save locations, if they were given
        if request.POST['mode'] == 'update_locations':
            new_locations = simplejson.loads(request.POST['locations'])
            
            for loc in new_locations:
                # make sure this location ID exists & is
                # linked with the current game_id
                try:
                    existing_loc = locations.get(pk=loc['id'])
                except ObjectDoesNotExist:
                    error_msgs.append(
                        'location[%d] not linked to game[%d]'
                        % (int(loc['id']), int(game_id)))
                
                # set the new lat/lon
                existing_loc.latitude = str(loc['lat'])
                existing_loc.longitude = str(loc['lon'])
                
                # save any game-specific data (from the edit_XX.html templates)
                if isinstance(game, TreasureHuntGame):
                    existing_loc.clue = request.POST['clue_%d' % (existing_loc.id,)]
                
                existing_loc.save()

        # add a new point
        elif request.POST['mode'] == 'add_point':
            new_loc = Location(latitude=game.center_latitude,
                               longitude=game.center_longitude,
                               created=datetime.now(),
                               visible=datetime.now(),
                               expires=datetime.now(),
                               gameID=game)
            new_loc.save()
            
            # re-load the locations to grab the new point
            locations = game.location_set.all().order_by('id')
    
    # if this is a game with an ordering to the points,
    # grab that order for the map to connect the points
    point_order = []
    if isinstance(game, TreasureHuntGame):
        point_order = utils.csv_to_list(game.ordered_locations)
    
    points = utils.locations_to_points(locations)
    gmap = Map('gmap', points, point_order)
    gmap.center = (game.center_latitude, game.center_longitude)
    gmap.zoom = '15'
    
    context = RequestContext(request)
    context['gmap'] = gmap
    context['error_msgs'] = error_msgs
    context['game'] = game
    context['locations'] = locations
    a = ActivityStreamItem(actor=request.user,verb="has modified ",target=game,occurred=datetime.now())
    a.save()   
    context = page_info(context)
    return render_to_response('games/edit.html', context)