def getUserFromCookie(cookie_value): if cookie_value: if encrypt.valid_cookie(cookie_value): userid = encrypt.getUserId(cookie_value) person = dbmodels.Person.get_by_id( userid ) # TODO handle case when memcache is flushed and database row itself gets deleted return person
def get(self): logging.error("MainPage Get") cookie_value = self.request.cookies.get(USERID_COOKIE) person = None post_mba_jobs = [] if cookie_value: if encrypt.valid_cookie(cookie_value): userid = encrypt.getUserId(cookie_value) logging.error("userid %d" % userid) person = dbmodels.Person.get_by_id( userid ) # TODO handle case when memcache is flushed and database row itself gets deleted logging.error("person") logging.error(person.linkedin_id + " " + person.fname) for pjob in person.person_job: post_mba_jobs.append((pjob.title, pjob.company.company_name, pjob.jobkey)) oauth_token = self.request.get("oauth_token") if not person: if not oauth_token: # user has not attempted login yet, show him/her login page logging.error("user has not attempted on login in") self.response.out.write(render_str("login.html")) else: # user has attempted linkedin login logging.error("user has attempted linkedin login") consumer = oauth.Consumer(consumer_key, consumer_secret) logging.error("oauth_token: " + oauth_token) oauth_token_secret = memcache.get(oauth_token) oauth_verifier = self.request.get("oauth_verifier") token = oauth.Token(oauth_token, oauth_token_secret) token.set_verifier(oauth_verifier) client = oauth.Client(consumer, token) resp, content = client.request(access_token_url, "POST") access_token = dict(urlparse.parse_qsl(content)) logging.error(access_token) oauth_expires_in = long(access_token["oauth_expires_in"]) logging.error("oauth expires in %d" % oauth_expires_in) # API call to retrieve profile using access token token = oauth.Token(key=access_token["oauth_token"], secret=access_token["oauth_token_secret"]) url = "http://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,headline,public-profile-url,picture-url,location:(name),industry,num-connections,positions:(title,start-date,end-date,is-current,company),educations:(school-name,field-of-study,start-date,end-date,degree),date-of-birth)?format=json" client = oauth.Client(consumer, token) resp3, content = client.request(url) logging.debug("content:" + content) (person, post_mba_jobs) = linkedinparser.parseContent(content, oauth_expires_in) logging.error(person) if not person: logging.error("print this ") self.redirect("/jobc/inviteonly/") return else: logging.error("person.fname " + person.fname) self.response.headers.add_header( "Set-Cookie", "%s=%s; Path=/" % (USERID_COOKIE, encrypt.make_cookie(str(person.key().id()))) ) self.response.out.write( render_str( "alumpage.html", user=person.fname, fullname=person.fname, pictureUrl=person.picture_url, njobs=len(post_mba_jobs), job=post_mba_jobs, ) ) else: # valid user if ( person.oauth_expires_in - int(round(time.time())) ) < 24 * 60 * 60: # linkedin oauth expires in 60 days, keeping a margin of 10 logging.error( "users access token has expired %d - %d" % (person.oauth_expires_in, int(round(time.time()))) ) self.response.out.write(render_str("login.html")) else: self.username = person.fname self.response.headers.add_header( "Set-Cookie", "%s=%s; Path=/" % (USERID_COOKIE, encrypt.make_cookie(str(person.key().id()))) ) self.response.out.write( render_str( "alumpage.html", user=person.fname, fullname=person.fname, pictureUrl=person.picture_url, njobs=len(post_mba_jobs), job=post_mba_jobs, ) )