def detail(self): tmpls = get_template_loader().templates dag_id = request.args.get('dag_id') dbDag = DagenDagQueryset().get_dag(dag_id) template = tmpls[dbDag.template_id] return self.render_template('dagen/detail.html', dbDag=dbDag, dag_id=dag_id)
def edit(self, ): template = 'dagen/edit-dag.html' dag_id = request.args.get('dag_id') qs = DagenDagQueryset() dbDag = qs.get_dag(dag_id) versions = { version.version: version.dict_repr for version in dbDag.versions } try: tmpl = get_template_loader().get_template_class(dbDag.template_id) except TemplateNotFoundError as e: flash(e, category='error') flash( 'Either delete this DAG or add back the template with given template ID' ) return self._redirect_home() try: init_data = {**dbDag.dict_repr, **dbDag.live_version.dict_repr} except Exception as e: self.log.exception(e) init_data = dbDag.dict_repr form = tmpl.as_form(data=init_data) if request.method == 'POST' and request.form: form.process(request.form) if form.validate(): ret = form.update( dbDag, user=g.user, form_version=request.form.get('live_version')) flash( f'Dagen "{dag_id}" version {"updated" if ret else "unchanged"}!' ) if ret: refresh_dagbag(dag_id=dag_id) return self._handle_form_submission(request.form) return self.render_template(template, dbDag=dbDag, dagVersions=versions, dag_id=dag_id, form=form)
async def asyncSave(self): # dag_qs needed to load versions later self.version_qs = DagenDagVersionQueryset() # HACK: eager load versions here so update works existing = DagenDagQueryset().get_all(eager_load_versions=True) self.dagmap = {dbDag.dag_id: dbDag for dbDag in existing} self.tmpl_clazz = get_template_loader().get_template_class( self.template_id.data) # FileStorage uses BytesIO stream so first read, decode and convert to StringIO stream stream = StringIO(self.csv_data.data.read().decode('UTF-8')) # Close the file object self.csv_data.data.close() cronreader = csv.DictReader(stream) result = await async_gather_dict( {row['dag_id']: self.process_row(row) for row in cronreader}) self.version_qs.done() return result
def bulk_save(self): template = 'dagen/bulk-save.html' tmpls = get_template_loader().template_classes.keys() # make mark_approved no-op if no permission to approve has_approve_perm = self._has_permission('can_approve') form = BulkSyncDagenForm(templates=tmpls, has_approve_perm=has_approve_perm) context = {'form': form} if request.method == 'POST' and form.validate(): dag_results = form.save(g.user) success_results, failed_results = dict(), dict() for dag_id, (is_success, message) in dag_results.items(): if is_success: success_results[dag_id] = message else: failed_results[dag_id] = message context['res_success'] = success_results context['res_failure'] = failed_results return self.render_template(template, **context)
def create(self): template = 'dagen/create-dag.html' tmpls = get_template_loader().template_classes forms = {key: tmpl.as_form() for key, tmpl in tmpls.items()} if request.method == 'POST' and request.form: tmplId = request.form.get('template_id') form = forms[tmplId] form.process(request.form) if form.validate(): ret = form.create(template_id=tmplId, user=g.user) msg = f'"{ret.dag_id}" created successfully' if ret else "failed to create" flash(f'Dagen - {msg}!') if ret: return self._handle_form_submission(request.form) return self.render_template(template, template_classes=tmpls, template_id=tmplId, forms=forms) return self.render_template(template, template_classes=tmpls, forms=forms)