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))
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))