예제 #1
0
def create_group():
    groupname = request.args.get('groupname')
    bounds = request.args.get('bounds')
    bounds_arr = request.args.get('bounds').split(" ")
    opengroup = 'false'
    if request.args.get('opengroup') == 'on':
        opengroup = 'true'
    cur.execute(
        "SELECT count(*) from groups where groupname = '{}' and userid = {} and bounds = '{}'"
        .format(groupname, session['userid'], bounds))
    response = cur.fetchall()
    for row in response:
        if row[0] > 0:
            return jsonify("group already exists")
    cur.execute(
        "INSERT INTO groups (geom, groupname, userid, bounds,opengroup) "
        "VALUES (ST_Centroid(ST_GeomFromText('MULTIPOINT ({} {},{} {})')), '{}', {}, '{}', {})"
        .format(bounds_arr[0], bounds_arr[1], bounds_arr[2], bounds_arr[3],
                groupname, session['userid'], bounds, opengroup))
    cur.execute(
        "SELECT groupid FROM groups WHERE groupname = '{}'".format(groupname))
    response = cur.fetchall()
    for row in response:
        groupid = row[0]
    cur.execute("INSERT INTO usersgroups VALUES ({},{})".format(
        session['userid'], groupid))
    pgconnect.commit()
    return jsonify(groupid=groupid)
예제 #2
0
def get_user_invites():
    invreq = {'invites': [], 'requests': []}
    for gr, g, u in sess.query(GroupRequests, Group, Users).filter_by(invitee=session['userid']).\
            filter_by(complete='f').join(Group).join(Users):
        invreq['requests'].append({
            "requestid": gr.requestid,
            "requester": u.username,
            "group": g.groupname,
            "date": gr.dateissued
        })

    cur.execute(
        "SELECT inviteme.requestid, users.username, groups.groupname, inviteme.date "
        "FROM inviteme INNER JOIN users ON users.userid = inviteme.userid "
        "JOIN groups ON groups.groupid = inviteme.groupid  "
        "WHERE accepted is null AND groups.userid = '{}'".format(
            session['userid']))
    response = cur.fetchall()
    for row in response:
        invreq['invites'].append({
            "requestid": row[0],
            "requester": row[1],
            "group": [2],
            "date": row[3]
        })
    pgconnect.commit()
    return jsonify(invites=invreq)
예제 #3
0
def delete_group():
    cur.execute("Delete from mapobjects where groupid = {}".format(
        session['groupid']))
    pgconnect.commit()
    sess.query(Post).filter_by(groupid=session['groupid']).delete()
    sess.query(Thread).filter_by(groupid=session['groupid']).delete()
    sess.query(Group).filter_by(groupid=session['groupid']).delete()
    username = sess.query(Users).filter_by(
        userid=session['userid']).one().username
    return render_template('groupselect.html', username=username)
예제 #4
0
def add_geojson():
    json = urlparse.unquote(request.form['geojson'])

    group_admin = sess.query(Group).filter_by(
        groupid=session['groupid']).one().userid
    if session['userid'] != group_admin:
        return jsonify("not allowed")
    cur.execute(
        "INSERT INTO mapobjects (geom, groupid, userid, date) VALUES "
        "(ST_Transform(ST_GeomFromText('{}',4326),3857), {}, {}, '{}');".
        format(json, session['groupid'], session['userid'],
               datetime.datetime.utcnow()))
    pgconnect.commit()
    return jsonify('success')
예제 #5
0
def manage_request():
    requestid = request.form['requestid']
    action = request.form['submit']
    cur.execute(
        "SELECT groupid,invitee FROM grouprequests WHERE requestid = {};".
        format(requestid))
    response = cur.fetchall()
    for row in response:
        if action == 'accept':
            # make sure it doesn't add twice
            cur.execute("INSERT INTO usersgroups VALUES ({},{})".format(
                row[1], row[0]))
        cur.execute(
            "UPDATE grouprequests set complete = 't' WHERE requestid = {}".
            format(requestid))
        pgconnect.commit()
    return render_template('groupselect.html')
예제 #6
0
def delete_post():
    postid = request.args.get('postid', 0, type=int)
    is_this_my_post = sess.query(Post).filter_by(userid=session['userid']).filter_by(postid=postid).one()
    if is_this_my_post.userid != session['userid']:
        return jsonify('request not allowed')
    objectid = is_this_my_post.objectid
    canIdelete = sess.query(Post).filter_by(responseto=postid).count()
    if canIdelete > 0:
        return jsonify('request not allowed')
    sess.query(Post).filter_by(userid=session['userid']).filter_by(postid=postid).delete()
    sess.commit()
    # check if you can delete the geometry
    usingobject = cur.execute("SELECT count(*) from posts where objectid = {}".format(objectid))
    response = cur.fetchall()
    for row in response:
        if row[0] == 0:
            cur.execute("DELETE from mapobjects where objectid = {}".format(objectid))
            pgconnect.commit()
    return jsonify('success')
예제 #7
0
def quit_group():
    groupid = request.form['groupid']
    uid = sess.query(UsersGroups).filter_by(groupid=groupid).filter_by(
        userid=session['userid']).count()
    if uid > 0:
        cur.execute(
            "DELETE FROM Votes where userid = {} and postid in (Select postid from posts where groupid = {})"
            .format(session['userid'], groupid))
        sess.query(Post).filter_by(groupid=groupid).filter_by(
            userid=session['userid']).delete()
        cur.execute(
            "DELETE FROM mapobjects Where userid = {} and groupid = {}".format(
                session['userid'], groupid))
        pgconnect.commit()
        sess.query(UsersGroups).filter_by(groupid=groupid).filter_by(
            userid=session['userid']).delete()
        sess.query(GroupRequests).filter_by(groupid=groupid).filter_by(
            invitee=session['userid']).delete()
        sess.query(InviteMe).filter_by(groupid=groupid).filter_by(
            userid=session['userid']).delete()
        sess.commit()
    username = sess.query(Users).filter_by(
        userid=session['userid']).one().username
    return render_template('groupselect.html', username=username)
예제 #8
0
def oauth_authorized(resp):
    next_url = request.args.get('next') or url_for('index')
    if resp is None:
        flash(u'You denied the request to sign in.')
        return redirect(next_url)

    session['twitter_token'] = (resp['oauth_token'],
                                resp['oauth_token_secret'])
    username = resp['screen_name']
    session['twitter_user'] = username
    if session.has_key('groupid'):
        open = sess.query(Group).filter_by(groupid=session['groupid'])
    # check if twitter user has already logged in to cartoforum
    twitteruser = sess.query(TwitterUsers).filter_by(username=username).count()

    flash('You were signed in as %s' % username)
    if twitteruser == 0:
        tu = TwitterUsers(oauth_provider='twitter',
                          username=username,
                          oauth_uid=resp['user_id'],
                          oauth_token=resp['oauth_token'],
                          oauth_secret=resp['oauth_token_secret'])
        sess.add(tu)
        sess.commit()

    else:
        tu = sess.query(TwitterUsers).filter_by(username=username).first()
        tu.oauth_token = resp['oauth_token']
        tu.oauth_secret = resp['oauth_token_secret']
        sess.commit()

    # check if the twitter users screen name has already been taken.
    userquery = sess.query(Users).filter_by(
        username='******'.format(username)).count()

    if userquery == 0:
        # move on with their twitter screen name
        newuser = Users(username='******'.format(username),
                        password='******',
                        twitterid=resp['user_id'])
        sess.add(newuser)
        sess.commit()
    else:
        # this username exists, is the twitterid different from what we have logged?
        twitterid = sess.query(TwitterUsers).filter_by(
            username='******'.format(username)).count()
        if twitterid == 0:
            # offer them a different name
            alt_name = utils.get_alternate_username(session['twitter_user'])
            if not session['groupid']:
                return render_template('select_username.html',
                                       alt_name=alt_name)
        else:
            username = sess.query(Users).filter_by(
                twitterid=resp['user_id']).one().username

    tulogged = sess.query(Users).filter_by(
        username='******'.format(username)).one()
    session['userid'] = tulogged.userid
    session['logged_in'] = True
    if session.has_key('groupid'):
        cur.execute("INSERT INTO usersgroups VALUES ({},{})".format(
            session['userid'], session['groupid']))
        cur.execute(
            "SELECT groupname,bounds from groups where groupid = {}".format(
                session['groupid']))
        pgconnect.commit()
        response = cur.fetchall()
        for row in response:
            groupname = row[0]
            bounds = row[1]
        # Check for group membership, return group name and bounds and username
        user = sess.query(Users).filter_by(userid=session['userid']).one()
        username = user.username
        basemap = user.basemap
        color = user.color
        # TODO: check that user is a member of group
        return render_template('map.html',
                               groupid=session['groupid'],
                               userid=session['userid'],
                               username=username,
                               basemap=basemap,
                               color=color,
                               groupname=groupname,
                               bounds=bounds)
    else:
        return render_template('groupselect.html', username=username)