-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.py
160 lines (140 loc) · 5.58 KB
/
db.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
158
159
160
#!/usr/bin/python
'''
SMS Attendance
Copyright (C) 2010-2012 jishnu7@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import MySQLdb
import sys
import datetime
DBHOST = "localhost"
DBUSER = " "
DBPASS = " "
DB = " "
class database():
def __init__(self):
try:
self.db = MySQLdb.connect(host=DBHOST, \
user=DBUSER, \
passwd=DBPASS, \
db=DB)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
def delete(self, username):
try:
query = "DELETE FROM users WHERE username = '" \
+ username + "' LIMIT 1"
error = self.db.cursor()
error.execute(query)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
def restore(self, DEL_FILENAME):
# Restore accounts from delete log file
f = open(DEL_FILENAME, 'r')
for line in f.readlines():
if line.split(' DELETE ')[0] == \
str(datetime.date.today().day) + "-" + \
str(datetime.date.today().month):
a = line.split(' DELETE ')[1].split('\n')[0]
account = a.split(' ')[0]
password = account.split('.')[1]
mob = a.split(' ')[1]
try:
cursor = self.db.cursor()
cursor.execute("SELECT `username` FROM `users` " \
"WHERE `username`='" + account + "' " \
"AND `mob`='" + mob + "'")
rows = int(cursor.rowcount)
if rows > 0:
continue
cursor.execute("INSERT INTO `users` " \
"(`username` ,`passwd` , `mob`) " \
"VALUES ( '" + account + "', '" \
+ password + "', '" + mob + "')")
print "RESTORE DELETED ACCOUNTS " \
+ account + '--' + password + '--' + mob
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
continue
# Empty the file
open(DEL_FILENAME, 'w').close()
def fetch(self, start, limit):
users = []
end = limit
i = 0
while True:
cursor = self.db.cursor()
query = "SELECT `username`, `passwd`, `sem`, `mob` " \
"FROM `users` WHERE `disabled`=0 ORDER BY `id` ASC "
if limit != 0:
query = query + " LIMIT " + str(start) + ',' + str(end)
try:
cursor.execute(query)
rows = int(cursor.rowcount)
print start, end, rows
for x in range(0, rows):
i += 1
row = cursor.fetchone()
print i, x + 1, row[0], row[1], row[2], row[3]
users.append(row[0], row[1], row[2], row[3])
if i >= limit:
break
if rows < limit:
start = 0
end = limit - rows
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
return users, end
def fetch_one(self, num=0):
print num
cursor = self.db.cursor()
query = "SELECT `username`, `passwd`, `sem`, `mob` " \
"FROM `users` WHERE `disabled`=0 ORDER BY `id` ASC "
if num != 0:
query = query + " LIMIT " + str(num) + ',1'
try:
cursor.execute(query)
row = cursor.fetchone()
# username, password, semester, mobile
return row[0], row[1], row[2], row[3]
except:
return None
def update_check(self, username, date):
# Check whether there is an update after last access
print "Update check : ", username, date
cursor = self.db.cursor()
query = "SELECT `mob` FROM `users` " \
"WHERE ( `last_update` < '" + date + "' " \
"OR `last_update` IS NULL ) " \
"AND `username` = '" + username + "' LIMIT 1"
try:
cursor.execute(query)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
rows = int(cursor.rowcount)
if rows > 0:
return True
else:
print "No update"
return False
def update(self, username, date):
# Update last access date in user account
cursor = self.db.cursor()
query = "UPDATE `users` SET `last_update`='" + date + "' "\
"WHERE `username`='" + username + "' LIMIT 1"
try:
cursor.execute(query)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
return