Exemplo n.º 1
0
def user_add(request):
    session = DBSession()
    username = request.params.get("username")
    if session.query(User).get(username) is None:
        session.add(User(username))
        session.flush()
    return HTTPFound(location=request.route_url('user', id=username))
Exemplo n.º 2
0
def oauth_callback(request):
    # the request token we have in the user session should be the same
    # as the one passed to the callback
    session = request.session
    request_token = session.get('request_token')
    if request.params.get('oauth_token') != request_token['oauth_token']:
        return HTTPBadRequest('Tokens don\'t match')
    # get the access token
    token = oauth.Token(request_token['oauth_token'],
                        request_token['oauth_token_secret'])
    verifier = request.params.get('oauth_verifier')
    token.set_verifier(verifier)
    client = oauth.Client(consumer, token)
    resp, content = client.request(ACCESS_TOKEN_URL, "POST")
    access_token = dict(urlparse.parse_qsl(content))
    token = access_token['oauth_token']
    token_secret = access_token['oauth_token_secret']
    # get the user details, finally
    token = oauth.Token(token, token_secret)
    client = oauth.Client(consumer, token)
    resp, content = client.request(USER_DETAILS_URL, "GET")
    user_elt = ElementTree.XML(content).find('user')
    # save the user's "display name" in the session
    if 'display_name' in user_elt.attrib:
        username = user_elt.attrib['display_name']
        db_session = DBSession()
        if db_session.query(User).get(username) is None:
            db_session.add(User(username))
            db_session.flush()
        headers = remember(request, username, max_age=20*7*24*60*60)

    # and redirect to the main page
    return HTTPFound(location=session.get('came_from'), headers=headers)
Exemplo n.º 3
0
def oauth_callback(request):
    # the request token we have in the user session should be the same
    # as the one passed to the callback
    session = request.session
    request_token = session.get('request_token')
    if request.params.get('oauth_token') != request_token['oauth_token']:
        return HTTPBadRequest('Tokens don\'t match')
    # get the access token
    token = oauth.Token(request_token['oauth_token'],
                        request_token['oauth_token_secret'])
    verifier = request.params.get('oauth_verifier')
    token.set_verifier(verifier)
    client = oauth.Client(consumer, token)
    resp, content = client.request(ACCESS_TOKEN_URL, "POST")
    access_token = dict(urlparse.parse_qsl(content))
    token = access_token['oauth_token']
    token_secret = access_token['oauth_token_secret']
    # get the user details, finally
    token = oauth.Token(token, token_secret)
    client = oauth.Client(consumer, token)
    resp, content = client.request(USER_DETAILS_URL, "GET")
    user_elt = ElementTree.XML(content).find('user')
    # save the user's "display name" in the session
    if 'display_name' in user_elt.attrib:
        username = user_elt.attrib['display_name']
        db_session = DBSession()
        if db_session.query(User).get(username) is None:
            db_session.add(User(username))
            db_session.flush()
        headers = remember(request, username, max_age=20 * 7 * 24 * 60 * 60)

    # and redirect to the main page
    return HTTPFound(location=session.get('came_from'), headers=headers)
Exemplo n.º 4
0
def job_edit(request):
    id = request.matchdict['job']
    session = DBSession()
    job = session.query(Job).get(id)

    licenses = session.query(License).all()

    if 'form.submitted' in request.params:
        job.title = request.params['title']
        job.short_description = request.params['short_description']
        job.description = request.params['description']
        job.workflow = request.params['workflow']
        josm_preset = request.params['josm_preset']
        josm_preset = josm_preset.value.decode(
            'UTF-8') if josm_preset != '' else ''
        job.josm_preset = josm_preset
        job.is_private = request.params.get('is_private') == 'on'
        job.imagery = request.params['imagery']
        job.task_extra = request.params['task_extra']

        if request.params['license_id'] != "":
            license_id = int(request.params['license_id'])
            license = session.query(License).get(license_id)
            job.license = license

        session.add(job)
        return HTTPFound(location=route_url('job', request, job=job.id))

    return dict(job=job, licenses=licenses)
Exemplo n.º 5
0
def user_add(request):
    session = DBSession()
    username = request.params.get("username")
    if session.query(User).get(username) is None:
        session.add(User(username))
        session.flush()
    return HTTPFound(location=request.route_url("user", id=username))
Exemplo n.º 6
0
def done(request):
    job_id = request.matchdict['job']
    x = request.matchdict['x']
    y = request.matchdict['y']
    zoom = request.matchdict['zoom']
    session = DBSession()
    username = authenticated_userid(request)
    tile = session.query(Tile).get((x, y, zoom, job_id))
    tile.comment = request.params['comment']
    if 'invalidate' in request.params:
        # task goes back to the queue
        tile.checkin = 0
        tile.username = username
    elif 'validate' in request.params:
        # task goes back to the queue
        tile.checkin = 2
        tile.username = username
    else:
        #task is done
        tile.checkin = 1
    tile.change = True
    tile.checkout = False
    session.add(tile)
    session.flush()

    # reset tile values
    tile.username = None
    tile.change = False
    tile.comment = None
    session.add(tile)
    return dict(success=True, tile=dict(x=tile.x, y=tile.y, z=tile.zoom))
Exemplo n.º 7
0
def checkTask(tile):
    session = DBSession()
    if tile.username is not None:
        if datetime.now() > tile.update + EXPIRATION_DURATION:
            tile.username = None 
            tile.update = datetime.now()
            session.add(tile)
Exemplo n.º 8
0
def checkTask(tile):
    session = DBSession()
    if tile.checkout is not None:
        if datetime.now() > tile.checkout + EXPIRATION_DURATION:
            tile.username = None 
            tile.checkout = None 
            session.add(tile)
Exemplo n.º 9
0
def job_edit(request):
    id = request.matchdict['job']
    session = DBSession()
    job = session.query(Job).get(id)

    licenses = session.query(License).all()

    if 'form.submitted' in request.params:
        job.title = request.params['title']
        job.short_description = request.params['short_description']
        job.description = request.params['description']
        job.workflow = request.params['workflow']
        josm_preset = request.params['josm_preset']
        josm_preset = josm_preset.value.decode('UTF-8') if josm_preset != '' else ''
        job.josm_preset = josm_preset 
        job.is_private = request.params.get('is_private') == 'on'
        job.imagery = request.params['imagery']
        job.task_extra = request.params['task_extra']

        if request.params['license_id'] != "":
            license_id = int(request.params['license_id'])
            license = session.query(License).get(license_id)
            job.license = license

        session.add(job)
        return HTTPFound(location = route_url('job', request, job=job.id))

    return dict(job=job, licenses=licenses)
Exemplo n.º 10
0
def checkTask(tile):
    session = DBSession()
    if tile.checkout is not False and tile.checkout is not None:
        if datetime.now() > tile.update + EXPIRATION_DURATION:
            tile.username = None
            tile.checkout = False
            tile.update = datetime.now()
            session.add(tile)
Exemplo n.º 11
0
def license_new(request):
    session = DBSession()
    license = License()
    license.name = ''
    license.description = ''
    license.plain_text = ''

    session.add(license)
    session.flush()
    return HTTPFound(location = route_url('license_edit', request, license=license.id))
Exemplo n.º 12
0
def job_new(request):
    if 'form.submitted' in request.params:
        session = DBSession()
        job = Job(request.params['title'], request.params['geometry'],
                  request.params['zoom'], authenticated_userid(request))

        session.add(job)
        session.flush()
        return HTTPFound(location=route_url('job_edit', request, job=job.id))
    return {}
Exemplo n.º 13
0
def job_feature(request):
    id = request.matchdict['job']
    session = DBSession()

    job = session.query(Job).get(id)
    job.featured = not job.featured 
    session.add(job)

    request.session.flash('Job "%s" featured status changed!' % job.title)
    return HTTPFound(location = route_url('home', request))
Exemplo n.º 14
0
def unlock(request):
    job_id = request.matchdict['job']
    x = request.matchdict['x']
    y = request.matchdict['y']
    session = DBSession()
    tile = session.query(Tile).get((x, y, job_id))
    tile.username = None 
    tile.checkout = None 
    session.add(tile)
    return HTTPFound(location=request.route_url('job', job=job_id))
Exemplo n.º 15
0
def job_publish(request):
    id = request.matchdict['job']
    session = DBSession()

    job = session.query(Job).get(id)
    job.status = 1
    session.add(job)

    request.session.flash('Job "%s" published!' % job.title)
    return HTTPFound(location=route_url('home', request))
Exemplo n.º 16
0
def job_feature(request):
    id = request.matchdict['job']
    session = DBSession()

    job = session.query(Job).get(id)
    job.featured = not job.featured
    session.add(job)

    request.session.flash('Job "%s" featured status changed!' % job.title)
    return HTTPFound(location=route_url('home', request))
Exemplo n.º 17
0
def job_publish(request):
    id = request.matchdict['job']
    session = DBSession()

    job = session.query(Job).get(id)
    job.status = 1
    session.add(job)

    request.session.flash('Job "%s" published!' % job.title)
    return HTTPFound(location = route_url('home', request))
Exemplo n.º 18
0
def unlock(request):
    job_id = request.matchdict['job']
    x = request.matchdict['x']
    y = request.matchdict['y']
    session = DBSession()
    tile = session.query(Tile).get((x, y, job_id))
    tile.username = None 
    tile.update = datetime.now()
    session.add(tile)
    return dict(job=tile.job,
                prev_task=tile)
Exemplo n.º 19
0
def unlock(request):
    job_id = request.matchdict['job']
    x = request.matchdict['x']
    y = request.matchdict['y']
    zoom = request.matchdict['zoom']
    session = DBSession()
    tile = session.query(Tile).get((x, y, zoom, job_id))
    tile.username = None
    tile.checkout = False
    tile.change = False
    session.add(tile)
    return dict(job=tile.job, prev_task=tile)
Exemplo n.º 20
0
Arquivo: tasks.py Projeto: fosm/OSMTM
def unlock(request):
    job_id = request.matchdict['job']
    x = request.matchdict['x']
    y = request.matchdict['y']
    session = DBSession()
    tile = session.query(Tile).get((x, y, job_id))
    tile.username = None 
    tile.checkout = False
    tile.change = False
    session.add(tile)
    return dict(job=tile.job,
                prev_task=tile)
Exemplo n.º 21
0
def job_new(request):
    if 'form.submitted' in request.params:
        session = DBSession()
        job = Job(
            request.params['title'],
            request.params['geometry'],
            request.params['zoom']
        )

        session.add(job)
        session.flush()
        return HTTPFound(location = route_url('job_edit', request, job=job.id))
    return {} 
Exemplo n.º 22
0
def unlock(request):
    job_id = request.matchdict['job']
    x = request.matchdict['x']
    y = request.matchdict['y']
    zoom = request.matchdict['zoom']
    session = DBSession()
    tile = session.query(Tile).get((x, y, zoom, job_id))
    tile.comment = request.params['comment']
    tile.username = None 
    tile.checkout = False
    tile.change = False
    session.add(tile)
    return dict(success=True, tile=dict(x=tile.x, y=tile.y, z=tile.zoom))
Exemplo n.º 23
0
def license_edit(request):
    id = request.matchdict['license']
    session = DBSession()
    license = session.query(License).get(id)

    if 'form.submitted' in request.params:
        license.name = request.params['name']
        license.description = request.params['description']
        license.plain_text = request.params['plain_text']

        session.add(license)
        request.session.flash('License updated!')
    return dict(license=license)
Exemplo n.º 24
0
def job_edit(request):
    id = request.matchdict["job"]
    session = DBSession()
    job = session.query(Job).get(id)

    if "form.submitted" in request.params:
        job.title = request.params["title"]
        job.description = request.params["description"]
        job.workflow = request.params["workflow"]

        session.add(job)
        return HTTPFound(location=route_url("job", request, job=job.id))

    return dict(job=job)
Exemplo n.º 25
0
def job_edit(request):
    id = request.matchdict['job']
    session = DBSession()
    job = session.query(Job).get(id)

    if 'form.submitted' in request.params:
        job.title = request.params['title']
        job.short_description = request.params['short_description']
        job.description = request.params['description']
        job.workflow = request.params['workflow']

        session.add(job)
        return HTTPFound(location = route_url('job', request, job=job.id))

    return dict(job=job)
Exemplo n.º 26
0
def done(request):
    job_id = request.matchdict['job']
    x = request.matchdict['x']
    y = request.matchdict['y']
    session = DBSession()
    tile = session.query(Tile).get((x, y, job_id))
    tile.username = None 
    tile.checkout = None 
    tile.comment = request.params['comment']
    if 'invalidate' in request.params:
        # task goes back to the queue
        tile.checkin = 0
    else:
        tile.checkin = tile.checkin + 1
    session.add(tile)
    return HTTPFound(location=request.route_url('job', job=job_id))
Exemplo n.º 27
0
def job_new(request):
    if 'form.submitted' in request.params:
        session = DBSession()
        job = Job(
            request.params['title'],
            request.params['short_description'],
            request.params['description'],
            request.params['workflow'],
            request.params['geometry'],
            request.params['zoom'],
            request.params.get('is_private', 0),
            request.params['imagery'],
            request.params.get('requires_nextview', 0)
        )

        session.add(job)
        session.flush()
        return HTTPFound(location = route_url('job', request, job=job.id))
    return {} 
Exemplo n.º 28
0
def take(request):
    job_id = request.matchdict['job']
    checkin = request.matchdict['checkin']
    session = DBSession()
    username = authenticated_userid(request)
    user = session.query(User).get(username)
    # first check if user has no task he's currently working on
    filter = and_(Tile.username==username, Tile.job_id==job_id)
    tiles = session.query(Tile).filter(filter).all()
    if len(tiles) > 0:
        request.session.flash('You already have a task to work on. Finish it before you can accept a new one.')
        return HTTPFound(location=request.route_url('job', job=job_id))

    filter = and_(Tile.checkin==checkin, Tile.job_id==job_id)
    tiles = session.query(Tile).filter(filter).all()
    filter = and_(TileHistory.username==username, TileHistory.job_id==job_id)
    # get the tile the user worked on previously
    p = session.query(TileHistory).filter(filter).order_by(TileHistory.checkout.desc()).limit(4).all()
    tile = None
    if p is not None and len(p) > 0:
        p = p[len(p) -1]
        neighbours = [
            (p.x - 1, p.y - 1), (p.x - 1, p.y), (p.x - 1, p.y + 1),
            (p.x, p.y - 1), (p.x, p.y + 1),
            (p.x + 1, p.y - 1), (p.x + 1, p.y), (p.x + 1, p.y + 1)]
        for t in tiles:
            if (t.x, t.y) in neighbours:
                tile = t
                break
    try:
        if tile is None:
            tile = tiles[random.randrange(0, len(tiles))]
        tile.username = username 
        tile.checkout = datetime.now()
        session.add(tile)
        return HTTPFound(location=request.route_url('task', job=job_id, x=tile.x, y=tile.y))
    except:
        if checkin == 1:
            request.session.flash('Sorry. No task available to validate.')
        else:
            request.session.flash('Sorry. No task available to take.')
        return HTTPFound(location=request.referrer)
Exemplo n.º 29
0
def done(request):
    job_id = request.matchdict['job']
    x = request.matchdict['x']
    y = request.matchdict['y']
    session = DBSession()
    tile = session.query(Tile).get((x, y, job_id))
    tile.username = None 
    tile.update = datetime.now()
    tile.comment = request.params['comment']
    if 'invalidate' in request.params:
        # task goes back to the queue
        tile.checkin = 0
    elif 'validate' in request.params:
        # task in validated
        tile.checkin = 2
    else:
        #task is done
        tile.checkin = 1
    session.add(tile)
    return dict(job=tile.job)
Exemplo n.º 30
0
def job_edit(request):
    id = request.matchdict['job']
    session = DBSession()
    job = session.query(Job).get(id)

    if 'form.submitted' in request.params:
        job.title = request.params['title']
        job.short_description = request.params['short_description']
        job.description = request.params['description']
        job.workflow = request.params['workflow']
        josm_preset = request.params['josm_preset']
        josm_preset = josm_preset.value.decode('UTF-8') if josm_preset != '' else ''
        job.josm_preset = josm_preset 
        job.is_private = request.params.get('is_private') == 'on'
        job.requires_nextview = request.params.get('requires_nextview') == 'on'

        session.add(job)
        return HTTPFound(location = route_url('job', request, job=job.id))

    return dict(job=job)
Exemplo n.º 31
0
def done(request):
    job_id = request.matchdict['job']
    x = request.matchdict['x']
    y = request.matchdict['y']
    zoom = request.matchdict['zoom']
    session = DBSession()
    tile = session.query(Tile).get((x, y, zoom, job_id))
    tile.comment = request.params['comment']
    if 'invalidate' in request.params:
        # task goes back to the queue
        tile.checkin = 0
    elif 'validate' in request.params:
        # task in validated
        tile.checkin = 2
    else:
        #task is done
        tile.checkin = 1
    tile.checkout = False
    tile.change = True
    session.add(tile)
    return dict(job=tile.job)
Exemplo n.º 32
0
def done(request):
    job_id = request.matchdict['job']
    x = request.matchdict['x']
    y = request.matchdict['y']
    zoom = request.matchdict['zoom']
    session = DBSession()
    tile = session.query(Tile).get((x, y, zoom, job_id))
    tile.comment = request.params['comment']
    if 'invalidate' in request.params:
        # task goes back to the queue
        tile.checkin = 0
    elif 'validate' in request.params:
        # task in validated
        tile.checkin = 2
    else:
        #task is done
        tile.checkin = 1
    tile.checkout = False
    tile.change = True
    session.add(tile)
    return dict(job=tile.job)
Exemplo n.º 33
0
def job_edit(request):
    id = request.matchdict['job']
    session = DBSession()
    job = session.query(Job).get(id)

    if 'form.submitted' in request.params:
        job.title = request.params['title']
        job.short_description = request.params['short_description']
        job.description = request.params['description']
        job.workflow = request.params['workflow']
        josm_preset = request.params['josm_preset']
        josm_preset = josm_preset.value.decode(
            'UTF-8') if josm_preset != '' else ''
        job.josm_preset = josm_preset
        job.is_private = request.params.get('is_private') == 'on'
        job.requires_nextview = request.params.get('requires_nextview') == 'on'
        job.imagery = request.params['imagery']

        session.add(job)
        return HTTPFound(location=route_url('job', request, job=job.id))

    return dict(job=job)
Exemplo n.º 34
0
def job_new(request):
    if "form.submitted" in request.params:
        session = DBSession()
        job = Job()
        job.title = request.params["title"]
        job.description = request.params["description"]
        job.geometry = request.params["geometry"]
        job.workflow = request.params["workflow"]
        job.imagery = request.params["imagery"]
        job.zoom = request.params["zoom"]
        job.is_private = request.params.get("is_private", 0)
        job.requires_nextview = request.params.get("requires_nextview", 0)

        tiles = []
        for i in get_tiles_in_geom(loads(job.geometry), int(job.zoom)):
            tiles.append(Tile(i[0], i[1]))
        job.tiles = tiles

        session.add(job)
        session.flush()
        return HTTPFound(location=route_url("job", request, job=job.id))
    return {}
Exemplo n.º 35
0
def job_new(request):
    if 'form.submitted' in request.params:
        session = DBSession()
        job = Job()
        job.title = request.params['title']
        job.description = request.params['description']
        job.geometry = request.params['geometry']
        job.workflow = request.params['workflow']
        job.imagery = request.params['imagery']
        job.zoom = request.params['zoom']
        job.is_private = request.params.get('is_private', 0)
        job.requires_nextview = request.params.get('requires_nextview', 0)

        tiles = []
        for i in get_tiles_in_geom(loads(job.geometry), int(job.zoom)):
            tiles.append(Tile(i[0], i[1]))
        job.tiles = tiles

        session.add(job)
        session.flush()
        return HTTPFound(location = route_url('job', request, job=job.id))
    return {} 
Exemplo n.º 36
0
def lock(request):
    job_id = request.matchdict['job']
    session = DBSession()
    username = authenticated_userid(request)
    user = session.query(User).get(username)
    job = session.query(Job).get(job_id)

    x = request.matchdict['x']
    y = request.matchdict['y']
    zoom = request.matchdict['zoom']
    tile = session.query(Tile).get((x, y, zoom, job_id))

    # task is already checked out by someone else
    if tile.checkout is True and tile.username != user:
        msg = 'You cannot lock this task. Someone else is already working on it.'
        return dict(error_msg=msg)

    # check if user has no task he's currently working on
    filter = and_(Tile.username==username, Tile.checkout==True, Tile.job_id==job_id)
    tiles_current = session.query(Tile).filter(filter).all()
    if len(tiles_current) > 0 and tile.user != user:
        msg = 'You already have a task to work on. Finish it before you can accept a new one.'
        return dict(error_msg=msg)

    try:
        tile.username = username
        tile.checkout = True
        tile.change = False
        tile.comment = None
        session.add(tile)
        return dict(success=True, tile=dict(x=tile.x, y=tile.y, z=tile.zoom))
    except:
        if int(checkin) == 1:
            msg = 'Sorry. No task available to validate.'
        else:
            msg = 'Sorry. No task available to take.'
        return dict(job=job, error_msg=msg)
Exemplo n.º 37
0
def split_tile(request):
    """
    Split the tile and copy history of the parent tile
    """
    job_id = request.matchdict['job']
    x = request.matchdict['x']
    y = request.matchdict['y']
    zoom = request.matchdict['zoom']
    session = DBSession()
    job = session.query(Job).get(job_id)
    tile = session.query(Tile).get((x, y, zoom, job_id))
    session.delete(tile)

    # reference tile history
    tileHistory = (
        session.query(TileHistory)
        .filter_by(x=x, y=y, zoom=zoom, job_id=job_id)
        .order_by(TileHistory.update)
        .all()
    )

    new_tiles = []
    t = []
    for i in range(0, 2):
        for j in range(0, 2):
            # add new tile
            X = int(x) * 2 + i
            Y = int(y) * 2 + j
            Zoom = int(zoom) + 1
            newTile = Tile(X, Y, Zoom)
            newTile.job = job

            for idx, historyRecord in enumerate(tileHistory):
                # copy tileHistory... use negative versions to prevent unique
                # key conflicts, and enable filtering (exclusion) when
                # generating stats
                newTileHistory = TileHistory(
                    x=newTile.x,
                    y=newTile.y,
                    zoom=newTile.zoom,
                    username=historyRecord.username,
                    update=historyRecord.update,
                    checkout=historyRecord.checkout,
                    checkin=historyRecord.checkin,
                    change=historyRecord.change,
                    comment=historyRecord.comment,
                    job_id=job.id,
                    version=-idx
                )
                session.add(newTileHistory)

            t.append(newTile)
    for tile in t:
        new_tiles.append(
            Feature(
                geometry=tile.to_polygon(),
                id=str(tile.x) + '-' + str(tile.y) + '-' + str(tile.zoom)))
    return dict(
        success=True, split_id="-".join([x, y, zoom]),
        new_tiles=FeatureCollection(new_tiles)
    )
Exemplo n.º 38
0
#!env/bin/python
__requires__ = 'OSMTM'

import os
import sys
import transaction

from sqlalchemy import create_engine

from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )

from OSMTM.models import (
    DBSession,
    Job,
    Tile,
    TileHistory,
    Base,
    )

from OSMTM.utils import *

engine = create_engine('sqlite:///OSMTM.db')
DBSession.configure(bind=engine)
with transaction.manager:
    tile = DBSession.query(Tile).filter(Tile.job_id==292, Tile.x==19372, Tile.y==17805).one()
    tile.checkin = 1
    DBSession.add(tile)
Exemplo n.º 39
0
def take(request):
    job_id = request.matchdict['job']
    if "checkin" in request.matchdict:
        checkin = request.matchdict['checkin']
    else:
        checkin = None
    session = DBSession()
    username = authenticated_userid(request)
    user = session.query(User).get(username)
    job = session.query(Job).get(job_id)

    filter = and_(Tile.checkin == checkin, Tile.job_id == job_id)
    tiles = session.query(Tile).filter(filter).all()
    # take random tile
    if checkin is not None:
        # get the tile the user worked on previously
        filter = and_(TileHistory.username == username,
                      TileHistory.job_id == job_id)
        p = session.query(TileHistory).filter(filter).order_by(
            TileHistory.update.desc()).limit(4).all()
        tile = None
        if p is not None and len(p) > 0:
            p = p[len(p) - 1]
            neighbours = [(p.x - 1, p.y - 1),
                          (p.x - 1, p.y), (p.x - 1, p.y + 1), (p.x, p.y - 1),
                          (p.x, p.y + 1), (p.x + 1, p.y - 1), (p.x + 1, p.y),
                          (p.x + 1, p.y + 1)]
            for t in tiles:
                if (t.x, t.y) in neighbours:
                    tile = t
                    break
    # x / y given, selecting the tile
    else:
        x = request.matchdict['x']
        y = request.matchdict['y']
        zoom = request.matchdict['zoom']
        tile = session.query(Tile).get((x, y, zoom, job_id))

        # task is already checked out by someone else
        if tile.checkout is True and tile.username != user:
            msg = 'You cannot take this task. Someone else is already working on it.'
            return dict(job=job, error_msg=msg)

        if tile.checkin >= 2:
            msg = 'This tile has already been validated.'
            return dict(job=job, error_msg=msg)

    # check if user has no task he's currently working on
    filter = and_(Tile.username == username, Tile.checkout == True,
                  Tile.job_id == job_id)
    tiles_current = session.query(Tile).filter(filter).all()
    if len(tiles_current) > 0 and tile.user != user:
        request.session.flash(
            'You already have a task to work on. Finish it before you can accept a new one.'
        )
        return HTTPFound(location=request.route_url('job', job=job_id))

    try:
        if tile is None:
            tile = tiles[random.randrange(0, len(tiles))]
        tile.username = username
        tile.checkout = True
        tile.change = False
        session.add(tile)
        return HTTPFound(location=request.route_url(
            'task', job=job_id, x=tile.x, y=tile.y, zoom=tile.zoom))
    except:
        if int(checkin) == 1:
            msg = 'Sorry. No task available to validate.'
        else:
            msg = 'Sorry. No task available to take.'
        return dict(job=job, error_msg=msg)
Exemplo n.º 40
0
__requires__ = 'OSMTM'

import os
import sys
import transaction

from sqlalchemy import create_engine

from pyramid.paster import (
    get_appsettings,
    setup_logging,
)

from OSMTM.models import (
    DBSession,
    Job,
    Tile,
    TileHistory,
    Base,
)

from OSMTM.utils import *

engine = create_engine('sqlite:///OSMTM.db')
DBSession.configure(bind=engine)
with transaction.manager:
    tile = DBSession.query(Tile).filter(Tile.job_id == 292, Tile.x == 19372,
                                        Tile.y == 17805).one()
    tile.checkin = 1
    DBSession.add(tile)
Exemplo n.º 41
0
from sqlalchemy import create_engine
from OSMTM.models import (
    Tile,
    DBSession
)
import json

engine = create_engine('sqlite:///OSMTM.db')
DBSession.configure(bind=engine)


json_data=open('census.geojson')
data = json.load(json_data)
json_data.close()

x = 0
for f in data['features']:
  wkt = ''
  for c in f['geometry']['coordinates'][0]:
    if wkt != '':
      wkt = wkt + ','
    wkt = wkt + str(c[0]) + " " + str(c[1])

  t = Tile(x,0,0,'POLYGON((' + wkt + '))')
  x = x + 1
  t.job_id = 7

  with transaction.manager:
    DBSession.add(t)
    DBSession.flush()
Exemplo n.º 42
0
def take(request):
    job_id = request.matchdict['job']
    if "checkin" in request.matchdict:
        checkin = request.matchdict['checkin']
    else:
        checkin = None
    session = DBSession()
    username = authenticated_userid(request)
    user = session.query(User).get(username)
    job = session.query(Job).get(job_id)

    filter = and_(Tile.checkin==checkin, Tile.job_id==job_id)
    tiles = session.query(Tile).filter(filter).all()
    # take random tile
    if checkin is not None:
        # get the tile the user worked on previously
        filter = and_(TileHistory.username==username, TileHistory.job_id==job_id)
        p = session.query(TileHistory).filter(filter).order_by(TileHistory.update.desc()).limit(4).all()
        tile = None
        if p is not None and len(p) > 0:
            p = p[len(p) -1]
            neighbours = [
                (p.x - 1, p.y - 1), (p.x - 1, p.y), (p.x - 1, p.y + 1),
                (p.x, p.y - 1), (p.x, p.y + 1),
                (p.x + 1, p.y - 1), (p.x + 1, p.y), (p.x + 1, p.y + 1)]
            for t in tiles:
                if (t.x, t.y) in neighbours:
                    tile = t
                    break
    # x / y given, selecting the tile
    else:
        tilex = request.matchdict['x']
        tiley = request.matchdict['y']
        tile = session.query(Tile).get((tilex, tiley, job_id))

        # task is already checked out by someone else
        if tile.username is not None and tile.user != user:
            msg = 'You cannot take this task. Someone else is already working on it.'
            return dict(job=job, error_msg=msg)

        if tile.checkin >= 2:
            msg = 'This tile has already been validated.'
            return dict(job=job, error_msg=msg)

    # check if user has no task he's currently working on
    filter = and_(Tile.username==username, Tile.job_id==job_id)
    tiles_current = session.query(Tile).filter(filter).all()
    if len(tiles_current) > 0 and tile.user != user:
        request.session.flash('You already have a task to work on. Finish it before you can accept a new one.')
        return HTTPFound(location=request.route_url('job', job=job_id))

    try:
        if tile is None:
            tile = tiles[random.randrange(0, len(tiles))]
        tile.username = username
        tile.update = datetime.now()
        session.add(tile)
        return HTTPFound(location=request.route_url('task', job=job_id, x=tile.x, y=tile.y))
    except:
        if int(checkin) == 1:
            msg = 'Sorry. No task available to validate.'
        else:
            msg = 'Sorry. No task available to take.'
        return dict(job=job, error_msg=msg)