def coordlink(request): world = World.objects.get(name=request.POST['namespace']) if not permissions.can_coordlink(request.user, world): return response_403() tileY, tileX = int(request.POST['tileY']), int(request.POST['tileX']) tile, _ = Tile.objects.get_or_create(world=world, tileY=tileY, tileX=tileX) if tile.properties.get('protected'): if not permissions.can_admin(request.user, world): # TODO: log? return HttpResponse('') # Must convert to str because that's how JsonField reads the existing keys charY = int(request.POST['charY']) charX = int(request.POST['charX']) assert charY < Tile.ROWS assert charX < Tile.COLS charY, charX = str(charY), str(charX) link_tileY = str(int(request.POST['link_tileY'])) link_tileX = str(int(request.POST['link_tileX'])) if 'cell_props' not in tile.properties: tile.properties['cell_props'] = {} if charY not in tile.properties['cell_props']: tile.properties['cell_props'][charY] = {} if charX not in tile.properties['cell_props'][charY]: tile.properties['cell_props'][charY][charX] = {} tile.properties['cell_props'][charY][charX]['link'] = { 'type': 'coord', 'link_tileY': link_tileY, 'link_tileX': link_tileX, } tile.save() log.info('ACTION:COORDLINK %s %s %s %s %s %s %s' % (world.id, tileY, tileX, charY, charX, link_tileY, link_tileX)) return HttpResponse('')
def urllink(request): # TODO: factor out w/above world = World.objects.get(name=request.POST['namespace']) if not permissions.can_urllink(request.user, world): return response_403() tileY, tileX = int(request.POST['tileY']), int(request.POST['tileX']) tile, _ = Tile.objects.get_or_create(world=world, tileY=tileY, tileX=tileX) if tile.properties.get('protected'): if not permissions.can_admin(request.user, world): # TODO: log? return HttpResponse('') # Must convert to str because that's how JsonField reads the existing keys charY = int(request.POST['charY']) charX = int(request.POST['charX']) assert charY < Tile.ROWS assert charX < Tile.COLS charY, charX = str(charY), str(charX) url = request.POST['url'].strip() if not urlparse.urlparse(url)[0]: # no scheme url = 'http://' + url if 'cell_props' not in tile.properties: tile.properties['cell_props'] = {} if charY not in tile.properties['cell_props']: tile.properties['cell_props'][charY] = {} if charX not in tile.properties['cell_props'][charY]: tile.properties['cell_props'][charY][charX] = {} tile.properties['cell_props'][charY][charX]['link'] = { 'type': 'url', 'url': url, } tile.save() log.info('ACTION:URLLINK %s %s %s %s %s %s' % (world.id, tileY, tileX, charY, charX, url)) return HttpResponse('')
def protect(request): world = World.objects.get(name=request.POST['namespace']) if not permissions.can_admin(request.user, world): return response_403() tileY, tileX = request.POST['tileY'], request.POST['tileX'] # TODO: select for update tile, _ = Tile.objects.get_or_create(world=world, tileY=tileY, tileX=tileX) tile.properties['protected'] = True tile.save() log.info('ACTION:PROTECT %s %s %s' % (world.id, tileY, tileX)) return HttpResponse('')
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))