def poke(request): response = dict() userid = request.REQUEST['userid'] friendid = request.REQUEST['friendid'] lastHour = datetime.utcnow().replace(tzinfo=pytz.utc) - timedelta(hours=1) try: user = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") try: targetUser = UserProfile.getUser(friendid) except UserProfile.DoesNotExist: return errorResponse("Invalid target user id") if targetUser not in user.friends.all(): return errorResponse("User is not your friend") poke = Poke.objects.filter(sender=user, recipient=targetUser, created__gt=lastHour) if poke: return errorResponse("Already poked user in the last hour") poke = Poke.objects.create(sender=user, recipient=targetUser) # sendPokeNotification(poke) response['success'] = True return HttpResponse(json.dumps(response))
def unblockFriend(request): response = dict() userid = request.REQUEST['userid'] friendid = request.REQUEST['friendid'] try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") try: friendProfile = UserProfile.getUser(friendid) except UserProfile.DoesNotExist: return errorResponse("Invalid friend id") if friendProfile not in userProfile.friends.all(): return errorResponse("Target is not your friend") if friendProfile in userProfile.blockedFriends.all(): userProfile.blockedFriends.remove(friendProfile) userProfile.save() response['success'] = True return HttpResponse(json.dumps(response))
def inviteToStatus(request): response = dict() userid = request.REQUEST['userid'] statusId = request.REQUEST['statusid'] friends = request.REQUEST.get('friends', '[]') friends = json.loads(friends) try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse('Invalid User') try: status = Status.getStatus(statusId) except Status.DoesNotExist: return errorResponse('Invalid Status') # if status.user != userProfile: # if status.visibility == Status.VIS_FRIENDS or status.visibility == Status.VIS_CUSTOM: # return errorResponse("Cant invite people to private events") buddyupFriends = list() facebookFriends = list() for friendId in friends: friendId = str(friendId) if friendId[:2] == 'fb': friendId = friendId[2:] try: friend = UserProfile.objects.get(facebookUID=friendId) if friend != status.user and friend not in status.attending.all(): buddyupFriends.append(friend) except UserProfile.DoesNotExist: try: fbFriend = FacebookUser.objects.get(facebookUID=friendId) except FacebookUser.DoesNotExist: fbFriend = FacebookUser.objects.create(facebookUID=friendId) facebookFriends.append(fbFriend) else: try: friend = UserProfile.getUser(friendId) if friend != status.user and friend not in status.attending.all(): buddyupFriends.append(friend) except UserProfile.DoesNotExist: pass status.invited.add(*buddyupFriends) status.fbInvited.add(*facebookFriends) createInvitedToStatusNotification(buddyupFriends, userProfile, status) sendInvitedToStatusNotification(status, userProfile, buddyupFriends) response['success'] = True return HttpResponse(json.dumps(response))
def setGroupMembers(request): response = dict() userid = request.REQUEST['userid'] groupid = request.REQUEST['groupid'] friendids = request.REQUEST.get('friendids', []) friendids = json.loads(friendids) try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") try: group = Group.objects.get(pk=groupid) except Group.DoesNotExist: return errorResponse("Invalid group id") if group.user != userProfile: return errorResponse("User does not own that group") userFriends = userProfile.friends.all() group.members.clear() group.fbMembers.clear() for friendid in friendids: friendid = str(friendid) if friendid[:2] == 'fb': friendid = friendid[2:] if friendid: try: friendProfile = UserProfile.objects.get(facebookUID=friendid) group.members.add(friendProfile) except UserProfile.DoesNotExist: try: facebookUser = FacebookUser.objects.get(facebookUID=friendid) except FacebookUser.DoesNotExist: facebookUser = FacebookUser.objects.create(facebookUID=friendid) group.fbMembers.add(facebookUser) else: try: friend = UserProfile.getUser(friendid) except User.DoesNotExist: return errorResponse("Friend does not exist") if friend not in userFriends: return errorResponse("User is not a friend") group.members.add(friend) group.save() response['success'] = True return HttpResponse(json.dumps(response))
def suggestLocationTime(request): response = dict() userid = request.REQUEST['userid'] statusid = request.REQUEST['statusid'] suggestionType = request.REQUEST['type'] location = request.REQUEST.get('location', None) time = request.REQUEST.get('time', None) if suggestionType != 'location' or suggestionType != 'time': return errorResponse('type must be location or time') try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse('Invalid User') try: cacheKey = Status.getCacheKey(statusid) status = Status.getStatus(statusid) except Status.DoesNotExist: return errorResponse('Invalid status id') if suggestionType == 'location': location = getLocationObjectFromJson(json.loads(location)) locationSuggestion = LocationSuggestion.objects.get_or_create(user=userProfile, status=status, location=location) if suggestionType == 'time': date = datetime.strptime(time, DATETIME_FORMAT) timeSuggestion = TimeSuggestion.objects.get_or_create(user=userProfile, status=status, dateSuggested=time) response['success'] = True return HttpResponse(json.dumps(response))
def cancelStatus(request): response = dict() userid = request.REQUEST['userid'] statusid = request.REQUEST['statusid'] try: userProfile = UserProfile.getUser(userid) cacheKey = Status.getCacheKey(statusid) status = cache.get(cacheKey) if status is None: status = Status.getStatus(statusid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") except Status.DoesNotExist: return errorResponse("Invalid statusid") if status.user != userProfile: return errorResponse("User does not own this status") now = datetime.utcnow() if status.expires > now: status.expires = now status.save() response['success'] = True return HttpResponse(json.dumps(response))
def editGroupName(request): response = dict() userid = request.REQUEST['userid'] groupName = request.REQUEST['groupname'] groupid = request.REQUEST['groupid'] try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") try: group = Group.objects.get(pk=groupid) except Group.DoesNotExist: return errorResponse("Invalid group id") if group.user != userProfile: return errorResponse("Group does not belong to user") group.name = groupName group.save() response['success'] = True return HttpResponse(json.dumps(response))
def getNewData(request): response = dict() userid = request.REQUEST['userid'] since = request.REQUEST.get('since', None) if since: since = datetime.strptime(since, MICROSECOND_DATETIME_FORMAT) try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") newSince = datetime.now().strftime(MICROSECOND_DATETIME_FORMAT) pokes = getNewPokesData(userProfile, since) chats = getNewChatsData(userProfile, since) notifications = getNotificationsJson(userProfile, since) response['chats'] = chats response['newsince'] = newSince response['notifications'] = notifications response['success'] = True response['pokes'] = pokes return HttpResponse(json.dumps(response))
def deleteStatus(request): response = dict() userid = request.REQUEST['userid'] statusid = request.REQUEST['statusid'] try: userProfile = UserProfile.getUser(userid) cacheKey = Status.getCacheKey(statusid) status = cache.get(cacheKey) if status is None: status = Status.getStatus(statusid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") except Status.DoesNotExist: return errorResponse("Invalid statusid") if status.user == userProfile: status.deleted = True status.save() response['success'] = True createDeleteStatusNotification(status) sendDeleteStatusNotfication(status) else: response['success'] = False response['error'] = "Can not delete another user's status" return HttpResponse(json.dumps(response))
def sendStatusMessage(request): response = dict() text = request.REQUEST['text'] userid = request.REQUEST['userid'] statusid = request.REQUEST['statusid'] lastMessageId = request.REQUEST.get('lastmessageid', None) try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse('Invalid User') try: status = Status.getStatus(statusid) except Status.DoesNotExist: return errorResponse('Invalid status id') message = StatusMessage.objects.create(user=userProfile, text=text, status=status) sendStatusMessageNotification(message) createCreateStatusMessageNotification(message) response['success'] = True response['messages'] = getNewStatusMessages(status, lastMessageId) return HttpResponse(json.dumps(response))
def rsvpStatus(request): response = dict() userId = request.REQUEST['userid'] statusId = request.REQUEST['statusid'] attending = request.REQUEST['attending'] try: userProfile = UserProfile.getUser(userId) except UserProfile.DoesNotExist: return errorResponse('Invalid User') try: status = Status.getStatus(statusId) except Status.DoesNotExist: return errorResponse('Invalid Status') if attending == 'true' or attending == 'True': status.attending.add(userProfile) createAttendingStatusNotification(status, userProfile) sendAttendingStatusPushNotification(status, userProfile) elif attending == 'false' or attending == 'False': status.attending.remove(userProfile) else: return errorResponse("Invalid Attending value. Must be true or false") response['success'] = True return HttpResponse(json.dumps(response))
def registerForPushNotifications(request): response = dict() userid = request.REQUEST['userid'] token = request.REQUEST['token'] platform = request.REQUEST['platform'] try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("User does not exist") if platform == 'ios': try: device = APNSDevice.objects.get(registration_id=token) device.user = userProfile device.save() except APNSDevice.DoesNotExist: device = APNSDevice.objects.create(user=userProfile, registration_id=token) elif platform == 'android': try: device = GCMDevice.objects.get(registration_id=token) device.user = userProfile device.save() except GCMDevice.DoesNotExist: device = GCMDevice.objects.create(user=userProfile, registration_id=token) else: return errorResponse("platform must be ios or android") response['success'] = True return HttpResponse(json.dumps(response))
def setSetting(request): response = dict() userid = request.REQUEST['userid'] key = request.REQUEST['key'] value = request.REQUEST['value'] if key != 'statusradius' and key != 'imboredtext': return errorResponse("unknown key. Must be statusradius or imboredtext") try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("User does not exist") try: setting = userProfile.settings.get(key=key) setting.value = value setting.save() except Setting.DoesNotExist: Setting.objects.create(user=userProfile, value=value, key=key) response['success'] = True return HttpResponse(json.dumps(response))
def getUserDetails(request): response = dict() userid = request.REQUEST.get('userid', None) userids = request.REQUEST.get('userids', '[]') userids = json.loads(userids) if userid is None and len(userids) == 0: return errorResponse("Need to supply userid or userids") users = list() if userid is not None: try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") response['firstname'] = userProfile.user.first_name response['lastname'] = userProfile.user.last_name response['facebookid'] = userProfile.facebookUID response['userid'] = userProfile.id elif len(userids) > 0: for userid in userids: try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") userData = dict() userData['firstname'] = userProfile.user.first_name userData['lastname'] = userProfile.user.last_name userData['facebookid'] = userProfile.facebookUID userData['userid'] = userProfile.id users.append(userData) response['users'] = users response['success'] = True return HttpResponse(json.dumps(response))
def removeGroupMember(request): response = dict() userid = request.REQUEST['userid'] friendid = str(request.REQUEST['friendid']) groupid = request.REQUEST['groupid'] try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") try: group = Group.objects.get(pk=groupid) except Group.DoesNotExist: return errorResponse("Invalid group id") if group.user != userProfile: return errorResponse("Group does not belong to user") if friendid[:2] == 'fb': friendid = friendid[2:] if friendid: try: friendProfile = UserProfile.objects.get(facebookUID=friendid) group.members.remove(friendProfile) except UserProfile.DoesNotExist: try: facebookUser = FacebookUser.objects.get(facebookUID=friendid) except FacebookUser.DoesNotExist: facebookUser = FacebookUser.objects.create(facebookUID=friendid) group.fbMembers.remove(facebookUser) else: try: friend = UserProfile.getUser(friendid) group.members.remove(friend) except User.DoesNotExist: return errorResponse("Friend does not exist") response['success'] = True return HttpResponse(json.dumps(response))
def getMyStatuses(request): response = dict() userid = request.REQUEST['userid'] try: user = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") statusesData = getMyStatusesJsonResponse(user) response['success'] = True response['statuses'] = statusesData return HttpResponse(json.dumps(response))
def submitFeedback(request): response = dict() userid = request.REQUEST['userid'] text = request.REQUEST['text'] try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") Feedback.objects.create(user=userProfile, text=text) response['success'] = True return HttpResponse(json.dumps(response))
def createGroup(request): response = dict() userid = request.REQUEST['userid'] groupName = request.REQUEST['groupname'] try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") group, created = Group.objects.get_or_create(name=groupName, user=userProfile) response['success'] = True response['groupid'] = group.id return HttpResponse(json.dumps(response))
def setFavoritesNotifications(request): userId = request.REQUEST['userid'] value = request.REQUEST['value'] try: userProfile = UserProfile.getUser(userId) except UserProfile.DoesNotExist: return errorResponse("User does not exist") if not value or value == 'False' or value == 'false': userProfile.favoritesNotifications = False else: userProfile.favoritesNotifications = True userProfile.save() response = dict() response['success'] = True return HttpResponse(json.dumps(response))
def goOffline(request): response = dict() userid = request.REQUEST['userid'] try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("User does not exist") activeStatuses = userProfile.getActiveStatuses() now = datetime.utcnow() for status in activeStatuses: status.expires = now status.save() response['success'] = True return HttpResponse(json.dumps(response))
def getSetting(request): response = dict() userid = request.REQUEST['userid'] key = request.REQUEST['key'] try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("User does not exist") try: setting = userProfile.settings.get(key=key) value = setting.value except Setting.DoesNotExist: value = '' response['success'] = True response['value'] = value return HttpResponse(json.dumps(response))
def refreshFacebookFriends(request): response = dict() userid = request.REQUEST.get('userid', None) accessToken = request.REQUEST.get('accesstoken') try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") facebookProfile = FacebookProfile(userProfile, accessToken) friends = facebookProfile.getFacebookFriends() response['users'] = [] for friend in friends: response['users'].append(getUserProfileDetailsJson(friend)) response['success'] = True return HttpResponse(json.dumps(response))
def getStatuses(request): response = dict() userid = request.REQUEST['userid'] lat = request.REQUEST.get('lat', None) lng = request.REQUEST.get('lng', None) radius = request.REQUEST.get('radius', None) since = request.REQUEST.get('since', None) if since: since = datetime.strptime(since, MICROSECOND_DATETIME_FORMAT) try: userprofile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse('Invalid User Id') statusesData, newSince = getNewStatusesJsonResponse(userprofile, since, lat, lng, radius) response['success'] = True response['newsince'] = newSince.strftime(MICROSECOND_DATETIME_FORMAT) response['statuses'] = statusesData return HttpResponse(json.dumps(response))
def getFriends(request): response = dict() userid = request.REQUEST['userid'] try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") # blockedFriends = userProfile.blockedFriends.all() friendsData = list() for friend in userProfile.friends.all(): # blocked = False # if friend in blockedFriends: # blocked = True friendData = getUserProfileDetailsJson(friend) friendsData.append(friendData) response['success'] = True response['users'] = friendsData return HttpResponse(json.dumps(response))
def setGroups(request): response = dict() userid = request.REQUEST['userid'] friendid = str(request.REQUEST['friendid']) groupids = request.REQUEST.get('groupids', '[]') groupids = json.loads(groupids) try: userProfile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("Invalid user id") if friendid[:2] == 'fb': friendid = friendid[2:] if friendid: try: friendProfile = UserProfile.objects.get(facebookUID=friendid) except UserProfile.DoesNotExist: try: friendProfile = FacebookUser.objects.get(facebookUID=friendid) except FacebookUser.DoesNotExist: friendProfile = FacebookUser.objects.create(facebookUID=friendid) else: try: friendProfile = UserProfile.getUser(friendid) except UserProfile.DoesNotExist: return errorResponse("Friend user does not exist") if friendProfile not in userProfile.friends.all(): return errorResponse("That user is not your friend") newGroups = [] for groupid in groupids: try: group = Group.objects.get(pk=groupid) except Group.DoesNotExist: return errorResponse("Group does not exist") if group not in userProfile.groups.all(): return errorResponse("Invalid groupid") newGroups.append(group) for group in userProfile.groups.all(): members = None if isinstance(friendProfile, UserProfile): members = group.members elif isinstance(friendProfile, FacebookUser): members = group.fbMembers # if friend in current group and current group not in newGroups then remove from group if friendProfile in members.all(): if group not in newGroups: members.remove(friendProfile) group.save() # if friend not in current group and current group is in newGroups then add to group else: if group in newGroups: members.add(friendProfile) group.save() response['success'] = True return HttpResponse(json.dumps(response))
def testUserNotFound(self): print("User Not Found") with self.assertRaises(UserProfile.DoesNotExist): UserProfile.getUser(111232323223232)
def postStatus(request): response = dict() text = request.REQUEST['text'] userid = request.REQUEST['userid'] groupids = request.REQUEST.get('groupids', '[]') expires = request.REQUEST.get('expires', None) starts = request.REQUEST.get('starts', None) locationData = request.REQUEST.get('location', '{}') statusid = request.REQUEST.get('statusid', 0) accessToken = request.REQUEST.get('accesstoken', None) shareOnFacebook = request.REQUEST.get('facebookshare', False) statusType = request.REQUEST.get('type', 'other') visibility = request.REQUEST.get('visibility', 'friends') visibilityFriends = request.REQUEST.get('visibilityfriends', '[]') imageUrl = request.REQUEST.get('imageurl', None) imageOrientation = request.REQUEST.get('imageorientation', None) groupids = json.loads(groupids) locationData = json.loads(locationData) if starts: starts = datetime.strptime(starts, DATETIME_FORMAT).replace(tzinfo=pytz.utc) else: starts = datetime.utcnow() if expires: expires = datetime.strptime(expires, DATETIME_FORMAT).replace(tzinfo=pytz.utc) else: expires = starts + timedelta(hours=8) try: userprofile = UserProfile.getUser(userid) except UserProfile.DoesNotExist: return errorResponse("User Id not valid") if statusid: try: status = Status.getStatus(statusid) createStatusChangedNotification(status) shouldPostNotification = False except Status.DoesNotExist: return errorResponse('status does not exist with that id') else: status = Status(user=userprofile) shouldPostNotification = True status.expires = expires status.text = text status.starts = starts status.statusType = statusType status.visibility = visibility status.imageOrientation = imageOrientation if imageUrl: status.imageUrl = imageUrl if locationData: location = getLocationObjectFromJson(locationData) status.location = location status.save() status.attending.add(userprofile) if status.visibility == 'custom': visibilityFriends = json.loads(visibilityFriends) for friendId in visibilityFriends: if str(friendId)[:2] == 'fb': friendId = str(friendId)[2:] try: friendProfile = UserProfile.objects.get(facebookUID=friendId) status.friendsVisible.add(friendProfile) except UserProfile.DoesNotExist: try: facebookUser = FacebookUser.objects.get(facebookUID=friendId) except FacebookUser.DoesNotExist: facebookUser = FacebookUser.objects.create(facebookUID=friendId) status.fbFriendsVisible.add(facebookUser) else: try: friendProfile = UserProfile.getUser(friendId) status.friendsVisible.add(friendProfile) except UserProfile.DoesNotExist: pass if groupids: groups = Group.objects.filter(id__in=groupids) status.groups.add(*groups) else: status.groups.clear() status.save() if shareOnFacebook: if accessToken is not None: fbProfile = FacebookProfile(userprofile, accessToken) fbProfile.shareStatus(status, request) if shouldPostNotification: sendFavoritesStatusPushNotification(status) else: sendEditStatusNotification(status) response['success'] = True response['statusid'] = status.id cacheKey = status.getCacheKeyNoneStatic() statusDuration = status.getStatusDuration() cache.set(cacheKey, status, statusDuration) return HttpResponse(json.dumps(response))