-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
252 lines (201 loc) · 8.78 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import sqlite3
conn = sqlite3.connect("database.db")
curs = conn.cursor()
class User():
def __init__(self, username, password=None, profilepicurl=None, firstname=None, lastname=None, email=None, country=None, sex=None, age=None):
self.username = username
self.password = password
self.profilepicurl = profilepicurl
self.firstname = firstname
self.lastname = lastname
self.email = email
self.country = country
self.sex = sex
self.age = age
@staticmethod
def create(username, password, profilepicurl, firstname, lastname, email, country, sex, age):
curs.execute("INSERT INTO users VALUES (?,?,?,?,?,?,?,?,?)", (username, password, profilepicurl, firstname, lastname, email, country, sex, age))
conn.commit()
print(firstname)
return User(username, password, profilepicurl, firstname, lastname, email, country, sex, age)
@staticmethod
def find(username):
curs.execute("SELECT * FROM users WHERE username = ?", (username,))
result = curs.fetchone()
if result:
username, password, profilepicurl, firstname, lastname, email, country, sex, age = result
return User(username, password, profilepicurl, firstname, lastname, email, country, sex, age)
else:
return None
def getpassword(self):
return self.password
def getprofilepicurl(self):
return self.profilepicurl
def getfirstname(self):
return self.firstname
def getlastname(self):
return self.lastname
def getcountry(self):
return self.country
def getsex(self):
return self.sex
def getemail(self):
return self.email
def setpassword(self, password):
curs.execute("UPDATE users SET password = ? WHERE username = ?", (password, self.username))
conn.commit()
return
def setemail(self, email):
curs.execute("UPDATE users SET email = ? WHERE username = ?", (email, self.username))
conn.commit()
return
def setcountry(self, location):
curs.execute("UPDATE users SET country = ? WHERE username = ?", (country, self.username))
conn.commit()
return
def addfriend(self, friend):
curs.execute("INSERT INTO friends (username, friend) VALUES (?,?)", (self.username, friend))
conn.commit()
return
def delfriend(self, friend):
curs.execute("DELETE FROM friends WHERE username = ? AND friend = ?", (self.username, friend))
conn.commit()
return
def isfriends(self, other_user):
self.listfriends()
friend_names = []
for friend in self.friends:
friend_names.append(friend.username)
if other_user in friend_names:
return True
else:
return False
def listfriends(self):
curs.execute("SELECT friend FROM friends WHERE username = ?", (self.username,))
friends = curs.fetchall()
self.friends = []
for friend in friends:
self.friends.append(User.find(friend[0]))
return self.friends
def exists(self):
result = curs.execute("SELECT COUNT(*) FROM users WHERE username = ?", (self.username,)).fetchone()[0]
if result == 0:
return False
else:
return True
def firstnamesearch(self, firstname):
result = curs.execute("SELECT * FROM users WHERE firstname = ?", (firstname,)).fetchall()
return result
def lastnamesearch(self, lastname):
result = curs.execute("SELECT * FROM users WHERE lastname = ?", (lastname,)).fetchall()
return result
def lastnamesearch(self, lastname):
result = curs.execute("SELECT * FROM users WHERE lastname = ?", (lastname,)).fetchall()
return result
def locationsearch(self, country):
result = curs.execute("SELECT * FROM users WHERE country = ?", (country,)).fetchall()
return result
def get_picture(self):
import hashlib
if self.email:
identifier = self.email
else:
identifier = self.username
hashed = hashlib.md5(identifier.encode("utf-8")).hexdigest()
return "http://www.gravatar.com/avatar/" + hashed + "?d=retro"
def __repr__(self):
return "<User: {}>".format(self.username)
def __str__(self):
return self.__repr__()
class Photo():
def __init__(self, id, latitude, longitude, description, uploader, uploaddate, caption, artist, url):
self.id = id
self.latitude = latitude
self.longitude = longitude
self.description = description
self.uploader = uploader
self.uploaddate = uploaddate
self.caption = caption
self.artist = artist
self.url = url
@staticmethod
def create(uploader, caption, url, latitude=None, longitude=None, description=None, artist=None):
curs.execute("INSERT INTO photos (latitude, longitude, description, uploader, uploaddate, caption, artist, url) VALUES (?,?,?,?,datetime('now'),?,?,?)",
(latitude, longitude, description, uploader, caption, artist, url))
conn.commit()
#curs.execute("SELECT id FROM photos WHERE id == (SELECT max(id) FROM photos)")
curs.execute("SELECT max(id) FROM photos")
id = curs.fetchone()[0]
return Photo.find(id)
@staticmethod
def find(id):
curs.execute("SELECT * FROM photos WHERE id = ?", (id,))
id, latitude, longitude, description, uploader, uploaddate, caption, artist, url = curs.fetchone()
return Photo(id, latitude, longitude, description, uploader, uploaddate, caption, artist, url)
def setprofilepicurl(self, profilepicurl):
curs.execute("UPDATE users SET profilepicurl = ? WHERE id = ?", (profilepicurl, self.id))
conn.commit()
def getlocation(self):
return self.location
def get_uploader(self):
return User.find(self.uploader)
@staticmethod
def getpicsbyusername(uploader):
#print(uploader)
curs.execute("SELECT * FROM photos WHERE uploader = ?", (uploader,))
piclist = []
for i in curs.fetchall():
id, lat, long, description, uploader, uploaddate, caption, artist, url = i
currentpicture = Photo(id, lat, long, description, uploader, uploaddate, caption, artist, url)
piclist.append(currentpicture)
return piclist # This will print in IDLE
@staticmethod
def getallpics(limit = None):
if limit == None:
curs.execute("SELECT * FROM photos ORDER BY uploaddate DESC")
else:
curs.execute("SELECT * FROM photos ORDER BY uploaddate DESC LIMIT " + str(int(limit)))
piclist = []
for i in curs.fetchall():
id, lat, long, description, uploader, uploaddate, caption, artist, url = i
currentpicture = Photo(id, lat, long, description, uploader, uploaddate, caption, artist, url)
piclist.append(currentpicture)
return piclist # This will print in IDLE
def __repr__(self):
return "<Photo: {} ({})>".format(self.caption, self.uploader)
def __str__(self):
return self.__repr__()
## RETURN LIST
## def getpicsurl(self, criteria, order, limit):
## curs.execute("SELECT url FROM photos WHERE ? ORDER BY ? DESC LIMIT ?", criteria, order, limit)
## self.url = curs.fetchall()
## return self.url
class Tag():
def __init__(self, tagid):
self.tagid = tagid
self.tagstring = None
self.photoid = None
self.tagger = None
def create(self, tagid, tagstring, photoid, tagger):
curs.execute("INSERT INTO tags VALUES (?,?,?,?)", (tagid, tagstring, photoid, tagger))
conn.commit()
return
def gettagid(self, tagid):
curs.execute("SELECT tagid FROM tags WHERE tagid = ?", (self.tagid,))
self.tagid = curs.fetchone()[0]
def getalltagstring(self):
pass
def getallphoto(self, tagstring):
pass
##testuser = User("Jess_User")
##print(testuser.getfirstname() + " " + testuser.getlastname())
##print(testuser.getpassword())
##testuser.setpassword("23456")
##print(testuser.getpassword())
if __name__ == "__main__":
testuser = User.find("bkum")
## print(testuser)
## p = Photo.create("Smerity", "lolcats", "0.jpg")
if not User.find("smerity"):
u = User.create("smerity", "smurdy", None, "Stephen", "Merity", "[email protected]", None, "M", 18)
u = User.find("smerity")