Exemplo n.º 1
0
 def test_points_correct(self):
     points = utils.locations_to_points(self.game.location_set.all())
     self.assertEqual(self.response.context["gmap"].points, points)
Exemplo n.º 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)