def check_user_seesion(): try: if not "HTTP_COOKIE" in os.environ or not os.environ["HTTP_COOKIE"]: return False cookie = cookies.SimpleCookie(os.environ["HTTP_COOKIE"]) sess = session.Session(expires=365*24*60*60, cookie_path='/') #lastvisit = sess.data.get('lastvisit') #sess.data['lastvisit'] = repr(time.time()) #print print_page('index.html', "Inicio") #print cookie["sid"].value conn = Connection() result = conn.valid_username_cookie_id(sess.cookie['sid'].value) cookie_file = format_cookie_path(sess.cookie['sid'].value) #isfile = os.path.exists(cookie_file) #print result #print isfile #print cookie["sid"].value != sess.cookie["sid"].value if not result: return False else: return True except cookies.CookieError as error: return False
def __place_order(user): parser = helpers.FormParser() parser.discover_values() address = parser.get_value('address', '') if __validate_properties(address): return False conn = Connection() if not conn.place_order(user.user_id, address): __ERRORS[constants.VALIDATION_ERROR] = conn.errors() return False return True
def __build_detail_list_html(): conn = Connection() product_list = conn.fetch_products_by_keywords(__field_keywords()) detail_list = [] if not product_list: return "" for product in product_list: detail_list.append(__details_html().format(product.code, product.descr, product.price, product.username, product.entry_date)) return ''.join([x for x in detail_list])
def __build_dynamic_content(): parser = FormParser() parser.discover_values() code = parser.get_value("code", "") if not code or not parser.elements_count: return '' conn = Connection() product = conn.fetch_product_by_code(code) if not product: return '' return loadhtml("productdetail.html").format(product.code, product.descr, product.price, \ product.username, product.entry_date).replace("**add_qty**", __form_add_qty_content().format(product.code) if helpers.check_user_session() else '')
def create_cookie(username): sess = session.Session(expires=365*24*60*60, cookie_path='/') #lastvisit = sess.data.get('lastvisit') #if lastvisit: # message = 'Welcome back. Your last visit was at ' + \ # time.asctime(time.gmtime(float(lastvisit))) #else: # message = 'New session' # Save the current time in the session conn = Connection() #sess.data['lastvisit'] = repr(time.time()) date = datetime.fromtimestamp(int(sess.cookie['sid']['expires'])).strftime('%Y-%m-%d %H:%M:%S') conn.insert_user_cookie(sess.cookie['sid'].value, username, date) print("Location: index.py") print(sess.cookie) print("Content-type: text/html\n\n")
def __process_request(user): parser = FormParser() parser.discover_values() code = parser.get_value("code", "") descr = parser.get_value("descr", "") price = parser.get_value("price", "") if __validate_properties(code, descr, price): return False else: product = Product(0, user.user_id, code, current_date(), \ descr, float(price) if is_float(price) else 0) conn = Connection() if not conn.create_product(product): __ERRORS[constants.VALIDATION_ERROR] = conn.errors() return False return True
def __process_update_cart_qty(user): parser = FormParser() parser.discover_values() code = parser.get_value("code", "") quantity = parser.get_value("quantity", "") if __validate_properties(user.user_id, code, quantity): return False conn = Connection() product = conn.fetch_product_by_code(code) if not product: __ERRORS[ constants. VALIDATION_ERROR] = 'El producto no se encuentra registrado en el sistema.' return False if not conn.increase_cart_qty( ShoppingCart(user.user_id, product.product_id, quantity)): __ERRORS[constants.VALIDATION_ERROR] = conn.errors() return False return True
"""Validate the product properties before save it to database""" errorMessage = None if not message and not reason: errorMessage = 'all' elif not message: errorMessage = 'message' elif not reason: errorMessage = 'reason' return errorMessage if query_string: parser = FormParser() parser.parse_values(query_string) conn = Connection() reason = parser.get_value("reason", "") message = parser.get_value("message", "") sess = session.Session(expires=365 * 24 * 60 * 60, cookie_path='/') #lastvisit = sess.data.get('lastvisit') #sess.data['lastvisit'] = repr(time.time()) user = conn.autorized_session(sess.cookie['sid'].value) if user is not None and user is not '403': user_id = user.user_id name = user.username email = user.email if message and reason: suggestion = Suggestion(user_id, '', reason, message, name, email) creeateSuggestion = conn.create_suggestion(suggestion) else:
def __build_dynamic_content(): conn = Connection() product_list = conn.fetch_products_by_user_id( request_handler.fetch_authorized_user_session().user_id) return loadhtml("myproducts.html").replace( "**details**", __build_detail_list_html(product_list))
def fetch_authorized_user_session(): sess = helpers.build_session_entity() conn = Connection() return conn.autorized_session(sess.cookie['sid'].value)
#PACKAGE_PARENT = __SCRIPT_DIR = os.path.dirname( os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) __SCRIPT_DIR = os.path.normpath(os.path.join(__SCRIPT_DIR, '..')) if not __SCRIPT_DIR in sys.path: sys.path.append(__SCRIPT_DIR) from data.dao import Connection from data.models import User from utils.helpers import FormParser, pagetemplate, valiadtionMessage, ucgiprint, loadhtml from utils import constants, helpers if helpers.request_method() == 'GET' and helpers.check_user_session(): helpers.redirect('index.py') __CONN = Connection() __ERRORS = {} __BODY = {} def __validate_properties(name, username, password, email): """Validate the product properties before save it to database""" helpers.validate_string_input('name', name, 50, 'Nombre', __ERRORS, False) helpers.validate_string_input('username', username, 50, 'Usuario', __ERRORS) helpers.validate_string_input('email', email, 50, 'Correo', __ERRORS) helpers.validate_string_input('password', password, None, 'Contraseña', __ERRORS) return __ERRORS
def __build_dynamic_content(): conn = Connection() results = conn.fetch_cart_products_by_user_id(request_handler.fetch_authorized_user_session().user_id) subtotal = sum(c.total() for c in results) return loadhtml("cart.html").replace("**details**", __build_detail_list_html(results)) \ .replace("**subtotal**", str(subtotal))