def create(self): require.dataset.create() try: dataset = dict(request.params) dataset['territories'] = request.params.getall('territories') dataset['languages'] = request.params.getall('languages') model = {'dataset': dataset} schema = dataset_schema(ValidationState(model)) data = schema.deserialize(dataset) if Dataset.by_name(data['name']) is not None: raise Invalid( SchemaNode(String(), name='dataset.name'), _("A dataset with this identifer already exists!")) dataset = Dataset({'dataset': data}) dataset.private = True dataset.managers.append(c.account) db.session.add(dataset) db.session.commit() redirect( h.url_for(controller='editor', action='index', dataset=dataset.name)) except Invalid, i: errors = i.asdict() return self.new(errors)
def dimensions_update(self, dataset, format='html'): self._get_dataset(dataset) require.dataset.update(c.dataset) if len(c.dataset): abort(400, _("You cannot edit the dimensions model when " "data is loaded for the dataset.")) errors, mapping, saved = {}, None, False try: mapping = json.loads(request.params.get('mapping')) model = c.dataset.model model['mapping'] = mapping schema = mapping_schema(ValidationState(model)) new_mapping = schema.deserialize(mapping) c.dataset.data['mapping'] = new_mapping c.dataset.drop() c.dataset._load_model() c.dataset.generate() db.session.commit() # h.flash_success(_("The mapping has been updated.")) saved = True except (ValueError, TypeError, AttributeError): abort(400, _("The mapping data could not be decoded as JSON!")) except Invalid as i: errors = i.asdict() return self.dimensions_edit(dataset, errors=errors, mapping=mapping, saved=saved)
def dimensions_update(self, dataset, format='html'): dry_run = False self._get_dataset(dataset) operation = request.params.get('operation', 'Save') if operation == 'Verify': dry_run = True require.dataset.update(c.dataset) if len(c.dataset): abort(400, _("You cannot edit the dimensions model when " \ "data is loaded for the dataset.")) errors, mapping = {}, None try: mapping = json.loads(request.params.get('mapping')) model = c.dataset.model model['mapping'] = mapping schema = mapping_schema(ValidationState(model)) new_mapping = schema.deserialize(mapping) if not dry_run: # erm... c.dataset.data['mapping'] = new_mapping c.dataset.drop() c.dataset._load_model() c.dataset.generate() db.session.commit() h.flash_success(_("The mapping has been updated.")) else: h.flash_success(_("The mapping has been validated successfully.")) except (ValueError, TypeError, AttributeError): abort(400, _("The mapping data could not be decoded as JSON!")) except Invalid, i: errors = i.asdict()
def views_update(self, dataset, format='html'): self._get_dataset(dataset) require.dataset.update(c.dataset) errors, views = {}, None try: views = json.loads(request.params.get('views')) schema = views_schema(ValidationState(c.dataset.model)) c.dataset.data['views'] = schema.deserialize(views) db.session.commit() h.flash_success(_("The views have been updated.")) except (ValueError, TypeError): abort(400, _("The views could not be decoded as JSON!")) except Invalid, i: errors = i.asdict()
def update(name): """ Update a dataset with a json object and name from the dataset form """ try: dataset = get_dataset(name) schema = dataset_schema(ValidationState(dataset)) data = schema.deserialize(api_form_data()) dataset.update(data) db.session.commit() #clear_index_cache() return jsonify({"success": True}) except Exception, e: print e return jsonify({"errors": ['Unknown Error has occurred']})
def create(): """ This takes a json format post with label, name, description and creates a private dataset to put sources in The json_errors return a json object """ if not require.dataset.create(): return jsonify( {"errors": ["Can not create new dataset. Permission denied"]}) try: dataset = api_form_data() if not dataset.get("dataorg", None): return jsonify( {"errors": ["You must select the data source organization"]}) model = {'data': dataset} schema = dataset_schema(ValidationState(model)) data = schema.deserialize(dataset) #should have a better place for sluggify if (data.get('name', None)): tempname = slugify(str(data.get('name')), max_length=50) else: tempname = slugify(str(data.get('label')), max_length=50) if Dataset.by_name(tempname) is not None: return jsonify( {"errors": ["A dataset with this name already exists "]}) dataset = Dataset(data=data) dataset.managers.append(current_user) db.session.add(dataset) dataset_source = Source.by_source_name(dataset.name) if not dataset_source: dataset_source = Source(dataset=dataset, name=dataset.name) db.session.add(dataset_source) else: dataset_source.dataset = dataset #creating a new dataset so we have to create a source as well db.session.commit() return jsonify({"success": True, "dataset": dataset.name}) except Exception, e: ex_type, ex, tb = sys.exc_info() print traceback.print_tb(tb) return jsonify({"errors": ['Unknown Error has occurred: ' + str(e)]})
def core_update(self, dataset, format='html'): self._get_dataset(dataset) require.dataset.update(c.dataset) errors = {} try: schema = dataset_schema(ValidationState(c.dataset.model)) data = dict(request.params) data['territories'] = request.params.getall('territories') data['languages'] = request.params.getall('languages') data = schema.deserialize(data) c.dataset.label = data['label'] c.dataset.currency = data['currency'] c.dataset.description = data['description'] c.dataset.territories = data['territories'] c.dataset.languages = data['languages'] db.session.commit() h.flash_success(_("The dataset has been updated.")) except Invalid, i: errors = i.asdict()
def update_model_createnew(datasetname): #refactor to include the update dataset = get_dataset(datasetname) #source will have name and URL sourceapi = api_form_data() if not sourceapi['name']: sourceapi['name'] = dataset.name #return jsonify({"errors":["You must enter a data name " + str(e)]}) #verify that name is unique and URL is real #model = {'source': source} schema = source_schema(ValidationState(sourceapi)) try: data = schema.deserialize(sourceapi) except Invalid, e: #print message in thefuture return jsonify({"errors": ["Invalid field " + str(e)]})
def validate_model(model): """ Apply model validation. """ state = ValidationState(model) schema = model_schema(state) return schema.deserialize(model)