def submit(self): Basket.empty() notify_user( translations("woost.extensions.ecommerce." "empty_basket_notice"), category = "success" ) Location.get_current().go("GET")
def submit(self): Form.submit(self) purchase = self.instance["purchase"] product = purchase.product purchase.delete() Basket.store() notify_user( translations( "woost.extensions.ecommerce.delete_purchase_notice", product = product ), category = "success" ) Location.get_current().go("GET")
def get_content_cache_key(self, publishable, **context): user = get_current_user() cache_key = ( str(Location.get_current(relative = False)), None if user is None or user.anonymous else tuple(role.id for role in user.roles) ) key_qualifier = None expression = self.cache_key_expression if expression: expression = expression.replace("\r", "") context["publishable"] = publishable exec expression in context key_qualifier = context.get("cache_key") else: request = context.get("request") if request: key_qualifier = tuple(request.params.items()) if key_qualifier: cache_key = cache_key + (key_qualifier,) return cache_key
def render_pdf(self): # Create a temporary folder to hold the PDF temp_path = mkdtemp() pdf_file_path = os.path.join(temp_path, "file.pdf") try: location = Location.get_current(relative = False) location.query_string["format"] = "html" # Create the file command = extension.command % { "url": unicode(location), "output_file": pdf_file_path } proc = Popen(command.encode("utf-8"), shell = True) stdout, stderr = proc.communicate() if proc.returncode: raise OSError("Error generating PDF" + (": " + stderr) if stderr else "" ) # Serve the file cherrypy.response.headers["Content-Type"] = "application/x-pdf" return serve_file( pdf_file_path, content_type = "application/x-pdf", disposition = "attachment", name = translations(self.context["publishable"]) + ".pdf" ) finally: rmtree(temp_path)
def focus_block(block): location = Location.get_current() location.hash = "block" + str(block.id) location.query_string.pop("action", None) location.query_string.pop("block_parent", None) location.query_string.pop("block_slot", None) location.query_string.pop("block", None) location.go("GET")
def submit(self): Form.submit(self) for purchase, quantity in zip( Basket.get().purchases, self.instance["quantity"] ): purchase.quantity = quantity Basket.store() notify_user( translations("woost.extensions.ecommerce." "set_quantities_notice"), category = "success" ) if self.controller.action != "proceed": Location.get_current().go("GET")
def submit(self): Form.submit(self) Basket.get().add_purchase(self.instance) Basket.store() notify_user(translations( "woost.extensions.ecommerce.product_added_notice", product=self.product), "product_added", transient=False) if self.redirect_to_basket: raise cherrypy.HTTPRedirect( Publishable.require_instance( qname="woost.extensions.ecommerce.basket_page"). get_uri()) else: Location.get_current().go("GET")
def resolve(self, path): # Allow application modules (ie. language) to process the URI before # resolving the requested publishable item self._process_path(path) request = cherrypy.request # Item resolution publishable = self._resolve_path(path) self.context["publishable"] = publishable # HTTP/HTTPS check self._apply_https_policy(publishable) # Check maintenance mode self._maintenance_check(publishable) # Controller resolution controller = publishable.resolve_controller() if controller is None: raise cherrypy.NotFound() # Add the selected language to the current URI if publishable.per_language_publication: if not request.language_specified: location = Location.get_current() location.path_info = app.language.translate_uri() location.go() # Remove the language selection from the current URI elif request.language_specified: location = Location.get_current() location.path_info = \ "/" + "/".join(location.path_info.strip("/").split("/")[1:]) location.go() return controller
def invoke(self, controller, selection): target = selection[0] consumer = Consumer(target.app_id, target.app_secret) def request(client, *args, **kwargs): response, body = client.request(*args, **kwargs) if response["status"] != "200": raise TwitterAPIError(body) return body try: oauth_token = cherrypy.request.params.get("oauth_token") oauth_verifier = cherrypy.request.params.get("oauth_verifier") oauth_secret_session_key = "twitter_auth.%s.oauth_secret" % self.id if not oauth_token or not oauth_verifier: # Obtain a request token client = Client(consumer) location = Location.get_current(relative=False) location.query_string["item_action"] = self.id callback = quote_plus(str(location)) body = request( client, "https://api.twitter.com/oauth/request_token" "?oauth_callback=" + callback, "GET") data = dict(parse_qsl(body)) session[oauth_secret_session_key] = data["oauth_token_secret"] # Redirect the user to the login form auth_url = ("https://api.twitter.com/oauth/authorize?" "oauth_token=%s" "&oauth_callback=%s" % (data["oauth_token"], callback)) raise cherrypy.HTTPRedirect(auth_url) else: token = Token(oauth_token, session[oauth_secret_session_key]) token.set_verifier(oauth_verifier) client = Client(consumer, token) body = request(client, 'https://api.twitter.com/oauth/access_token', "POST") data = dict(parse_qsl(body)) target.auth_token = data["oauth_token"] target.auth_secret = data["oauth_token_secret"] datastore.commit() except TwitterAPIError, ex: notify_user(translations(ex), category="error", transient=False)
def process_login(self): params = cherrypy.request.params if "authenticate" in params: self.login( params.get("user"), params.get("password") ) # Request the current location again, with all authentication # parameters stripped location = Location.get_current() for params in (location.query_string, location.form_data): params.pop("user", None) params.pop("password", None) params.pop("authenticate", None) location.go("GET")
def _after_process_comments(self, comment): raise cherrypy.HTTPRedirect( "%s#comment-%s" % (unicode(Location.get_current()).encode('utf-8'), str(comment.id)))
def _require_edit_node(self): redirect = False context_item = self.context["cms_item"] edit_stacks_manager = self.context["edit_stacks_manager"] edit_stack = edit_stacks_manager.current_edit_stack # Spawn a new edit stack if edit_stack is None: edit_stack = edit_stacks_manager.create_edit_stack() edit_stacks_manager.current_edit_stack = edit_stack redirect = True else: # Integral part; add a new relation node (won't be shown to the # user) member_name = self.params.read(schema.String("member")) if member_name: node = RelationNode() node.member = edit_stack[-1].content_type[member_name] # Preserve the selected tab group = node.member.member_group if group: pos = group.find(".") if pos != -1: group = group[:pos] edit_stack[-1].tab = group edit_stack.push(node) redirect = True # Make sure the top node of the stack is an edit node if not edit_stack \ or not isinstance(edit_stack[-1], EditNode) \ or (context_item and context_item.id != edit_stack[-1].item.id): # New item if context_item is None: content_type = get_parameter( schema.Reference("item_type", class_family=Item)) item = content_type() # Existing item else: item = context_item node_class = resolve(item.edit_node_class) node = node_class(item) edit_stack.push(node) redirect = True if not item.is_inserted: node.initialize_new_item(item, self.visible_languages) # If the stack is modified a redirection is triggered so that any # further request mentions the new stack position in its parameters. # However, the redirection won't occur if the controller itself is the # final target of the current request - if that is the case, submit() # will end up redirecting the user to the default section anyway if redirect and self is not cherrypy.request.handler: location = Location.get_current() location.method = "GET" location.params["edit_stack"] = edit_stack.to_param() location.params.pop("member", None) location.hash = Location.empty_hash location.go() return edit_stack
def invoke(self, controller, selection): publication_target = selection[0] code = cherrypy.request.params.get("code") error = cherrypy.request.params.get("error") error_reason = cherrypy.request.params.get("error_reason") error_description = cherrypy.request.params.get("error_description") # Authorization failed if error: notify_user(translations( "woost.extensions.facebookauthentication." "fbpublish_auth.error", ), category="error", transient=False) # Authorization successful elif code: location = Location.get_current(relative=False) del location.query_string["code"] auth_url = ("https://graph.facebook.com/oauth/access_token?" "client_id=%s" "&redirect_uri=%s" "&client_secret=%s" "&code=%s" % (publication_target.app_id, quote_plus(str(location)), publication_target.app_secret, code)) response = urlopen(auth_url) response_status = response.getcode() response_body = response.read() if response_status < 200 or response_status > 299: notify_user(translations( "woost.extensions.facebookauthentication." "fbpublish_auth.oauth_error", response=response_body), category="error", transient=False) return auth_token = response_body.split("&")[0].split("=")[1] # Facebook pages require an additional authentication step fb_page_id = cherrypy.request.params.get("fb_page") if fb_page_id: accounts_url = ( "https://graph.facebook.com/%s/accounts?access_token=%s" % (publication_target.administrator_id, auth_token)) response_data = urlopen(accounts_url).read() json = loads(response_data) for account in json["data"]: if account["id"] == fb_page_id: auth_token = account["access_token"] break else: auth_token = None publication_target.auth_token = auth_token datastore.commit() notify_user(translations("woost.extensions.facebookauthentication." "fbpublish_auth.success"), category="success") # Begin authorization request else: # Determine if the selected graph URL is a Facebook Page graph_url = "http://graph.facebook.com/%s?metadata=1" \ % publication_target.graph_object_id response_data = urlopen(graph_url).read() json = loads(response_data) fb_page_id = json["metadata"]["type"] == "page" and json["id"] location = Location.get_current(relative=False) location.query_string["item_action"] = self.id permissions = "publish_stream,offline_access,user_photos" if fb_page_id: location.query_string["fb_page"] = fb_page_id permissions += ",manage_pages" auth_url = ("https://www.facebook.com/dialog/oauth?" "client_id=%s" "&redirect_uri=%s" "&scope=%s" % (publication_target.app_id, quote_plus(str(location)), permissions)) raise cherrypy.HTTPRedirect(auth_url)