Get data in Django view

0

At the time of registering a new user, he chooses which group is part of a select. Each group has a responsible person, how do I get the email from this person to send an authorization request email?

In this code I send email but to the email that the user filled in and not to the person in charge of the group that was chosen.

models.py

class Grupo(models.Model):

    grupo = models.CharField(max_length=255)
    divisao = models.ForeignKey(Divisao)
    responsavel = models.ForeignKey(User)

    @property
      def get_responsavel(self):
        return self.responsavel
    def __unicode__(self):
        return self.grupo


class Perfil(User):
    telefone = models.CharField(max_length=255)
    ramal = models.CharField(max_length=255)
    data_inicio = models.DateField(default=datetime.now, blank=True)
    data_fim = models.DateField(null=True, blank=True)
    e_responsavel = models.BooleanField(default=False)
    vinculo = models.ForeignKey(Vinculo)
    grupos = models.ForeignKey(Grupo)
    divisao = models.ForeignKey(Divisao)

    def __unicode__(self):
        return self.first_name

views.py

def cadastro(request, group_id=None):
    if request.method == 'POST':
        form = UserForm(request.POST)
        formulario = form.save()

        if form.is_valid():
            formulario.set_password(form.cleaned_data['password'])
            formulario.save()

            # Envio de email
            current_site = get_current_site(request)
            message = render_to_string('acc_active_email.html', {
                'user': formulario,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(formulario.pk)),
                'token': account_activation_token.make_token(formulario),
            })
            mail_subject = 'Novo usuario no sistema'


            group = Grupo.objects.get(pk=group_id)
            responsavel = group.get_responsavel()
            to_email = responsavel.email
            email = EmailMessage(mail_subject, message, to=[to_email])
            email.send()

            return HttpResponseRedirect('/cadastro/login/')

    else:
        form = UserForm()
    return render(request, 'cadastro.html', {
        'form': form,

    })
    
asked by anonymous 14.09.2017 / 15:27

1 answer

0

Hello

You can make a filter, to get the group that the user chose, you can do so, you need to get the group id by sending the form, then you do the filter:

To get the moderator, you can make a decorator in your group model

Ex:

class Grupo(models.Model):
    grupo = models.CharField(max_length=255)
    divisao = models.ForeignKey(Divisao)
    responsavel = models.ForeignKey(User)

    @property
    def get_responsavel(self):
        return self.responsavel

    def __unicode__(self):
        return self.grupo

With this you can get the group moderator

group = Grupo.objects.get(pk=group_id)
responsavel = group.get_responsavel()
responsavel.email

It's a simple tip, but if you have more questions, it would be interesting to see how your models are, to get the right relationships

    
14.09.2017 / 17:03