def replycomplain(request): if Complain.objects.filter(viewed=False, replied=False): allcomplain=Complain.objects.filter(viewed=False, replied=False) return render(request, 'admin/replycomplain.html', {'complain': allcomplain,'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request,'No New Complains') return render(request, 'admin/viewcomplain.html',{'username':adminhome.username,'image':adminhome.adminimage})
def modifyelection(request): if Election.objects.filter(status='active'): e=Election.objects.filter(status='active') return render(request, 'admin/modifyelection.html', {'elections': e,'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request, 'No elections running') return render(request, 'admin/modifyelection.html',{'username':adminhome.username,'image':adminhome.adminimage})
def editcandidatedetails(request): if request.method == 'POST': candidate_id = request.POST['cid'] name = request.POST['name'] father_name = request.POST['fname'] address = request.POST['address'] mobile_no = request.POST['mno'] state = request.POST['state'] pincode = request.POST['pincode'] constituency = request.POST['Constituency'] if constituency == "Parliamentary": parliamentary = request.POST['Constituency1'] else: assembly = request.POST['Constituency1'] candidate_party = request.POST['party'] party_image = request.FILES['pphoto'] ecandidate = Candidates.objects.get(candidate_id=candidate_id) ecandidate.name = name ecandidate.father_name = father_name ecandidate.address = address ecandidate.mobile_no = mobile_no ecandidate.state = state ecandidate.pincode = pincode ecandidate.constituency = constituency if constituency == "Parliamentary": ecandidate.parliamentary = parliamentary else: ecandidate.assembly = assembly ecandidate.candidate_party=candidate_party ecandidate.party_image=party_image ecandidate.save() messages.info(request, 'candidate details updated') return render(request, 'admin/edit candidate.html',{'username':adminhome.username,'image':adminhome.adminimage})
def login(request): if (request.method == 'POST'): username = request.POST['username'] password = request.POST['password'] loginas = request.POST['loginas'] if loginas == "voter": user = auth.authenticate(username=username, password=password) if user is not None and user.is_superuser == False: auth.login(request, user) request.session['v_id'] = username return redirect('vhome') else: messages.info(request, 'Invalid Credentials') return render(request, 'index.html') elif loginas == "admin": user = auth.authenticate(username=username, password=password) if user is not None and user.is_superuser == True: auth.login(request, user) request.session['admin_id'] = username return redirect('adminhome') else: messages.info(request, 'Invalid Credentials') return render(request, 'index.html') else: return render(request, 'index.html')
def modify_election(request): if request.method == "POST": election_id=request.POST['eno'] end_date=request.POST['edate'] end_time=request.POST['etime'] modifyele=Election.objects.get(election_id=election_id) modifyele.end_date=end_date modifyele.end_time=end_time modifyele.save() messages.info(request,'Election Modified') return render(request,'admin/modifyelection.html',{'username':adminhome.username,'image':adminhome.adminimage})
def setnewpassword(request): if request.method == "POST": password1 = request.POST['password1'] password2 = request.POST['password2'] if password1 == password2: u = User.objects.get(username=forgot_password.voter_id) if u is not None: u.set_password(password1) u.save() messages.info(request, 'New password updated') return render(request, 'index.html')
def forgotpassotp(request): if (request.method == "POST"): userotp = request.POST['otp'] url = "http://2factor.in/API/V1/8f35e7b6-6699-11ea-9fa5-0200cd936042/SMS/VERIFY/" + request.session[ 'otp_session_data'] + "/" + userotp response = requests.request("GET", url) data = response.json() if data['Status'] == "Success": return render(request, 'newpassword.html') else: messages.info(request, 'Invalid OTP') return render(request, 'forgotpassotp.html')
def complete_election(request): if request.method == 'POST': election_id = request.POST['eno'] status = 'active' e = Election.objects.get(election_id=election_id, status=status) if e is not None: e.status = 'not active' e.save() messages.info(request, 'Election Completed') return render(request, 'admin/completeelection.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request, 'Election already completed') return render(request, 'admin/completeelection.html',{'username':adminhome.username,'image':adminhome.adminimage})
def submitevmvote(request): candidate_id = request.POST.getlist('candidateid') votes = request.POST.getlist('votes') j = 0 for i in candidate_id: v = Votes.objects.get(election_id=viewcandidatesforaddvote.election_id, candidate_id=i) v.evm_votes += int(votes[j]) v.save() j += 1 v = Votes.objects.all() for k in v: k.total_votes = k.online_votes + k.evm_votes k.save() messages.info(request, 'EVM Vote submitted') return render(request, 'admin/addevmvotes.html',{'username':adminhome.username,'image':adminhome.adminimage})
def register(request): if request.method == "POST": form = UserCreationForm(request.POST) if form.is_valid(): user = form.save() username = form.cleaned_data.get('username') messages.success(request, f"Llogaria e re eshte krijuar:{username}") login(request, user) messages.info(request, f"Ju jeni te loguar si {username} ") return redirect("register") else: for msg in form.error_messages: messages.error(request, f"{msg}: {form.error_messages[msg]}") form = UserCreationForm return render(request, "identifikim/register.html", context={"form": form})
def login(request): if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] user = auth.authenticate(username=username, password=password) if user is not None: auth.login(request, user) print('c bon') return redirect('/') else: messages.info(request, 'invalid credentials') return redirect('$/affaires/affaires') else: return render(request, 'login.html')
def forgot_password(request): if request.method == "POST": forgot_password.voter_id = request.POST['vid'] voter = User.objects.get(username=forgot_password.voter_id) if voter is not None: v = Voters.objects.get(voterid_no=forgot_password.voter_id) vmobno = str(v.mobile_no) url = "http://2factor.in/API/V1/8f35e7b6-6699-11ea-9fa5-0200cd936042/SMS/" + vmobno + "/AUTOGEN" response = requests.request("GET", url) data = response.json() request.session['otp_session_data'] = data['Details'] messages.info( request, 'an OTP has been sent to registered mobile number ending with') mobno = vmobno[6:] return render(request, 'forgotpassotp.html', {'mno': mobno}) else: messages.info(request, 'Invalid Voter ID') return render(request, 'forgotpassword.html')
def login_request(request): if request.method == 'POST': form = AuthenticationForm(request=request, data=request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(username=username, password=password) if user is not None: login(request, user) messages.info(request, f"You are now logged in as {username}") return redirect('/') else: messages.error(request, "Invalid username or password.") else: messages.error(request, "Invalid username or password.") form = AuthenticationForm() return render(request=request, template_name="identifikim/login.html", context={"form": form})
def view_candidate(request): if request.method == 'POST': state = request.POST['states'] constituency1 = request.POST['constituency1'] constituency2 = request.POST['constituency2'] if constituency1 == 'Parliamentary': candidates = Candidates.objects.filter(state=state, constituency=constituency1, parliamentary=constituency2) if candidates: return render(request, 'admin/view candidate.html', {'constituency':constituency2,'candidates': candidates,'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request, 'No Candidate Found') return render(request, 'admin/view candidate.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: candidates = Candidates.objects.filter(state=state, constituency=constituency1, assembly=constituency2) if candidates: return render(request, 'admin/view candidate.html', {'constituency':constituency2,'candidates': candidates,'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request, 'No Candidate Found') return render(request, 'admin/view candidate.html',{'username':adminhome.username,'image':adminhome.adminimage})
def UserLogin(request): if request.user.is_authenticated: return redirect('hotel:start') else: if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') user = authenticate(request, username=username, password=password) if user is not None: login(request, user) userdata = UserProfile.objects.get(user=user) print(userdata) return redirect('hotel:start') else: messages.info(request, 'Username or Password is incorrect') return render(request, 'hotel/login.html')
def add_candidate(request): if request.method == 'POST': candidate_id = request.POST['cid'] name = request.POST['name'] father_name = request.POST['fname'] gender = request.POST['gender'] dateofbirth = request.POST['dob'] address = request.POST['address'] mobile_no = request.POST['mno'] state = request.POST['state'] pincode = request.POST['pincode'] constituency = request.POST['Constituency'] if constituency == "Parliamentary": parliamentary = request.POST['Constituency1'] else: assembly = request.POST['Constituency1'] candidate_party = request.POST['party'] candidate_image = request.FILES['cphoto'] party_image = request.FILES['pphoto'] affidavit = request.FILES['affidavit'] if (Candidates.objects.filter(candidate_id=candidate_id).exists()): messages.info(request, 'candidate id already registered') return render(request, 'admin/add candidate.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: if constituency == "Parliamentary": add_candidate = Candidates(candidate_id=candidate_id, name=name, father_name=father_name, gender=gender, dateofbirth=dateofbirth, address=address, mobile_no=mobile_no, state=state, pincode=pincode, constituency=constituency, parliamentary=parliamentary, candidate_party=candidate_party, candidate_image=candidate_image, party_image=party_image,affidavit=affidavit) add_candidate.save() else: add_candidate = Candidates(candidate_id=candidate_id, name=name, father_name=father_name, gender=gender, dateofbirth=dateofbirth, address=address, mobile_no=mobile_no, state=state, pincode=pincode, constituency=constituency, assembly=assembly, candidate_party=candidate_party, candidate_image=candidate_image, party_image=party_image,affidavit=affidavit) add_candidate.save() messages.info(request, 'candidate added') return render(request, 'admin/add candidate.html',{'username':adminhome.username,'image':adminhome.adminimage})
def register(request): print('c bon') if request.method == 'POST': first_name = request.POST['first_name'] last_name = request.POST['last_name'] username = request.POST['username'] email = request.POST['email'] password1 = request.POST['password1'] password2 = request.POST['password2'] if password1 == password2: if User.objects.filter(username=username).exists(): messages.info(request, 'Username Taken') return redirect('register') elif User.objects.filter(email=email).exists(): messages.info(request, 'email Taken') return redirect('register') else: user = User.objects.create_user(first_name=first_name, last_name=last_name, username=username, email=email, password=password1) user.save() print('user created') return redirect('login') else: messages.info(request, 'password not matching...') return redirect('register') return redirect('/') else: return render(request, 'register.html')
def edit_voter(request): if request.method == 'POST': if request.POST.get('editvoter'): voter_id = request.POST['vid'] if Voters.objects.filter(voterid_no=voter_id): voter = Voters.objects.get(voterid_no=voter_id) return render(request, 'admin/edit voterdetails.html', {'voterid_no': voter.voterid_no, 'name': voter.name, 'father_name': voter.father_name, 'gender': voter.gender, 'dateofbirth': voter.dateofbirth, 'address': voter.address, 'mobile_no': voter.mobile_no, 'state': voter.state, 'pincode': voter.pincode, 'parliamentary': voter.parliamentary, 'assembly': voter.assembly, 'voter_image': voter.voter_image,'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request,'Voter not found') return render(request,'admin/edit voter.html',{'username':adminhome.username,'image':adminhome.adminimage}) elif request.POST.get('deletevoter'): voter_id = request.POST['vid'] if Voters.objects.filter(voterid_no=voter_id): Voters.objects.get(voterid_no=voter_id).delete() User.objects.get(username=voter_id).delete() messages.info(request, 'Voter deleted') return render(request, 'admin/edit voter.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request,'Voter not found') return render(request,'admin/edit voter.html',{'username':adminhome.username,'image':adminhome.adminimage})
def editvoterdetails(request): if request.method == 'POST': voterid_no = request.POST['vid'] name = request.POST['name'] father_name = request.POST['fname'] address = request.POST['address'] mobile_no = request.POST['mno'] state = request.POST['state'] pincode = request.POST['pincode'] parliamentary = request.POST['ParliamentaryConstituency'] assembly = request.POST['AssemblyConstituency'] evoter = Voters.objects.get(voterid_no=voterid_no) evoter.name = name evoter.father_name = father_name evoter.address = address evoter.mobile_no = mobile_no evoter.state = state evoter.pincode = pincode evoter.parliamentary = parliamentary evoter.assmebly = assembly evoter.save() messages.info(request, 'voter details updated') return render(request, 'admin/edit voter.html',{'username':adminhome.username,'image':adminhome.adminimage})
def add_voter(request): if (request.method == 'POST'): voterid_no = request.POST['vid'] name = request.POST['name'] father_name = request.POST['fname'] gender = request.POST['gender'] dateofbirth = request.POST['dob'] address = request.POST['address'] mobile_no = request.POST['mno'] state = request.POST['state'] pincode = request.POST['pincode'] parliamentary = request.POST['ParliamentaryConstituency'] assembly = request.POST['AssemblyConstituency'] voter_image = request.FILES['vphoto'] if (Voters.objects.filter(voterid_no=voterid_no).exists()): messages.info(request, 'voter id already registered') return render(request, 'admin/addvoter.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: add_voter = Voters(voterid_no=voterid_no, name=name, father_name=father_name, gender=gender, dateofbirth=dateofbirth, address=address, mobile_no=mobile_no, state=state, pincode=pincode, parliamentary=parliamentary, assembly=assembly, voter_image=voter_image) add_voter.save() messages.info(request, 'voter added') return render(request, 'admin/addvoter.html',{'username':adminhome.username,'image':adminhome.adminimage})
def generate_election(request): if request.method == 'POST': election_id = request.POST['eno'] election_type = request.POST['electiontype'] state = request.POST['state'] start_date = request.POST['sdate'] start_time = request.POST['stime'] end_date = request.POST['edate'] end_time = request.POST['etime'] status = 'active' if (Election.objects.filter(election_id=election_id).exists()): messages.info(request, 'election id already registered') return render(request, 'admin/generateelection.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: generate_election = Election(election_id=election_id, election_type=election_type, state=state, start_date=start_date, start_time=start_time, end_date=end_date, end_time=end_time, status=status) generate_election.save() if election_type == 'PC-GENERAL': c = Candidates.objects.filter(state=state, constituency='Parliamentary') for i in c: candidates_votes = Votes(election_id=election_id, candidate_id=i.candidate_id, candidate_name=i.name, candidate_party=i.candidate_party, state=i.state, constituency=i.parliamentary) candidates_votes.save() has_voted = 'no' #Voted.objects.filter(state=state).delete() v = Voters.objects.filter(state=state) for i in v: voters_voted = Voted(election_id=election_id, voter_id=i.voterid_no, state=i.state, constituency=i.parliamentary, has_voted=has_voted) voters_voted.save() messages.info(request, 'election generated') return render(request, 'admin/generateelection.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: c = Candidates.objects.filter(state=state, constituency='Assembly') for i in c: candidates_votes = Votes(election_id=election_id, candidate_id=i.candidate_id, candidate_name=i.name, candidate_party=i.candidate_party, state=i.state, constituency=i.parliamentary) candidates_votes.save() has_voted = 'no' #Voted.objects.filter(state=state).delete() v = Voters.objects.filter(state=state) for i in v: voters_voted = Voted(election_id=election_id, voter_id=i.voterid_no, state=i.state, constituency=i.assembly, has_voted=has_voted) voters_voted.save() messages.info(request, 'election generated') return render(request, 'admin/generateelection.html',{'username':adminhome.username,'image':adminhome.adminimage})
def votervotesub(request): if request.method == 'POST': election_id = request.POST['eid'] vid = request.POST['vid'] if (Voted.objects.filter(election_id=election_id, voter_id=vid).exists()): v = Voted.objects.get(election_id=election_id, voter_id=vid) if v.has_voted == 'no': v.has_voted = 'yes' v.where_voted = 'offline' v.datetime = datetime.datetime.now() v.save() messages.info(request, 'Vote submitted') return render(request, 'admin/voting.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request, 'Already voted') return render(request, 'admin/voting.html',{'username':adminhome.username,'image':adminhome.adminimage, 'where':v.where_voted}) else: messages.info(request, 'voter not found') return render(request, 'admin/voting.html',{'username':adminhome.username,'image':adminhome.adminimage})
def achange_password(request): if request.method == "POST": a_id = request.session['admin_id'] oldpass = request.POST['oldpass'] newpass = request.POST['password1'] newpass2 = request.POST['password2'] u=auth.authenticate(username=a_id,password=oldpass) if u is not None: u=User.objects.get(username=a_id) if oldpass!=newpass: if newpass == newpass2: u.set_password(newpass) u.save() messages.info(request, 'Password Changed') return render(request, 'admin/achangepassword.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request, 'New password is same as old password') return render(request, 'admin/achangepassword.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request, 'Old Password not matching') return render(request, 'admin/achangepassword.html',{'username':adminhome.username,'image':adminhome.adminimage})
def edit_candidate(request): if request.method == 'POST': if request.POST.get('editcandidate'): candidate_id = request.POST['cid'] if Candidates.objects.filter(candidate_id=candidate_id): candidate = Candidates.objects.get(candidate_id=candidate_id) if candidate.constituency == "Parliamentary": return render(request, 'admin/edit candidatedetails.html', {'candidate_id': candidate.candidate_id, 'name': candidate.name, 'father_name': candidate.father_name, 'gender': candidate.gender, 'dateofbirth': candidate.dateofbirth, 'address': candidate.address, 'mobile_no': candidate.mobile_no, 'state': candidate.state, 'pincode': candidate.pincode, 'constituency': candidate.constituency, 'parliamentary': candidate.parliamentary, 'candidate_image': candidate.candidate_image, 'candidate_party': candidate.candidate_party, 'party_image': candidate.party_image,'affidavit':candidate.affidavit,'username':adminhome.username,'image':adminhome.adminimage}) else: return render(request, 'admin/edit candidatedetails.html', {'candidate_id': candidate.candidate_id, 'name': candidate.name, 'father_name': candidate.father_name, 'gender': candidate.gender, 'dateofbirth': candidate.dateofbirth, 'address': candidate.address, 'mobile_no': candidate.mobile_no, 'state': candidate.state, 'pincode': candidate.pincode, 'constituency': candidate.constituency, 'parliamentary': candidate.parliamentary, 'candidate_image': candidate.candidate_image, 'candidate_party': candidate.candidate_party, 'party_image': candidate.party_image,'affidavit':candidate.affidavit,'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request, 'Candidate not found') return render(request, 'admin/edit candidate.html',{'username':adminhome.username,'image':adminhome.adminimage}) elif request.POST.get('deletecandidate'): candidate_id = request.POST['cid'] if Candidates.objects.filter(candidate_id=candidate_id): candidate = Candidates.objects.get(candidate_id=candidate_id).delete() messages.info(request, 'Candidate deleted') return render(request, 'admin/edit candidate.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request, 'Candidate not found') return render(request,'admin/edit candidate.html',{'username':adminhome.username,'image':adminhome.adminimage})
def generate_report(request): election_id = request.POST['eno'] state = request.POST['states'] constituency = request.POST['constituency2'] if Voted.objects.filter(election_id=election_id, state=state, constituency=constituency): vvotedall=Voted.objects.filter(election_id=election_id, state=state, constituency=constituency) e_male=0 e_female=0 e_others=0 e_total=0 for i in vvotedall: e_total=e_total+1 for i in vvotedall: vdetail=Voters.objects.get(voterid_no=i.voter_id) if vdetail.gender == 'Male': e_male=e_male+1 elif vdetail.gender == 'Female': e_female=e_female+1 elif vdetails.gender == 'Others': e_others=e_others+1 has_voted='yes' if Voted.objects.filter(election_id=election_id, state=state, constituency=constituency, has_voted=has_voted): vvotedyes=Voted.objects.filter(election_id=election_id, state=state, constituency=constituency, has_voted=has_voted) v_male=0 v_female=0 v_others=0 v_total=0 for i in vvotedyes: v_total=v_total+1 for i in vvotedyes: vdetail=Voters.objects.get(voterid_no=i.voter_id) if vdetail.gender == 'Male': v_male=v_male+1 elif vdetail.gender == 'Female': v_female=v_female+1 elif vdetails.gender == 'Others': v_others=v_others+1 if e_male!=0: p_male=(v_male/e_male)*100 poll_male=float("{:.2f}".format(p_male)) else: p_male=0 poll_male=float("{:.2f}".format(p_male)) if e_female!=0: p_female=(v_female/e_female)*100 poll_female=float("{:.2f}".format(p_female)) else: p_female=0 poll_female=float("{:.2f}".format(p_female)) if e_others!=0: p_others=(v_others/e_others)*100 poll_others=float("{:.2f}".format(p_others)) else: p_others=0 poll_others=float("{:.2f}".format(p_others)) if e_total!=0: p_total=(v_total/e_total)*100 poll_total=float("{:.2f}".format(p_total)) else: p_total=0 poll_total=float("{:.2f}".format(p_total)) report=Reports(election_id=election_id, state=state, constituency=constituency, electors_male=e_male, electors_female=e_female, electors_others=e_others, electors_total=e_total, voters_male=v_male, voters_female=v_female, voters_others=v_others, voters_total=v_total, poll_male=poll_male, poll_female=poll_female, poll_others=poll_others, poll_total=poll_total) report.save() messages.info(request, 'report generated') return render(request, 'admin/generatereport.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request, 'no one voted') return render(request, 'admin/generatereport.html',{'username':adminhome.username,'image':adminhome.adminimage}) else: messages.info(request, 'not found') return render(request, 'admin/generatereport.html',{'username':adminhome.username,'image':adminhome.adminimage})
def logout_request(request): logout(request) messages.info(request, "Logged out successfully!") return redirect('/')