def package_dict_save(pkg_dict, context): model = context["model"] package = context.get("package") allow_partial_update = context.get("allow_partial_update", False) if package: pkg_dict["id"] = package.id Package = model.Package if 'metadata_created' in pkg_dict: del pkg_dict['metadata_created'] if 'metadata_modified' in pkg_dict: del pkg_dict['metadata_modified'] pkg = table_dict_save(pkg_dict, Package, context) package_resource_list_save(pkg_dict.get("resources", []), pkg, context) package_tag_list_save(pkg_dict.get("tags", []), pkg, context) package_membership_list_save(pkg_dict.get("groups", []), pkg, context) subjects = pkg_dict.get('relationships_as_subject', []) relationship_list_save(subjects, pkg, 'relationships_as_subject', context) objects = pkg_dict.get('relationships_as_object', []) relationship_list_save(subjects, pkg, 'relationships_as_object', context) extras = package_extras_save(pkg_dict.get("extras", []), pkg, context) return pkg
def inventory_entry_bulk_create(context, data_dict): model = context['model'] schema = context['schema'] session = context['session'] organization = model.Group.get(context['organization_name']) inventory_entry_dict = {'group_id': organization.id} if not data_dict['field-name-input-0']: raise ValidationError({'error': [_('Please add at least one inventory entry.')]}) for inventory_entry_id in range(10): inventory_entry_name = data_dict['field-name-input-' + str(inventory_entry_id)] if not inventory_entry_name: break inventory_entry_dict['title'] = inventory_entry_name inventory_entry_dict['recurring_interval'] = data_dict['field-recurring-input-' + str(inventory_entry_id)] inventory_entry_dict['is_recurring'] = (inventory_entry_dict['recurring_interval'] != '0') data, errors = navl_validate(inventory_entry_dict, schema, context) if errors: session.rollback() # TODO @palcu: i18n raise ValidationError({'error': [_('Verificati intrarea cu numarul {0}.'.format(inventory_entry_id+1))]}) obj = table_dict_save(inventory_entry_dict, InventoryEntry, context) model.repo.commit() return table_dictize(obj, context)
def group_dict_save(group_dict, context): import uuid model = context["model"] session = context["session"] group = context.get("group") allow_partial_update = context.get("allow_partial_update", False) Group = model.Group if group: group_dict["id"] = group.id group = table_dict_save(group_dict, Group, context) if not group.id: group.id = str(uuid.uuid4()) context['group'] = group group_member_save(context, group_dict, 'packages') group_member_save(context, group_dict, 'users') group_member_save(context, group_dict, 'groups') group_member_save(context, group_dict, 'tags') extras = group_extras_save(group_dict.get("extras", {}), context) if extras or not allow_partial_update: old_extras = set(group.extras.keys()) new_extras = set(extras.keys()) for key in old_extras - new_extras: del group.extras[key] for key in new_extras: group.extras[key] = extras[key] return group
def relationship_list_save(relationship_dicts, package, attr, context): allow_partial_update = context.get("allow_partial_update", False) if relationship_dicts is None and allow_partial_update: return model = context["model"] session = context["session"] pending = context.get('pending') relationship_list = getattr(package, attr) old_list = relationship_list[:] relationships = [] for relationship_dict in relationship_dicts or []: obj = d.table_dict_save(relationship_dict, model.PackageRelationship, context) relationships.append(obj) relationship_list[:] = relationships for relationship in set(old_list) - set(relationship_list): if pending and relationship.state <> 'deleted': relationship.state = 'pending-deleted' else: relationship.state = 'deleted' relationship_list.append(relationship)
def tag_dict_save(tag_dict, context): model = context["model"] tag = context.get("tag") if tag: tag_dict["id"] = tag.id tag = d.table_dict_save(tag_dict, model.Tag, context) return tag
def tag_dict_save(tag_dict, context): model = context['model'] tag = context.get('tag') if tag: tag_dict['id'] = tag.id tag = d.table_dict_save(tag_dict, model.Tag, context) return tag
def package_dict_save(pkg_dict, context): model = context["model"] package = context.get("package") allow_partial_update = context.get("allow_partial_update", False) if package: pkg_dict["id"] = package.id Package = model.Package if 'metadata_created' in pkg_dict: del pkg_dict['metadata_created'] if 'metadata_modified' in pkg_dict: del pkg_dict['metadata_modified'] pkg = d.table_dict_save(pkg_dict, Package, context) if not pkg.id: pkg.id = str(uuid.uuid4()) package_resource_list_save(pkg_dict.get("resources"), pkg, context) package_tag_list_save(pkg_dict.get("tags"), pkg, context) package_membership_list_save(pkg_dict.get("groups"), pkg, context) # relationships are not considered 'part' of the package, so only # process this if the key is provided if 'relationships_as_subject' in pkg_dict: subjects = pkg_dict.get('relationships_as_subject') relationship_list_save(subjects, pkg, 'relationships_as_subject', context) if 'relationships_as_object' in pkg_dict: objects = pkg_dict.get('relationships_as_object') relationship_list_save(objects, pkg, 'relationships_as_object', context) extras = package_extras_save(pkg_dict.get("extras"), pkg, context) return pkg
def package_tag_list_save(tag_dicts, package, context): allow_partial_update = context.get("allow_partial_update", False) if not tag_dicts and allow_partial_update: return model = context["model"] session = context["session"] pending = context.get('pending') tag_package_tag = dict((package_tag.tag, package_tag) for package_tag in package.package_tag_all) tags = set() for tag_dict in tag_dicts: obj = table_dict_save(tag_dict, model.Tag, context) tags.add(obj) for tag in set(tag_package_tag.keys()) - tags: package_tag = tag_package_tag[tag] if pending and package_tag.state <> 'deleted': package_tag.state = 'pending-deleted' else: package_tag.state = 'deleted' for tag in tags - set(tag_package_tag.keys()): state = 'pending' if pending else 'active' package_tag_obj = model.PackageTag(package, tag, state) session.add(package_tag_obj) tag_package_tag[tag] = package_tag_obj package.package_tag_all[:] = tag_package_tag.values()
def user_extra_save(user_dict, context): ''' Save user profile extra information to database. Modified from ckan/lib/dictization/model_save.py:445 (user_dict_save). @param user_dict: dict containing user and extra information @param context: @return: list of saved model objects ''' user = context.get('user_obj') user_extras = [] UserExtra = kmodel.UserExtra if user: user_dict['id'] = user.id for field in EXTRAS: if user_dict.has_key(field): extra_row = {} extra_row['key'] = field extra_row['value'] = user_dict[field] user_extra = kmodel.UserExtra.by_userid_key(user.id, field) if user_extra: extra_row['id'] = user_extra.id user_extras.append( dictization.table_dict_save(extra_row, UserExtra, context)) return user_extras
def group_dict_save(group_dict, context, prevent_packages_update=False): from ckan.lib.search import rebuild model = context["model"] session = context["session"] group = context.get("group") allow_partial_update = context.get("allow_partial_update", False) Group = model.Group if group: group_dict["id"] = group.id group = d.table_dict_save(group_dict, Group, context) if not group.id: group.id = str(uuid.uuid4()) context['group'] = group # Under the new org rules we do not want to be able to update datasets # via group edit so we need a way to prevent this. It may be more # sensible in future to send a list of allowed/disallowed updates for # groups, users, tabs etc. if not prevent_packages_update: pkgs_edited = group_member_save(context, group_dict, 'packages') else: pkgs_edited = { 'added': [], 'removed': [] } group_users_changed = group_member_save(context, group_dict, 'users') group_groups_changed = group_member_save(context, group_dict, 'groups') group_tags_changed = group_member_save(context, group_dict, 'tags') log.debug('Group save membership changes - Packages: %r Users: %r ' 'Groups: %r Tags: %r', pkgs_edited, group_users_changed, group_groups_changed, group_tags_changed) extras = group_dict.get("extras", []) new_extras = {i['key'] for i in extras} if extras: old_extras = group.extras for key in set(old_extras) - new_extras: del group.extras[key] for x in extras: if 'deleted' in x and x['key'] in old_extras: del group.extras[x['key']] continue group.extras[x['key']] = x['value'] # We will get a list of packages that we have either added or # removed from the group, and trigger a re-index. package_ids = pkgs_edited['removed'] package_ids.extend( pkgs_edited['added'] ) if package_ids: session.commit() [rebuild(package_id) for package_id in package_ids] return group
def task_status_dict_save(task_status_dict, context): model = context["model"] task_status = context.get("task_status") allow_partial_update = context.get("allow_partial_update", False) if task_status: task_status_dict["id"] = task_status.id task_status = d.table_dict_save(task_status_dict, model.TaskStatus, context) return task_status
def test_07_table_simple_save(self): context = {"model": model, "session": model.Session} anna1 = model.Session.query(model.Package).filter_by(name='annakarenina').one() anna_dictized = self.remove_changable_columns(table_dictize(anna1, context)) anna_dictized["name"] = 'annakarenina2' model.repo.new_revision() table_dict_save(anna_dictized, model.Package, context) model.Session.commit() pkg = model.Session.query(model.Package).filter_by(name='annakarenina2').one() assert self.remove_changable_columns(table_dictize(pkg, context)) == anna_dictized, self.remove_changable_columns(table_dictize(pkg, context))
def group_dict_save(group_dict, context, prevent_packages_update=False): from ckan.lib.search import rebuild model = context["model"] session = context["session"] group = context.get("group") allow_partial_update = context.get("allow_partial_update", False) Group = model.Group if group: group_dict["id"] = group.id group = d.table_dict_save(group_dict, Group, context) if not group.id: group.id = str(uuid.uuid4()) context['group'] = group # Under the new org rules we do not want to be able to update datasets # via group edit so we need a way to prevent this. It may be more # sensible in future to send a list of allowed/disallowed updates for # groups, users, tabs etc. if not prevent_packages_update: pkgs_edited = group_member_save(context, group_dict, 'packages') else: pkgs_edited = { 'added': [], 'removed': [] } group_users_changed = group_member_save(context, group_dict, 'users') group_groups_changed = group_member_save(context, group_dict, 'groups') group_tags_changed = group_member_save(context, group_dict, 'tags') log.debug('Group save membership changes - Packages: %r Users: %r ' 'Groups: %r Tags: %r', pkgs_edited, group_users_changed, group_groups_changed, group_tags_changed) extras = group_dict.get("extras", []) new_extras = {i['key'] for i in extras} if extras: old_extras = group.extras for key in set(old_extras) - new_extras: del group.extras[key] for x in extras: if 'deleted' in x and x['key'] in old_extras: del group.extras[x['key']] continue group.extras[x['key']] = x['value'] # We will get a list of packages that we have either added or # removed from the group, and trigger a re-index. package_ids = pkgs_edited['removed'] package_ids.extend( pkgs_edited['added'] ) if package_ids: session.commit() map( rebuild, package_ids ) return group
def api_token_save(data_dict: dict[str, Any], context: Context) -> 'model.ApiToken': model = context[u"model"] user = model.User.get(data_dict['user']) assert user return d.table_dict_save({ u"user_id": user.id, u"name": data_dict[u"name"] }, model.ApiToken, context)
def test_table_simple_save(self): CreateTestData.create() context = {"model": model, "session": model.Session} anna1 = model.Package.get("annakarenina") anna_dictized = self.remove_changable_columns( table_dictize(anna1, context) ) anna_dictized["name"] = "annakarenina2" table_dict_save(anna_dictized, model.Package, context) model.Session.commit() pkg = model.Package.get("annakarenina2") assert ( self.remove_changable_columns(table_dictize(pkg, context)) == anna_dictized ), self.remove_changable_columns(table_dictize(pkg, context))
def group_dict_save(group_dict, context): from ckan.lib.search import rebuild model = context["model"] session = context["session"] group = context.get("group") allow_partial_update = context.get("allow_partial_update", False) prevent_packages_update = context.get("prevent_packages_update", False) Group = model.Group if group: group_dict["id"] = group.id group = d.table_dict_save(group_dict, Group, context) if not group.id: group.id = str(uuid.uuid4()) context["group"] = group # Under the new org rules we do not want to be able to update datasets # via group edit so we need a way to prevent this. It may be more # sensible in future to send a list of allowed/disallowed updates for # groups, users, tabs etc. if not prevent_packages_update: pkgs_edited = group_member_save(context, group_dict, "packages") else: pkgs_edited = {"added": [], "removed": []} group_users_changed = group_member_save(context, group_dict, "users") group_groups_changed = group_member_save(context, group_dict, "groups") group_tags_changed = group_member_save(context, group_dict, "tags") log.debug( "Group save membership changes - Packages: %r Users: %r " "Groups: %r Tags: %r", pkgs_edited, group_users_changed, group_groups_changed, group_tags_changed, ) # We will get a list of packages that we have either added or # removed from the group, and trigger a re-index. package_ids = pkgs_edited["removed"] package_ids.extend(pkgs_edited["added"]) if package_ids: session.commit() map(rebuild, package_ids) extras = group_extras_save(group_dict.get("extras", {}), context) if extras or not allow_partial_update: old_extras = set(group.extras.keys()) new_extras = set(extras.keys()) for key in old_extras - new_extras: del group.extras[key] for key in new_extras: group.extras[key] = extras[key] return group
def task_status_dict_save(task_status_dict: dict[str, Any], context: Context) -> 'model.TaskStatus': model = context["model"] task_status = context.get("task_status") if task_status: task_status_dict["id"] = task_status.id task_status = d.table_dict_save(task_status_dict, model.TaskStatus, context) return task_status
def resource_view_dict_save(data_dict, context): model = context['model'] resource_view = context.get('resource_view') if resource_view: data_dict['id'] = resource_view.id config = {} for key, value in data_dict.iteritems(): if key not in model.ResourceView.get_columns(): config[key] = value data_dict['config'] = config return d.table_dict_save(data_dict, model.ResourceView, context)
def test_07_table_simple_save(self): context = {"model": model, "session": model.Session} anna1 = (model.Session.query( model.Package).filter_by(name="annakarenina").one()) anna_dictized = self.remove_changable_columns( table_dictize(anna1, context)) anna_dictized["name"] = "annakarenina2" table_dict_save(anna_dictized, model.Package, context) model.Session.commit() pkg = (model.Session.query( model.Package).filter_by(name="annakarenina2").one()) assert (self.remove_changable_columns(table_dictize( pkg, context)) == anna_dictized), self.remove_changable_columns( table_dictize(pkg, context))
def test_package_tag_list_save_duplicates(self): name = u"testpkg19" context = {"model": model, "session": model.Session} pkg_dict = {"name": name} package = table_dict_save(pkg_dict, model.Package, context) tag_dicts = [{"name": "tag1"}, {"name": "tag1"}] # duplicate package_tag_list_save(tag_dicts, package, context) model.repo.commit_and_remove() pkg = model.Package.by_name(name) assert set([tag.name for tag in pkg.get_tags()]) == set(("tag1", ))
def test_07_table_simple_save(self): context = {"model": model, "session": model.Session} anna1 = model.Session.query( model.Package).filter_by(name='annakarenina').one() anna_dictized = self.remove_changable_columns( table_dictize(anna1, context)) anna_dictized["name"] = 'annakarenina2' model.repo.new_revision() table_dict_save(anna_dictized, model.Package, context) model.Session.commit() pkg = model.Session.query( model.Package).filter_by(name='annakarenina2').one() assert self.remove_changable_columns(table_dictize( pkg, context)) == anna_dictized, self.remove_changable_columns( table_dictize(pkg, context))
def test_18_package_tag_list_save(self): name = u'testpkg18' context = {'model': model, 'session': model.Session} pkg_dict = {'name': name} package = table_dict_save(pkg_dict, model.Package, context) tag_dicts = [{'name': 'tag1'}, {'name': 'tag2'}] package_tag_list_save(tag_dicts, package, context) model.repo.commit_and_remove() pkg = model.Package.by_name(name) assert_equal(set([tag.name for tag in pkg.get_tags()]), set(('tag1', 'tag2')))
def package_tag_list_save(tag_dicts, package, context): allow_partial_update = context.get("allow_partial_update", False) if not tag_dicts and allow_partial_update: return model = context["model"] session = context["session"] pending = context.get('pending') tag_package_tag = dict((package_tag.tag, package_tag) for package_tag in package.package_tag_all) tag_package_tag_inactive = dict( [ (tag,pt) for tag,pt in tag_package_tag.items() if pt.state in ['deleted', 'pending-deleted'] ] ) tag_names = set() tags = set() for tag_dict in tag_dicts: if tag_dict.get('name') not in tag_names: tag_obj = table_dict_save(tag_dict, model.Tag, context) tags.add(tag_obj) tag_names.add(tag_obj.name) # 3 cases # case 1: currently active but not in new list for tag in set(tag_package_tag.keys()) - tags: package_tag = tag_package_tag[tag] if pending and package_tag.state != 'deleted': package_tag.state = 'pending-deleted' else: package_tag.state = 'deleted' # in new list but never used before for tag in tags - set(tag_package_tag.keys()): state = 'pending' if pending else 'active' package_tag_obj = model.PackageTag(package, tag, state) session.add(package_tag_obj) tag_package_tag[tag] = package_tag_obj # in new list and already used but in deleted state for tag in tags.intersection(set(tag_package_tag_inactive.keys())): state = 'pending' if pending else 'active' package_tag = tag_package_tag[tag] package_tag.state = state package.package_tag_all[:] = tag_package_tag.values()
def test_19_package_tag_list_save_duplicates(self): name = u"testpkg19" context = {"model": model, "session": model.Session} pkg_dict = {"name": name} rev = model.repo.new_revision() package = table_dict_save(pkg_dict, model.Package, context) tag_dicts = [{"name": "tag1"}, {"name": "tag1"}] # duplicate package_tag_list_save(tag_dicts, package, context) model.repo.commit_and_remove() pkg = model.Package.by_name(name) assert_equal(set([tag.name for tag in pkg.get_tags()]), set(("tag1",)))
def test_19_package_tag_list_save_duplicates(self): name = u'testpkg19' context = {'model': model, 'session': model.Session} pkg_dict = {'name': name} rev = model.repo.new_revision() package = table_dict_save(pkg_dict, model.Package, context) tag_dicts = [{'name': 'tag1'}, {'name': 'tag1'}] # duplicate package_tag_list_save(tag_dicts, package, context) model.repo.commit_and_remove() pkg = model.Package.by_name(name) assert_equal(set([tag.name for tag in pkg.tags]), set(('tag1', )))
def test_19_package_tag_list_save_duplicates(self): name = u'testpkg19' context = {'model': model, 'session': model.Session} pkg_dict = {'name': name} rev = model.repo.new_revision() package = table_dict_save(pkg_dict, model.Package, context) tag_dicts = [{'name': 'tag1'}, {'name': 'tag1'}] # duplicate package_tag_list_save(tag_dicts, package, context) model.repo.commit_and_remove() pkg = model.Package.by_name(name) assert_equal(set([tag.name for tag in pkg.get_tags()]), set(('tag1',)))
def user_dict_save(user_dict, context): model = context['model'] session = context['session'] user = context.get('user_obj') User = model.User if user: user_dict['id'] = user.id if 'password' in user_dict and not len(user_dict['password']): del user_dict['password'] user = d.table_dict_save(user_dict, User, context) return user
def user_dict_save(user_dict, context): model = context["model"] session = context["session"] user = context.get("user_obj") User = model.User if user: user_dict["id"] = user.id if "password" in user_dict and not len(user_dict["password"]): del user_dict["password"] user = d.table_dict_save(user_dict, User, context) return user
def package_tag_list_save(tag_dicts, package, context): allow_partial_update = context.get("allow_partial_update", False) if not tag_dicts and allow_partial_update: return model = context["model"] session = context["session"] pending = context.get("pending") tag_package_tag = dict((package_tag.tag, package_tag) for package_tag in package.package_tag_all) tag_package_tag_inactive = dict( [(tag, pt) for tag, pt in tag_package_tag.items() if pt.state in ["deleted", "pending-deleted"]] ) tag_name_vocab = set() tags = set() for tag_dict in tag_dicts: if (tag_dict.get("name"), tag_dict.get("vocabulary_id")) not in tag_name_vocab: tag_obj = d.table_dict_save(tag_dict, model.Tag, context) tags.add(tag_obj) tag_name_vocab.add((tag_obj.name, tag_obj.vocabulary_id)) # 3 cases # case 1: currently active but not in new list for tag in set(tag_package_tag.keys()) - tags: package_tag = tag_package_tag[tag] if pending and package_tag.state != "deleted": package_tag.state = "pending-deleted" else: package_tag.state = "deleted" # case 2: in new list but never used before for tag in tags - set(tag_package_tag.keys()): state = "pending" if pending else "active" package_tag_obj = model.PackageTag(package, tag, state) session.add(package_tag_obj) tag_package_tag[tag] = package_tag_obj # case 3: in new list and already used but in deleted state for tag in tags.intersection(set(tag_package_tag_inactive.keys())): state = "pending" if pending else "active" package_tag = tag_package_tag[tag] package_tag.state = state package.package_tag_all[:] = tag_package_tag.values()
def package_tag_list_save(tag_dicts: Optional[list[dict[str, Any]]], package: 'model.Package', context: Context) -> None: allow_partial_update = context.get("allow_partial_update", False) if tag_dicts is None and allow_partial_update: return model = context["model"] session = context["session"] tag_package_tag = dict( (package_tag.tag, package_tag) for package_tag in package.package_tags) tag_package_tag_inactive = { tag: pt for tag, pt in tag_package_tag.items() if pt.state in ['deleted'] } tag_name_vocab: set[tuple[str, str]] = set() tags: set[model.Tag] = set() for tag_dict in tag_dicts or []: name_vocab = (tag_dict.get('name'), tag_dict.get('vocabulary_id')) if name_vocab not in tag_name_vocab: tag_obj = d.table_dict_save(tag_dict, model.Tag, context) tags.add(tag_obj) tag_name_vocab.add((tag_obj.name, tag_obj.vocabulary_id)) # 3 cases # case 1: currently active but not in new list for tag in set(tag_package_tag.keys()) - tags: package_tag = tag_package_tag[tag] package_tag.state = 'deleted' # case 2: in new list but never used before for tag in tags - set(tag_package_tag.keys()): state = 'active' package_tag_obj = model.PackageTag(package, tag, state) session.add(package_tag_obj) tag_package_tag[tag] = package_tag_obj # case 3: in new list and already used but in deleted state for tag in tags.intersection(set(tag_package_tag_inactive.keys())): state = 'active' package_tag = tag_package_tag[tag] package_tag.state = state package.package_tags[:] = tag_package_tag.values()
def _basket_element_add(context, model, package_id, basket): pkg = tk.get_action('package_show')(context, {'id': package_id}) if pkg['id'] not in basket.as_dict()['packages']: basket_association = d.table_dict_save( { 'basket_id': basket.id, 'package_id': pkg['id'] }, BasketAssociation, context) basket_association = basket_association.as_dict() else: return None if not context.get('defer_commit'): model.repo.commit() return basket_association
def group_dict_save(group_dict, context): from ckan.lib.search import rebuild model = context["model"] session = context["session"] group = context.get("group") allow_partial_update = context.get("allow_partial_update", False) Group = model.Group if group: group_dict["id"] = group.id group = d.table_dict_save(group_dict, Group, context) if not group.id: group.id = str(uuid.uuid4()) context['group'] = group pkgs_edited = group_member_save(context, group_dict, 'packages') group_users_changed = group_member_save(context, group_dict, 'users') group_groups_changed = group_member_save(context, group_dict, 'groups') group_tags_changed = group_member_save(context, group_dict, 'tags') log.debug( 'Group save membership changes - Packages: %r Users: %r ' 'Groups: %r Tags: %r', pkgs_edited, group_users_changed, group_groups_changed, group_tags_changed) # We will get a list of packages that we have either added or # removed from the group, and trigger a re-index. package_ids = pkgs_edited['removed'] package_ids.extend(pkgs_edited['added']) if package_ids: session.commit() map(rebuild, package_ids) extras = group_extras_save(group_dict.get("extras", {}), context) if extras or not allow_partial_update: old_extras = set(group.extras.keys()) new_extras = set(extras.keys()) for key in old_extras - new_extras: del group.extras[key] for key in new_extras: group.extras[key] = extras[key] return group
def package_tag_list_save(tag_dicts, package, context): allow_partial_update = context.get("allow_partial_update", False) if tag_dicts is None and allow_partial_update: return model = context["model"] session = context["session"] tag_package_tag = dict((package_tag.tag, package_tag) for package_tag in package.package_tag_all) tag_package_tag_inactive = dict( [ (tag,pt) for tag,pt in tag_package_tag.items() if pt.state in ['deleted'] ] ) tag_name_vocab = set() tags = set() for tag_dict in tag_dicts or []: if (tag_dict.get('name'), tag_dict.get('vocabulary_id')) not in tag_name_vocab: tag_obj = d.table_dict_save(tag_dict, model.Tag, context) tags.add(tag_obj) tag_name_vocab.add((tag_obj.name, tag_obj.vocabulary_id)) # 3 cases # case 1: currently active but not in new list for tag in set(tag_package_tag.keys()) - tags: package_tag = tag_package_tag[tag] package_tag.state = 'deleted' # case 2: in new list but never used before for tag in tags - set(tag_package_tag.keys()): state = 'active' package_tag_obj = model.PackageTag(package, tag, state) session.add(package_tag_obj) tag_package_tag[tag] = package_tag_obj # case 3: in new list and already used but in deleted state for tag in tags.intersection(set(tag_package_tag_inactive.keys())): state = 'active' package_tag = tag_package_tag[tag] package_tag.state = state package.package_tag_all[:] = tag_package_tag.values()
def group_dict_save(group_dict, context): from ckan.lib.search import rebuild model = context["model"] session = context["session"] group = context.get("group") allow_partial_update = context.get("allow_partial_update", False) Group = model.Group if group: group_dict["id"] = group.id group = d.table_dict_save(group_dict, Group, context) if not group.id: group.id = str(uuid.uuid4()) context['group'] = group pkgs_edited = group_member_save(context, group_dict, 'packages') group_users_changed = group_member_save(context, group_dict, 'users') group_groups_changed = group_member_save(context, group_dict, 'groups') group_tags_changed = group_member_save(context, group_dict, 'tags') log.debug('Group save membership changes - Packages: %r Users: %r ' 'Groups: %r Tags: %r', pkgs_edited, group_users_changed, group_groups_changed, group_tags_changed) # We will get a list of packages that we have either added or # removed from the group, and trigger a re-index. package_ids = pkgs_edited['removed'] package_ids.extend( pkgs_edited['added'] ) if package_ids: session.commit() map( rebuild, package_ids ) extras = group_extras_save(group_dict.get("extras", {}), context) if extras or not allow_partial_update: old_extras = set(group.extras.keys()) new_extras = set(extras.keys()) for key in old_extras - new_extras: del group.extras[key] for key in new_extras: group.extras[key] = extras[key] return group
def inventory_entry_update(context, data_dict): # TODO @palcu: DRY this w/ inventory_entry_create model = context['model'] schema = context['schema'] session = context['session'] organization = model.Group.get(context['organization_name']) data_dict['group_id'] = organization.id data_dict['is_recurring'] = (data_dict['recurring_interval'] != '0') data, errors = navl_validate(data_dict, schema, context) if errors: session.rollback() raise ValidationError(errors) obj = table_dict_save(data_dict, InventoryEntry, context) model.repo.commit() return table_dictize(obj, context)
def inventory_entry_bulk_create(context, data_dict): model = context['model'] schema = context['schema'] session = context['session'] organization = model.Group.get(context['organization_name']) inventory_entry_dict = {'group_id': organization.id} if not data_dict['field-name-input-0']: raise ValidationError( {'error': [_('Please add at least one inventory entry.')]}) for inventory_entry_id in range(10): inventory_entry_name = data_dict['field-name-input-' + str(inventory_entry_id)] if not inventory_entry_name: break inventory_entry_dict['title'] = inventory_entry_name inventory_entry_dict['recurring_interval'] = data_dict[ 'field-recurring-input-' + str(inventory_entry_id)] inventory_entry_dict['is_recurring'] = ( inventory_entry_dict['recurring_interval'] != '0') data, errors = navl_validate(inventory_entry_dict, schema, context) if errors: session.rollback() # TODO @palcu: i18n raise ValidationError({ 'error': [ _('Verificati intrarea cu numarul {0}.'.format( inventory_entry_id + 1)) ] }) obj = table_dict_save(inventory_entry_dict, InventoryEntry, context) model.repo.commit() return table_dictize(obj, context)
def group_dict_save(group_dict, context): from ckan.lib.search import rebuild model = context["model"] session = context["session"] group = context.get("group") allow_partial_update = context.get("allow_partial_update", False) Group = model.Group if group: group_dict["id"] = group.id group = d.table_dict_save(group_dict, Group, context) if not group.id: group.id = str(uuid.uuid4()) context["group"] = group pkgs_edited = group_member_save(context, group_dict, "packages") group_member_save(context, group_dict, "users") group_member_save(context, group_dict, "groups") group_member_save(context, group_dict, "tags") # We will get a list of packages that we have either added or # removed from the group, and trigger a re-index. package_ids = pkgs_edited["removed"] package_ids.extend(pkgs_edited["added"]) if package_ids: session.commit() map(rebuild, package_ids) extras = group_extras_save(group_dict.get("extras", {}), context) if extras or not allow_partial_update: old_extras = set(group.extras.keys()) new_extras = set(extras.keys()) for key in old_extras - new_extras: del group.extras[key] for key in new_extras: group.extras[key] = extras[key] return group
def user_dict_save(user_dict, context): model = context['model'] session = context['session'] user = context.get('user_obj') User = model.User if user: user_dict['id'] = user.id if 'password' in user_dict and not len(user_dict['password']): del user_dict['password'] user = d.table_dict_save( user_dict, User, context, extra_attrs=['_password'], # for setting password_hash directly ) return user
def group_dict_save(group_dict, context): model = context["model"] session = context["session"] group = context.get("group") allow_partial_update = context.get("allow_partial_update", False) Group = model.Group Package = model.Package if group: group_dict["id"] = group.id group = table_dict_save(group_dict, Group, context) extras = group_extras_save(group_dict.get("extras", {}), context) if extras or not allow_partial_update: old_extras = set(group.extras.keys()) new_extras = set(extras.keys()) for key in old_extras - new_extras: del group.extras[key] for key in new_extras: group.extras[key] = extras[key] package_dicts = group_dict.get("packages", []) packages = [] for package in package_dicts: pkg = None id = package.get("id") if id: pkg = session.query(Package).get(id) if not pkg: pkg = session.query(Package).filter_by(name=package["name"]).first() if pkg: packages.append(pkg) if packages or not allow_partial_update: group.packages[:] = packages return group
def group_dict_save(group_dict, context): model = context["model"] session = context["session"] group = context.get("group") allow_partial_update = context.get("allow_partial_update", False) Group = model.Group Package = model.Package if group: group_dict["id"] = group.id group = table_dict_save(group_dict, Group, context) extras = group_extras_save(group_dict.get("extras", {}), context) if extras or not allow_partial_update: old_extras = set(group.extras.keys()) new_extras = set(extras.keys()) for key in old_extras - new_extras: del group.extras[key] for key in new_extras: group.extras[key] = extras[key] package_dicts = group_dict.get("packages", []) packages = [] for package in package_dicts: pkg = None id = package.get("id") if id: pkg = session.query(Package).get(id) if not pkg: pkg = session.query(Package).filter_by(name=package["name"]).first() if pkg and pkg not in packages: packages.append(pkg) if packages or not allow_partial_update: group.packages[:] = packages return group
def relationship_list_save(relationship_dicts, package, attr, context): allow_partial_update = context.get("allow_partial_update", False) if relationship_dicts is None and allow_partial_update: return model = context["model"] session = context["session"] relationship_list = getattr(package, attr) old_list = relationship_list[:] relationships = [] for relationship_dict in relationship_dicts or []: obj = d.table_dict_save(relationship_dict, model.PackageRelationship, context) relationships.append(obj) relationship_list[:] = relationships for relationship in set(old_list) - set(relationship_list): relationship.state = 'deleted' relationship_list.append(relationship)
def package_dict_save(pkg_dict, context): model = context["model"] package = context.get("package") allow_partial_update = context.get("allow_partial_update", False) if package: pkg_dict["id"] = package.id Package = model.Package pkg = table_dict_save(pkg_dict, Package, context) package_resource_list_save(pkg_dict.get("resources", []), pkg, context) package_tag_list_save(pkg_dict.get("tags", []), pkg, context) package_group_list_save(pkg_dict.get("groups", []), pkg, context) subjects = pkg_dict.get('relationships_as_subject', []) relationship_list_save(subjects, pkg, 'relationships_as_subject', context) objects = pkg_dict.get('relationships_as_object', []) relationship_list_save(subjects, pkg, 'relationships_as_object', context) extras = package_extras_save(pkg_dict.get("extras", []), pkg, context) return pkg
def marsavin_pages_save(context, data_dict): schema = context["schema"] model = context["model"] session = context["session"] marsavin_page_dict, errors = _validate(data=data_dict, schema=schema, context=context) if errors: raise ValidationError(errors) marsavin_page = d.table_dict_save(marsavin_page_dict, MarsavinPages, context) # generate the user id session.flush() if not context.get('defer_commit'): try: model.repo.commit() except Exception as e: log.debug(e.message) session.rollback() return marsavin_page
def basket_update(context, data_dict): """Update a basket :param id: The id of the basket :type id: string :returns: """ tk.check_access('basket_owner_only', context, data_dict) model = context['model'] id = _get_or_bust(data_dict, 'id') basket = Basket.get(id) context["basket"] = basket if basket is None: raise tk.ObjectNotFound('Basket was not found.') tk.check_access('basket_update', context, data_dict) basket = d.table_dict_save(data_dict, Basket, context) if not context.get('defer_commit'): model.repo.commit() return basket.as_dict()
def related_dict_save(related_dict, context): model = context['model'] session = context['session'] return d.table_dict_save(related_dict, model.Related, context)
def role_permission_dict_save(role_permission_dict, context): return d.table_dict_save(role_permission_dict, extmodel.RolePermission, context)
def user_role_dict_save(user_role_dict, context): return d.table_dict_save(user_role_dict, extmodel.UserRole, context)
def role_dict_save(role_dict, context): role = context.get('role') if role: role_dict['id'] = role.id return d.table_dict_save(role_dict, extmodel.Role, context)
def related_dict_save(related_dict, context): model = context['model'] session = context['session'] return d.table_dict_save(related_dict,model.Related, context)
def reqaccess_dict_save(reqaccess_dict, context): reqaccess = d.table_dict_save(reqaccess_dict, AccessRequests, context) return reqaccess
def related_dict_save(related_dict, context): model = context["model"] session = context["session"] return d.table_dict_save(related_dict, model.Related, context)
def inventory_item_create(context, data_dict): model = context['model'] obj = table_dict_save(data_dict, InventoryItem, context) model.repo.commit() return table_dictize(obj, context)