Example #1
0
def approve_batch_aspirants_upload(request):
    password = request.POST.get('password')
    return_data = {}
    if not request.user.check_password(password):
        return_data['STATUS'] = '0'
        return_data['MESSAGE'] = 'Wrong password'
    else:
        return_data['ASPIRANTS_REG'] = []
        excel_file = ExcelFile(request.session['ASPIRANTS_FILE_URL'])
        records = excel_file.get_records(True)
        for record in records:
            record = list(record)
            aspirant_reg = {'full_name': '{} {}'.format(record[0], record[1])}
            try:
                user_create = User.objects.create_user(
                    first_name=record[0],
                    last_name=record[1],
                    username=record[4],
                    email=record[4],
                    is_superuser=False,
                    is_staff=True,
                    date_joined=datetime.datetime.now())
                aspirant_reg['registered_as_user'] = '******'
                aspirant = Client()
                aspirant.alias_name = record[2]
                aspirant.phone_number = SMS.format_phone_number(record[3])
                aspirant.user = User.objects.get(username=record[4])
                aspirant.seat = Seat.objects.get(
                    Q(seat_name__icontains=record[5])
                    | Q(short_code__icontains=record[5]))
                aspirant.region_name = record[6]
                aspirant.country = Country.objects.get(country_name='Kenya')
                aspirant.county = County.objects.get(county_name=record[7])
                aspirant.constituency = Constituency.objects.get(
                    constituency_name=record[8])
                aspirant.ward = Ward.objects.get(ward_name=record[9])
                try:
                    aspirant.save()
                    aspirant_reg['registered_as_aspirant'] = '1'
                except:
                    aspirant_reg['registered_as_aspirant'] = '0'
            except:
                aspirant_reg['registered_as_user'] = '******'

            return_data['ASPIRANTS_REG'].append(aspirant_reg)

    return HttpResponse(json.dumps(return_data))
Example #2
0
def register_single_aspirant(request):
    password = request.POST.get('password')
    return_data = {}
    if not request.user.check_password(password):
        return_data['STATUS'] = '0'
        return_data['MESSAGE'] = 'Wrong password'
    else:
        first_name = request.POST.get('first_name')
        last_name = request.POST.get('last_name')
        email = request.POST.get('email')
        alias_name = request.POST.get('alias_name')
        sms = SMS()
        phone_number = sms.format_phone_number(
            request.POST.get('phone_number'))
        seat = Seat.objects.get(id=request.POST.get('seat'))
        region_name = request.POST.get('region_name')
        county_id = request.POST.get('county_id')
        constituency_id = request.POST.get('constituency_id')
        ward_id = request.POST.get('ward_id')

        try:
            user_create = User.objects.create_user(
                first_name=first_name,
                last_name=last_name,
                username=email,
                email=email,
                is_superuser=False,
                is_staff=True,
                date_joined=datetime.datetime.now())

            aspirant_reg = {'full_name': '{} {}'.format(first_name, last_name)}

            return_data['MESSAGE'] = []

            return_data['STATUS'] = '1'
            return_data['MESSAGE'].append({
                'STATUS':
                '1',
                'MESSAGE':
                'Client has been registered as system user'
            })

            aspirant = Client()
            aspirant.alias_name = alias_name
            aspirant.phone_number = phone_number
            aspirant.user = User.objects.get(username=email)
            aspirant.seat = seat
            aspirant.region_name = region_name
            aspirant.country = Country.objects.get(country_name='Kenya')
            aspirant.county = County.objects.get(id=county_id)

            if len(constituency_id) > 0 and constituency_id != 'NULL':
                aspirant.constituency = Constituency.objects.get(
                    id=constituency_id)
            if len(ward_id) > 0 and ward_id != 'NULL':
                aspirant.ward = Ward.objects.get(id=ward_id)

            try:
                aspirant.save()
                return_data['MESSAGE'].append({
                    'STATUS':
                    '1',
                    'MESSAGE':
                    'Client has been registered as a system admin for {}'.
                    format(region_name)
                })

                try:
                    recipient = [user_create]
                    email = SendEmail()
                    txt_template = 'email/email_temlate_aspirant_registration.txt'
                    print("0000000")
                    html_template = 'email/email_template_aspirant_registration.html'
                    print("11111111")
                    send_mail = email.send_email(recipient, request,
                                                 txt_template, html_template)
                    print("222222222")
                    if send_mail[0]['STATUS'] == '1':
                        return_data['MESSAGE'].append({
                            'STATUS':
                            '1',
                            'MESSAGE':
                            'An email has been sent to the client'
                        })
                    else:
                        print("*****")
                        print(send_mail)
                        return_data['MESSAGE'].append({
                            'STATUS':
                            '0',
                            'MESSAGE':
                            'Email delivery to client failed permanently'
                        })
                except Exception, exp:
                    print(str(exp))
                    return_data['MESSAGE'].append({
                        'STATUS':
                        '0',
                        'MESSAGE':
                        'Email delivery to client failed permanently'
                    })
            except:
                return_data['MESSAGE'].append({
                    'STATUS':
                    '0',
                    'MESSAGE':
                    'Client has not been registered as a system admin for {}'.
                    format(region_name)
                })

        except:
            return_data['MESSAGE'].append({
                'STATUS':
                '0',
                'MESSAGE':
                'Client was not registered'
            })

    return HttpResponse(json.dumps(return_data))
Example #3
0
def inscriptionClientMobil(request):
    #token = request.POST["token"]
    nom = request.POST["nom"]
    prenom = request.POST["prenom"]
    email = request.POST["email"]
    numero = request.POST["numero"]
    password = request.POST["password"]
    try:
        _user = User.objects.filter(username=numero, is_active=1)[0]
        if _user:
            error_numero = "un compte est déjà enregistré avec ce numero"
            return HttpResponse(error_numero, status=402)
    except:
        check = 0
        exist = 0
        listeValidation = Validation.objects.filter(estValider=0)
        while check == 0:
            var_tmp = ""
            while len(var_tmp) < 6:
                d = randint(0, 9)
                var_tmp = var_tmp + str(d)
            for validation in listeValidation:
                if validation.keyValidation == var_tmp:
                    exist = 1
            if exist == 0:
                check = 1
        personne = Personne()
        personne.nomPersonne = nom
        personne.prenomPersonne = prenom
        personne.numeroPersonne = numero
        personne.emailPersonne = email
        personne.save()
        try:
            old_user = User.objects.filter(username=numero, is_active=0)[0]
            if old_user:
                old_user.delete()
        except:
            v = 1
        user = User(username=numero,
                    email=email,
                    first_name=nom,
                    last_name=prenom,
                    is_active=1)
        user.set_password(password)
        user.save()
        client = Client()
        client.user = user
        client.personne = personne
        # client.code=password
        client.type = 2
        client.save()
        validation = Validation()
        validation.client = client
        validation.keyValidation = var_tmp
        validation.save()
        data_message = {"code": var_tmp, "numero": numero}
        sendSms(numero, var_tmp)
        #sendSms(data_message)
        log = Log()
        log.utilisateur = request.user.username
        log.action = "Creation compte client " + nom + " " + prenom + " " + numero
        log.save()
        nom = user.client.personne.nomPersonne
        prenom = user.client.personne.prenomPersonne
        idClient = user.client.idclient
        data = '{'
        data += '"nom":"' + nom + '",'
        data += '"prenom":"' + prenom + '",'
        data += '"idClient":"' + str(idClient) + '"'
        data += '}'
        return HttpResponse(data)