def _send_groupme_dm(agent_name): text = request.form['text'] if text: # Should make it possible to instantiate a User object from an agent_name query = """ MATCH (a:User) WHERE a.agent_name = {agent_name} RETURN a.groupme_id """ groupme_id = graph.find_one("User", "agent_name", agent_name)['groupme_id'] access_token = session['groupme_token'] gm = GroupmeUser(access_token) response = gm.direct_message(groupme_id, text) if response.status_code == 201: flash('Message sent') elif response.status_code == 403: flash('User has been auto-banned for sending too many messages.') elif response.status_code == 400: flash( 'The application encountered a problem sending your message. Contact the site administrator' ) else: flash( 'Something unexpected happened. Contact the site administrator with the following information:' ) flash(response.json()) else: flash('Enter text to send a message.') return redirect(url_for('profile', agent_name=agent_name))
def _groupme_callback(): access_token = request.args.get('access_token') try: gm = GroupmeUser(access_token) username = verify_agent(id=gm.id, token=app_token, service='groupme') except requests.ConnectionError, e: if app.debug: from collections import namedtuple gm = namedtuple('GroupmeUser', ['id', 'nickname']) username = '******' gm.id = -1 gm.nickname = username else: raise e
def _modify_connections(): print "Modifications recieved" print request.form['btn'] if request.form['btn'] != 'Modify Connections': return redirect(url_for('connections')) access_token = request.args.get('access_token') gm = GroupmeUser(access_token) user = User(session['groupme_id']) for k, v in request.form.iteritems(): if k == 'btn' or not v: continue action, id = k.split('_') print action, id, v if action == 'mod': # Modify cost of an existing connection user.modify_verified_relationship(groupme_id=id, cost=int(v)) if action == 'disconn': # Disconnect from node. Should only be available if no other/inbound verified relationships to node user.disconnect(groupme_id=id) if action == 'block': # Disconnect and block all routes through node. Register an alarm of some kind. user.block(groupme_id=id) return redirect(url_for('connections'))
def _verify_connections(): print "Modifications recieved" print request.form['btn'] if request.form['btn'] != 'Verify Connections': return redirect(url_for('connections')) access_token = request.args.get('access_token') gm = GroupmeUser(access_token) user = User(session['groupme_id']) for k, v in request.form.iteritems(): if k == 'btn' or not v: continue action, id = k.split('_') print action, id, v if action == 'verify': user.set_user_relationship(target=User(groupme_id=id).node, cost=int(v), verified=True, override=True) if action == 'block': # Disconnect and block all routes through node. Register an alarm of some kind. user.block(groupme_id=id) return redirect(url_for('connections'))
class ConnectionSuggesterGM(object): def __init__(self, groupme_id, groupme_token): self.id = groupme_id self.user = User(groupme_id) self.gm = GroupmeUser(groupme_token) self.neighbors = [n['groupme_id'] for n, _ in self.user.neighbors()] def new_connections(self, n=0): """ Return users connected through groupme that are not connected to the current user in any way. """ if not hasattr(self, '_new_connections'): self._new_connections = [] for sugg in self.gm.similar_users(n): # 0=no limit # Should I change this to use .verified_neighbors instead of .neighbors? if sugg['id'] not in self.neighbors: self._new_connections.append(sugg) self._current_sort = 'groupmeGroups' return self._new_connections @property def verify_connections(self): """ Find users who have connected to the current user, but the connection as not been reciprocated. """ if not hasattr(self, '_verify_connections'): self._verify_connections = self.user.unverified_neighbors_similarity( )
def _submit_new_connections(): """ Receives groupme_ids from connections.html AJAX call. Runs agent verification (and in the future, !met verification) on each ID. Sends IDs and agentnames to models.User.add_verified_relationship to create appropriate edges. Returns list of IDs for whom agent verification (and/or !met verification) was unsuccessful. """ print "Message recieved" print request.form['btn'] if request.form['btn'] != 'Submit New Connections': print "Wrong button, returning early" return redirect(url_for('connections')) print "right button, continuing." access_token = request.args.get('access_token') gm = GroupmeUser(access_token) user = User(session['groupme_id']) for id, est_hours in request.form.iteritems(): # This is a pretty big POST request. Unused form options still get submitted. # Is there a simple way to only submit populated form items? Or will I need # to write some jquery? :p if id == 'btn': continue if est_hours: print "k,v:", id, est_hours print type(est_hours), est_hours est_hours = int(est_hours) #print id, est_hours verified = [] io_verif_fail = [] agent = verify_agent(id=id, token=app_token, service='groupme') if agent: print "Adding link {src} -> {tgt}".format(src=user.agent_name, tgt=agent) user.add_verified_relationship( groupme_id=id, agent_name=agent, cost=est_hours, default_cost=est_hours) # why not else: io_verif_fail.append(id) return redirect(url_for('connections'))
def __init__(self, groupme_id, groupme_token): self.id = groupme_id self.user = User(groupme_id) self.gm = GroupmeUser(groupme_token) self.neighbors = [n['groupme_id'] for n, _ in self.user.neighbors()]