def createClubRequest(self, request): collegeId = ndb.Key('CollegeDb',int(request.college_id)) print("Required College ID",collegeId) college_ret = collegeId.get() print("College Ret",college_ret) if(college_ret): club_ret = Club.query(Club.name == request.club_name).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) ) print("Notification to be inserted in club creation request",newNotif) newNotifKey = newNotif.put() data = {'message': clubRequest.club_name,"title": "Creation Request"} print (data) gcmId = currentProfile.gcmId gcm_message = GCMMessage(gcmId, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) print("Finished the clubRequest") return message_types.VoidMessage()
def myNotificationFeed(self, request): pid = ndb.Key('Profile',int(request.pid)) notificationslist = Notifications.query(Notifications.to_pid == pid).order(-Notifications.timestamp).fetch() listresponse = NotificationList() for obj in notificationslist: newListObj = NotificationResponseForm() #if( pid in obj.to_pid): if(obj.clubName != None): newListObj.clubName = obj.clubName else: newListObj.clubName = None if(obj.clubId != None): newListObj.clubId = str(obj.clubId.id()) else: newListObj.clubId = None if(obj.clubphotoUrl != None): newListObj.clubphotoUrl = obj.clubphotoUrl else: newListObj.clubphotoUrl = None if(obj.eventName != None): newListObj.eventName = obj.eventName else: newListObj.eventName = None if(obj.eventId != None): newListObj.eventId = str(obj.eventId.id()) else: newListObj.eventId = None if(obj.postName != None): newListObj.postName = obj.postName else : newListObj.postName = None if(obj.postId != None): newListObj.postId = str(obj.postId.id()) else : newListObj.postId = None newListObj.timestamp = str(obj.timestamp) newListObj.type = obj.type listresponse.list.append(newListObj) return listresponse
def joinClubApi(self,request): if request: clubKey = ndb.Key('Club',int(request.club_id)) club = clubKey.get() profileKey = ndb.Key('Profile',int(request.from_pid)) profile = profileKey.get() if (club and profile and profileKey not in club.members): joinCreationObj = Join_Creation() joinCreationObj.from_pid = profileKey joinCreationObj.to_pid = club.admin to_pidProfile = joinCreationObj.to_pid.get() joinCreationObj.club_id = clubKey joinCreationObj.timestamp = dt.datetime.now().replace(microsecond = 0) print("join obj",joinCreationObj) joinCreationObjKey = joinCreationObj.put() newNotif = Notifications( clubName = club.name, clubId = club.key, clubphotoUrl = club.photoUrl, to_pid = joinCreationObj.to_pid, type = "Join Request", timestamp = joinCreationObj.timestamp ) print("Notification to be inserted in join approval",newNotif) newNotifKey = newNotif.put() data = {'message': profile.name + " wishes to join","title": club.name} print (data) gcmId = to_pidProfile.gcmId gcm_message = GCMMessage(gcmId, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) return message_types.VoidMessage()
def createPostRequest(self, request): print("Entered Post Request Portion") clubRequest = postRequest(request) currentProfile = clubRequest.to_pid.get() newNotif = Notifications( clubName = clubRequest.club_id.get().name, clubId = clubRequest.club_id, clubphotoUrl = clubRequest.photoUrl, to_pid = clubRequest.to_pid, type = "Post Creation Request", timestamp = dt.datetime.now().replace(microsecond = 0) ) print("Notification to be inserted in Post Creation Request",newNotif) newNotifKey = newNotif.put() data = {'message': clubRequest.title,"title": "Post Creation Request"} print (data) gcmId = currentProfile.gcmId gcm_message = GCMMessage(gcmId, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) print("Inserted into the post request table") return message_types.VoidMessage()
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 createEvent(self, request): response = MessageResponse() print("Entered Event Entry Portion") try: person_key = ndb.Key('Profile',int(request.event_creator)) print(person_key) profile = person_key.get() club_key = ndb.Key('Club',int(request.club_id)) if club_key in profile.clubsJoined: newEvent = eventEntry(request) response.status = "1" response.text = "Inserted into Posts Table" group = newEvent.club_id.get() groupName = group.name data = {'message': groupName,"title": newEvent.title} #get the followers of the club pids. Get GCM Id's from those and send print ("GROUP FOLLOWS LIST ", group.follows) eventlist = [] if (group.follows): for pid in group.follows: person = pid.get() gcmId = person.gcmId if (gcmId): print ("GCM ID is",gcmId) eventlist.append(gcmId) newNotif = Notifications( clubName = groupName, clubId = newEvent.club_id, clubphotoUrl = group.photoUrl, eventName = newEvent.title, eventId = newEvent.key, timestamp = newEvent.timestamp, type = "Event", to_pid = pid ) print("Notification to be inserted",newNotif) newNotifKey = newNotif.put() print ("Event list is",eventlist) gcm_message = GCMMessage(eventlist, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) print("Should have worked") else: print "Not Present" response.status = "2" response.text = "Could not insert" except Exception,e: print "Error" print str(e) response.status = "3" response.text = "Error"
def createPost(self, request): response = MessageResponse() print("Entered Post Entry Portion") flag=0 try: person_key = ndb.Key('Profile',int(request.from_pid)) profile = person_key.get() print(profile) club_key = ndb.Key('Club',int(request.club_id)) if club_key in profile.follows: print "Present" newPost = postEntry(request,flag) print("NEW POST",newPost) response.status = "1" response.text = "Inserted into Posts Table" #Create Notification Feed group = newPost.club_id.get() groupName = group.name data = {'message': groupName,"title": newPost.title} postlist = [] if (group.follows): for pid in group.follows: person = pid.get() print ("PID is",person) gcmId = person.gcmId if (gcmId): print ("GCM ID is",gcmId) postlist.append(gcmId) newNotif = Notifications( clubName = groupName, clubId = newPost.club_id, clubphotoUrl = group.photoUrl, postName = newPost.title, postId = newPost.key, timestamp = newPost.timestamp, type = "Post", to_pid = pid ) print("Notification to be inserted",newNotif) newNotifKey = newNotif.put() print ("post list is",postlist) gcm_message = GCMMessage(postlist, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) else: print "Not present" clubRequest = postRequest(request) response.status = "2" response.text = "Inserted into Posts Requests Table" except: print "Error" response.status = "3" response.text = "Couldn't insert into Posts Table" print("Inserted into the posts table") return response
def approveClub(self,request): #Obtain the club request object clubRequest = ndb.Key('Club_Creation',int(request.req_id)) action = request.action print ("Action is",action) req = clubRequest.get() currentProfile = req.from_pid.get() print("From Pid profile is",currentProfile) if (action == 'N'): print ("Disapproving request and removing entry") print("Request Approval Denied") newNotif = Notifications( clubName = req.club_name, clubphotoUrl = req.photoUrl, to_pid = req.from_pid, type = "Rejected Club Creation Request", timestamp = dt.datetime.now().replace(microsecond = 0) ) print("Notification to be inserted in club approval rejection",newNotif) newNotifKey = newNotif.put() data = {'message': req.club_name,"title": "Creation Request Denied"} print (data) gcmId = currentProfile.gcmId gcm_message = GCMMessage(gcmId, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) req.key.delete() elif (req and req.approval_status == "N"): status = approveClub(req) if(status == "Y"): print("Request Approval Granted") newClub = createClubAfterApproval(req) currentProfile = newClub.admin.get() if(newClub): newNotif = Notifications( clubName = newClub.name, clubId = newClub.key, clubphotoUrl = newClub.photoUrl, to_pid = newClub.admin, type = "Approved Club Creation Request", timestamp = dt.datetime.now().replace(microsecond = 0) ) print("Notification to be inserted in club approval ",newNotif) newNotifKey = newNotif.put() data = {'message': newClub.name,"title": "Creation Request Approved"} print (data) gcmId = currentProfile.gcmId gcm_message = GCMMessage(gcmId, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) print("The club that has been created is",newClub) req.key.delete() else: print("Request Approval Denied") newNotif = Notifications( clubName = req.club_name, clubphotoUrl = req.photoUrl, to_pid = req.from_pid, type = "Rejected Club Creation Request", timestamp = dt.datetime.now().replace(microsecond = 0) ) print("Notification to be inserted in club approval rejection",newNotif) newNotifKey = newNotif.put() data = {'message': req.club_name,"title": "Creation Request Denied"} print (data) gcmId = currentProfile.gcmId gcm_message = GCMMessage(gcmId, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) req.key.delete() return message_types.VoidMessage()
def joinClubApprovalApi(self,request): if request: joinCreation = ndb.Key('Join_Creation',int(request.req_id)).get() club = joinCreation.club_id.get() profileKey = joinCreation.from_pid profile = profileKey.get() print("Retrieved Profile ",profile) if(request.action == "N"): #send notif message to the guy who has req,saying that it has been rejected newNotif = Notifications( clubName = club.name, clubId = club.key, clubphotoUrl = club.photoUrl, to_pid = joinCreation.from_pid, type = "Approval Rejection", timestamp = dt.datetime.now().replace(microsecond = 0) ) print("Notification to be inserted in join approval",newNotif) newNotifKey = newNotif.put() data = {'message': "Approval Denied","title": club.name} print (data) gcmId = profile.gcmId gcm_message = GCMMessage(gcmId, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) joinCreation.key.delete() elif (club and profile and (profileKey not in club.members)): print("entered here") currentClub = club currentClub.members.append(profile.key) currentProfile = profile currentProfile.clubsJoined.append(currentClub.key) if (currentProfile.key not in currentClub.follows): print ("I've entered this because these guys are totally new") currentProfile.follows.append(currentClub.key) currentClub.follows.append(profile.key) currentClub.put() currentProfile.put() #Create Notification here where the to_pid = guy who has made the join req #Send push notification to the gcm id of this dude. newNotif = Notifications( clubName = club.name, clubId = club.key, clubphotoUrl = club.photoUrl, to_pid = joinCreation.from_pid, type = "Approved Join Request", timestamp = dt.datetime.now().replace(microsecond = 0) ) print("Notification to be inserted in join approval",newNotif) newNotifKey = newNotif.put() data = {'message': "You are now a member","title": currentClub.name} print (data) gcmId = currentProfile.gcmId gcm_message = GCMMessage(gcmId, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) joinCreation.key.delete() return message_types.VoidMessage()
def getEventsBasedonTimeLeft(): logging.basicConfig(level=logging.DEBUG) LOG = logging.getLogger(__name__) current = dt.datetime.now().replace(microsecond=0) #current_utc = current - dt.timedelta(hours =5,minutes=30) currentDate = current.date() currentTime = current.time() LOG.info("Current time") LOG.info(currentTime) eventlist = [] event_query = Event.query().fetch() for event in event_query: LOG.info("Event") LOG.info(event.title) LOG.info(event.start_time) start_time_utc = event.start_time - dt.timedelta(hours=5, minutes=30) start_date = start_time_utc.date() diff = start_date - currentDate LOG.info("Considering this event") LOG.info(event.title) if (diff == dt.timedelta(hours=0) and diff == dt.timedelta(minutes=0) and diff == dt.timedelta(seconds=0)): LOG.info("this event is happening today") LOG.info(event.title) start_time = start_time_utc.time() FMT = '%H:%M:%S' tdelta = datetime.strptime(str(start_time), FMT) - datetime.strptime( str(currentTime), FMT) LOG.info("Time delta is") LOG.info(tdelta) b = dt.timedelta(days=0) c = dt.timedelta(hours=2) if tdelta >= b: LOG.info("made through first part") if (tdelta <= c): LOG.info(event.title) eventlist.append(event.key) LOG.info("has reached here") LOG.info("Creating notification") group = event.club_id.get() groupName = group.name data = { 'message': event.title + "About to start soon", "title": groupName } LOG.info(data) #get the followers of the club pids. Get GCM Id's from those and send LOG.info("Event attendees list") LOG.info(event.attendees) attendeeslist = [] if (event.attendees): for pid in event.attendees: person = pid.get() LOG.info("PID is") LOG.info(person) gcmId = person.gcmId if (gcmId): attendeeslist.append(gcmId) newNotif = Notifications( clubName=groupName, clubId=event.club_id, clubphotoUrl=group.photoUrl, eventName=event.title, eventId=event.key, timestamp=dt.datetime.now().replace( microsecond=0), type="Reminder", to_pid=pid) newNotifKey = newNotif.put() LOG.info("Attendees GCM list is") LOG.info(attendeeslist) gcm_message = GCMMessage(attendeeslist, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) LOG.info("Chill") else: LOG.info( "This event is still some time away from notification") else: LOG.info("This event is over") LOG.info(eventlist)
def getEventsBasedonTimeLeft(): logging.basicConfig(level=logging.DEBUG) LOG = logging.getLogger(__name__) current = dt.datetime.now().replace(microsecond = 0) #current_utc = current - dt.timedelta(hours =5,minutes=30) currentDate = current.date() currentTime = current.time() LOG.info("Current time") LOG.info(currentTime) eventlist = [] event_query = Event.query().fetch() for event in event_query: LOG.info("Event") LOG.info(event.title) LOG.info(event.start_time) start_time_utc = event.start_time - dt.timedelta(hours=5,minutes=30) start_date = start_time_utc.date() diff = start_date - currentDate LOG.info("Considering this event") LOG.info(event.title) if(diff == dt.timedelta(hours=0) and diff == dt.timedelta(minutes=0) and diff == dt.timedelta(seconds = 0)): LOG.info("this event is happening today") LOG.info(event.title) start_time = start_time_utc.time() FMT = '%H:%M:%S' tdelta = datetime.strptime(str(start_time), FMT) - datetime.strptime(str(currentTime), FMT) LOG.info("Time delta is") LOG.info(tdelta) b = dt.timedelta(days = 0) c = dt.timedelta(hours = 2) if tdelta >= b: LOG.info("made through first part") if(tdelta<=c): LOG.info(event.title) eventlist.append(event.key) LOG.info("has reached here") LOG.info("Creating notification") group = event.club_id.get() groupName = group.name data = {'message': event.title + "About to start soon","title": groupName } LOG.info(data) #get the followers of the club pids. Get GCM Id's from those and send LOG.info("Event attendees list") LOG.info(event.attendees) attendeeslist = [] if (event.attendees): for pid in event.attendees: person = pid.get() LOG.info("PID is") LOG.info(person) gcmId = person.gcmId if (gcmId): attendeeslist.append(gcmId) newNotif = Notifications( clubName = groupName, clubId = event.club_id, clubphotoUrl = group.photoUrl, eventName = event.title, eventId = event.key, timestamp = dt.datetime.now().replace(microsecond = 0), type = "Reminder", to_pid = pid) newNotifKey = newNotif.put() LOG.info("Attendees GCM list is") LOG.info(attendeeslist) gcm_message = GCMMessage(attendeeslist, data) gcm_conn = GCMConnection() gcm_conn.notify_device(gcm_message) LOG.info("Chill") else: LOG.info("This event is still some time away from notification") else: LOG.info("This event is over") LOG.info(eventlist)
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()