edanm avatar edanm committed 455d662

Fixed a few sorting bugs

Comments (0)

Files changed (5)

stack2blog/stack2blogapp/static_media/style.css

-#header {margin-top:10px; }
-
-
+#header {
+	margin-top:10px;
+	margin-bottom:40px;
+}
 
 /***********************
  General stylings
 /***********************
  Home stylings
  ***********************/
+#home_container .error_messages {
+	font-size:22px;
+	color: #fa4545;
+	margin-top:20px;
+}
+
 #home_container .details_wrap {
 	margin-top:40px;
 	font-size:16px;
 #userpage_container h2 {
 	margin-top:20px;
 	font-size:24px;
+	margin-bottom:10px;
+
 }
 
 #userpage_container .answers_table_header {
 	padding-bottom:10px;
 }
 
+#userpage_container .filter_form {
+	margin-bottom:20px;
+}
+
 #userpage_container .answer {
 	font-size:14px;
 	border-bottom:1px dotted black;

stack2blog/stack2blogapp/templates/home.html

 		</form>
 		<div class="clear"></div>
 	</div>
+
+	<div class="error_messages grid_12">{{error_message}}</div>
+	<div class="clear"></div>
+
+	<div class="clear"></div>
 </div>
 
 {% endblock %}

stack2blog/stack2blogapp/templates/how_to_find_id.html

 
 <div id="how_to_find_id_container" class="container_12">
 
-
 </div>
 
 {% endblock %}

stack2blog/stack2blogapp/templates/userpage.html

 	</p>
 	<div class="clear"></div>
 
-	<form id="user_details" class="grid_12" action="" method="get">
+	<!-- Unpublished answers -->
+	<h2 class="grid_12">Answers you haven't published ({{ unpublished_answers|length }} shown, {{num_answers_remaining}} filtered):</h2>
+	<div class="clear"></div>
+
+	<form class="grid_12 filter_form" action="" method="get">
 		<label for="length_filter">Only displaying answers with at least: </label>
 		<input class="length_filter" type="text" name="length_filter" value="{{length_filter}}" id="userid"/> characters.
 		<input type="submit" value="Update filter" />
 	</form>
-	<div class="clear"></div>
 
-	<!-- Unpublished answers -->
-	<h2 class="grid_12">Answers you haven't posted yet ({{ unpublished_answers|length }}):</h2>
 	<div class="clear"></div>
 
 	<!-- The headers -->
 	<ul class="answers_table_header unpublished_answers_table_header">
-		<li class="grid_1"><a href="?length_filter={{length_filter|urlencode}}&amp;search_sort=votes&amp;search_order={{search_order_post}}">Votes</a></li>
-		<li class="grid_4"><a href="?length_filter={{length_filter|urlencode}}&amp;search_sort=question&amp;search_order={{search_order_post}}">Title</a> <span class="sort_help">(sort by activity)</span></li>
-		<li class="grid_6"><a href="?length_filter={{length_filter|urlencode}}&amp;search_sort=answer&amp;search_order={{search_order_post}}">Answer</a> <span class="sort_help">(sort by length)</span></li>
+		<li class="grid_1"><a href="?length_filter={{length_filter|urlencode}}&amp;search_sort=votes&amp;search_order={{search_order_post_votes}}">Votes</a></li>
+		<li class="grid_4"><a href="?length_filter={{length_filter|urlencode}}&amp;search_sort=question&amp;search_order={{search_order_post_title}}">Title</a> <span class="sort_help">(sort by activity)</span></li>
+		<li class="grid_6"><a href="?length_filter={{length_filter|urlencode}}&amp;search_sort=answer&amp;search_order={{search_order_post_answer}}">Answer</a> <span class="sort_help">(sort by length)</span></li>
 		<div class="clear"></div>
 	</ul>
 

stack2blog/stack2blogapp/views.py

 def home_page(request):
 	# Get is someone looking at the homepage for the first time.
 	if request.method == 'GET':
+		# Get any error messages waiting.
+		error_message = request.session["error_message"]
+		request.session["error_message"] = ""
+
 		return render_to_response('home.html', locals(), context_instance=RequestContext(request))
 
 	# Post. Make sure all is well.
 		try:
 			userid = int(request.POST.get("userid", ""))
 		except:
+			request.session["error_message"] = "Invalid User id (must be a number)"
+			return HttpResponseRedirect("/")
 			raise RuntimeError("Invalid user id")
 
 		# TODO: Try and get the SO user here to make sure the userid is valid.
 	try:
 		userid = int(userid)
 	except ValueError:
-		raise Http404 # Send back to homepage with an error.
+		request.session["error_message"] = "Invalid user ID"
+		return HttpResponseRedirect("/")
 
 	cached = False
 	# Check if this user is the one in the session.
 	published_answers = [blurbifyAnswer(answer) for answer in answers if PublishedPost.objects.filter(postid = answer.id).count() > 0]
 	unpublished_answers = [blurbifyAnswer(answer) for answer in answers if PublishedPost.objects.filter(postid = answer.id).count() == 0]
 
+	len_unpublished = len(unpublished_answers)
+
 	# Get filter and sorting options.
 	try:
 		length_filter = request.GET["length_filter"]
 	# Apply the filters to the published answers.
 	unpublished_answers = [answer for answer in unpublished_answers if len(answer.body) > length_filter]
 
+	num_answers_remaining = len_unpublished - len(unpublished_answers)
+
 	# Sort by the sort field.
 	if search_sort == 'votes':
 		def sortKey(answer):
 	reverse = (search_order == "dec")
 	unpublished_answers.sort(key = sortKey, reverse=reverse)
 
-	if search_order == 'asc':
-		search_order_post = 'dec'
-	else:
-		search_order_post = 'asc'
+	search_order_post_votes = "dec"
+	search_order_post_title = "dec"
+	search_order_post_answer = "dec"
+
+	if search_sort == 'votes':
+		if search_order == 'asc':
+			search_order_post_votes = 'dec'
+		else:
+			search_order_post_votes = 'asc'
+	elif search_sort == 'answer':
+		if search_order == 'asc':
+			search_order_post_title = 'dec'
+		else:
+			search_order_post_title = 'asc'
+	elif search_sort == 'question':
+		if search_order == 'asc':
+			search_order_post_answer = 'dec'
+		else:
+			search_order_answer = 'asc'
 
 	return render_to_response('userpage.html', locals(), context_instance=RequestContext(request))
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.