Ejemplo n.º 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))
Ejemplo n.º 2
0
def edit_page_post(client_instance, request):  # TeamRome
    """ views.py ClientEditMain(TemplateView) POST method. """
    """ Входные данные для сохранения: """
    user = request.user
    user_name = check_input_str(request.POST['client_first_name'])
    last_name = check_input_str(request.POST['client_last_name'])
    patronymic = check_input_str(request.POST['client_middle_name'])
    sex = Sex.objects.get(
        sex_word=request.POST['sex']) if request.POST['sex'] else None
    date = request.POST['date_born'] if request.POST['date_born'] else None
    citizenship = Citizenship.objects.get(
        country_word=request.POST['citizenship']
    ) if request.POST['citizenship'] else None
    family_state = FamilyState.objects.get(
        state_word=request.POST['family_state']
    ) if request.POST['family_state'] else None
    children = Children.objects.get(children_word=request.POST['children']
                                    ) if request.POST['children'] else None
    country = Citizenship.objects.get(country_word=request.POST['country']
                                      ) if request.POST['country'] else None
    city = City.objects.get(
        city_word=request.POST['city']) if request.POST['city'] else None
    street = check_input_str(request.POST['street'])
    house = check_home_number(request.POST['house'])
    flat = check_home_number(request.POST['flat'])
    telegram_link = check_telegram(request.POST['telegram_link'])
    skype = check_input_str(request.POST['skype_id'])
    email = request.POST['email']
    link_linkedin = request.POST['link_linkedin']
    state = State.objects.get(
        state_word=request.POST['state']) if request.POST['state'] else None
    # print(user_name, last_name, patronymic, sex, date, citizenship, family_state, children, country, city,
    #       street, house, flat, telegram_link, skype, email, link_linkedin, state)

    if not client_instance:
        """ Если карточки нету - создаём. """
        print('\tUser Profile DO NOT exists - creating!')

        client = Client(
            user_client=user,
            patronymic=patronymic,
            sex=sex,
            date_born=date,
            citizenship=citizenship,
            family_state=family_state,
            children=children,
            country=country,
            city=city,
            street=street,
            house=house,
            flat=flat,
            telegram_link=telegram_link,
            skype=skype,
            link_linkedin=link_linkedin,
            state=state,
        )
        client.save()
    else:
        """ Если карточка есть - достаём из БД Объект = Клиент_id.
        Перезаписываем (изменяем) существующие данныев. """
        print('\tUser Profile exists - Overwriting user data')

        user_model = UserModel.objects.get(id=client_instance.user_client_id)
        user_model.first_name = user_name
        user_model.last_name = last_name
        user_model.email = email
        user_model.save()

        client = client_instance
        client.name = user_name
        client.last_name = last_name
        client.patronymic = patronymic
        client.sex = sex
        client.date_born = date
        client.citizenship = citizenship
        client.family_state = family_state
        client.children = children
        client.country = country
        client.city = city
        client.street = street
        client.house = house
        client.flat = flat
        client.telegram_link = telegram_link
        client.skype = skype
        client.email = email
        client.link_linkedin = link_linkedin
        client.state = state
        client.save()
    """ Сохранение телефонных номеров клиента """
    tel = request.POST.getlist('phone')
    if any(tel):
        Telephone.objects.filter(client_phone=client_instance).delete()
    for t in tel:
        t = check_phone(t)
        if t:
            phone = Telephone(
                client_phone=client,
                telephone_number=t,
            )
            phone.save()
Ejemplo n.º 3
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))