def taggedPosts(request, tag): context = RequestContext(request) if request.method == 'GET': try: if request.user.is_authenticated(): postList = [] viewer = Author.objects.get(user=request.user) posts = post_utils.getVisibleToAuthor(viewer) taggedPosts = PostCategory.getPostWithCategory(tag) for categorizedPost in taggedPosts: jsonPost = post_utils.get_post_json(categorizedPost.post) if jsonPost in posts: postList.append(jsonPost) context['posts'] = _getDetailedPosts(postList) context['visibility'] = post_utils.getVisibilityTypes() context['page_header'] = 'Posts tagged as %s' % tag context['specific'] = True return render_to_response('index.html', context) except Exception as e: print "Error in posts: %s" % e return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def post(request, post_id): context = RequestContext(request) if request.method == 'GET' or request.method == 'POST': if 'application/json' in request.META['HTTP_ACCEPT']: return HttpResponseRedirect('/api/posts/%s' % post_id, status=302) else: try: if request.user.is_authenticated(): viewer = Author.objects.get(user=request.user) else: viewer = None post = post_utils.getPostById(post_id, viewer) context['posts'] = _getDetailedPosts([post]) # context indicating that we are seeing a specific user stream context['specific'] = True context['page_header'] = 'Posts with ID %s' % post_id return render_to_response('index.html', context) except Exception as e: print "Error in posts: %s" % e elif request.method == 'PUT': try: jsonData = json.load(request.body) post = post_utils.updatePost(jsonData) return HttpResponse(json.dumps(post_utils.get_post_json(post)), content_type='application/json', status=200) except Author.DoesNotExist: return HttpResponse(status=403) return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def get_post(request, post_id): if request.method == 'GET': try: query_data = json.load(request.body) author_id = query_data['author']['id'] author_host = query_data['author']['host'] friends = query_data['friends'] post = Post.objects.get(guid=post_id) for friend in friends: friend_check = requests.get(author_host + "friends/" + author_id + "/" + friend) if friend_check.status_code == 200: friend_check_data = json.load(friend_check.content) if friend_check_data['friends'] == "YES": friend_obj = utils.getRemoteUserHost(friend) if friend_obj is not None: get_friend_host = request.get(friend_obj.host + "author/" + friend) if get_friend_host.status_code == 200: friend_data = json.load( get_friend_host.content) foaf_check_req_host = request.get( friend_data['host'] + "friends/" + friend + "/" + author_id) foaf_check_local_host = request.get( friend_data['host'] + "friends/" + friend + "/" + post.author.uuid) if foaf_check_req_host.status_code == 200 and foaf_check_local_host.status_code == 200: foaf_check_req_host_data = json.load( foaf_check_req_host.content) foaf_check_local_host_data = json.load( foaf_check_local_host.content) if (foaf_check_req_host_data['friends'] == "YES" and foaf_check_local_host_data[ 'friends'] == "YES"): return HttpResponse( json.dumps( post_utils.get_post_json( post)), content_type='application/json') else: HttpResponse(status=405) except Exception as e: return HttpResponse(e.message, content_type='text/plain', status=500) return HttpResponse(status=405)
def get_post(request, post_id): if request.method == 'GET': try: query_data = json.load(request.body) author_id = query_data['author']['id'] author_host = query_data['author']['host'] friends = query_data['friends'] post = Post.objects.get(guid=post_id) for friend in friends: friend_check = requests.get(author_host + "friends/" + author_id + "/" + friend) if friend_check.status_code == 200: friend_check_data = json.load(friend_check.content) if friend_check_data['friends'] == "YES": friend_obj = utils.getRemoteUserHost(friend) if friend_obj is not None: get_friend_host = request.get(friend_obj.host + "author/" + friend) if get_friend_host.status_code == 200: friend_data = json.load(get_friend_host.content) foaf_check_req_host = request.get( friend_data['host'] + "friends/" + friend + "/" + author_id) foaf_check_local_host = request.get( friend_data['host'] + "friends/" + friend + "/" + post.author.uuid) if foaf_check_req_host.status_code == 200 and foaf_check_local_host.status_code == 200: foaf_check_req_host_data = json.load(foaf_check_req_host.content) foaf_check_local_host_data = json.load(foaf_check_local_host.content) if (foaf_check_req_host_data['friends'] == "YES" and foaf_check_local_host_data['friends'] == "YES"): return HttpResponse(json.dumps(post_utils.get_post_json(post)), content_type='application/json') else: HttpResponse(status=405) except Exception as e: return HttpResponse(e.message, content_type='text/plain', status=500) return HttpResponse(status=405)