def test_modify(self): from time import sleep from datetime import datetime from woost.models import (Document, User, ChangeSet, changeset_context) author = User() author.insert() with changeset_context(author) as creation: item = Document() item.set("title", u"Foo!", "en") item.resource_type = u"text/foo" item.hidden = True item.insert() # Make sure creation_time and last_update_time don't match sleep(0.1) with changeset_context(author) as modification: item.set("title", u"Bar!", "en") item.resource_type = u"text/bar" item.hidden = True assert list(ChangeSet.select()) == [creation, modification] assert modification.author is author assert isinstance(modification.date, datetime) assert modification.changes.keys() == [item.id] change = modification.changes[item.id] assert change.target is item assert change.action == "modify" assert change.changeset is modification assert change.changed_members == set(["title", "resource_type"]) assert item.changes == [creation.changes[item.id], change] for key in "id", "changes", "creation_time", "last_update_time": assert not key in change.item_state assert change.item_state["title"] == {"en": u"Bar!"} assert change.item_state["resource_type"] == u"text/bar" assert change.item_state["hidden"] == True assert change.item_state["translation_enabled"] == \ {"en": item.get("translation_enabled", "en")} assert item.author is author assert item.creation_time assert item.last_update_time assert not item.creation_time == item.last_update_time
def submit(self): from woost.extensions.campaignmonitor import CampaignMonitorExtension extension = CampaignMonitorExtension.instance user = get_current_user() with changeset_context(author=user) as changeset: extension.synchronize_lists(restricted=True) # Report changed lists created = set() modified = set() deleted = set() for change in changeset.changes.itervalues(): if isinstance(change.target, CampaignMonitorList): action_id = change.action.identifier if action_id == "create": created.add(change.target) elif action_id == "modify": modified.add(change.target) elif action_id == "delete": deleted.add(change.target) datastore.commit() self.output["created_lists"] = created self.output["modified_lists"] = modified self.output["deleted_lists"] = deleted
def submit(self): document = self.controller.context["publishable"] # Write to the database if document.should_save_instances: with changeset_context(get_current_user()): UploadForm.submit(self) self.instance.insert() datastore.commit() # Do not write to the database, only process form data else: UploadForm.submit(self) # Send email messages based on the submitted data for email_template in document.email_notifications: email_template.send({ "form": document, "instance": self.instance }) # Redirect the user to a confirmation page if document.redirection: raise cherrypy.HTTPRedirect(self.controller.context["cms"].uri( document.redirection, form=document.id, instance=None if not document.should_save_instances else self.instance.id))
def submit(self): collection = self.collection selection = self.selection parent = self.item position = self.position related_end = self.member.related_end size = len(collection) if position < 0: position = size + position position = min(position, size) if any(parent.descends_from(item) for item in selection): raise TreeCycleError() for i in range(self.MAX_TRANSACTION_ATTEMPTS): with changeset_context(get_current_user()): for item in reversed(selection): if isinstance(related_end, Reference) \ and item.get(related_end) is parent: collection.remove(item) collection.insert(position, item) try: datastore.commit() except ConflictError: datastore.abort() datastore.sync() else: break
def test_delete(self): from datetime import datetime from woost.models import (Item, User, ChangeSet, changeset_context) author = User() author.insert() item = Item() item.insert() with changeset_context(author) as changeset: item.delete() assert list(ChangeSet.select()) == [changeset] assert changeset.author is author assert isinstance(changeset.date, datetime) assert changeset.changes.keys() == [item.id] change = changeset.changes[item.id] assert change.target is item assert change.action == "delete" assert change.changeset is changeset assert item.changes == [change] assert not item.id in Item.index
def create_export(): with changeset_context(app.user): export = Export.new() export.destination = self.destination export.user = app.user for action, publishable, language in self.iter_tasks(): export.add_task(action, publishable, language) return export
def initialize(self): self.reset_database() with changeset_context() as changeset: self.changeset = changeset self.create_content() mark_all_migrations_as_executed() datastore.commit()
def _delete_instances(self, query): user = get_current_user() class ValidatingDeletedSet(InstrumentedSet): def item_added(self, item): user.require_permission(DeletePermission, target=item) deleted_set = ValidatingDeletedSet() with changeset_context(user): for item in list(query): item.delete(deleted_set)
def test_insert(self): from datetime import datetime from woost.models import (Document, User, ChangeSet, changeset_context) author = User() author.insert() with changeset_context(author) as changeset: item = Document() item.set("title", u"Foo!", "en") item.resource_type = u"text/foo" item.hidden = True assert not changeset.changes item.insert() assert list(ChangeSet.select()) == [changeset] assert changeset.author is author assert isinstance(changeset.date, datetime) assert changeset.changes.keys() == [item.id] change = changeset.changes[item.id] assert change.target is item assert change.action == "create" assert change.changeset is changeset assert item.changes == [change] for key in "id", "changes", "creation_time", "last_update_time": assert not key in change.item_state print change.item_state assert change.item_state["title"] == {"en": u"Foo!"} assert change.item_state["resource_type"] == u"text/foo" assert change.item_state["hidden"] == True assert change.item_state["translation_enabled"] == \ {"en": item.get("translation_enabled", "en")} assert item.author is author assert item.creation_time assert item.last_update_time assert item.creation_time == item.last_update_time
def relate(): dragged_object = self.dragged_object target_object = self.target_object target_member = self.target_member user = get_current_user() user.require_permission(ModifyPermission, target=dragged_object) user.require_permission(ModifyPermission, target=target_object) with changeset_context(user): if isinstance(target_member, schema.Reference): target_object.set(target_member, dragged_object) else: collection = target_object.get(target_member) sibling = self.sibling relative_placement = self.relative_placement if sibling: try: current_index = collection.index(dragged_object) except ValueError: current_index = None index = collection.index(sibling) if relative_placement == "before": if current_index is not None and current_index < index: index -= 1 elif relative_placement == "after": if current_index is None or current_index >= index: index += 1 if isinstance(collection, (OrderedSet, InstrumentedOrderedSet)): kwargs = {"relocate": True} else: kwargs = {} collection.insert(index, dragged_object, **kwargs) else: collection.append(dragged_object)
def _synchronize(self): cmp = self.comparision get_param = cherrypy.request.params.get file_downloads = set() selection = self.sync_selection with changeset_context(get_current_user()): for obj in cmp["incomming"]: if obj.global_id in selection: obj.insert() if isinstance(obj, File): file_downloads.add(obj) for global_id, change in cmp["modified"].iteritems(): if global_id in selection: local = change["local"] remote = change["remote"] for member, lang in change["diff"]: value = schema.get(remote, member, language=lang) local.set(member, value, lang) if member in (File.file_hash, File.file_size): file_downloads.add(local) # Download files from the remote copy for file in file_downloads: self.synchronization.download_file(file) # Find importable content again self.comparision = self.synchronization.compare_content() notify_user( translations( "woost.controllers.InstallationSyncController.success"), "success")
def submit(self): self.imported_files = [] file = self.form_data["upload"]["file"] zip_file = ZipFile(file) upload_path = app.path("upload") temp_dir = mkdtemp() try: with changeset_context(get_current_user()): for file_path in zip_file.namelist(): file_name = os.path.basename(file_path) # Skip directories if not file_name: continue source = zip_file.open(file_path) temp_file = os.path.join(temp_dir, file_name) dest = open(temp_file, "wb") copyfileobj(source, dest) source.close() dest.close() try: imported_file = File.from_path(temp_file, upload_path) imported_file.insert() self.imported_files.append(imported_file) finally: os.remove(temp_file) datastore.commit() finally: os.rmdir(temp_dir)
def _update_instance(self, instance): with restricted_modification_context(instance): with changeset_context(get_current_user()): PersistentClassWebService._update_instance(self, instance)
def _store_new_instance(self, instance): with changeset_context(get_current_user()): instance.insert()
def save_item(self): for i in range(self.MAX_TRANSACTION_ATTEMPTS): user = get_current_user() stack_node = self.stack_node item = stack_node.item is_new = not item.is_inserted changeset = None with restricted_modification_context( item, user, member_subset = set(stack_node.form_schema.members()) ): with changeset_context(author = user) as changeset: self._apply_changes(item) try: datastore.commit() except ConflictError: datastore.abort() datastore.sync() else: break change = changeset.changes.get(item.id) if changeset else None # Edit stack event stack_node.committed( user = user, changeset = changeset ) # Application-wide event if change is not None: self.context["cms"].item_saved( item = item, user = user, is_new = is_new, change = change ) # User notification stack_node.item_saved_notification(is_new, change) # A new item was created if is_new: # The edit operation was the root of the edit stack; redirect the # browser to the new item if len(self.edit_stack) == 1: if self.edit_stack.root_url: self.edit_stack.go_back() else: raise cherrypy.HTTPRedirect( self.edit_uri(item) ) # The edit operation was nested; relate the created item to its # owner, and redirect the browser to the owner elif isinstance(stack_node.parent_node, RelationNode): member = stack_node.parent_node.member parent_edit_node = stack_node.get_ancestor_node(EditNode) parent_edit_node.relate(member, item) self.edit_stack.go(-3) if stack_node.parent_node is None and self.edit_stack.root_url: self.edit_stack.go_back()