def create_model(self, form): """ Create model from form. :param form: Form instance """ try: model = self.model() form.populate_obj(model) checksum = models.sha256_checksum(model.full_path) checksum_m = models.get_or_create(self.session, models.Checksum, value=checksum)[0] instance = self.session.query( self.model).filter_by(checksum=checksum_m).first() if instance: model = instance self.session.add(model) self._on_model_change(form, model, True) self.session.commit() except Exception as ex: if not self.handle_view_exception(ex): flash( gettext('Failed to create record. %(error)s', error=str(ex)), 'error') logger.exception('Failed to create record.') self.session.rollback() return False else: self.after_model_change(form, model, True) return model
def create_model(self, form): model = Model(self.last_id) self.last_id += 1 form.populate_obj(model) self.created_models.append(model) self.all_models[model.id] = model return True
def update_model(self, form, model): try: form.populate_obj(model) self.on_model_change(form, model) model.save() # For peewee have to save inline forms after model was saved save_inline(form, model) return True except Exception, ex: flash(gettext('Failed to update model. %(error)s', error=str(ex)), 'error') logging.exception('Failed to update model') return False
def register_view(self): form = RegisterForm(request.form) if helpers.validate_form_on_submit(form): user = Account() form.populate_obj(user) db.session.add(user) db.session.commit() flask_login.login_user(user) return redirect(url_for('all_employees')) link = '<p>Already have an account? <a href="' + url_for('.login_view') + '">Click here to log in.</a></p>' self._template_args['form'] = form self._template_args['link'] = link return super(MyAdminIndexView, self).index()
def register_view(self): form = RegistrationForm(request.form) if helpers.validate_form_on_submit(form): user = User() form.populate_obj(user) # we hash the users password to avoid saving it as plaintext in the db, # remove to use plain text: user.password = generate_password_hash(form.password.data) user.id = 10 # db.session.add(user) user.add() # db.session.commit() login.login_user(user) return redirect(url_for('.index')) link = '<p>Already have an account? <a href="' + url_for( '.login_view') + '">Click here to log in.</a></p>' self._template_args['form'] = form self._template_args['link'] = link return super(MyAdminIndexView, self).index()
def update_model(self, form, model): form.populate_obj(model) if current_user.has_role('developer'): if model.name != current_user.first_name: flash('You are not the project owner!', 'error') return else: flash('Notes updated!', 'message') if current_user.has_role('reviewer1'): if model.reviewer1 != current_user.first_name: flash('You are not the project owner!', 'error') return else: flash('Review updated!', 'message') if current_user.has_role('reviewer2'): if model.review1 == None or model.review1 ==0: flash('You are not the current approver!', 'error') return else: if model.reviewer2 != current_user.first_name: flash('You are not the project owner!', 'error') return else: flash('Review updated!', 'message') if current_user.has_role('superuser'): if model.review1 == None or model.review1 ==0 or model.review2 == None or model.review2 ==0: flash('You are not the current approver!', 'error') return else: flash('Review updated!', 'message') if current_user.has_role('administrator'): flash('You are not the project owner!', 'error') return self.session.add(model) self._on_model_change(form, model, False) self.session.commit()
def update_model(self, form, model): form.populate_obj(model) self.updated_models.append(model) return True