def getAuthorPosts(request, author_id): """ Retrieves all posts made by the author specified and that are visible to currently authenticated user """ if 'application/json' in request.META['HTTP_ACCEPT']: return getAuthorPostsAsJSON(request, author_id) elif 'text/html' in request.META['HTTP_ACCEPT']: context = RequestContext(request) if not request.user.is_authenticated(): return render_to_response('login/index.html', context) viewer = Author.objects.get(user=request.user) author = Author.objects.get(guid=author_id) postIds = AuthorPost.objects.filter(author=author).values_list( 'post', flat=True) posts = Post.getViewablePosts(viewer, author) comments = [] categories = [] visibilityExceptions = [] images = [] for post in posts: categoryIds = PostCategory.objects.filter(post = post).values_list( 'category', flat=True) visExceptions = PostVisibilityException.objects.filter( post=post) authorIds = [e.author.guid for e in visExceptions] imageIds = ImagePost.objects.filter(post=post).values_list( 'image', flat=True) comments.append(Comment.objects.filter(post_ref=post)) categories.append(Category.objects.filter(id__in=categoryIds)) visibilityExceptions.append(Author.objects.filter( guid__in=authorIds)) images.append(Image.objects.filter(id__in=imageIds)) # Convert Markdown into HTML for web browser # django.contrib.markup is deprecated in 1.6, so, workaround if post.contentType == post.MARKDOWN: post.content = markdown.markdown(post.content) context["posts"] = zip(posts, comments, categories, visibilityExceptions, images) context["author_id"] = author.guid return render_to_response('post/posts.html', context) else: return getAuthorPostsAsJSON(request, author_id)
def profile(request, author_id): """ GET: Returns the profile page / information of an author. """ if 'text/html' not in request.META['HTTP_ACCEPT']: try: author = Author.objects.get(guid=author_id) except ObjectDoesNotExist: return HttpResponse(json.dumps({"message": "User not found", "status": 404}), status=404, content_type="application/json") return HttpResponse(json.dumps(author.as_dict()), content_type="application/json") else: if request.user.is_authenticated(): viewer = Author.objects.get(user=request.user) try: author = Author.objects.get(guid=author_id) except Author.DoesNotExist: context = RequestContext(request) context['author_id'] = viewer.guid context['doge'] = doges[random.randint(0,6)] if not getRemoteAuthorProfile(context, author_id): # Error conncecting with remote server return render_to_response('error/doge_error.html', context) return render_to_response('author/remote_profile.html', context) user = author.user payload = { } # This is what we send in the RequestContext payload['author_id'] = viewer.guid payload['firstName'] = user.first_name or "" payload['lastName'] = user.last_name or "" payload['username'] = user.username payload['githubUsername'] = author.githubUsername or "" payload['host'] = author.host or "" payload['url'] = author.url or request.build_absolute_uri(author.get_absolute_url()) payload['userIsAuthor'] = (user.username == request.user.username) context = RequestContext(request, payload) viewer = Author.objects.get(user=User.objects.get( username=request.user)) context['authPosts'] = Post.getViewablePosts(viewer, author) context['doge'] = doges[random.randint(0,6)] return render_to_response('author/profile.html', context) else: return redirect('/login/')