Пример #1
0
	def get(self, linkid):
		# check for login cookie.
		logging.info("Checking for cookie")
		idHash = self.request.cookies.get(user.USER_COOKIE_KEY)
		if idHash == None:
			logging.info("No cookie found")
			self.response.headers.add_header('Set-Cookie', user.LogoutUser())
		else:
			logging.info("Got Cookie: %s" %idHash)
			cookie_username = library.check_hash_str(idHash)
			logging.info("resulting username: %s" %cookie_username)
			username_verify = user.queryUsername(cookie_username)
			if username_verify != None:
				logging.info("Loading user and logging in")
				user.loadUserAccount(cookie_username)
				user.LoginUser()
			else:
				logging.info("Unable to find cookie match")
				self.response.headers.add_header('Set-Cookie', user.LogoutUser())

		"""Get a page for just the link identified."""
		links_list = link.getLinkByID(long(linkid))
		if links_list == None:
			self.response.set_status(404, 'Not Found')
			return      
		
		
		links = [links_list]

		template_values = create_template_dict_with_single_link(user, links_list, 'Link', nexturi=None, prevuri=None, page=0)
		template_file = os.path.join(os.path.dirname(__file__), 'templates/singlelink.html')    
		self.response.out.write(template.render(template_file, template_values))
Пример #2
0
	def post(self, linkid):
		# Collect the data from the form.
		username = self.request.get("username")
		password = self.request.get("password")
		button = self.request.get("submit")
		remember = self.request.get("rememberMe")
		links_list = link.getLinkByID(long(linkid))
		template_to_use = 'templates/singlelink.html'

		if button == "register":
			self.redirect("/register")
		
		if button == "submit link":
			#logging.info("Submit Link button pressed")
			if not user.LOGGED_IN:
				template_values  = {
					'user': user,
					'loggedin' : user.LOGGED_IN,
					'links' : link_for_template(links_list, user, 0),
					'section': 0,
					'nexturi': 0,
					'prevuri': 0,
					'error_msg' : 'You must be registered and logged in to submit a link'
				}
				template_file = os.path.join(os.path.dirname(__file__), template_to_use)
				self.response.out.write(template.render(template_file, template_values))
				return
			else:
				self.redirect("/submit_link")
			
		if button == "logout":
			#logging.info("Check to see if the user is logged in")
			if user.LOGGED_IN:
				logging.info("user is logged in, so creating cookie to log out")
				#cookieStr = user.createUserCookie("no")
				self.response.headers.add_header('Set-Cookie', user.LogoutUser())

			#logging.info("Rending main page again...")
			template_values  = {
				'user': user,
				'loggedin' : user.LOGGED_IN,
				'links' : link_for_template(links_list, user, 0),
				'section': 0,
				'nexturi': 0,
				'prevuri': 0
			}
			template_file = os.path.join(os.path.dirname(__file__), template_to_use)
			self.response.out.write(template.render(template_file, template_values))
			return
			
		error_str = ''
		
		if username and password:
			if user.loadUserAccount(username) == True:
				#logging.info("User found, validating user")
				if user.validateUser(username, password) == True:
					#Username and password has been validated, now log them in.
					#logging.info("User validated, logging in")
					user.LoginUser()
					# create the cookie string
					#logging.info("Creating cookie")
					cookieStr = user.createUserCookie(remember)
					# Write the cookie.
					#logging.info("setting cookie")
					self.response.headers.add_header('Set-Cookie', cookieStr)
					#logging.info("Rendering main...")
				else:
					error_str = 'Password mismatch'
			else:
				error_str = 'Username not found'
		else:
			error_str = 'Need to enter a username and password!'
		
		#logging.info("user username = "******"user loggedin = " + str(user.LOGGED_IN))
		template_values  = {
			'user': user,
			'loggedin' : user.LOGGED_IN,
			'links' : link_for_template(links_list, user, 0),
			'section': 0,
			'nexturi': 0,
			'prevuri': 0,
			'error' : error_str
		}
		template_file = os.path.join(os.path.dirname(__file__), template_to_use)
		self.response.out.write(template.render(template_file, template_values))