예제 #1
0
def group_delete(request, id_grp):
    grupo = Group.objects.filter(id=id_grp).first()
    if request.method == 'POST':
        perform_log(obj=grupo, request=request, af=2)
        grupo.delete()
        return redirect(reverse_lazy('admin_group'))
    return render(request, 'layouts/admin/groups_delete.html',
                  {'grupo': grupo})
예제 #2
0
def change_pass(request):
    passform = PasswordChangeForm(request.user)
    if request.method == 'POST':
        passform = PasswordChangeForm(request.user, request.POST)
        if passform.is_valid():
            usr = passform.save()
            update_session_auth_hash(request, usr)
            perform_log(obj=usr, request=request, af=1)
            return redirect(reverse_lazy('perfil_detail'))
    return render(request, 'layouts/admin/change_pass.html',
                  {'passform': passform})
예제 #3
0
def user_deactivate(request, id_usr):
    if id_usr != request.user.id:
        usr = User.objects.filter(id=id_usr).first()
        if request.method == 'POST':
            usr.is_active = not usr.is_active
            usr.save()
            perform_log(obj=usr, request=request, af=2)
            return redirect(reverse_lazy('admin_user'))
        return render(request, 'layouts/admin/user_deactivate.html',
                      {'usr': usr})
    return redirect(reverse_lazy('admin_user'))
예제 #4
0
def user_setpass(request, id_usr):
    usr = User.objects.filter(id=id_usr).first()
    form = UserPasswordForm(instance=usr)
    if request.method == 'POST':
        form = UserPasswordForm(request.POST, instance=usr)
        if form.is_valid():
            usr.set_password(form.cleaned_data.get('password'))
            usr.save()
            perform_log(obj=usr, request=request, af=1)
            return redirect(reverse_lazy('admin_user'))
    return render(request, 'layouts/admin/user_setpass.html', {
        'form': form,
        'usr': usr
    })
예제 #5
0
def group_edit(request, id_group):
    grp = Group.objects.filter(id=id_group).first()
    if request.method == 'POST':
        form = GroupForm(request.POST, instance=grp)
        if form.is_valid():
            grp = form.save()
            perform_log(obj=grp, request=request, af=1)
            return redirect(reverse_lazy('admin_group'))
    else:
        form = GroupForm(instance=grp)
    return render(request, 'layouts/admin/groups_form.html', {
        'form': form,
        'grp': grp
    })
예제 #6
0
def group_add(request):
    if request.method == 'POST':
        form = GroupForm(request.POST)
        if form.is_valid():
            grp = form.save()
            perform_log(obj=grp, request=request, af=0)
            return redirect('admin_group')
        else:
            return render(request, 'layouts/admin/groups_form.html',
                          {'form': form})
    else:
        form = GroupForm()
        return render(request, 'layouts/admin/groups_form.html',
                      {'form': form})
예제 #7
0
def login_page(request):
    if request.user.is_authenticated:
        return redirect(reverse_lazy('base_dashboard'))
    else:
        store_url_names()
        if request.method == 'POST':
            form = LoginForm(request.POST)
            if form.is_valid():
                form_data = form.cleaned_data
                user_pass = form_data.get('password_login')
                user_name = form_data.get('username_login')
                not_expiry = form_data.get('remenber_me')
                user = User.objects.filter(
                    Q(email=user_name) | Q(username=user_name)).first()
                if user and user.is_active:
                    access = authenticate(username=user.username,
                                          password=user_pass)
                    if access:
                        login(request, access)
                        if not_expiry:
                            request.session.set_expiry(4838400)
                        perform_log(obj=user, request=request, af=3)
                        return redirect(reverse_lazy('base_dashboard'))
                    else:
                        form.add_error(
                            'password_login',
                            _('Combinación no válida de usuario y contraseña'))
                else:
                    form.add_error(
                        'username_login',
                        _('Ese usuario no exitse o está incativo contacte con el administrador del sistema'
                          ))
            else:
                form.add_error('password_login',
                               _('Error de usuario o contraseña no valido'))
            print(form.errors)
            return render(request, 'layouts/login.html', {'form': form})
        else:
            form = LoginForm()
            return render(request, 'layouts/login.html', {'form': form})
예제 #8
0
def user_edit(request, id_usuario):
    usr = User.objects.filter(id=id_usuario).first()
    prf = Perfil.objects.filter(datos_usuario=usr.id).first()
    prs = PersonaNatural.objects.filter(id=prf.datos_personales.id).first()
    if request.method == 'POST':
        form = UserNPForm(request.POST, instance=usr)
        formprs = PersonaNaturalMForm(request.POST, instance=prs)
        formprf = PerfilMForm(request.POST, instance=prf)
        if form.is_valid() and formprf.is_valid() and formprs.is_valid():
            usr = form.save()
            formprs.save()
            formprf.save()
            perform_log(obj=usr, request=request, af=1)
            return redirect(reverse_lazy('admin_user'))
    form = UserNPForm(instance=usr)
    formprs = PersonaNaturalMForm(instance=prs)
    formprf = PerfilMForm(instance=prf)
    return render(request, 'layouts/admin/users_form.html', {
        'form': form,
        'formprs': formprs,
        'formprf': formprf,
        'usuario': usr
    })
예제 #9
0
def user_add(request):
    exist_perfil = False
    exist_persona = False
    if all_apps.get_app_configs():
        for app in all_apps.get_app_configs():
            if 'dpv_perfil' in app.label:
                exist_perfil = True
            if 'dpv_persona' in app.label:
                exist_persona = True
    if request.method == 'POST':
        form = UserMForm(request.POST)
        if exist_persona:
            formprs = PersonaNaturalMForm(request.POST)
        if exist_perfil:
            formprf = PerfilMForm(request.POST)
        if form.is_valid() and formprs.is_valid() and formprf.is_valid():
            usr = form.save()
            usr.set_password(form.cleaned_data.get('password'))
            usr.save()
            try:
                prs = formprs.save()
                prs.nombre = usr.first_name
                prs.apellidos = usr.last_name
                prs.email_address = usr.email
                prs.save()
                try:
                    prf = Perfil()
                    prf.centro_trabajo = formprf.cleaned_data.get(
                        'centro_trabajo')
                    prf.depto_trabajo = formprf.cleaned_data.get(
                        'depto_trabajo')
                    prf.notificacion_email = formprf.cleaned_data.get(
                        'notificacion_email')
                    prf.documentacion_email = formprf.cleaned_data.get(
                        'documentacion_email')
                    prf.datos_personales = prs
                    prf.datos_usuario = usr
                    prf.save()
                except:
                    usr.delete()
                    prs.delete()
            except:
                form.add_error(
                    'email',
                    'ya existe algún usuario que está usando ese email y es asi'
                )
                return render(request, 'layouts/admin/users_form.html', {
                    'form': form,
                    'formprs': formprs,
                    'formprf': formprf
                })
            perform_log(obj=usr, request=request, af=0)
            return redirect('admin_user')
        else:
            return render(request, 'layouts/admin/users_form.html', {
                'form': form,
                'formprs': formprs,
                'formprf': formprf
            })
    else:
        form = UserMForm()
        if exist_persona:
            formprs = PersonaNaturalMForm()
        if exist_perfil:
            if request.user.perfil_usuario.centro_trabajo.oc:
                formprf = PerfilMForm()
            else:
                prfl = Perfil()
                prfl.centro_trabajo = request.user.perfil_usuario.centro_trabajo
                formprf = PerfilMForm(instance=prfl)

    return render(request, 'layouts/admin/users_form.html', {
        'form': form,
        'formprs': formprs,
        'formprf': formprf
    })