Ejemplo n.º 1
0
def unfold(short):
	s = Shortened.query.filter(Shortened.short == short).first()
	if s == None:
		abort(404)
	else:
		time = Log(s)
		db_session.add(time)
		db_session.commit()
		return redirect(s.url)
Ejemplo n.º 2
0
def callback():
	print request
	if request.method=='GET':

		oauth_token = request.args['oauth_token']
		oauth_verifier = request.args['oauth_verifier']

		token = oauth.Token(session['request_token']['oauth_token'],session['request_token']['oauth_token_secret'])
		token.set_verifier(oauth_verifier)
		
		consumer=oauth.Consumer(app.config['CONSUMER_KEY'],app.config['CONSUMER_SECRET'])
		client = oauth.Client(consumer, token)

		resp, content = client.request(app.config['ACCESS_TOKEN_URL'], "POST")
		
		if resp['status'] != '200':
			raise Exception("Invalid response %s." % resp['status'])
		
		access_token = dict(urlparse.parse_qsl(content))

		# print "------"
		# print access_token
		# print "------"
		# print session
		# print "------"

		user = User.query.filter(User.username == access_token['screen_name']).first()
		if user == None:
			user = User(access_token['user_id'], access_token['screen_name'], access_token['oauth_token'], access_token['oauth_token_secret'])
			db_session.add(user)
			db_session.commit()

		user.oauth_token = access_token['oauth_token']
		user.oauth_token_secret = access_token['oauth_token_secret']
		db_session.add(user)
		db_session.commit()		
		
		session['username'] = user.username
		session['user_id'] = user.id
		return redirect("/")
Ejemplo n.º 3
0
def short():
	if 'username' in session:
		username = session['username']
	error = None
	url = None
	short = None
	if request.method == 'GET':
		if 'url' in request.args:
			url = request.args['url']
			if 'custom' in request.args and request.args['custom']:
				short = request.args['custom']
				s = Shortened.query.filter(Shortened.short == short).first()
				if s == None:
					s = Shortened(url, short)
					db_session.add(s)
					db_session.commit()

					if 'username' in session:
						user = User.query.filter(User.id == session['user_id']).first()
						if s not in user.shorts:
							user.shorts.append(s)
							db_session.add(user)
							db_session.commit()

				else:
					s = Shortened.query.filter(Shortened.url == url).first()
					if s == None:
						flash("Custom URL already taken")
						return redirect("/?url={}&custom={}".format(url,short))
					elif 'username' in session:
						user = User.query.filter(User.id == session['user_id']).first()
						if s not in user.shorts:
							user.shorts.append(s)
							db_session.add(user)
							db_session.commit()

			else:
				s = Shortened.query.filter(Shortened.url == url).filter(Shortened.custom == False).first()
				if s == None:
					s = Shortened(url)
					db_session.add(s)
					db_session.commit()

					if 'username' in session:
						user = User.query.filter(User.id == session['user_id']).first()
						if s not in user.shorts:
							user.shorts.append(s)
							db_session.add(user)
							db_session.commit()

				elif 'username' in session:
					user = User.query.filter(User.id == session['user_id']).first()
					if s not in user.shorts:
						user.shorts.append(s)
						db_session.add(user)
						db_session.commit()

			short = s.short
			
		else:
			error = "There's no url"
	return render_template('short.html', error=error, url=url, short=short,session=session)