Esempio n. 1
0
  def wrapper(self, req, resp):
    # Extract user credentials
    session = get_new_session()
    cookies = req.cookies
    cookie_token = cookies['api_token'] if ('api_token' in cookies) else None
    user_id = cookies['user_id'] if ('user_id' in cookies) else None

    if not user_id or not cookie_token:
      resp.status = falcon.HTTP_401
      resp.body = "No login credentials present in cookie." 
      self.me = None
      return

    query = session.query(User).filter(and_(User.id == user_id, User.api_token == cookie_token))
    if query.count() == 0:
      resp.status = falcon.HTTP_401
      resp.body = "Login credentials supplied do not match any user in database." 
      self.me = None
      return
    user = query.first()
    self.me = user
    session.commit()
    fun(self, req, resp)
Esempio n. 2
0
  def on_post(self, req, resp):
    """ Handles POST requests"""
    session = get_new_session()
    data = req.context['doc']

    if 'slyp_url' in data:
      raw_url = data['slyp_url']
    else:
      resp.status = falcon.HTTP_400
      resp.body = "URL not supplied."
      return

    # create slyp object by:
    # get from db OR create new
    mySlyp = Slyp(raw_url=raw_url)
    ret = session.query(exists().where(Slyp.raw_url==raw_url)).scalar()
    if (ret): #TODO: ensure mySlyp has topic_id set
      mySlyp = session.query(Slyp).filter(Slyp.raw_url == raw_url).first() 
      logging.info('Using slyp already created.')
    else:
      try:
        mySlyp.set_slyp()
      except:
        resp.status = falcon.HTTP_501
        resp.body = "Couldn't create slyp for this URL."
        return
      session.add(mySlyp)
      session.commit()

      for kwrd in mySlyp.keywords:
        ret = session.query(exists().where(Keyword.keyword == kwrd)).scalar()
        if (not ret):
          keyword = Keyword(keyword=kwrd)
          session.add(keyword)
          session.commit()
          slypKeyWrd = SlypKeyword(keyword_id=keyword.id, slyp_id=mySlyp.id)
          session.add(slypKeyWrd)
      
      if len(mySlyp.keywords) > 0:
          tpc = mySlyp.keywords[0]  # our current definition of 'topic' -- 8/13/15
      else:
          tpc = ''
      ret = session.query(exists().where(Topic.topic == tpc)).scalar()
      if (not ret):
        topic = Topic(topic=tpc)
        session.add(topic)
        session.commit()
      else:
        topic = session.query(Topic).filter(Topic.topic == tpc).first()
      mySlyp.topic_id = topic.id
      logging.info('Created new slyp. title: %s', mySlyp.title)

    #TODO remove creation of UserSlyp. Do this in grape

    # Create UserSlyp
    ret = session.query(exists().where(and_(UserSlyp.user_id == self.me.id, UserSlyp.slyp_id == mySlyp.id))).scalar()
    if (ret):
      myUserSlyp = session.query(UserSlyp).filter(and_(UserSlyp.user_id == self.me.id, UserSlyp.slyp_id == mySlyp.id)).first()
      logging.info('Slyp already exists in user\'s collection.')
    else:
      myUserSlyp = UserSlyp(slyp_id=mySlyp.id, user_id=self.me.id, sender_id=self.me.id) # origin 0 is 'self', 1 is 'friend'
      session.add(myUserSlyp)
      logging.info('Created new userslyp. user_id: %d, slyp_id: %d', myUserSlyp.user_id, myUserSlyp.slyp_id)

    session.commit()

    resp.status = falcon.HTTP_201
    resp.data = json.dumps(mySlyp.as_dict(), default=json_serial)
    return