Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions static/website/templates/ajax-keyword-search.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@
</td>

<td>
{{ question.views}}
{{ question.views }}
</td>

<td>
{{ question.answer_set.count }}
{{ question.total_answers }}
</td>

<td>
<span class="title" data-toggle="tooltip" data-placement="top" title="{{ question.user }}">
{{ question.user|truncatechars:10 }}
<span class="title" data-toggle="tooltip" data-placement="top" title="{{ question.cached_user|default:question.user }}">
{{ question.cached_user|default:question.user|truncatechars:10 }}
</span>
</td>

Expand Down
8 changes: 4 additions & 4 deletions static/website/templates/ajax-time-search.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@
</td>

<td>
{{ question.views}}
{{ question.views }}
</td>

<td>
{{ question.answer_set.count }}
{{ question.total_answers }}
</td>

<td>
<span class="title" data-toggle="tooltip" data-placement="top" title="{{ question.user }}">
{{ question.user|truncatechars:10 }}
<span class="title" data-toggle="tooltip" data-placement="top" title="{{ question.cached_user|default:question.user }}">
{{ question.cached_user|default:question.user|truncatechars:10 }}
</span>
</td>

Expand Down
12 changes: 6 additions & 6 deletions static/website/templates/filter.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ <h5>
</td>

<td>
{{ question.views}}
{{ question.views }}
</td>

<td>
{{ question.answer_set.count }}
{{ question.total_answers }}
</td>

<td>
<span class="title" data-toggle="tooltip" data-placement="top" title="{{ question.user }}">
{{ question.user|truncatechars:10 }}
<span class="title" data-toggle="tooltip" data-placement="top" title="{{ question.cached_user|default:question.user }}">
{{ question.cached_user|default:question.user|truncatechars:10 }}
</span>
</td>

Expand Down
6 changes: 3 additions & 3 deletions static/website/templates/get-question.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ <h5>Question</h5>
</small>

<span class="user">
{{ question.user }}
{{ question.cached_user|default:question.user }}
</span>
</span>
</div> <!-- /.question -->
Expand Down Expand Up @@ -169,7 +169,7 @@ <h4><u>Answers:</u></h4>
</small>

<span class="user">
{{ answer.user }}
{{ answer.cached_user|default:answer.user }}
</span>
</span>
{% if user|can_edit:answer %}
Expand Down Expand Up @@ -199,7 +199,7 @@ <h4><u>Answers:</u></h4>
</small>

<span class="user">
{{ comment.user }}
{{ comment.cached_user|default:comment.user }}
</span>
</span>
{% if user|can_edit:comment %}
Expand Down
2 changes: 1 addition & 1 deletion static/website/templates/notifications.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h4 class="pull-left">Notifications</h4>
</a>
<div class="clearfix"></div>
{% for notification in notifications %}
{% get_notification notification.id %}
{% get_notification notification %}
{% endfor %}
{% endblock %}

Expand Down
8 changes: 4 additions & 4 deletions static/website/templates/questions.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ <h5>
</td>

<td class="col-md-1">
{{ question.views}}
{{ question.views }}
</td>

<td class="col-md-1">
{{ question.answer_set.count }}
{{ question.total_answers }}
</td>

<td class="col-md-1">
<span class="title" data-toggle="tooltip" data-placement="top" title="{{ question.user }}">
{{ question.user|truncatechars:10 }}
<span class="title" data-toggle="tooltip" data-placement="top" title="{{ question.cached_user|default:question.user }}">
{{ question.cached_user|default:question.user|truncatechars:10 }}
</span>
</td>
</tr>
Expand Down
17 changes: 5 additions & 12 deletions static/website/templates/recent-questions.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{% extends 'website/templates/base.html' %}
{% load static %}
{% load count_tags %}

{% block content %}
<h4><u>Recent Questions</u></h4>
<h4><u>Recent Questions</u></h4>
<table class="table table-bordered table-hover">
<th> FOSS </th>
<th> Tutorial</th>
Expand Down Expand Up @@ -65,16 +63,16 @@ <h4><u>Recent Questions</u></h4>
</td>

<td>
{{ question.views}}
{{ question.views }}
</td>

<td>
{{ question.answer_set.count }}
{{ question.total_answers }}
</td>

<td>
<span class="title" data-toggle="tooltip" data-placement="top" title="{{ question.user }}">
{{ question.user|truncatechars:10 }}
<span class="title" data-toggle="tooltip" data-placement="top" title="{{ question.cached_user|default:question.user }}">
{{ question.cached_user|default:question.user|truncatechars:10 }}
</span>
</td>
</tr>
Expand All @@ -96,11 +94,6 @@ <h4><u>Recent Questions</u></h4>
{% endfor %}
</ul>
{% endif %}

{% endblock %}

{% block javascript %}
<script>
$('span').tooltip();
</script>
{% endblock %}
35 changes: 30 additions & 5 deletions website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ class Question(models.Model):
# votes = models.IntegerField(default=0)

def user(self):
cached_username = getattr(self, "cached_user", None)
if cached_username is not None:
return cached_username
user = User.objects.get(id=self.uid)
return user.username
username = user.username
self.cached_user = username
return username

def last_post_user(self):
cached_username = getattr(self, "cached_last_post_user", None)
if cached_username is not None:
return cached_username
user = User.objects.filter(id=self.last_post_by).first()
return user.username if user else "Unknown User"
username = user.username if user else "Unknown User"
self.cached_last_post_user = username
return username

class Meta:
get_latest_by = "date_created"
Expand Down Expand Up @@ -54,8 +64,13 @@ class Answer(models.Model):
# votes = models.IntegerField(default=0)

def user(self):
cached_username = getattr(self, "cached_user", None)
if cached_username is not None:
return cached_username
user = User.objects.get(id=self.uid)
return user.username
username = user.username
self.cached_user = username
return username


class AnswerVote(models.Model):
Expand All @@ -71,8 +86,13 @@ class AnswerComment(models.Model):
date_modified = models.DateTimeField(auto_now=True)

def user(self):
cached_username = getattr(self, "cached_user", None)
if cached_username is not None:
return cached_username
user = User.objects.get(id=self.uid)
return user.username
username = user.username
self.cached_user = username
return username


class Notification(models.Model):
Expand All @@ -84,7 +104,12 @@ class Notification(models.Model):
date_created = models.DateTimeField(auto_now_add=True)

def poster(self):
cached_username = getattr(self, "cached_poster", None)
if cached_username is not None:
return cached_username
user = User.objects.get(id=self.pid)
return user.username
username = user.username
self.cached_poster = username
return username

# CDEEP database created using inspectdb arg of manage.py
12 changes: 9 additions & 3 deletions website/templatetags/count_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
register = template.Library()


# Counts the number of questions in <category>
# Counts the number of questions in <category>, cached for faster rendering
def category_question_count(category):
category_question_count = Question.objects.filter(category=category).count()
return category_question_count
cache_key = f"stats:category_question_count:{category}"
count = cache.get(cache_key)
if count is not None:
return count

count = Question.objects.filter(category=category).count()
cache.set(cache_key, count, 300)
return count


register.simple_tag(category_question_count)
Expand Down
20 changes: 10 additions & 10 deletions website/templatetags/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
register = template.Library()


def get_notification(nid):
notification = Notification.objects.get(pk=nid)
try:
question = Question.objects.get(pk=notification.qid)
except Question.DoesNotExist:
question = None
try:
answer = Answer.objects.get(pk=notification.aid)
except Answer.DoesNotExist:
answer = None
def get_notification(notification):
"""
Render a single notification.

The view (`user_notifications`) attaches `cached_question`, `cached_answer`
and `cached_poster` attributes on each `Notification` instance so that this
tag does not need to perform any additional database queries.
"""
question = getattr(notification, "cached_question", None)
answer = getattr(notification, "cached_answer", None)
context = {
'notification': notification,
'question': question,
Expand Down
15 changes: 12 additions & 3 deletions website/templatetags/sidebar_tags.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from django import template
from django.db.models import Count

from website.models import Question

register = template.Library()


def recent_questions():
recent_questions = Question.objects.all().order_by('-id')[:5]
return {'recent_questions': recent_questions}
recent_questions = (
Question.objects.all()
.annotate(total_answers=Count('answer'))
.order_by('-id')[:5]
)
return {
'questions': recent_questions,
'total': 10,
'marker': 0,
}


register.inclusion_tag('website/templates/recent_questions.html')(recent_questions)
register.inclusion_tag('website/templates/recent-questions.html')(recent_questions)
Loading