-
Notifications
You must be signed in to change notification settings - Fork 0
/
userops.py
256 lines (223 loc) · 8.38 KB
/
userops.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
253
import pymongo
import whirlpool
import logging
import datetime
import os
import uuid
from urlvalidation import validate_url
logger = logging.getLogger(__file__)
dbconn = pymongo.Connection()
db = dbconn['slshr']
#TODO better table name
coll = db['realusers']
db = dbconn['list']
#schema: _id: default, author:string
playlistcol = db['playlists']
#schema: _id: username, hash: string, salt: string
usercol = db['users']
#stupid col, probably should not even exist...
playcol = db['playlists']
#schema: _id: username, followts = timestamp followers = [{ username: username, followts: timestamp }] #TODO check this schema
followerscol = db['followers']
#schema: _id: username, following = [{ username: username, followts: timestamp }]
followingcol = db['following']
#schema: _id default, url: string, headline: string, insertts: timestamp
#thats schema for the playlist where the collection name is the curator
def usercheck(creds, request):
return credcheck(creds['login'], creds['password'])
def credcheck(login, password):
#first lookup in db and validate
cursor = coll.find({"_id" : login.lower()})
if cursor.count() == 0:
logger.info("looked up " + login + ", found 0 users")
return False
if cursor.count() > 1:
logger.warning("looked up " + login + ", found multiple users!")
return False
userdoc = cursor[0]
#now generate the hash
wp = whirlpool.new("" + password + userdoc['salt'])
passhash = wp.hexdigest()
if passhash == userdoc['hash']:
return True
return False
def gensalt():
return os.urandom(512).encode('base64')#length of the hash output i think...
#confirms there is a user is usercol that has this username
def user_exists(username):
cursor = coll.find({'_id': username})
if cursor.count() == 0:
return False
elif cursor.count() > 1:
logger.error('BAD: there is are multiple users named: ' + username)
#TODO do we want to throw an exception here?
return True
else:
return True
def create_new_user(username, userpass):
#TODO do I need this checks?
#check for valid
if username is None or userpass is None:
return False
if user_exists(username):
return False
#generate hash
salt = gensalt()
wp = whirlpool.new("" + userpass + salt)
passhash = wp.hexdigest()
#insert into db
userdoc = {'_id': username.lower()}
userdoc['hash'] = passhash
userdoc['salt'] = salt
userdoc['signupts'] = datetime.datetime.utcnow()
userdoc['following'] = []
userdoc['followers'] = []
userdoc['links'] = []
coll.insert(userdoc)
return True
def get_all_playlists():
playlists = []
for user in coll.find():
playlists.append(user)
return playlists
#NOTE: assumes that the user exists and username is not None
def get_user_articles(username):
user = coll.find({'_id': username})[0]
articles = user['links']
articles.sort(key=lambda article: article['timestamp'])
articles.reverse()
return articles
#need to pass error up here
#NOTE: assumes that the user is already verified
def insert_user_article(username, headline, url):
if headline is not None and url is not None:
if validate_url(url):
user = coll.find({'_id': username})[0]
ts = datetime.datetime.utcnow()
id = uuid.uuid4()
newarticle = {'url': url, 'headline': headline, 'timestamp': ts, "id": id}
user['links'].append(newarticle)
coll.update({'_id': username}, user)
return newarticle
return None
#NOTE: assumes that the user is already verified and targetentry is not None
def delete_user_article_by_id(username, targetid):
logger.info("in delete_user_article_by_id")
logger.debug(targetid)
targetuuid = uuid.UUID(targetid)
articles = get_user_articles(username)
for i, article in enumerate(articles):
if "id" in article.keys() and article['id'] == targetuuid:
logger.debug("found article to delete")
del articles[i]
break
coll.update({"_id": username}, {"$set": {"links": articles}})
def delete_user_article_by_idx(username, targetidx):
logger.info("in delete_user_article_by_idx")
#first get an ordered list of the users articles
articles = get_user_articles(username)
#now remove the deleted element
del articles[targetidx]
#call update
coll.update({"_id": username}, {"$set": {"links": articles}})
#NOTE: assumes these are valid users
def follows(follower, followee):
#first get following and followers
following = get_user_following(follower)
followers = get_user_followers(followee)
alreadyfollowing = False
alreadyfollowed = False
for user in following:
if user['username'] == followee:
alreadyfollowing = True
break
for user in followers:
if user['username'] == follower:
alreadyfollowed = True
#if alreadyfollowing != alreadyfollowed
if alreadyfollowing != alreadyfollowed:
logger.error('users are not in same following/followed state: ' + follower + ' and ' + followee)
return False
if alreadyfollowing:
return True
return False
#get all the follows for a user
#NOTE: assumes the user exists and is valid
def get_user_following(username):
cursor = coll.find({'_id': username})
count = cursor.count()
following = []
if count == 0:
logger.error('no following entry for ' + username)
elif count == 1:
following = cursor[0]['following']
else:
logger.error('got more than one use in followcoll in followers_view')
#TODO is this the right behavior?
following = cursor[0]['following']
return following
#get all the follows for a user
#NOTE: assumes the user exists and is valid
def get_user_followers(username):
cursor = coll.find({'_id': username})
count = cursor.count()
followers = []
if count == 1:
followers = cursor[0]['followers']
elif count > 1:
logger.error('got more than one use in followercol in get_user_followers')
followers = cursor[0]['followers']
else:
logger.error('no followers entry for ' + username)
return followers
#NOTE: assumes both users exist are are valid
def new_follow(follower, followee):
followcursor = coll.find({'_id': followee})
followcount = followcursor.count()
if followcount == 0:
#TODO should this be an exception?
logger.error('no entry in followers for user ' + followee)
return False
if followcount > 1:
looger.error('followcol has multiple users with name ' + followee)
return False
followingcursor = coll.find({'_id': follower})
followingcount = followingcursor.count()
if followingcount == 0:
logger.error('no entry in followers for user ' + username)
return False
if followcount > 1:
looger.error('followcol has multiple users with name ' + followee)
return False
#return false if already follows
if follows(follower, followee):
return False
#both are the right count
ts = datetime.datetime.utcnow()
coll.update({'_id': followee}, {"$push" : { "followers": { "username": follower, "followts": ts}}})
coll.update({'_id': follower}, {"$push": { "following": { "username": followee, "followts": ts}}})
#NOTE: assumes that both users exist and are valid
def unfollow(follower, followee):
followcursor = coll.find({'_id': followee})
followcount = followcursor.count()
if followcount == 0:
#TODO should this be an exception?
logger.error('no entry in followers for user ' + followee)
return False
if followcount > 1:
logger.error('followcol has multiple users with name ' + followee)
return False
followingcursor = coll.find({'_id': follower})
followingcount = followingcursor.count()
if followingcount == 0:
logger.error('no entry in followers for user ' + username)
return False
if followcount > 1:
logger.error('followcol has multiple users with name ' + followee)
return False
if not follows(follower, followee):
logger.info(follower + " does not follow " + followee)
return False
coll.update({'_id': followee}, {"$pull" : { "followers": { "username": follower}}})
coll.update({'_id': follower}, {"$pull": { "following": { "username": followee}}})
return True