def del_setting(self, name): settings = json.loads(self.settings) try: settings.pop(name) except KeyError: pass self.settings = json.dumps(settings)
def save(self, text=None, script=None, settings=None): """ save uri(schemes) or script(cron) to mongodb server return True if success else False """ if text is not None: try: schemes = settings.pop('schemes', None) assert self.save_text(text, schemes) except AssertionError as e: content = "%s : Error occured when saving text" % (type(e)) CrawlerGeneratorErrorLog(name="ERROR_SAVE", content=content, hostname=socket.gethostname()).save() elif script is not None: if 'cron' not in settings: content = "cron is not found in settings" CrawlerGeneratorErrorLog(name="ERROR_SAVE", content=content, hostname=socket.gethostname()).save() return if 'code_type' not in settings: content = "code type is not found in settings" CrawlerGeneratorErrorLog(name="ERROR_SAVE", content=content, hostname=socket.gethostname()).save() return try: schemes = settings.pop('schemes', None) assert self.save_script(script, settings['cron'], settings['code_type'], schemes) except AssertionError: CrawlerGeneratorErrorLog(name="ERROR_SAVE", content="Error occured when saving script ", hostname=socket.gethostname()).save() else: CrawlerGeneratorErrorLog(name="ERROR_SAVE", content="No text or script found .", hostname=socket.gethostname()).save()
def pop_settings(self, pop_settings): settings = json.loads(self.settings) for setting in pop_settings: if setting in settings: settings.pop(setting) self.settings = json.dumps(settings, cls=LazyDateTimeEncoder) self.save()
def _prepare_cloned_data(self, original_asset, source_version, partial_update): """ Some business rules must be applied when cloning an asset to another with a different type. It prepares the data to be cloned accordingly. It raises an exception if source and destination are not compatible for cloning. :param original_asset: Asset :param source_version: AssetVersion :param partial_update: Boolean :return: dict """ if self._validate_destination_type(original_asset): # `to_clone_dict()` returns only `name`, `content`, `asset_type`, # and `tag_string` cloned_data = original_asset.to_clone_dict(version=source_version) # Allow the user's request data to override `cloned_data` cloned_data.update(self.request.data.items()) if partial_update: # Because we're updating an asset from another which can have another type, # we need to remove `asset_type` from clone data to ensure it's not updated # when serializer is initialized. cloned_data.pop("asset_type", None) else: # Change asset_type if needed. cloned_data["asset_type"] = self.request.data.get( ASSET_TYPE_ARG_NAME, original_asset.asset_type) cloned_asset_type = cloned_data.get("asset_type") # Settings are: Country, Description, Sector and Share-metadata # Copy settings only when original_asset is `survey` or `template` # and `asset_type` property of `cloned_data` is `survey` or `template` # or None (partial_update) if cloned_asset_type in [None, ASSET_TYPE_TEMPLATE, ASSET_TYPE_SURVEY] and \ original_asset.asset_type in [ASSET_TYPE_TEMPLATE, ASSET_TYPE_SURVEY]: settings = original_asset.settings.copy() settings.pop("share-metadata", None) settings.update(cloned_data.get('settings', {})) cloned_data['settings'] = json.dumps(settings) # until we get content passed as a dict, transform the content obj to a str # TODO, verify whether `Asset.content.settings.id_string` should be cleared out. cloned_data["content"] = json.dumps(cloned_data.get("content")) return cloned_data else: raise BadAssetTypeException( "Destination type is not compatible with source type")
def adjust_content_on_save(self): """ This is called on save by default if content exists. Can be disabled / skipped by calling with parameter: asset.save(adjust_content=False) """ self._standardize(self.content) self._make_default_translation_first(self.content) self._strip_empty_rows(self.content) self._assign_kuids(self.content) self._autoname(self.content) self._unlink_list_items(self.content) self._remove_empty_expressions(self.content) settings = self.content['settings'] _title = settings.pop('form_title', None) id_string = settings.get('id_string') filename = self.summary.pop('filename', None) if filename: # if we have filename available, set the id_string # and/or form_title from the filename. if not id_string: id_string = sluggify_label(filename) settings['id_string'] = id_string if not _title: _title = filename if self.asset_type not in [ASSET_TYPE_SURVEY, ASSET_TYPE_TEMPLATE]: # instead of deleting the settings, simply clear them out self.content['settings'] = {} strip_kobo_locking_profile(self.content) if _title is not None: self.name = _title
def _update_client_settings(self, settings): update_fields = [] if 'algorithm' in settings: algorithm = settings.pop('algorithm') if algorithm != self.algorithm: self.algorithm = algorithm update_fields.append('algorithm') return update_fields
def __init__(self, *args, **settings): self.shib_idp_attrib_name = settings.pop('shib_idp_attrib_name', app_settings.SHIB_IDP_ATTRIB_NAME) self.shib_authorized_idps = settings.pop('shib_authorized_idps', app_settings.SHIB_AUTHORIZED_IDPS) self.shib_attribute_map = settings.pop('shib_attribute_map', app_settings.SHIB_ATTRIBUTE_MAP) self.shib_mock = settings.pop('shib_mock', app_settings.SHIB_MOCK) self.shib_mock_attributes = settings.pop('shib_mock_attributes', app_settings.SHIB_MOCK_ATTRIBUTES) self.shib_username_attrib_name = settings.pop('shib_username_attrib_name', app_settings.SHIB_USERNAME_ATTRIB_NAME) self.shib_group_attributes = settings.pop('shib_group_attributes', app_settings.SHIB_GROUP_ATTRIBUTES) self.shib_groups_by_idp = settings.pop('shib_groups_by_idp', app_settings.SHIB_GROUPS_BY_IDP) super().__init__(*args, **settings)
def _update_setting_string(self, app, group): settings = { setting.code: None for setting in Settings.objects.filter(compact=True, app=app) } settings_string = '' #Force QuerySet evaltion so the database is only hit once for user defaults user_settings = list( UserDefault.objects.filter(user=self, setting__app=app)) while group != None: #Apply user defaults for this group context for default in user_settings.filter(group=group): code = default.setting.code if code in settings: settings_string += '&' + code + '=' + default.settingVal.value #Remove the setting from dict, so new value cannot be set settings.pop(code) #Find group-level defaults for setttings that have not been set yet for default in GroupDefault.objects.filter(group=group, setting__app=app): code = default.setting.code if code in settings and settings[code] == None: settings[code] = default.settingVal.value #Move up to next group in the hierarchy group = group.parent #Add all group defaults that had no user defaults for code, value in settings.iteritems(): if value == None: value = Setting.objects.get(app=app, code=code).default.value settings_string += '&' + code + '=' + value #Set the value in the corresonding CompactSettings, remove the first '&' compact, c = CompactSettings.objects.get_or_create(user=self, group=group, app=app) compact.string = settings_string[1:] compact.save()
def _update_setting_string(self, app, group): settings = {setting.code: None for setting in Settings.objects.filter(compact=True, app=app)} settings_string = '' #Force QuerySet evaltion so the database is only hit once for user defaults user_settings = list(UserDefault.objects.filter(user=self, setting__app=app)) while group != None: #Apply user defaults for this group context for default in user_settings.filter(group=group): code = default.setting.code if code in settings: settings_string += '&'+ code +'='+ default.settingVal.value #Remove the setting from dict, so new value cannot be set settings.pop(code) #Find group-level defaults for setttings that have not been set yet for default in GroupDefault.objects.filter(group=group, setting__app=app): code = default.setting.code if code in settings and settings[code] == None: settings[code] = default.settingVal.value #Move up to next group in the hierarchy group = group.parent #Add all group defaults that had no user defaults for code, value in settings.iteritems(): if value == None: value = Setting.objects.get(app=app, code=code).default.value settings_string += '&'+ code +'='+ value #Set the value in the corresonding CompactSettings, remove the first '&' compact, c = CompactSettings.objects.get_or_create(user=self, group=group, app=app) compact.string = settings_string[1:] compact.save()
def pop(name, default=None): return settings.pop(name) or default
def pop(name, default=None): try: return settings.pop(name) or default except KeyError: return default;