Example #1
0
    def form_valid(self, form):
        """!
        Función que valida si el formulario es correcto

        @author William Páez <*****@*****.**>
        @param self <b>{object}</b> Objeto que instancia la clase
        @param form <b>{object}</b> Objeto que contiene el formulario de
            registro
        @return Formulario validado
        """

        self.object = form.save(commit=False)
        self.object.grant = form.cleaned_data['grant']
        self.object.save()

        admin, admin_email = '', ''
        if settings.ADMINS:
            admin = settings.ADMINS[0][0]
            admin_email = settings.ADMINS[0][1]

        if self.object.grant:
            send_email(
                self.object.profile.user.email,
                'user/certificate_download.mail',
                'Sofi - Descarga de Certificado', {
                    'url': get_current_site(self.request).name,
                    'event_id': self.object.event.id,
                    'admin': admin,
                    'admin_email': admin_email,
                })
        return super().form_valid(form)
Example #2
0
    def form_valid(self, form):
        """!
        Metodo que valida si el formulario es correcto

        @author William Páez (paez.william8 at gmail.com)
        @param self <b>{object}</b> Objeto que instancia la clase
        @param form <b>{object}</b> Objeto que contiene el formulario de
            registro
        @return Retorna el formulario validado
        """

        self.object = form.save()
        self.object.username = form.cleaned_data['username']
        self.object.first_name = form.cleaned_data['first_name']
        self.object.last_name = form.cleaned_data['last_name']
        self.object.email = form.cleaned_data['email']
        password = User.objects.make_random_password()
        self.object.set_password(password)
        self.object.is_active = True
        self.object.save()
        self.object.groups.add(Group.objects.get(name='Líder de Comunidad'))

        profile = Profile.objects.create(phone=form.cleaned_data['phone'],
                                         user=self.object)

        communal_council = CommunalCouncil.objects.get(
            pk=form.cleaned_data['communal_council'])
        CommunityLeader.objects.create(communal_council=communal_council,
                                       profile=profile)

        admin, admin_email = '', ''
        if settings.ADMINS:
            admin = settings.ADMINS[0][0]
            admin_email = settings.ADMINS[0][1]

        ubch_level = UbchLevel.objects.get(profile=self.request.user.profile)

        sent = send_email(
            self.object.email, 'user/welcome.mail', 'Bienvenido a Censo', {
                'first_name': self.request.user.first_name,
                'last_name': self.request.user.last_name,
                'email': self.request.user.email,
                'ubch': ubch_level.ubch,
                'username': self.object.username,
                'password': password,
                'admin': admin,
                'admin_email': admin_email,
                'emailapp': settings.EMAIL_HOST_USER,
                'url': get_current_site(self.request).name
            })

        if not sent:
            logger.warning(
                str('Ocurrió un inconveniente al enviar por correo las \
                    credenciales del usuario [%s]' % self.object.username))
        return super().form_valid(form)
Example #3
0
    def form_valid(self, form):
        self.object = form.save()
        self.object.username = form.cleaned_data['username']
        self.object.first_name = form.cleaned_data['first_name']
        self.object.last_name = form.cleaned_data['last_name']
        self.object.email = form.cleaned_data['email']
        self.object.set_password(form.cleaned_data['password'])
        self.object.is_active = True
        self.object.groups.add(Group.objects.get(name='Nivel Encuestador'))
        self.object.save()

        profile = Profile.objects.create(phone=form.cleaned_data['phone'],
                                         user=self.object)

        communal_council_level = CommunalCouncilLevel.objects.get(
            profile=self.request.user.profile)
        Pollster.objects.create(communal_council_level=communal_council_level,
                                profile=profile)

        admin, admin_email = '', ''
        if settings.ADMINS:
            admin = settings.ADMINS[0][0]
            admin_email = settings.ADMINS[0][1]

        send_email(
            self.object.email, 'user/welcome.mail', EMAIL_SUBJECT, {
                'first_name': self.request.user.first_name,
                'last_name': self.request.user.last_name,
                'email': self.request.user.email,
                'phone': self.request.user.profile.phone,
                'communal_council_level': communal_council_level,
                'username': self.object.username,
                'password': form.cleaned_data['password'],
                'admin': admin,
                'admin_email': admin_email,
                'emailapp': settings.EMAIL_FROM,
                'url': get_current_site(self.request).name
            })
        return super().form_valid(form)
Example #4
0
    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.username = form.cleaned_data['username']
        self.object.first_name = form.cleaned_data['first_name']
        self.object.last_name = form.cleaned_data['last_name']
        self.object.email = form.cleaned_data['email']
        self.object.set_password(form.cleaned_data['password'])
        self.object.is_active = True
        self.object.save()

        user = User.objects.get(username=self.object.username)
        profile = Profile.objects.create(phone=form.cleaned_data['phone'],
                                         level=7,
                                         user=user)

        street_leader = StreetLeader.objects.get(
            profile=self.request.user.profile)
        FamilyGroup.objects.create(street_leader=street_leader,
                                   profile=profile)

        admin, admin_email = '', ''
        if settings.ADMINS:
            admin = settings.ADMINS[0][0]
            admin_email = settings.ADMINS[0][1]

        sent = send_email(
            self.object.email, 'user/welcome.mail', EMAIL_SUBJECT, {
                'level': 'Jefe de calle',
                'first_name': self.request.user.first_name,
                'last_name': self.request.user.last_name,
                'email': self.request.user.email,
                'username': self.object.username,
                'password': form.cleaned_data['password'],
                'admin': admin,
                'admin_email': admin_email,
                'emailapp': settings.EMAIL_FROM
            })

        if not sent:
            logger.warning(
                str(
                    _('Ocurrió un inconveniente al enviar por correo las credenciales del usuario [%s]'
                      ) % self.object.username))

        return super(FamilyGroupCreateView, self).form_valid(form)
Example #5
0
    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.username = form.cleaned_data['username']
        self.object.first_name = form.cleaned_data['first_name']
        self.object.last_name = form.cleaned_data['last_name']
        self.object.email = form.cleaned_data['email']
        self.object.set_password(form.cleaned_data['password'])
        self.object.is_active = True
        self.object.save()
        self.object.groups.add(Group.objects.get(name='Nivel Estadal'))

        profile = Profile.objects.create(
            phone=form.cleaned_data['phone'],
            user= self.object
        )

        state = State.objects.get(pk=form.cleaned_data['state'])
        state_level = StateLevel.objects.create(
            state = state,
            profile = profile
        )

        admin, admin_email = '', ''
        if settings.ADMINS:
            admin = settings.ADMINS[0][0]
            admin_email = settings.ADMINS[0][1]

        national_level = NationalLevel.objects.get(profile=self.request.user.profile)

        sent = send_email(self.object.email, 'user/welcome.mail', EMAIL_SUBJECT, {'first_name':self.request.user.first_name,
            'last_name':self.request.user.last_name, 'email':self.request.user.email, 'phone':self.request.user.profile.phone,
            'level2':state_level.state,'username':self.object.username, 'password':form.cleaned_data['password'],
            'admin':admin, 'admin_email':admin_email, 'emailapp':settings.EMAIL_HOST_USER, 'url':get_current_site(self.request).name,
            'level1':national_level.country
        })

        if not sent:
            logger.warning(
                str(_("Ocurrió un inconveniente al enviar por correo las credenciales del usuario [%s]") % self.object.username)
            )

        return super(StateLevelCreateView, self).form_valid(form)
Example #6
0
    def post(self, request, *args, **kwargs):
        record = json.loads(request.body.decode('utf-8'))
        family_group_form = FamilyGroupForm(record)
        if not family_group_form.is_valid():
            return JsonResponse(
                {
                    'status': False,
                    'message': 'Error en los campos',
                    'errors': {
                        'family_group': family_group_form.errors,
                        'people': [{}]
                    }
                },
                status=422)
        i = 0
        data = {}
        for person in record['people']:
            for key, value in person.items():
                form_name = 'form-' + str(i) + '-' + key
                data[form_name] = value
            i = i + 1
        data['form-TOTAL_FORMS'] = i
        data['form-INITIAL_FORMS'] = 0
        personformset = PersonFormSet(data)
        if not personformset.is_valid():
            return JsonResponse(
                {
                    'status': False,
                    'message': 'Error en los campos',
                    'errors': {
                        'people': personformset.errors,
                        'general_error': personformset.non_form_errors()
                    }
                },
                status=422)
        password = User.objects.make_random_password()
        user = User.objects.create_user(
            record['username'],
            record['email'],
            password,
        )
        # user.first_name = record['first_name']
        # user.last_name = record['last_name']
        user.is_active = False
        user.save()
        user.groups.add(Group.objects.get(name='Grupo Familiar'))
        profile = Profile.objects.create(
            # phone=record['phone'],
            user=user)
        street_leader = StreetLeader.objects.get(
            profile=self.request.user.profile)
        family_group = FamilyGroup.objects.create(street_leader=street_leader,
                                                  profile=profile)
        c = 1
        for person in record['people']:
            vote_type = VoteType.objects.get(pk=person['vote_type_id'])
            relationship = Relationship.objects.get(
                pk=person['relationship_id'])
            if person['has_id_number'] == 'y':
                if person['family_head']:
                    value = True
                else:
                    value = False
                Person.objects.create(first_name=person['first_name'],
                                      last_name=person['last_name'],
                                      id_number=person['id_number'],
                                      email=person['email'],
                                      phone=person['phone'],
                                      vote_type=vote_type,
                                      relationship=relationship,
                                      family_head=value,
                                      family_group=family_group)
            else:
                p = Person.objects.get(family_group=family_group,
                                       family_head=True)
                Person.objects.create(first_name=person['first_name'],
                                      last_name=person['last_name'],
                                      id_number=p.id_number + '-' + str(c),
                                      email=person['email'],
                                      phone=person['phone'],
                                      vote_type=vote_type,
                                      relationship=relationship,
                                      family_head=False,
                                      family_group=family_group)
                c = c + 1
        admin, admin_email = '', ''
        if settings.ADMINS:
            admin = settings.ADMINS[0][0]
            admin_email = settings.ADMINS[0][1]

        send_email(
            user.email, 'user/welcome.mail', 'Bienvenido a Censo', {
                'first_name': self.request.user.first_name,
                'last_name': self.request.user.last_name,
                'email': self.request.user.email,
                'ubch': street_leader.community_leader.communal_council.ubch,
                'username': user.username,
                'password': password,
                'admin': admin,
                'admin_email': admin_email,
                'emailapp': settings.EMAIL_HOST_USER,
                'url': get_current_site(self.request).name
            })
        return JsonResponse(
            {
                'status': True,
                'message': 'Datos guardados con éxito',
                'redirect': '/user/family-group/list/',
            },
            status=200)