def create_view(self): return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_create: return redirect(return_url) form = self.create_form() if not hasattr(form, '_validated_ruleset') or not \ form._validated_ruleset: self._validate_form_instance(ruleset=self._form_create_rules, form=form) if self.validate_form(form): model = self.create_model(form) if model: flash(gettext('Record was successfully created.'), 'success') return redirect( self.get_save_return_url(model, is_created=True)) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_create_rules) if self.create_modal and request.args.get('modal'): template = self.create_modal_template else: template = self.create_template perm = ActionNeedPermission('storage', Finish) refuse_op = ('出库', Finish) if perm.can() else None form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) details_columns = self.get_column_names( self.review_details_columns or self.column_details_list, None) cfg = ActionViewCfg('', 'fa-bell-o', None, ('保存', Create), ('出库', Finish)) return self.render(template, action=self.get_url('.create_view', verb='creat'), details_columns=details_columns, get_value=self.get_list_value, return_url=return_url, cancel_url=return_url, action_name=cfg.action_name, icon_value=cfg.icon_value, form=form, agreed=cfg.agreed, refuse=refuse_op, form_opts=form_opts, one_line_columns=self.one_line_columns)
def details_view(self): sub = request.args.get('sub', self.default_subordinate_view) self._template_args.update({ 'sub': sub, 'time_formatter': datetime.time, }) self._edit_form_class = self._delegate_to_sub('form') self.override_fields(self._edit_form_class) return_url = get_redirect_target() or self.get_url('.index_view') id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model is None: flash(gettext('Record does not exist.'), 'error') return redirect(return_url) form = self.edit_form(obj=model) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) return self.render(self.details_modal_template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def __init__(self, form, session, model, prop, inline_view, **kwargs): """ Default constructor. :param form: Form for the related model :param session: SQLAlchemy session :param model: Related model :param prop: Related property name :param inline_view: Inline view """ self.form = form self.session = session self.model = model self.prop = prop self.inline_view = inline_view self._pk = get_primary_key(model) # Generate inline form field form_opts = FormOpts(widget_args=getattr(inline_view, 'form_widget_args', None), form_rules=inline_view._form_rules) form_field = self.form_field_type(form, self._pk, form_opts=form_opts) super(InlineModelFormList, self).__init__(form_field, **kwargs)
def simple_new(self): form = self.create_simple_form() if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_create_rules, form=form) if self.validate_form(form): # in versions 1.1.0 and before, this returns a boolean # in later versions, this is the model itself model = self.create_model(form) if model: return jsonify({ "status": "success", "message": gettext('Record was successfully created.') }) # scripts = [] # # flash(gettext('Record was successfully created.'), 'success') # tmpl = u"iziToast.success({message: '%s', position: 'center'});" # scripts.append(tmpl % (gettext('Record was successfully created.'))) # scripts.append('$("#modal_window_add_page").modal("hide");') # return u'<script language="javascript">%s</script>' % ('\n'.join(scripts), ) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_simple_create_rules) template = 'admin/model/modals/model_simple_create.html' return self.render(template, form=form, form_opts=form_opts, return_url='')
def create_view(self): """customize the create view""" return_url = '/' form = self.create_form() if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_create_rules, form=form) if self.validate_form(form): model = self.create_model(form) if model: # flash('Software was successfully submitted.', 'success') return redirect( url_for('submit_software.success', software_id=model.id)) elif is_form_submitted(): flash('Some fields are missing', 'error') form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_create_rules) return self.render( 'admin/custom_create.html', # model=model, form=form, form_opts=form_opts, return_url=return_url)
def approve_edit_view(self): ao_id = request.args.get('id', '') return_url = get_redirect_target() or self.get_url('.index_view') model = Storage.query.filter_by(id=ao_id).first() if not self.can_edit: return redirect(return_url) if model is None: flash(gettext('Record does not exist.'), 'error') return redirect(return_url) column = self.get_edit_details_view_colums(model.instoreCategory) self._template_args.update({ 'table_columns': self.init_table_columns(column), 'table_datas': self.get_table_data_from_db(model), }) model_id = model.id form = self.edit_form(obj=model) if not hasattr(form, '_validated_ruleset') or not\ form._validated_ruleset: self._validate_form_instance(ruleset=self._form_edit_rules, form=form) if self.validate_form(form): if self.update_model(form, model): if request.method == 'POST' and self.support_flow: self._custom_action(model, request.form) flash(gettext('Record was successfully saved.'), 'success') return redirect( self.get_save_return_url(model, is_created=False)) perm = ActionNeedPermission('storage', Finish) refuse_op = ('入库', Finish) if perm.can() else None form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) details_columns = self.get_column_names( self.review_details_columns or self.column_details_list, None) cfg = ActionViewCfg('', 'fa-bell-o', None, ('保存', Edit), ('入库', Finish)) return self.render(self.approve_edit_template, action=self.get_url('.approve_edit_view', verb='edit', id=model_id), model=model, details_columns=details_columns, get_value=self.get_list_value, return_url=return_url, cancel_url=return_url, action_name=cfg.action_name, icon_value=cfg.icon_value, form=form, agreed=cfg.agreed, refuse=refuse_op, form_opts=form_opts, model_id=model_id, one_line_columns=self.one_line_columns)
def edit_view(self): """ Edit model view """ if self.support_flow: if '/edit/' in request.url: url = request.url.replace('edit', 'approve-edit-view') return redirect(url) return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_edit: return redirect(return_url) id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model is None: flash(gettext('Record does not exist.'), 'error') return redirect(return_url) form = self.edit_form(obj=model) if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_edit_rules, form=form) if self.validate_form(form): if self.update_model(form, model): flash(gettext('Record was successfully saved.'), 'success') if '_add_another' in request.form: return redirect( self.get_url('.create_view', url=return_url)) elif '_continue_editing' in request.form: return redirect(request.url) else: # save button return redirect( self.get_save_return_url(model, is_created=False)) if request.method == 'GET' or form.errors: self.on_form_prefill(form, id) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) if self.edit_modal and request.args.get('modal'): template = self.edit_modal_template else: template = self.edit_template return self.render(template, model=model, form=form, form_opts=form_opts, return_url=return_url, one_line_columns=self.one_line_columns)
def create_view(self): """ Create model view """ return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_create: return redirect(return_url) form = self.create_form() if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_create_rules, form=form) if self.validate_form(form): # in versions 1.1.0 and before, this returns a boolean # in later versions, this is the model itself model = self.create_model(form) if model: flash(gettext('Record was successfully created.'), 'success') if '_add_another' in request.form: return redirect(request.url) elif '_continue_editing' in request.form: # if we have a valid model, try to go to the edit view print('我在_continue_editing') if model is not True: url = self.get_url('.edit_view', id=self.get_pk_value(model), url=return_url) else: url = return_url return redirect(url) else: # save button return redirect( self.get_save_return_url(model, is_created=True)) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_create_rules) if self.create_modal and request.args.get('modal'): template = self.create_modal_template else: template = self.create_template area_list = [(v.id, v.name) for v in Area.query.filter_by(status=1).all()] area = area_list.append((0, '')) own_list = [(v.id, v.name) for v in ShopCategory.query.filter_by(status=1).all()] own = own_list.append((0, '')) form.area_id.choices = area_list form.own.choices = own_list return self.render(template, form=form, form_opts=form_opts, return_url=return_url)
def resetpass_view(self): """ Reset password view """ return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_edit: return redirect(return_url) id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model is None: flash(gettext('Record does not exist.'), 'error') return redirect(return_url) form = self.action_form(obj=model) if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=form._form_edit_rules, form=form) if self.validate_form(form): if self.update_model(form, model): flash(gettext('Record was successfully saved.'), 'success') if '_add_another' in request.form: return redirect( self.get_url('.create_view', url=return_url)) elif '_continue_editing' in request.form: return redirect(request.url) else: # save button return redirect( self.get_save_return_url(model, is_created=False)) if request.method == 'GET' or form.errors: self.on_form_prefill(form, id) form_opts = FormOpts(widget_args=form.form_widget_args, form_rules=form._form_edit_rules) template = 'model/resetpass.html' return self.render(template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def edit_view(self): """ Edit model view """ return_url = self.get_return_url() model = self.validate_model() if not model: return redirect(return_url) csr_id = get_mdict_item_or_list(request.args, 'id') form = self.edit_form(obj=model) if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_edit_rules, form=form) if self.validate_form(form) and self.update_model(form, model): # Trim the user name, if necessary. updated_csr = CSR.query.filter_by(csr_id=csr_id).first() check_uservalues(updated_csr) socketio.emit('clear_csr_cache', {"id": csr_id}) socketio.emit('csr_update', { "csr_id": csr_id, "receptionist_ind": updated_csr.receptionist_ind }, room=current_user.office_id) flash(gettext('''Record was successfully saved.'''), 'success') request_redirect(self, return_url, model, request) if request.method == 'GET' or form.errors: self.on_form_prefill(form, id) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) if self.edit_modal and request.args.get('modal'): template = self.edit_modal_template else: template = self.edit_template return self.render(template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def create_view(self): """ Create model view """ return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_create: return redirect(return_url) form = self.create_form() if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_create_rules, form=form) if self.validate_form(form): # in versions 1.1.0 and before, this returns a boolean # in later versions, this is the model itself model = self.create_model(form) if model: flash(gettext('Record was successfully created.'), 'success') if '_add_another' in request.form: return redirect(request.url) elif '_continue_editing' in request.form: # if we have a valid model, try to go to the edit view if model is not True: url = self.get_url('.edit_view', id=self.get_pk_value(model), url=return_url) else: url = return_url return redirect(url) else: # save button return redirect( self.get_save_return_url(model, is_created=True)) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_create_rules) if self.create_modal and request.args.get('modal'): template = self.create_modal_template else: template = self.create_template return self.render(template, form=form, form_opts=form_opts, return_url=return_url, one_line_columns=self.one_line_columns)
def __init__(self, form, session, model, prop, inline_view, **kwargs): self.form = form self.session = session self.model = model self.prop = prop self.inline_view = inline_view self._pk = get_primary_key(model) # Generate inline form field form_opts = FormOpts( widget_args=getattr(inline_view, 'form_widget_args', None), form_rules=inline_view._form_rules ) super().__init__(form, self._pk, form_opts=form_opts, **kwargs)
def edit_view(self): """ Edit model view """ return_url = self.get_return_url() model = self.validate_model() if not model: return (return_url) form = self.edit_form(obj=model) if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_edit_rules, form=form) if self.validate_form(form) and self.update_model(form, model): flash( gettext('''Record was successfully saved. Note: it may take up to 5 minutes for these changes to be effective''' ), 'success') if '_add_another' in request.form: return redirect(self.get_url('.create_view', url=return_url)) elif '_continue_editing' in request.form: return redirect(request.url) else: # save button return redirect( self.get_save_return_url(model, is_created=False)) if request.method == 'GET' or form.errors: self.on_form_prefill(form, id) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) if self.edit_modal and request.args.get('modal'): template = self.edit_modal_template else: template = self.edit_template return self.render(template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def details_view(self): return_url = get_redirect_target() or self.get_url('.index_view') id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model is None: return redirect(return_url) form = self.edit_form(obj=model) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) self.on_form_prefill(form, id) return self.render(self.details_template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def edit_view_token(self, token): """customize the edit view""" not_found_url = url_for('submit_software.not_found') model = None id = self.confirm_software_id_token(token) if id: model = self.get_one(id) if model is None: flash('Software does not exist or the URL has expired.', 'error') return redirect(not_found_url) form = self.edit_form(obj=model) if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_create_rules, form=form) if self.validate_form(form): # model = self.create_model(form) # mark the software as pending for MolSSI review - not public model.is_pending = True if self.update_model(form, model): # flash('Software was successfully submitted.', 'success') return redirect( url_for('submit_software.success', software_id=model.id)) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_create_rules) return self.render('admin/custom_create.html', model=model, form=form, form_opts=form_opts, return_url=not_found_url)
class SecurityCertificatesForm(Form): form_opts = FormOpts(widget_args=_widget_args, form_rules=_form_rules) gen_webadmin = SelectField( "Webadmin certificate", default="1", choices=[("0", "No change"), ("1", "Generate new")], description= "Following files will be overwritten:<ul><li>{}</li><li>{}</li><li>{}</li><li>{}</li></ul>" .format(_webadmin_pem_cert, _webadmin_pem_key, _webadmin_der_cert, _webadmin_der_key), ) gen_opcua = SelectField( "OpcUa certificate", default="1", choices=[("0", "No change"), ("1", "Generate new")], description= "Following files will be overwritten:<ul><li>{}</li><li>{}</li><li>{}</li><li>{}</li></ul>" .format(_opcua_pem_cert, _opcua_pem_key, _opcua_der_cert, _opcua_der_key), ) cn = StringField( "Common name", default=default["cn"], validators=_validate_dns, render_kw={ "placeholder": "Hostname, DNS, IP-address or leave it blank" }, ) days = IntegerField("Days valid", default=7300) basicConstraints = StringField( "basicConstraints", default=default["basicConstraints"], validators=_validate_cmd, ) keyUsage = StringField("keyUsage", default=default["keyUsage"], validators=_validate_cmd) extendedKeyUsage = StringField( "extendedKeyUsage", default=default["extendedKeyUsage"], validators=_validate_cmd, ) uri_1 = StringField("URI.1", default=default["urn.1"], validators=_validate_url) uri_2 = StringField("URI.2", default="", validators=_validate_url) uri_3 = StringField("URI.3", default="", validators=_validate_url) dns_1 = StringField("DNS.1", default=default["dns.1"], validators=_validate_dns) dns_2 = StringField("DNS.2", default="", validators=_validate_dns) dns_3 = StringField("DNS.3", default="", validators=_validate_dns) ip_1 = StringField("IP.1", default=default["ip.1"], validators=_validate_ip) ip_2 = StringField("IP.2", default=default["ip.2"], validators=_validate_ip) ip_3 = StringField("IP.3", default=default["ip.3"], validators=_validate_ip) ip_4 = StringField("IP.4", default=default["ip.4"], validators=_validate_ip) ip_5 = StringField("IP.5", default=default["ip.5"], validators=_validate_ip) subjectAltName = HiddenField("subjectAltName:") current_password = PasswordField("Current password") @staticmethod def validate_current_password(form, field): validators.DataRequired()(form, field) if not utils.check_user_and_pass( current_app, flask_login.current_user.id, field.data): raise validators.ValidationError("Invalid password")
def approve_edit_view(self): """ Edit model view """ return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_edit: return redirect(return_url) id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model is None: return redirect(return_url) self.before_edit_form(model) action = request.args.get('action', 'edit') cfg = self._action_view_cfg[action] details_columns = self.get_column_names( self.review_details_columns or self.column_details_list, None) form = self.edit_form(obj=model) if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_edit_rules, form=form) if self.validate_form(form): if self.update_model(form, model): if request.method == 'POST' and self.support_flow: self._custom_action(id, request.form) flash(gettext('Record was successfully saved.'), 'success') return redirect( self.get_save_return_url(model, is_created=False)) if request.method == 'GET' or form.errors: self.on_form_prefill(form, id) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) return self.render(self.approve_edit_template, action=self.get_url('.approve_edit_view', id=id, verb=action), model=model, details_columns=details_columns, get_value=self.get_list_value, return_url=return_url, cancel_url=return_url, action_name=cfg.action_name, icon_value=cfg.icon_value, form=form, agreed=cfg.agreed, refuse=cfg.refuse, form_opts=form_opts, one_line_columns=self.one_line_columns)
def duplicate_view(self): """ Duplicate model view """ return_url = get_redirect_target() or self.get_url('.index_view') id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model is None: flash(gettext('Record does not exist.'), 'error') return redirect(return_url) form = self.edit_form(obj=model) if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_create_rules, form=form) if self.validate_form(form): # in versions 1.1.0 and before, this returns a boolean # in later versions, this is the model itself model = self.create_model(form) if model: flash(gettext('Record was successfully created.'), 'success') if '_add_another' in request.form: return redirect(request.url) elif '_continue_editing' in request.form: # if we have a valid model, try to go to the edit view if model is not True: url = self.get_url('.edit_view', id=self.get_pk_value(model), url=return_url) else: url = return_url return redirect(url) else: # save button return redirect( self.get_save_return_url(model, is_created=True)) if request.method == 'GET' or form.errors: self.on_form_prefill(form, id) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) if self.edit_modal and request.args.get('modal'): template = self.edit_modal_template else: template = self.edit_template return self.render(template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def create_view(self): if request.method == 'GET': ret = self.create_storge_list() self._template_args.update({ 'table_columns': self.init_table_columns(ret[1]), 'table_datas': ret[0], }) else: request_str = request.headers['Referer'] modelName = re.findall(r"model=(.+?)&", request_str)[0] action_id = None for val in request_str.split('&'): if 'id=' in val: action_id = val.split('=')[1] break # action_id = re.findall(r"&id=(.+?)", request_str)[0] self.create_storge_list(model_name=modelName, ao_id=action_id) # 处理加入入库按钮 return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_create: return redirect(return_url) form = self.create_form() model_name = request.args.get('model', '') if request.method == 'GET': self.get_accessory_from_before(form, model_name, self.types_dict) self.get_edit_details_view_colums(form.instoreCategory.data) if not hasattr(form, '_validated_ruleset') or not \ form._validated_ruleset: self._validate_form_instance(ruleset=self._form_create_rules, form=form) if self.validate_form(form): model = self.create_model(form) if model: flash(gettext('Record was successfully created.'), 'success') return redirect( self.get_save_return_url(model, is_created=True)) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_create_rules) if self.create_modal and request.args.get('modal'): template = self.create_modal_template else: template = self.create_template perm = ActionNeedPermission('storage', Finish) refuse_op = ('入库', Finish) if perm.can() else None form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) details_columns = self.get_column_names( self.review_details_columns or self.column_details_list, None) cfg = ActionViewCfg('', 'fa-bell-o', None, ('保存', Create), ('入库', Finish)) return self.render(template, action=self.get_url('.create_view', verb='creat'), details_columns=details_columns, get_value=self.get_list_value, return_url=return_url, cancel_url=return_url, action_name=cfg.action_name, icon_value=cfg.icon_value, form=form, agreed=cfg.agreed, refuse=refuse_op, form_opts=form_opts, one_line_columns=self.one_line_columns)
def edit_view(self): """Edit model view.""" return_url = get_redirect_target() or self.get_url('.index_view') if not self.can_edit: return redirect(return_url) id = get_mdict_item_or_list(request.args, 'id') if id is None: return redirect(return_url) model = self.get_one(id) if model is None: flash(gettext('Record does not exist.'), 'error') return redirect(return_url) form = self.edit_form(obj=model) if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_edit_rules, form=form) if self.validate_form(form): if self.update_model(form, model): with db.session.begin_nested(): """Get role by position""" if (current_app.config['USERPROFILES_EMAIL_ENABLED'] and form.position): role_name = get_role_by_position(form.position.data) roles1 = db.session.query(Role).filter_by( name=role_name).all() """Get account_user by user profile id""" account_user = db.session.query(User).filter_by( id=id).first() admin_role = current_app.config.get( "WEKO_USERPROFILES_ADMINISTRATOR_ROLE") userprofile_roles = current_app.config.get( "WEKO_USERPROFILES_ROLES") roles2 = [ role for role in account_user.roles if role not in userprofile_roles or role == admin_role ] roles = roles1 + roles2 account_user.roles = roles """Set role for user""" db.session.add(account_user) db.session.commit() flash(gettext('Record was successfully saved.'), 'success') if '_add_another' in request.form: return redirect( self.get_url('.create_view', url=return_url)) elif '_continue_editing' in request.form: return redirect(request.url) else: # save button return redirect( self.get_save_return_url(model, is_created=False)) if request.method == 'GET' or form.errors: self.on_form_prefill(form, id) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) if self.edit_modal and request.args.get('modal'): template = self.edit_modal_template else: template = self.edit_template return self.render(template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def edit_view(self): """ Edit model view """ return_url = get_redirect_target() or self.get_url('.index_view') # if not editable if not self.can_edit: return redirect(return_url) # get the requested id id = request.args.get('id') if id is None: return redirect(return_url) # get the dataset of the id dataset = db.session.query(Dataset).filter_by(id=id)[0] model = dataset.as_dict() # empty record if model is None: flash(gettext('Record does not exist.'), 'error') return redirect(return_url) form = EditForm() # set the already set values for the dataset if request.method == 'GET': form.name.data = model['name'] form.origin_x.data = model['coordinate_origin']['geometry'][ 'coordinates'][0] form.origin_y.data = model['coordinate_origin']['geometry'][ 'coordinates'][1] form.min_x.data = model['min']['geometry']['coordinates'][0] form.min_y.data = model['min']['geometry']['coordinates'][1] form.max_x.data = model['max']['geometry']['coordinates'][0] form.max_y.data = model['max']['geometry']['coordinates'][1] form.inverted_x.data = model['inverted_x'] form.inverted_y.data = model['inverted_y'] form.background_image.filename = model['background_image'] if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_edit_rules, form=form) # if post get the data and save it if request.method == 'POST': form = EditForm(request.form) if not form.validate(): flash(form.errors, 'error') return redirect(request.url) # check if background image has the right extension print('Hello world!', file=sys.stderr) if request.files['background_image']: file = request.files['background_image'] image_name = file.filename.lower() ALLOWED_IMAGE_EXTENSIONS = set(['png', 'jpg', 'jpeg']) if not ('.' in image_name and image_name.rsplit('.', 1)[-1] in ALLOWED_IMAGE_EXTENSIONS): flash( gettext( 'Wrong Filetype, you can upload only png,jpg,jpeg files' ), 'error') return redirect(request.url) current_milli_time = int(round(time.time() * 1000)) image_name = str(current_milli_time) + '.' + image_name.rsplit( '.', 1)[-1] image_name = os.path.join('static', 'img', secure_filename(image_name)) file.save(image_name) else: image_name = '' # update the dataset dataset.edit_update(form, image_name) db.session.commit() # feedback for user flash(gettext('Record was successfully saved.'), 'success') if '_add_another' in request.form: return redirect(self.get_url('.create_view', url=return_url)) elif '_continue_editing' in request.form: return redirect(request.url) else: # save button return redirect( self.get_save_return_url(model, is_created=False)) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) template = self.edit_template return self.render(template, model=model, form=form, form_opts=form_opts, return_url=return_url)
def edit_view(self): """ Edit model view """ return_url = self.get_return_url() model = self.validate_model() if not model: return redirect(return_url) # We know model is good. Save id of CSR you're editing for later use. csr_id = get_mdict_item_or_list(request.args, 'id') # Delete next two lines later. csr_old = CSR.query.filter_by(csr_id=csr_id).first() csr_office_old = csr_old.office.office_name form = self.edit_form(obj=model) if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: self._validate_form_instance(ruleset=self._form_edit_rules, form=form) if self.validate_form(form) and self.update_model(form, model): # Trim the user name, if necessary. updated_csr = CSR.query.filter_by(csr_id=csr_id).first() if updated_csr.username != updated_csr.username.strip(): updated_csr.username = updated_csr.username.strip() db.session.add(updated_csr) db.session.commit() socketio.emit('clear_csr_cache', {"id": csr_id}) socketio.emit('csr_update', \ {"csr_id": csr_id, \ "receptionist_ind": updated_csr.receptionist_ind}, \ room=current_user.office_id) flash(gettext('''Record was successfully saved.'''), 'success') if '_add_another' in request.form: return redirect(self.get_url('.create_view', url=return_url)) elif '_continue_editing' in request.form: return redirect(request.url) else: # save button return redirect( self.get_save_return_url(model, is_created=False)) if request.method == 'GET' or form.errors: self.on_form_prefill(form, id) form_opts = FormOpts(widget_args=self.form_widget_args, form_rules=self._form_edit_rules) if self.edit_modal and request.args.get('modal'): template = self.edit_modal_template else: template = self.edit_template return self.render(template, model=model, form=form, form_opts=form_opts, return_url=return_url)