def load(request: Request): user = request.user post_id = int(request.inputs['ext'][0]) post = Post.query().select().where('id', post_id).getOne() post_like: Post_like = Post_like.query().select().where( 'post_id', post_id).where('user_id', user.id).getOne() liked = False if post_like is not None: liked = True if post is None: posts = Post.query().select().where('user_id', user.id).get() view = View( 'post.view', { 'user': user, 'posts': posts, 'error': 'Incorrect index position. Please try again.' }, request.json) else: view = View('post.load', { 'user': user, 'post': post, 'whether_post_liked_by_user': liked }, request.json) return Response(ResponseType.valid, view)
def getPosts(self, request): print("Entered Get Posts Portion") temp = request.collegeId temp2 = request.clubId print "temp" + str(temp) print "temp2" + str(temp2) if(temp2==None): print "None" collegeId = ndb.Key('CollegeDb',int(temp)) posts = Post.query(Post.collegeId==collegeId).order(-Post.timestamp) elif(temp==None): print "None" clubId = ndb.Key('Club',int(temp2)) posts = Post.query(Post.club_id==clubId).order(-Post.timestamp) else: print "Not None" collegeId = ndb.Key('CollegeDb',int(temp)) clubId = ndb.Key('Club',int(temp2)) posts = Post.query(Post.collegeId==collegeId,Post.club_id==clubId).order(-Post.timestamp) #pylist =[copyPostToForm(x) for x in posts] #print "the list" #print pylist return Posts(items=list(reversed([copyPostToForm(x) for x in posts])))
def show(request: Request): user = request.user posts = Post.query().select().where('user_id', user.id).get() view = View('post.view', {'user': user, 'posts': posts}, request.json) return Response(ResponseType.valid, view)
def add(request: Request): user: User = request.user title = request.inputs['title'] content = request.inputs['content'] allow_comments = 0 if request.inputs['allow_comments'] == 'n' else 1 visibility = 0 if request.inputs['visibility'] == '0' else 1 tags = request.inputs['tags'] Post.query().insert([ user.id, title, content, allow_comments, visibility, tags, datetime.today(), datetime.today() ]) view = View('home', {'user': user}, request.json) return Response(ResponseType.valid, view)
def add(request: Request): user = request.user post_id = int(request.inputs['ext'][0]) content = request.inputs['content'] Comment.query().insert( [post_id, user.id, content, datetime.today(), datetime.today()]) post = Post.query().select().where('id', post_id).getOne() post_like: Post_like = Post_like.query().select().where( 'post_id', post_id).where('user_id', user.id).getOne() liked = False if post_like is not None: liked = True view = View('post.load', { 'user': user, 'post': post, 'whether_post_liked_by_user': liked }, request.json) return Response(ResponseType.valid, view)
def add_form(request: Request): user = request.user post_id = int(request.inputs['ext'][0]) post = Post.query().select().where('id', post_id).getOne() view = View('comment.add', {'user': user, 'post': post}, request.json) return Response(ResponseType.valid, view)
def show_all(request: Request): user: User = request.user posts = Post.query().select().where('public_visibility', 1).sortBy('created_at', False).get() view = View('post.view', {'user': user, 'posts': posts}, request.json) return Response(ResponseType.valid, view)
def getPosts(self, request): print("Entered Get Posts Portion") temp = request.collegeId temp2 = request.clubId print "temp2" + str(temp2) if(temp2==None): print "None" collegeId = ndb.Key('CollegeDb',int(temp)) posts = Post.query(Post.collegeId==collegeId) elif(temp==None): print "None" clubId = ndb.Key('Club',int(temp2)) posts = Post.query(Post.club_id==clubId) else: print "Not None" collegeId = ndb.Key('CollegeDb',int(temp)) clubId = ndb.Key('Club',int(temp2)) posts = Post.query(Post.collegeId==collegeId,Post.club_id==clubId) return Posts(items=[copyPostToForm(x) for x in posts])
def view(request: Request): user = request.user post_id = int(request.inputs['ext'][0]) post = Post.query().select().where('id', post_id).getOne() comments: List[Comment] = Comment.query().select().where( 'post_id', post_id).sortBy('created_at', False).get() view = View('comment.load', { 'user': user, 'post': post, 'comments': comments }, request.json) return Response(ResponseType.valid, view)
def home(request: Request): loggedUserId: int = request.json['user_id'] user: User = User.query().select().where('id', loggedUserId).getOne() notifications: List[Notification] = Notification.query().select( ).where('is_read', 0).get() posts = Post.query().select().where('user_id', user.id).get() view = View( 'home', { 'user': user, 'notification_count': len(notifications), 'my_post_count': len(posts) }, request.json) return Response(ResponseType.valid, view)
def dontTouch(self, request): pid = ndb.Key('Profile',int(request.pid)) clubId = Club.query(Club.admin==pid) pylist=[] for x in clubId: print x.key posts = Post.query(Post.club_id==x.key).order(-Post.timestamp) events = Event.query(Event.club_id==x.key).order(-Post.timestamp) list1=[] list2=[] list1 = [copyToCollegeFeed(y) for y in events] list2 = [copyToCollegeFeed(z) for z in posts] list1+=list2 print "List-1",list1 pylist+=list1 pylist.sort(key=lambda x: x.timestamp, reverse=True) return CollegeFeed(items=pylist)
def getEvents(self, request): print("Entered Get Events Portion") temp = request.collegeId temp2 = request.clubId print "temp2" + str(temp2) if(temp2==None): print "No CLubId" collegeId = ndb.Key('CollegeDb',int(temp)) events = Event.query(Event.collegeId==collegeId) elif(temp==None): print "No collegeID" clubId = ndb.Key('Club',int(temp2)) events = Event.query(Event.clubId==clubId) else: print "Not None" collegeId = ndb.Key('CollegeDb',int(temp)) clubId = ndb.Key('Club',int(temp2)) events = Post.query(Event.collegeId==collegeId,Event.clubId==clubId) return Events(items=[copyEventToForm(x) for x in events])
def login(request: Request): username = request.json['inputs']['name'] password: str = request.json['inputs']['password'] passHash = hashlib.sha256(password.encode()) passHash = passHash.hexdigest() users: List[User] = User.query().select().where( 'name', username).where('passwordHash', passHash).get() if not users: view = View('', {'error': 'Invalid credentials. Please try again...'}, request.json) return Response(ResponseType.error, view) user = users[0] token = user.hash() User_auth.query().insert( [user.id, token, datetime.today(), datetime.today()]) notifications: List[Notification] = Notification.query().select( ).where('is_read', 0).get() posts = Post.query().select().where('user_id', user.id).get() view = View( 'home', { 'user': user, 'notification_count': len(notifications), 'my_post_count': len(posts) }, { 'user_id': user.id, 'authToken': token }) return Response(ResponseType.valid, view)
def edit_password(request: Request): loggedUserId: int = request.json['user_id'] old_password = request.json['inputs']['old_password'] password = request.json['inputs']['password'] re_password = request.json['inputs']['re_password'] user: User = User.query().select().where('id', loggedUserId).getOne() if password != re_password: view = View('user.edit', {'error': 'passwords do not match'}, request.json) return Response(ResponseType.valid, view) passHash = hashlib.sha256(old_password.encode()) passHash = passHash.hexdigest() if user.passwordHash != passHash: view = View('user.edit', {'error': 'old password is wrong'}, request.json) return Response(ResponseType.valid, view) passHash = hashlib.sha256(password.encode()) passHash = passHash.hexdigest() user.passwordHash = passHash user.save() notifications: List[Notification] = Notification.query().select( ).where('is_read', 0).get() posts = Post.query().select().where('user_id', user.id).get() view = View( 'home', { 'user': user, 'notification_count': len(notifications), 'my_post_count': len(posts) }, request.json) return Response(ResponseType.valid, view)
def like(request: Request): user = request.user post_id = int(request.inputs['ext'][0]) post_like: Post_like = Post_like.query().select().where( 'post_id', post_id).where('user_id', user.id).getOne() liked = False if post_like is None: liked = True Post_like.query().insert( [user.id, post_id, datetime.today(), datetime.today()]) else: post_like.delete() post = Post.query().select().where('id', post_id).getOne() view = View('post.load', { 'user': user, 'post': post, 'whether_post_liked_by_user': liked }, request.json) return Response(ResponseType.valid, view)
def collegeFeed(self, request): print "Entered the Like Posts Section" temp = request.clubId flag =0 pageLimit = 5 skipCount=0 upperBound=pageLimit print request.pageNumber try: skipCount = (int(request.pageNumber)-1)*pageLimit upperBound = int(request.pageNumber)*pageLimit except: print "Didnt give pageNumber-Default to 1" if request.date != None: date = datetime.strptime(getattr(request,"date"),"%Y-%m-%d").date() flag = 1 #print "TYPE-DATE", type(date) if (temp==None): collegeId = ndb.Key('CollegeDb',int(request.collegeId)) posts = Post.query(Post.collegeId==collegeId).order(-Post.timestamp) events = Event.query(Event.collegeId==collegeId).order(-Event.timestamp) print "TEMP IS NONE" else: clubId = ndb.Key('Club',int(request.clubId)) posts = Post.query(Post.club_id==clubId).order(-Post.timestamp) events = Event.query(Event.club_id==clubId).order(-Event.timestamp) print events #CollegeFeed(items=[copyEventToForm(x) for x in posts]) #CollegeFeed(items=[copyEventToForm(x) for x in events]) #pylist = [copyToCollegeFeed(x) for x in events] pylist=[] for x in events: print "TImestamp",type(x.timestamp.strftime("%Y-%m-%d")) if flag==1: if x.timestamp.strftime("%Y-%m-%d") == str(date): pylist.append(copyToCollegeFeed(x)) else: pylist.append(copyToCollegeFeed(x)) print pylist pylist2 = [] for x in posts: print "TImestamp",type(x.timestamp.strftime("%Y-%m-%d")) if flag==1: if x.timestamp.strftime("%Y-%m-%d") == str(date): pylist.append(copyToCollegeFeed(x)) else: pylist.append(copyToCollegeFeed(x)) #pylist2 = [copyToCollegeFeed(x) for x in posts] pylist+=pylist2 #pylist.append(copyToCollegeFeed(x) for x in events) pylist.sort(key=lambda x: x.timestamp, reverse=True) #print pylist[1].timestamp #print pylist #CollegeFeed(items=pylist) #return CollegeFeed(items=[copyToCollegeFeed(x) for x in events]) #return CollegeFeed(items=pylist) finalList = [] for i in xrange(skipCount,upperBound): if(i>=len(pylist)): break finalList.append(pylist[i]) cf = CollegeFeed() cf.items = finalList cf.completed=str(0) if(upperBound>=len(pylist)): cf.completed=str(1) #print pylist[1].timestamp #print pylist CollegeFeed(items=finalList) #return CollegeFeed(items=[copyToCollegeFeed(x) for x in events]) return cf
def personalFeed(self, request): pid = ndb.Key('Profile',int(request.pid)) profile = pid.get() posts = [] events = [] pylist = [] pylist2 = [] pageLimit = 5 skipCount=0 upperBound=pageLimit print request.pageNumber try: skipCount = (int(request.pageNumber)-1)*pageLimit upperBound = int(request.pageNumber)*pageLimit except: print "Didnt give pageNumber-Default to 1" list1=[] list2=[] for x in profile.follows: print x print "LOOP-1" posts = (Post.query(Post.club_id==x)) events = (Event.query(Event.club_id==x)) list1=[] list2=[] #list1 = [copyToCollegeFeed(y) for y in events] #list2 = [copyToCollegeFeed(z) for z in posts] iteration=0 for y in posts: #if(iteration>=skipCount and iteration<upperBound): list1.append(copyToCollegeFeed(y)) #iteration+=1 #if(iteration==upperBound): # break iteration=0 for z in events: #if(iteration>=skipCount and iteration<upperBound): list1.append(copyToCollegeFeed(z)) #iteration+=1 #if(iteration==upperBound): # break print "LIST-1" print list1 pylist+=list1 pylist2+=list2 #for x in events: # print x #pylist = [copyToCollegeFeed(x) for x in events] #pylist2 = [copyToCollegeFeed(x) for x in posts] pylist+=pylist2 #pylist.append(copyToCollegeFeed(x) for x in events) pylist.sort(key=lambda x: x.timestamp, reverse=True) finalList = [] for i in xrange(skipCount,upperBound): if(i>=len(pylist)): break finalList.append(pylist[i]) cf = CollegeFeed() cf.items = finalList cf.completed=str(0) if(upperBound>=len(pylist)): cf.completed=str(1) #print pylist[1].timestamp #print pylist CollegeFeed(items=finalList) #return CollegeFeed(items=[copyToCollegeFeed(x) for x in events]) return cf
def register(request: Request): username = request.json['inputs']['name'] email = request.json['inputs']['email'] password: str = request.json['inputs']['password'] re_password = request.json['inputs']['re_password'] role = 1 if request.json['inputs']['role'] == '1' else 2 if len(password) < 8: view = View( '', objects={'error': 'Password too short. Please try again...'}) return Response(ResponseType.error, view) if password != re_password: view = View('', objects={ 'error': 'Passwords do not match. Please try again...' }) return Response(ResponseType.error, view) if '@' not in email or '.' not in email: view = View('', objects={ 'error': 'Email format is wrong. Please try again...' }) return Response(ResponseType.error, view) user = User.query().select().where('name', username).getOne() if user is not None: view = View('', objects={ 'error': 'Username already taken. Please try again...' }) return Response(ResponseType.error, view) passHash = hashlib.sha256(password.encode()) passHash = passHash.hexdigest() User.query().insert([ username, email, '', passHash, role, datetime.today(), datetime.today() ]) user: User = User.query().select().where('name', username).getOne() token = user.hash() User_auth.query().insert( [user.id, token, datetime.today(), datetime.today()]) NotificationController.notifyUser( user, 'Welcome to freelancerHub, Feel free to look around the site.') notifications: List[Notification] = Notification.query().select( ).where('is_read', 0).get() posts = Post.query().select().where('user_id', user.id).get() view = View( 'home', { 'user': user, 'notification_count': len(notifications), 'my_post_count': len(posts) }, { 'user_id': user.id, 'authToken': token }) return Response(ResponseType.valid, view)
def deleteClub(request): #Steps to be incorporated for deletion of a club #1) Remove the club key from the clubsJoined list of every profile #2) Remove the club key from the follows list of every profile #3) Remove the club key from the admin list of everyprofile #4) Remove from college group list #5) Remove notifications that have the club id = given club id #6)Remove Join Creations and Join Requests #Call deleteEvent and deletePost for all events and posts that belong to the club #Delete the club entity club_key_id = request.club_id pid = request.pid print("Club_key_id", club_key_id) clubKey = ndb.Key('Club', int(request.club_id)) pidKey = ndb.Key('Profile', int(request.pid)) profileconsidered = pidKey.get() club = clubKey.get() print("Club to be removed", club) # Check if the club's collegeId and Profile's collegeId are the same print("club.coolegeId", club.collegeId) print("profileconsidered.collegeId", profileconsidered.collegeId) if (club.collegeId == profileconsidered.collegeId): #check if the profile is the admin of the club or if he is the super admin of the college print("entered first part") print("Club.admin", club.admin) print("pidKey", pidKey) if (club.admin == pidKey or club.collegeId in profileconsidered.superadmin): # Operation 1 : for every profile key in member list of club, extract profile and remove the club # from the clubsJoined list print("Ive Entered Corrctly") for profile_key in club.members: profile = profile_key.get() profile.clubsJoined.remove(clubKey) profile.put() # Operation 2 : for every profile key in follows list of club, extract profile and remove the club # from the follows list of Profile for profile_key in club.follows: profile = profile_key.get() profile.follows.remove(clubKey) profile.put() #Operation 3 : Get the profile of the admin and remove the club key from his admin list adminProfile = club.admin.get() adminProfile.admin.remove(clubKey) adminProfile.put() #Operation 4 : Get the college and remove the club key from his grouplist college = club.collegeId.get() college.group_list.remove(clubKey) college.put() #Operation 5 : Get all notifications where it matches with clubKey and remove them notificationsRet = Notifications.query( Notifications.clubId == clubKey) for notif in notificationsRet: notif.key.delete() #Operation 6 : Get all JoinCreations and JoinRequests where it matches with clubKey and remove them joinCreationRet = Join_Creation.query( Join_Creation.club_id == clubKey) for jc in joinCreationRet: jc.key.delete() joinReqRet = Join_Request.query(Join_Request.club_id == clubKey) for jr in joinReqRet: jr.key.delete() postReqRet = Post_Request.query(Post_Request.club_id == clubKey) for pr in postReqRet: pr.key.delete() #Operation 7 - Posts and Events delete postRet = Post.query(Post.club_id == clubKey) for posts in postRet: likePostmini = LikePost() likePostmini.from_pid = str(club.admin.id()) likePostmini.postId = str(posts.key.id()) deletePost(likePostmini) eventRet = Event.query(Event.club_id == clubKey) for events in eventRet: modifyeventmini = ModifyEvent() modifyeventmini.from_pid = str(club.admin.id()) modifyeventmini.eventId = str(events.key.id()) deleteEvent(modifyeventmini) #Operation 8 - delete club clubKey.delete()
def deleteClub(request): # Steps to be incorporated for deletion of a club # 1) Remove the club key from the clubsJoined list of every profile # 2) Remove the club key from the follows list of every profile # 3) Remove the club key from the admin list of everyprofile # 4) Remove from college group list # 5) Remove notifications that have the club id = given club id # 6)Remove Join Creations and Join Requests # Call deleteEvent and deletePost for all events and posts that belong to the club # Delete the club entity club_key_id = request.club_id pid = request.pid print("Club_key_id", club_key_id) clubKey = ndb.Key("Club", int(request.club_id)) pidKey = ndb.Key("Profile", int(request.pid)) profileconsidered = pidKey.get() club = clubKey.get() print("Club to be removed", club) # Check if the club's collegeId and Profile's collegeId are the same print("club.coolegeId", club.collegeId) print("profileconsidered.collegeId", profileconsidered.collegeId) if club.collegeId == profileconsidered.collegeId: # check if the profile is the admin of the club or if he is the super admin of the college print("entered first part") print("Club.admin", club.admin) print("pidKey", pidKey) if club.admin == pidKey or club.collegeId in profileconsidered.superadmin: # Operation 1 : for every profile key in member list of club, extract profile and remove the club # from the clubsJoined list print("Ive Entered Corrctly") for profile_key in club.members: profile = profile_key.get() profile.clubsJoined.remove(clubKey) profile.put() # Operation 2 : for every profile key in follows list of club, extract profile and remove the club # from the follows list of Profile for profile_key in club.follows: profile = profile_key.get() profile.follows.remove(clubKey) profile.put() # Operation 3 : Get the profile of the admin and remove the club key from his admin list adminProfile = club.admin.get() adminProfile.admin.remove(clubKey) adminProfile.put() # Operation 4 : Get the college and remove the club key from his grouplist college = club.collegeId.get() college.group_list.remove(clubKey) college.put() # Operation 5 : Get all notifications where it matches with clubKey and remove them notificationsRet = Notifications.query(Notifications.clubId == clubKey) for notif in notificationsRet: notif.key.delete() # Operation 6 : Get all JoinCreations and JoinRequests where it matches with clubKey and remove them joinCreationRet = Join_Creation.query(Join_Creation.club_id == clubKey) for jc in joinCreationRet: jc.key.delete() joinReqRet = Join_Request.query(Join_Request.club_id == clubKey) for jr in joinReqRet: jr.key.delete() postReqRet = Post_Request.query(Post_Request.club_id == clubKey) for pr in postReqRet: pr.key.delete() # Operation 7 - Posts and Events delete postRet = Post.query(Post.club_id == clubKey) for posts in postRet: likePostmini = LikePost() likePostmini.from_pid = str(club.admin.id()) likePostmini.postId = str(posts.key.id()) deletePost(likePostmini) eventRet = Event.query(Event.club_id == clubKey) for events in eventRet: modifyeventmini = ModifyEvent() modifyeventmini.from_pid = str(club.admin.id()) modifyeventmini.eventId = str(events.key.id()) deleteEvent(modifyeventmini) # Operation 8 - delete club clubKey.delete()