def import_value(self, member, value): if value is None: return None elif isinstance(member, schema.Reference): if member.class_family: return resolve(value) else: return self.get_local_copy(value) elif isinstance(member, schema.DateTime): return datetime.datetime.strptime(value, self.datetime_format) elif isinstance(member, schema.Date): return datetime.datetime.strptime(value, self.date_format).date() elif isinstance(member, schema.Time): return datetime.datetime.strptime(value, self.time_format).time() elif isinstance(member, schema.Decimal): return Decimal(value) elif isinstance(member, schema.Fraction): return Fraction(value) elif isinstance(member, schema.Tuple): return tuple( self.import_value(submember, subvalue) for submember, subvalue in zip(member.items, value)) elif isinstance(member, schema.Collection): return member.default_type( [self.import_value(member.items, item) for item in value]) elif isinstance(member, schema.RegularExpression): return base64.b64decode(value) else: return value
def icon_resolver(self): try: return self._v_icon_resolver except AttributeError: expr = self.icon_resolver_expression icon_resolver = resolve(expr) if expr else app.icon_resolver self._v_icon_resolver = icon_resolver return icon_resolver
def __persistent_load(self, key): if key.startswith(self._persistent_id_prefix): parts = key.split("-") persistent_type = resolve(parts[1]) id = int(parts[2]) return persistent_type.get_instance(id) else: raise ValueError("Wrong persistent id: " + key)
def create_grouping_options(self, column, language): options = Element() options.add_class("grouping_options") title = Element("div") title.add_class("options_header") title.append(translations("woost.views.ContentTable grouping header")) options.append(title) grouping_class = resolve(column.grouping) variants = (None,) + grouping_class.variants table = Element("table") options.append(table) for variant in variants: tr = Element("tr") table.append(tr) td = Element("td") td.add_class("variant") td.append(grouping_class.translate_grouping_variant(variant)) tr.append(td) for sign in (PositiveExpression, NegativeExpression): grouping = grouping_class() grouping.member = column grouping.language = language grouping.sign = sign grouping.variant = variant td = Element("td") td.add_class("sign") tr.append(td) grouping_link = Element("a") grouping_link.add_class("grouping") grouping_link["href"] = \ "?" + view_state(grouping = grouping.request_value, page = 0) grouping_link.append( translations( "cocktail.controllers.grouping.MemberGrouping-" + ("ascending" if sign is PositiveExpression else "descending") ) ) td.append(grouping_link) return options
def compare_content(self, establish_relations=False): self.__local_copies.clear() # Find incomming / outgoing / modified items manifest_diff = self.compare_manifests() # Retrieve the state for remotely added/modified objects remote_ids = (manifest_diff["incomming"] | manifest_diff["modified"]) changes = {} if remote_ids: response = self._sync_request("state/" + ",".join(remote_ids)) remote_state = loads(response.read()) # Build a local copy of each object that has been added by the remote # installation for global_id in manifest_diff["incomming"]: state = remote_state[global_id] model = resolve(state["__class__"]) local_copy = model(id=None, global_id=global_id) local_copy.bidirectional = establish_relations self.__local_copies[global_id] = local_copy # Initialize the local copies with the received state for global_id in manifest_diff["incomming"]: self.import_object_state(self.__local_copies[global_id], remote_state[global_id]) for global_id in manifest_diff["modified"]: local_copy = Item.require_instance(global_id=global_id) item_remote_state = {} self.import_object_state( item_remote_state, remote_state[global_id], ) changes[global_id] = { "local": local_copy, "remote": item_remote_state, "diff": list((member, lang) for member, lang in schema.diff( local_copy, item_remote_state, schema=local_copy.__class__) if member.synchronizable) } return {"incomming": self.__local_copies.values(), "modified": changes}
def create_edit_stack(self): """Creates a new edit stack. @return: The new edit stack. @rtype: L{EditStack} """ edit_stack = resolve(self._edit_stack_class)() edit_stack.id = session.get(self._session_id_key, 0) edit_stack.root_url = cherrypy.request.params.get("root_url") session[self._session_id_key] = edit_stack.id + 1 self.__stack_map[edit_stack.id] = edit_stack self._remove_expired_edit_stacks() return edit_stack
def root_content_type(self): """The most basic possible content type for listed items. This property is used to constrain the set of eligible content types to all types that descend from the indicated type (inclusive). @type: L{Item<woost.models.Item>} subclass """ root_content_type = self.stack_content_type if root_content_type is None: root_content_type_param = self.params.read( schema.String("root_content_type")) root_content_type = resolve(root_content_type_param) return root_content_type or Item
def import_object_state(self, obj, state): if isinstance(obj.__class__, schema.Schema): obj_schema = obj.__class__ else: obj_schema = resolve(state["__class__"]) for key, value in state.iteritems(): if key == "__class__": continue member = obj_schema.get_member(key) if member is None: continue if member.synchronizable: self.import_member(obj, member, value)
def resolve(self, path): if not path: raise cherrypy.NotFound() # Identify the gateway involved in the payment gateway_id = path.pop(0) try: gateway_id = int(gateway_id) except: raise cherrypy.HTTPError(400) gateway = PaymentGateway.get_instance(gateway_id) if gateway is None: raise cherrypy.NotFound() # Forward the request to the gateway's controller controller_class = resolve(gateway.payment_gateway_controller_class) return controller_class(gateway)
def _process_comments(event): # Comments variables initialization comments_user_collection = None comments_schema = None comment_errors = None comment_data = {} controller = event.source comment_model = \ resolve(getattr(controller, "comment_model", Comment)) publishable = controller.context["publishable"] user = get_current_user() if publishable is not None and publishable.allow_comments: # Comments collection comments_user_collection = UserCollection(comment_model) comments_user_collection.allow_type_selection = False comments_user_collection.allow_filters = False comments_user_collection.allow_language_selection = False comments_user_collection.allow_member_selection = False comments_user_collection.params.prefix = "comments_" comments_user_collection.base_collection = publishable.comments # Show the last comments page if not specified if "comments_page" not in cherrypy.request.params: div, mod = divmod(len(comments_user_collection.subset), comments_user_collection.page_size) comments_page = div - 1 if not mod and div != 0 else div cherrypy.request.params.update( comments_page=str(comments_page)) # Adapting the comments model comments_schema = CommentsExtension.instance._adapt_comments_schema( comment_model) if user.anonymous \ and getattr(CommentsExtension.instance, "captcha_enabled", False): comments_schema.add_member(ReCaptcha("captcha")) # Insert a new comment if cherrypy.request.method == "POST" \ and "post_comment" in cherrypy.request.params: with changeset_context(user): get_parameter(comments_schema, target=comment_data, errors="ignore") comment_errors = schema.ErrorList( comments_schema.get_errors(comment_data)) if not comment_errors: comment = comment_model() adapter = CommentsExtension.instance._create_comments_adapter( comment_model) adapter.import_object( comment_data, comment, source_schema=comments_schema, target_schema=comment_model) comment.publishable = publishable user.require_permission(CreatePermission, target=comment) comment.insert() datastore.commit() CommentsExtension.instance._after_process_comments( comment) else: comment_errors = schema.ErrorList([]) # Update the output controller.output.update( comments_user_collection=comments_user_collection, comments_schema=comments_schema, comment_errors=comment_errors, comment_data=comment_data)
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 fields(self): return resolve(self.stack_node.item.edit_controller)
def show_detail(self): return resolve(self.stack_node.item.show_detail_controller)
def preview(self): return resolve(self.stack_node.item.preview_controller)
def handle_traversed(cls, event): controller = event.source controller.context["edit_stacks_manager"] = \ resolve(controller._edit_stacks_manager_class)()