Beispiel #1
0
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('')
Beispiel #2
0
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),
    })
Beispiel #3
0
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('')
Beispiel #4
0
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('')
Beispiel #5
0
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('')
Beispiel #6
0
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('')
Beispiel #7
0
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('')
Beispiel #8
0
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))
Beispiel #9
0
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))
Beispiel #10
0
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),
    })