-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopulate_voter.py
157 lines (144 loc) · 6.34 KB
/
populate_voter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# import mysql.connector
import sqlite3
import json
import os
from app import bcrypt
# mydb = mysql.connector.connect(
# host="localhost",
# user="root",
# passwd="",
# database="election"
# )
'''
my_cursor = mydb.cursor()
voter_id = 0
file_list = ['BAGG.txt', 'BMBT.txt', 'BNGA.txt',
'CEMA.txt', 'CMSA.txt', 'ECOA.txt',
'ENGA.txt', 'MCBA.txt', 'MCVA.txt',
'MMFI.txt', 'MTMA.txt', 'PHSA.txt',
'PLSA.txt', 'SOCA.txt', 'STSA.txt',
'BOBA.txt', 'CMEA.txt', 'CMSL.txt',
'COMA.txt', 'EDUC.txt', 'HISA.txt',
'MCMF.txt', 'MCMM.txt', 'MCMS.txt',
'MMCB.txt', 'MPHY.txt']
abs_path = os.path.dirname(os.path.abspath(__file__))
for file in file_list:
current_file = abs_path + '/student_data/' + file
with open(current_file, 'r') as f:
for line in f:
voter_id += 1
record = json.loads(line)
name = record["Name"]
roll = record["Roll"]
current_year = record["Current Year"]
registration_number = record["Registration Number"]
department = record["Department"]
# record = line[1:-2].split(', ')
# name = record[0][record[0].find(':') + 1 : ]
# roll = record[1][record[1].find(':') + 1 : ]
# current_year = record[2][record[2].find(':') + 1 : ]
# registration_number = record[3][record[3].find(':') + 1 : ]
# department = record[4][record[4].find(':') + 1 : ][2:-1]
# print(name, roll, current_year, registration_number, department)
# print(department)
# input()
my_cursor.execute("INSERT INTO voter (voter_id, voter_cin, voter_name, dept_code) VALUES (%s, %s, %s, %s)", (voter_id, roll, name, department))
mydb.commit()
print("Success, total voters: ", voter_id)
'''
def populate_table():
file_list = ['BAGG.txt', 'BMBT.txt', 'BNGA.txt',
'CEMA.txt', 'CMSA.txt', 'ECOA.txt',
'ENGA.txt', 'MCBA.txt', 'MCVA.txt',
'MMFI.txt', 'MTMA.txt', 'PHSA.txt',
'PLSA.txt', 'SOCA.txt', 'STSA.txt',
'BOBA.txt', 'CMEA.txt', 'CMSL.txt',
'COMA.txt', 'EDUC.txt', 'HISA.txt',
'MCMF.txt', 'MCMM.txt', 'MCMS.txt',
'MMCB.txt', 'MPHY.txt']
try:
sqliteConnection = sqlite3.connect('app/site.db')
cursor = sqliteConnection.cursor()
print("Successfully Connected to SQLite")
abs_path = os.path.dirname(os.path.abspath(__file__))
for file in file_list:
current_file = abs_path + '/student_data/' + file
# print(current_file)
with open(current_file, 'r') as f:
for line in f:
record = json.loads(line)
name = record["Name"]
cin = record["Roll"]
email = cin + '@email.com'
# current_year = record["Current Year"]
# registration_number = record["Registration Number"]
dept = record["Department"]
imagefile = 'default.jpg'
# password = 'password'
password = bcrypt.generate_password_hash('password').decode('utf-8')
join_year = 2016
is_admin = False
item = [cin, name, email, dept, imagefile, password, join_year, is_admin]
# print(item)
cursor.execute("INSERT INTO voter (cin, name, email, dept, imagefile, password, join_year, is_admin) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", item)
sqliteConnection.commit()
print("Successfully populated database!")
cursor.close()
except sqlite3.Error as error:
print("Error while connecting to sqlite", error)
finally:
if sqliteConnection:
sqliteConnection.close()
print("The SQLite connection is closed")
def update_password():
file_list = ['BAGG.txt', 'BMBT.txt', 'BNGA.txt',
'CEMA.txt', 'CMSA.txt', 'ECOA.txt',
'ENGA.txt', 'MCBA.txt', 'MCVA.txt',
'MMFI.txt', 'MTMA.txt', 'PHSA.txt',
'PLSA.txt', 'SOCA.txt', 'STSA.txt',
'BOBA.txt', 'CMEA.txt', 'CMSL.txt',
'COMA.txt', 'EDUC.txt', 'HISA.txt',
'MCMF.txt', 'MCMM.txt', 'MCMS.txt',
'MMCB.txt', 'MPHY.txt']
try:
sqliteConnection = sqlite3.connect('app/site.db')
cursor = sqliteConnection.cursor()
print("Successfully Connected to SQLite")
abs_path = os.path.dirname(os.path.abspath(__file__))
for file in file_list:
current_file = abs_path + '/student_data/' + file
# print(current_file)
with open(current_file, 'r') as f:
for line in f:
record = json.loads(line)
# name = record["Name"]
cin = record["Roll"]
# email = cin + '@email.com'
# current_year = record["Current Year"]
# registration_number = record["Registration Number"]
# dept = record["Department"]
# imagefile = 'default.jpg'
# password = 'password'
password = bcrypt.generate_password_hash('password').decode('utf-8')
# join_year = 2016
# is_admin = False
item = [password, cin]
# print(item)
cursor.execute("UPDATE voter SET password=? WHERE cin=?", item)
print(file,"parsed and records updated!")
sqliteConnection.commit()
print("Successfully updated database!")
cursor.close()
except sqlite3.Error as error:
print("Error while connecting to sqlite", error)
finally:
if sqliteConnection:
sqliteConnection.close()
print("The SQLite connection is closed")
update_password()
# print(file_content[0].split(', ')[0][1:].index(": ") + 1)
# print(file_content[0].split(', ')[1])
# print(file_content[0].split(', ')[2])
# print(file_content[0].split(', ')[3])
# print(file_content[0].split(', ')[4])
#['"Name": "Brandon Chater"', '"Roll": "2-09-16-0385"', '"Current Year": "2"', '"Registration Number": "A01-1112-0361-16"', '"Department": "BAGG"']