def yourworld(request, namespace): """Check permissions and route request.""" world, _ = World.get_or_create(namespace) if not permissions.can_read(request.user, world): return HttpResponseRedirect('/accounts/private/') if 'fetch' in request.GET: return fetch_updates(request, world) can_write = permissions.can_write(request.user, world) if request.method == 'POST': if not can_write: return response_403() return send_edits(request, world) state = { 'canWrite': can_write, 'canAdmin': permissions.can_admin(request.user, world), 'worldName': world.name, 'features': permissions.get_available_features(request.user, world), } if 'MSIE' in request.META.get('HTTP_USER_AGENT', ''): state[ 'announce'] = "Sorry, your World of Text doesn't work well with Internet Explorer." return req_render_to_response(request, 'yourworld.html', { 'settings': settings, 'state': simplejson.dumps(state), })
def send_edits(request, world): assert permissions.can_write(request.user, world) # Checked by router response = [] tiles = {} # a simple cache edits = [e.split(',', 5) for e in request.POST.getlist('edits')] for edit in edits: char = edit[5] tileY, tileX, charY, charX, timestamp = map(int, edit[:5]) assert len(char) == 1 # TODO: investigate these tracebacks keyname = "%d,%d" % (tileY, tileX) if keyname in tiles: tile = tiles[keyname] else: # TODO: select for update tile, _ = Tile.objects.get_or_create(world=world, tileY=tileY, tileX=tileX) tiles[keyname] = tile if tile.properties.get('protected'): if not permissions.can_admin(request.user, world): continue tile.set_char(charY, charX, char) # TODO: anything, please. if tile.properties: if 'cell_props' in tile.properties: if str(charY) in tile.properties[ 'cell_props']: #must be str because that's how JSON interprets int keys if str(charX) in tile.properties['cell_props'][str(charY)]: del tile.properties['cell_props'][str(charY)][str( charX)] if not tile.properties['cell_props'][str(charY)]: del tile.properties['cell_props'][str(charY)] if not tile.properties['cell_props']: del tile.properties['cell_props'] response.append([tileY, tileX, charY, charX, timestamp, char]) if len(edits) < 200: for tile in tiles.values(): tile.save() Edit.objects.create( world=world, user=request.user if request.user.is_authenticated() else None, content=repr(edits), ip=request.META['REMOTE_ADDR'], ) return HttpResponse(simplejson.dumps(response))
def send_edits(request, world): assert permissions.can_write(request.user, world) # Checked by router response = [] tiles = {} # a simple cache edits = [e.split(',', 5) for e in request.POST.getlist('edits')] for edit in edits: char = edit[5] tileY, tileX, charY, charX, timestamp = map(int, edit[:5]) assert len(char) == 1 # TODO: investigate these tracebacks keyname = "%d,%d" % (tileY, tileX) if keyname in tiles: tile = tiles[keyname] else: # TODO: select for update tile, _ = Tile.objects.get_or_create(world=world, tileY=tileY, tileX=tileX) tiles[keyname] = tile if tile.properties.get('protected'): if not permissions.can_admin(request.user, world): continue tile.set_char(charY, charX, char) # TODO: anything, please. if tile.properties: if 'cell_props' in tile.properties: if str(charY) in tile.properties['cell_props']: #must be str because that's how JSON interprets int keys if str(charX) in tile.properties['cell_props'][str(charY)]: del tile.properties['cell_props'][str(charY)][str(charX)] if not tile.properties['cell_props'][str(charY)]: del tile.properties['cell_props'][str(charY)] if not tile.properties['cell_props']: del tile.properties['cell_props'] response.append([tileY, tileX, charY, charX, timestamp, char]) if len(edits) < 200: for tile in tiles.values(): tile.save() Edit.objects.create(world=world, user=request.user if request.user.is_authenticated() else None, content=repr(edits), ip=request.META['REMOTE_ADDR'], ) return HttpResponse(simplejson.dumps(response))