Exemplo n.º 1
0
def new_user(id=None):
    """Agrega y actualiza los el objeto User"""
    if id is not None:
        user = User.query.filter(User.id == id).one_or_none()
        if user is None:
            flash(('danger', 'Lo sentimos algo salio mal!.'))
            return redirect(url_for('view_user'))
        if request.method == 'GET':
            user_form = forms.CreateUserForm(obj=user)
        elif request.method == 'POST':
            user_form = forms.CreateUserForm(request.form)
            user_form.location.choices = [(g.id, g.location_name) for g in Location.query.order_by('location_name').all()]
            user_c = User.query.filter_by(username=user_form.username.data).one_or_none()
            if user_c is not None and user_c.id != id:
                flash(('danger', 'El usuario ya se encuentra registrado!'))
                return render_template('new_user.html', form=user_form)
            if user_form.validate():
                user_form.populate_obj(user)
                try:
                    user.update()
                    flash(('success', 'Usuario creado correctamente.'))
                    return redirect(url_for('new_user'))
                except Exception as e:
                    print(e)
                    flash(('danger', 'Lo sentimos algo salio mal!.'))
                    return redirect(url_for('index'))
    else:
        user_form = forms.CreateUserForm(request.form)
        user_form.location.choices = [(g.id, g.location_name) for g in Location.query.order_by('location_name').all()]
        user = User.query.filter_by(username=user_form.username.data).first()
        if user is not None:
            flash(('danger', 'El usuario ya se encuentra registrado!'))
            return render_template('new_user.html', form=user_form)
        if request.method == 'POST' and user_form.validate():
            user = User(user_form.username.data,
                user_form.email.data,
                user_form.password.data,
                user_form.staff.data, user_form.first_name.data,
                user_form.last_name.data,
                user_form.location.data, user_form.phone.data)
            try:
                user.add()
                flash(('success', 'Usuario creado correctamente.'))
                return redirect(url_for('new_user'))
            except Exception as e:
                print(e)
                flash(('danger', 'Lo sentimos algo salio mal!.'))
                return redirect(url_for('index'))
    user_form.location.choices = [(g.id, g.location_name) for g in Location.query.order_by('location_name').all()]
    return render_template('new_user.html', form=user_form)
Exemplo n.º 2
0
def crear_usuario():

    create_form = forms.CreateUserForm(request.form)

    if form_is_valid(request.method, create_form):
        username = create_form.username.data
        password = create_form.password.data
        email = create_form.email.data
        user = User(username=username, password=password, email=email)
        db.session.add(user)
        db.session.commit()

        # Ejecutar procesos en segundo plano
        @copy_current_request_context
        def send_message(email, username):
            send_email(email, username)

        sender = threading.Thread(name='mail_sender',
                                  target=send_message,
                                  args=(user.email, user.username))
        sender.start()

        success_message = 'Usuario registrado en la base de datos'
        flash(success_message)

    titulo = 'Crear Usuario'
    return render_template('create_user.html', title=titulo, form=create_form)
Exemplo n.º 3
0
def auth(request):
    client = Client()
    if request.method == "POST":
        form = forms.CreateUserForm(request.POST)
        if form.is_valid():
            userName = form.cleaned_data["username"]
            userPass = form.cleaned_data["password"]
            userMail = form.cleaned_data["mail"]
            userToken = form.cleaned_data["token"]
            user = User.objects.create_user(
                userName, userMail,
                userPass)  # Nous créons un nouvel utilisateur
            user.save()
            profil = Profile(user=user, user_token=userToken)
            profil.save()
    else:
        form = forms.CreateUserForm()
        code = request.GET['code']
        access_token = client.exchange_code_for_token(
            client_id=13966,
            client_secret='a67b6efd42d941633fd631b35df2d22ae9b566c1',
            code=code)

    return render(request, 'auth.html', locals())
Exemplo n.º 4
0
def create_user():
    create_form = forms.CreateUserForm(request.form)
    if request.method == 'POST' and create_form.validate():
        user = User(username=create_form.username.data,
                    password=create_form.password.data,
                    email=create_form.email.data)
        db.session.add(user)
        db.session.commit()

        success_message = 'Usuario registrado en la base de datos'
        flash(success_message)

        return redirect(url_for('home'))

    return render_template('create.html', form=create_form)
Exemplo n.º 5
0
def create_user():
    """
    View for creating a user
    """

    from application.generate_keys import generate_randomkey

    form = forms.CreateUserForm(request.form)

    form.family.choices = get_family_choices()

    form.roles.choices = get_role_choices()

    if request.method == 'POST':
        if form.validate():
            # create the user with information specified in form
            fname = form.fname.data
            lname = form.lname.data
            cwruid = form.cwruid.data

            # generate a new temporary password
            password = generate_randomkey(16)

            # get optional attributes
            optional_attr = {}
            if form.mname.data != '':
                optional_attr['mname'] = form.mname.data

            if form.family.data != 'none':
                # look up family instance
                query = models.FamilyModel.all()
                query.filter('name =', form.family.data)
                families = query.fetch(1)
                if len(families) != 1:
                    form.family.errors.append(u'Family %s does not exist' %
                                              form.family.data)
                    return render_template('members/create.html',
                                           create_user_form=form)
                optional_attr['family'] = families[0].key()

            if form.big.data != '':
                # look up big instance
                users = find_users(cwruid=('=', form.big.data))
                if len(users) != 1:
                    form.big.errors.append(u'User %s does not exist' %
                                           form.big.data)
                    return render_template('members/create.html',
                                           create_user_form=form)
                optional_attr['big'] = users[0].key()

            if form.avatar.data != '':
                optional_attr['avatar'] = form.avatar.data

            try:
                new_user = accounts.create_user(fname, lname, cwruid, password,
                                                **optional_attr)
                if new_user is None:
                    raise AttributeError(
                        'Something went wrong with user creation')

                # add the case email address to the user
                email = models.EmailModel(user=new_user.key(),
                                          email='*****@*****.**' %
                                          new_user.cwruid,
                                          name='Case Email')
                email.put()

                # add the roles to the user
                for role in form.roles.data:
                    query = RoleModel.all()
                    query.filter('name =', role)

                    if query.count() != 1:
                        flash('Role %s does not exist' % role, 'error')
                        continue

                    desired_role = query.fetch(1)[0]

                    new_urole = UserRoleModel(user=new_user.key(),
                                              role=desired_role.key())
                    new_urole.put()

                flash('User created successfully', 'success')

                form = None
                form = forms.CreateUserForm()
                form.family.choices = get_family_choices()
                form.roles.choices = get_role_choices()

                send_new_user_mail(fname, lname, cwruid, password)
            except AttributeError, e:
                flash(str(e), 'error')