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 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))