Created
April 9, 2018 18:36
-
-
Save dafma/d4971f9383f995cc95143c96d1bf8592 to your computer and use it in GitHub Desktop.
custom create user and profile with @Reciver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def crearempresa(request): | |
field_ejecutivo = Profile.objects.filter(tipo_usuario='ejecutivo') | |
if request.method == 'POST': | |
user_form = UserEmpresaForm(data=request.POST or None) | |
if user_form.is_valid(): | |
user = user_form.save() | |
user.refresh_from_db() | |
user.profile.rfc = user_form.cleaned_data.get('rfc') | |
user.profile.tipo_usuario = 'empresa' | |
user.profile.formato = user_form.cleaned_data.get('formato') | |
user.profile.servicios = user_form.cleaned_data.get('servicios') | |
user.profile.ejecutivo_decuenta = request.POST['ejecutivo'] | |
user.save() | |
messages.success(request, 'creado con exito') | |
else: | |
user_form = UserEmpresaForm() | |
return render(request, 'creacionedicionusuarios/crear_empresa.html', {'user_form':user_form, 'field_ejecutivo':field_ejecutivo }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="overflow"> | |
<h1>Crear Empresa</h1> | |
<form method="post"> | |
{% csrf_token %} | |
<select name="ejecutivo" id="" class="form-control"> | |
{% for foo in field_ejecutivo %} | |
<option value="{{ foo.user }}">{{ foo.user }}</option> | |
{% endfor %} | |
</select> | |
{{ user_form }} | |
{{ user_form.errors }} | |
<input type="submit" value="Crear+"> | |
</form> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.conf import settings | |
from django.contrib.auth import authenticate | |
from django.contrib.auth.forms import UserCreationForm | |
from django.contrib.auth.models import User | |
from .models import Profile | |
class UserEmpresaForm(UserCreationForm): | |
rfc = forms.CharField(max_length=20) | |
formato = forms.ChoiceField(choices=FORMATOS_CHOICES, label="", initial='', widget=forms.Select()) | |
tel_movil = forms.CharField(max_length=20) | |
servicios = forms.ChoiceField(choices=SERVICIOS_CHOICES, label="", initial='', widget=forms.Select()) | |
class Meta: | |
model = User | |
fields = ('username', 'email') | |
def __init__(self, *args, **kwargs): | |
super(UserEmpresaForm, self).__init__(*args, **kwargs) | |
self.fields['username'].widget.attrs.update({'class': 'form-control'}) | |
self.fields['email'].widget.attrs.update({'class': 'form-control'}) | |
self.fields['rfc'].widget.attrs.update({'class': 'form-control'}) | |
self.fields['formato'].widget.attrs.update({'class': 'form-control'}) | |
self.fields['servicios'].widget.attrs.update({'class': 'form-control'}) | |
self.fields['tel_movil'].widget.attrs.update({'class': 'form-control'}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import AbstractUser, User | |
from django.dispatch import receiver | |
from django.db.models.signals import post_save | |
class Profile(models.Model): | |
user = models.OneToOneField(settings.AUTH_USER_MODEL) | |
tipo_usuario = models.CharField(max_length=10, choices=TIPO_USUARIO_CHOICE) | |
rfc = models.CharField(max_length=20, blank=True, null=True) | |
ext = models.SmallIntegerField(blank=True, null=True) | |
correo_i = models.EmailField(blank=True, null=True) | |
tel_casa = models.CharField(max_length=15, blank=True, null=True) | |
tel_movil = models.CharField(max_length=15, blank=True, null=True) | |
dias_descanso = models.CharField( max_length=2 ,choices=DIAS_DESCANSO, blank=True, null=True) | |
estacion_trabajo = models.CharField( 'Estación Trabajo' ,max_length=15 , blank=True, null=True) | |
tipo_sangre = models.CharField(max_length=3, blank=True, null=True ) | |
telefono_emergencia = models.CharField(max_length=15, blank=True, null=True) | |
nombre_familiar = models.CharField(max_length=50, blank=True, null=True) | |
parentesco = models.CharField(max_length=10, blank=True, null=True) | |
fecha_nacimiento = models.DateField( blank=True, null=True) | |
# for panel personal | |
ejecutivo_decuenta = models.CharField( max_length=30,blank=True, null=True) | |
formato = models.CharField(max_length=20, choices=FORMATOS_CHOICES, blank=True, null=True ) | |
servicios = models.CharField(max_length=20, choices=SERVICIOS_CHOICES, blank=True, null=True) | |
def __str__(self): | |
return self.user.username | |
@receiver(post_save, sender=User) | |
def update_user_profile(sender, instance, created, **kwargs): | |
if created: | |
Profile.objects.create(user=instance) | |
instance.profile.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment