def add(): if request.method == 'GET': return redirect(url_for('associated.index')) form = AssociatedForm() if form.validate_on_submit(): person = Person() form.populate_obj(person) person.cpf = clean_id(person.cpf) phone = clean_id(form.contact.data) person = dict(person) person['emails'] = [form.email.data] person['contacts'] = [{'ddd': phone[0:2], 'num': phone[2:]}] person['city_id'] = None if form.create_user.data: user = User(username=form.email.data, password='******') user = UserRepository().create(user) person['user_id'] = user.id person = person_repository.create(person) person_associated_repository.create( dict(person_id=current_user.person.id, associated_id=person.id)) flash('Associado adicionado com sucesso!', 'success') return render_template('associated/form.html', form=form)
def CEP(value): try: format_cep(value) except ValueError as e: raise Invalid(e) return clean_id(value)
def new_password(): form = NewPasswordForm(request.form) if form.validate_on_submit(): user = PersonRepository().findByCpf(clean_id(form.cpf.data)).user if user: user.password = form.password.data UserRepository().update(user.id, user, update_password=True) flash('Sua senha foi alterada com sucesso!', 'success') else: flash('Sua senha não foi alterada. Por favor, tente novamente!', 'error') return redirect(url_for('auth.login'))
def profile(): first_time = False person = person_repository.findByUser(current_user.id) if person is None: first_time = True person = Person(user_id=current_user.id) form = PersonForm(obj=person, federative_unit_id=str(person.city.federative_unit_id) if person.city else '', contact=next(('{}{}'.format(c.ddd, c.num) for c in person.contacts), None), email=next((email.email for email in person.emails), current_user.username)) if form.federative_unit_id.data != '': form.city_id.choices += [ (str(c.id), c.name) for c in city_repository.allByFederativeUnit( int(form.federative_unit_id.data), order_by=[('name', )]) ] if form.validate_on_submit(): form.populate_obj(person) person.cpf = clean_id(person.cpf) if person.postal_code: person.postal_code = clean_id(person.postal_code) person = dict(person) person['city_id'] = None if person['city_id'] == '' else int( person['city_id']) person['emails'] = [form.email.data] contact = clean_id(form.contact.data) person['contacts'] = [dict(ddd=contact[:2], num=contact[2:])] if person['id'] is not None: person_repository.update(person['id'], person) else: person_repository.create(person) flash('Perfil salvo com sucesso!', 'success') if first_time: return redirect(url_for('main.dashboard')) return redirect(url_for('profile.profile')) return render_template('profile/form.html', form=form)
def edit(person_id): person = person_repository.find(person_id) form = AssociatedForm(obj=person) if request.method == 'GET': email = next(person.emails, None) form.email.data = email.email if email else '' contact = next(person.contacts, None) form.contact.data = '{}{}'.format(contact.ddd, contact.num) if contact else '' return render_template('associated/modals/edit.html', form=form, person_id=person_id) if form.validate_on_submit(): form.populate_obj(person) person.cpf = clean_id(person.cpf) phone = clean_id(form.contact.data) person = dict(person) person['emails'] = [form.email.data] person['contacts'] = [{'ddd': phone[0:2], 'num': phone[2:]}] person = person_repository.update(person_id, person) flash('Associado atualizado com sucesso!', 'success') else: print(form.errors) return render_template('associated/form.html', form=form, person_id=person_id, error_edit=True) return redirect(url_for('associated.index'))
def _unique_cpf(form, field): person = PersonRepository().findByCpf(clean_id(field.data)) if person and person.user_id != current_user.id: raise ValidationError(message)
def CPF(value): if not validate_cpf(value): raise Invalid("Invalid CPF") return clean_id(value)
def CNPJ(value): if not validate_cnpj(value): raise Invalid("Invalid CNPJ") return clean_id(value)