def yourworld(request, namespace): """Check permissions and route request.""" # world, _ = World.get_or_create(namespace) world = get_object_or_404(World, name=namespace) # if user is exists and is authenticated # send edits should also send it to their world # create a world when they create an account if 'color' in request.session: color = request.session['color'] else: import random color = random.randint(0, 9) request.session['color'] = color 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) #well that's important and conversly put 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), 'properties': simplejson.dumps(world.properties), 'color': color, 'world': world, })
def yourworld(request, namespace): """Check permissions and route request.""" # world, _ = World.get_or_create(namespace) world = get_object_or_404(World, name=namespace) # if user is exists and is authenticated # send edits should also send it to their world # create a world when they create an account if 'color' in request.session: color = request.session['color'] else: import random color = random.randint(0,9) request.session['color'] = color 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) #well that's important and conversly put 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), 'properties': simplejson.dumps(world.properties), 'color': color, 'world': world, })
def send_edits(request, world): # log.info("sending edits") if request.user.is_authenticated(): authd= True worldU= World.objects.get(name="u_"+request.user.username) assert permissions.can_write(request.user, worldU) # Checked by router else: authd= False assert permissions.can_write(request.user, world) # Checked by router response = [] tiles = {} # a simple cache tilesU = {} # a simple cache edits = [e.split(',', 6) for e in request.POST.getlist('edits')] for edit in edits: char = edit[5] color= edit[6] 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) or (keyname in tilesU): tile = tiles[keyname] if authd: tileU = tilesU[keyname] else: # TODO: select for update tile, _ = Tile.objects.get_or_create(world=world, tileY=tileY, tileX=tileX) tiles[keyname] = tile # do same thing for personal world if authd: tileU, _U = Tile.objects.get_or_create(world=worldU, tileY=tileY, tileX=tileX) tilesU[keyname] = tileU if tile.properties.get('protected'): if not permissions.can_admin(request.user, world): continue tile.set_char(charY, charX, char, color) if authd: tileU.set_char(charY, charX, char, color) # The edits are being recorded correctly, this is a tile issue it seems. # 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, sessionid]) response.append([tileY, tileX, charY, charX, timestamp, char, color]) #end of for loop of edit in edits 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'], ) # same as above, but for personal world if authd: if len(edits) < 200: for tile in tilesU.values(): tile.save() Edit.objects.create(world=worldU, user=request.user, content=repr(edits), ip=request.META['REMOTE_ADDR'], ) return HttpResponse(simplejson.dumps(response))
def send_edits(request, world): # log.info("sending edits") if request.user.is_authenticated(): authd = True worldU = World.objects.get(name="u_" + request.user.username) assert permissions.can_write(request.user, worldU) # Checked by router else: authd = False assert permissions.can_write(request.user, world) # Checked by router response = [] tiles = {} # a simple cache tilesU = {} # a simple cache edits = [e.split(',', 6) for e in request.POST.getlist('edits')] for edit in edits: char = edit[5] color = edit[6] 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) or (keyname in tilesU): tile = tiles[keyname] if authd: tileU = tilesU[keyname] else: # TODO: select for update tile, _ = Tile.objects.get_or_create(world=world, tileY=tileY, tileX=tileX) tiles[keyname] = tile # do same thing for personal world if authd: tileU, _U = Tile.objects.get_or_create(world=worldU, tileY=tileY, tileX=tileX) tilesU[keyname] = tileU if tile.properties.get('protected'): if not permissions.can_admin(request.user, world): continue tile.set_char(charY, charX, char, color) if authd: tileU.set_char(charY, charX, char, color) # The edits are being recorded correctly, this is a tile issue it seems. # 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, sessionid]) response.append([tileY, tileX, charY, charX, timestamp, char, color]) #end of for loop of edit in edits 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'], ) # same as above, but for personal world if authd: if len(edits) < 200: for tile in tilesU.values(): tile.save() Edit.objects.create( world=worldU, user=request.user, content=repr(edits), ip=request.META['REMOTE_ADDR'], ) return HttpResponse(simplejson.dumps(response))