def createClubRequestHelper(request): collegeId = ndb.Key('CollegeDb',int(request.collegeId)) print("Required College ID",collegeId) college_ret = collegeId.get() print("College Ret",college_ret) if(college_ret): club_ret = Club.query(Club.name == request.clubName).filter(Club.abbreviation == request.abbreviation).filter(Club.collegeId == college_ret.key).fetch(1) print("Club Ret",club_ret) if(len(club_ret) == 0): clubRequest = createClub(request) currentProfile = clubRequest.to_pid.get() newNotif = Notifications( clubName = clubRequest.club_name, clubphotoUrl = clubRequest.photoUrl, #to_pid = clubRequest.to_pid, type = "Club Creation Request", timestamp = dt.datetime.now().replace(microsecond = 0) ) newNotif.to_pid_list.append(clubRequest.to_pid) print("Notification to be inserted in club creation request",newNotif) newNotifKey = newNotif.put() data = {'message': clubRequest.club_name,"title": "Creation Request",'id':str(clubRequest.key.id()),'type':"Admin"} print (data) gcmId = currentProfile.gcmId gcm_message = GCMMessage(gcmId, data) gcm_conn = GCMConnection() #gcm_conn.notify_device(gcm_message) print("Finished the clubRequest") return None
def createClubRequestHelper(request): collegeId = ndb.Key('CollegeDb', int(request.collegeId)) print("Required College ID", collegeId) college_ret = collegeId.get() print("College Ret", college_ret) if (college_ret): club_ret = Club.query(Club.name == request.clubName).filter( Club.abbreviation == request.abbreviation).filter( Club.collegeId == college_ret.key).fetch(1) print("Club Ret", club_ret) if (len(club_ret) == 0): clubRequest = createClub(request) currentProfile = clubRequest.to_pid.get() newNotif = Notifications( clubName=clubRequest.club_name, clubphotoUrl=clubRequest.photoUrl, #to_pid = clubRequest.to_pid, type="Club Creation Request", timestamp=dt.datetime.now().replace(microsecond=0)) newNotif.to_pid_list.append(clubRequest.to_pid) print("Notification to be inserted in club creation request", newNotif) newNotifKey = newNotif.put() data = { 'message': clubRequest.club_name, "title": "Creation Request", 'id': str(clubRequest.key.id()), 'type': "Admin" } print(data) gcmId = currentProfile.gcmId gcm_message = GCMMessage(gcmId, data) gcm_conn = GCMConnection() #gcm_conn.notify_device(gcm_message) print("Finished the clubRequest") return None
def postEntry(requestentity=None,check=0): newPost = Post() #college = CollegeDb(name = 'NITK',student_sup='Anirudh',collegeId='NITK-123') #college_key = college.put() query = CollegeDb.query() club_name = Club.query() if check==0: print "The request entity key is " + requestentity.clubId key1 = ndb.Key('Club',int(requestentity.clubId)) key2 = ndb.Key('Profile',int(requestentity.fromPid)) else: key1 = requestentity.clubId key2 = requestentity.fromPid persons = Profile.query() #print club_name[0] #print "The key is " + club_name[0].key club_key = key1 profile_key = key2 flag = 0 flag1 = 0 clubs = Club.query() print "Profile Key " + str(profile_key) for x in persons: print x.key if(x.key == profile_key): print "Same" flag=1 else: print "NOPE" for x in clubs: print x.key if(x.key == club_key): print "Same" flag1=1 else: print "NOPE" #setattr(clubRequest, field, profile_key) if(flag==1 and flag1==1): if requestentity: for field in ('title','description','clubId','fromPid','likes','views','timestamp','photo','photoUrl','tags'): if hasattr(requestentity, field): print(field,"is there") val = getattr(requestentity, field) if(field=="clubId"): print "Club_Id stage" setattr(newPost, 'club_id', club_key) elif field == "fromPid": print "Entered here" person = profile_key.get() print "Person's email-id ", person.email person_collegeId = person.collegeId print "His college Id ", person.collegeId college_details = person_collegeId.get() print "The sup is ", college_details.student_sup setattr(newPost, 'from_pid', profile_key) print "Put PID" setattr(newPost,'collegeId',person_collegeId) print "Put college id" elif field=="timestamp": setattr(newPost, field, val) elif field=="photoUrl" and val == None: setattr(newPost, field, "https://lh3.googleusercontent.com/VLbWVdaJaq2HoYnu6J3T5aKC9DP_ku0KC3eelxawe6sqsPdNTarc5Vc0sx6VGqZ1Y-MlguZNd0plkDEZKYM9OnDbvR2tomX-Kg") elif field=="tags": print("Value is",val) setattr(newPost, field, (val)) elif val: print("Value is",val) setattr(newPost, field, str(val)) else: if field == "timestamp": temp = datetime.strptime(getattr(requestentity,"date"),"%Y-%m-%d").date() temp1 = datetime.strptime(getattr(requestentity,"time"),"%H:%M:%S").time() setattr(newPost,field,datetime.combine(temp,temp1)) setattr(newPost, "likes", 0) setattr(newPost, "views", 0) print("About to create Post") print(newPost) newPost.put() else: print "Invalid Entry" return newPost
def deleteProfile(request): #Steps to be incorporated for deletion of a profile #1) Check if fromKey == pidKey from_key = ndb.Key('Profile', int(request.fromPid)) pid_key = ndb.Key('Profile', int(request.pid)) if (from_key == pid_key and pid_key != None): profile = pid_key.get() print profile.name #2) Remove the profile key from followers and members of every club #3) If the profile is in club.admin then remove it from club.admin and make Superadmin the admin of the club clubList = Club.query() for club in clubList: if (len(club.members) != 0): if pid_key in club.members: print("club key is", club.key) club.members.remove(pid_key) if (len(club.follows) != 0): if pid_key in club.follows: club.follows.remove(pid_key) if pid_key == club.admin: #obtain super admin profile of college college = club.collegeId.get() emailId = college.sup_emailId profileret = Profile.query(Profile.email == emailId) for superadmin in profileret: print("Superadmin", superadmin.name) superadmin.admin.append(club.key) club.admin = superadmin.key superadmin.clubsJoined.append(club.key) superadmin.follows.append(club.key) club.members.admin(superadmin.key) club.follows.admin(superadmin.key) superadmin.put() club.put() #4 Delete Posts which are created by the profile postRet = Post.query(Post.from_pid == pid_key) for posts in postRet: likePostmini = LikePost() likePostmini.from_pid = str(pid_key.id()) likePostmini.postId = str(posts.key.id()) deletePost(likePostmini) # Remove pid_key from event_attendees list eventlist = Event.query() for event in eventlist: if (len(event.attendees) != 0): if (pid_key in event.attendees): event.attendees.remove(pid_key) event.put() #Remove Events created by that profile eventRet = Event.query(Event.event_creator == pid_key) for events in eventRet: modifyeventmini = ModifyEvent() modifyeventmini.from_pid = str(pid_key.id()) modifyeventmini.eventId = str(events.key.id()) deleteEvent(modifyeventmini) #Remove Club_Creation requests by that profile or to that profile clubcreationlist = Club_Creation.query( ndb.OR(Club_Creation.from_pid == pid_key, Club_Creation.to_pid == pid_key)) for clubcreation in clubcreationlist: print clubcreation clubcreation.key.delete() #Remove Join Creation, Join Requests, Post_Requests joinCreationRet = Join_Creation.query( ndb.OR(Join_Creation.from_pid == pid_key, Join_Creation.to_pid == pid_key)) for jc in joinCreationRet: print jc jc.key.delete() joinReqRet = Join_Request.query( ndb.OR(Join_Request.from_pid == pid_key, Join_Request.to_pid == pid_key)) for jr in joinReqRet: print jr jr.key.delete() postReqRet = Post_Request.query( ndb.OR(Post_Request.from_pid == pid_key or Post_Request.to_pid == pid_key)) for pr in postReqRet: print pr pr.key.delete() commentlist = Comments.query(Comments.pid == pid_key) for comments in commentlist: print comments comments.key.delete() #notificationsRet = Notifications.query(Notifications.to_pid == pid_key) notificationsRet = Notifications.query( Notifications.to_pid_list.IN([pid_key])) for notif in notificationsRet: notif.key.delete() notificationsRet2 = Notifications.query(Notifications.to_pid == pid) for notif in notificationsRet2: notif.key.delete() #Delete the profile entity pid_key.delete()
def postEntry(requestentity=None, check=0): newPost = Post() #college = CollegeDb(name = 'NITK',student_sup='Anirudh',collegeId='NITK-123') #college_key = college.put() query = CollegeDb.query() club_name = Club.query() if check == 0: print "The request entity key is " + requestentity.clubId key1 = ndb.Key('Club', int(requestentity.clubId)) key2 = ndb.Key('Profile', int(requestentity.fromPid)) else: key1 = requestentity.clubId key2 = requestentity.fromPid persons = Profile.query() #print club_name[0] #print "The key is " + club_name[0].key club_key = key1 profile_key = key2 flag = 0 flag1 = 0 clubs = Club.query() print "Profile Key " + str(profile_key) for x in persons: print x.key if (x.key == profile_key): print "Same" flag = 1 else: print "NOPE" for x in clubs: print x.key if (x.key == club_key): print "Same" flag1 = 1 else: print "NOPE" #setattr(clubRequest, field, profile_key) if (flag == 1 and flag1 == 1): if requestentity: for field in ('title', 'description', 'clubId', 'fromPid', 'likes', 'views', 'timestamp', 'photo', 'photoUrl', 'tags'): if hasattr(requestentity, field): print(field, "is there") val = getattr(requestentity, field) if (field == "clubId"): print "Club_Id stage" setattr(newPost, 'club_id', club_key) elif field == "fromPid": print "Entered here" person = profile_key.get() print "Person's email-id ", person.email person_collegeId = person.collegeId print "His college Id ", person.collegeId college_details = person_collegeId.get() print "The sup is ", college_details.student_sup setattr(newPost, 'from_pid', profile_key) print "Put PID" setattr(newPost, 'collegeId', person_collegeId) print "Put college id" elif field == "timestamp": setattr(newPost, field, val) elif field == "photoUrl" and val == None: setattr( newPost, field, "https://lh3.googleusercontent.com/VLbWVdaJaq2HoYnu6J3T5aKC9DP_ku0KC3eelxawe6sqsPdNTarc5Vc0sx6VGqZ1Y-MlguZNd0plkDEZKYM9OnDbvR2tomX-Kg" ) elif field == "tags": print("Value is", val) setattr(newPost, field, (val)) elif val: print("Value is", val) setattr(newPost, field, str(val)) else: if field == "timestamp": temp = datetime.strptime( getattr(requestentity, "date"), "%Y-%m-%d").date() temp1 = datetime.strptime( getattr(requestentity, "time"), "%H:%M:%S").time() setattr(newPost, field, datetime.combine(temp, temp1)) setattr(newPost, "likes", 0) setattr(newPost, "views", 0) print("About to create Post") print(newPost) newPost.put() else: print "Invalid Entry" return newPost
def deleteProfile(request): #Steps to be incorporated for deletion of a profile #1) Check if fromKey == pidKey from_key = ndb.Key('Profile',int(request.fromPid)) pid_key = ndb.Key('Profile',int(request.pid)) if(from_key == pid_key and pid_key!=None): profile = pid_key.get() print profile.name #2) Remove the profile key from followers and members of every club #3) If the profile is in club.admin then remove it from club.admin and make Superadmin the admin of the club clubList = Club.query() for club in clubList: if(len(club.members)!=0): if pid_key in club.members: print ("club key is",club.key) club.members.remove(pid_key) if(len(club.follows)!=0): if pid_key in club.follows: club.follows.remove(pid_key) if pid_key == club.admin: #obtain super admin profile of college college = club.collegeId.get() emailId = college.sup_emailId profileret = Profile.query(Profile.email == emailId) for superadmin in profileret: print ("Superadmin",superadmin.name) superadmin.admin.append(club.key) club.admin = superadmin.key superadmin.clubsJoined.append(club.key) superadmin.follows.append(club.key) club.members.admin(superadmin.key) club.follows.admin(superadmin.key) superadmin.put() club.put() #4 Delete Posts which are created by the profile postRet = Post.query(Post.from_pid == pid_key) for posts in postRet: likePostmini = LikePost() likePostmini.from_pid = str(pid_key.id()) likePostmini.postId = str(posts.key.id()) deletePost(likePostmini) # Remove pid_key from event_attendees list eventlist = Event.query() for event in eventlist: if(len(event.attendees)!=0): if(pid_key in event.attendees): event.attendees.remove(pid_key) event.put() #Remove Events created by that profile eventRet = Event.query(Event.event_creator == pid_key) for events in eventRet: modifyeventmini = ModifyEvent() modifyeventmini.from_pid = str(pid_key.id()) modifyeventmini.eventId = str(events.key.id()) deleteEvent(modifyeventmini) #Remove Club_Creation requests by that profile or to that profile clubcreationlist = Club_Creation.query(ndb.OR(Club_Creation.from_pid == pid_key,Club_Creation.to_pid == pid_key)) for clubcreation in clubcreationlist: print clubcreation clubcreation.key.delete() #Remove Join Creation, Join Requests, Post_Requests joinCreationRet = Join_Creation.query(ndb.OR(Join_Creation.from_pid == pid_key,Join_Creation.to_pid == pid_key)) for jc in joinCreationRet: print jc jc.key.delete() joinReqRet = Join_Request.query(ndb.OR(Join_Request.from_pid == pid_key,Join_Request.to_pid == pid_key)) for jr in joinReqRet: print jr jr.key.delete() postReqRet = Post_Request.query(ndb.OR(Post_Request.from_pid == pid_key or Post_Request.to_pid == pid_key )) for pr in postReqRet: print pr pr.key.delete() commentlist = Comments.query(Comments.pid == pid_key) for comments in commentlist: print comments comments.key.delete() #notificationsRet = Notifications.query(Notifications.to_pid == pid_key) notificationsRet = Notifications.query(Notifications.to_pid_list.IN([pid_key])) for notif in notificationsRet: notif.key.delete() notificationsRet2 = Notifications.query(Notifications.to_pid == pid) for notif in notificationsRet2: notif.key.delete() #Delete the profile entity pid_key.delete()