Commit c7f768d0 authored by Joseph Walton-Rivers's avatar Joseph Walton-Rivers 🐦

Merge branch 'feature-database-performance' into 'master'

Feature database performance

See merge request !9
parents 36d6100f fd1d4207
Pipeline #2441 failed with stages
in 3 minutes and 1 second
*.py[co]
*.swp
db.sqlite3
var/
.venv/
......
......@@ -8,3 +8,4 @@ class TrackFilter(django_filters.FilterSet):
class Meta:
model = Track
fields = ["name", "allow_submit", "competition"]
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
from django.utils.functional import cached_property
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
......@@ -67,8 +68,14 @@ class CompetitionLink(models.Model):
def __str__(self):
return self.name
class TrackManager(models.Manager):
def get_queryset(self):
return super(TrackManager, self).get_queryset().prefetch_related('competition')
@python_2_unicode_compatible
class Track(models.Model):
objects = TrackManager()
"""A variation in the rules of a competition"""
name = models.CharField(max_length=100)
competition = models.ForeignKey(Competition, on_delete=models.CASCADE)
......@@ -155,14 +162,11 @@ class Submission(models.Model):
curr_upload = self.current
if not curr_upload:
return "No submission"
elif curr_upload.status not in ("BS", "VS"):
return curr_upload.get_status_display()
else:
return self.ranking
return self.ranking
@property
def is_valid(self):
return self.current and self.current.status in ('BS', 'VS')
return self.current and self.current.is_valid
@property
def versions(self):
......@@ -173,15 +177,15 @@ class Submission(models.Model):
else:
raise ValueError("unknown submission type")
@property
@cached_property
def current_upload(self):
return self.uploads.first()
@property
@cached_property
def current_text(self):
return self.text_submissions.first()
@property
@cached_property
def current(self):
if self.submission_type == "U":
return self.current_upload
......@@ -224,6 +228,10 @@ class BaseSubmission(models.Model):
created = models.DateTimeField(auto_now_add=True)
feedback = models.TextField(blank=True, null=True)
@property
def is_valid(self):
return self.status in ('BS', 'VS')
def check_stage(self, check_stage):
# check_stage = {upload, build, validate}
# self.status -> current status of the upload {BP, BF, ...}
......
......@@ -4,7 +4,6 @@
{% block head_title %}{{track}}{% endblock %}
{% block content_tab %}
<h2 class="sr-only">Leaderboard</h2>
<table class="table table-striped">
<thead class="thead-light">
......@@ -16,17 +15,20 @@
</thead>
<tbody>
{% for submission in track.submission_set.all %}
<tr {% if not submission.current %} class="warning" {% endif %}>
{% for submission in submissions %}
{% with current=submission.current %}
<tr {% if not current %} class="warning" {% endif %}>
<th><a href="{{submission.get_absolute_url}}">{{submission}}</a>{% if submission.sample %} <span class="badge badge-secondary">sample</span>{% endif %}</th>
<td>{% user_display submission.owner %}</td>
{% if submission.is_valid %}
<td>{{ submission.pretty_score|floatformat:3 }}</td>
{% if not current %}
<td>No Submission</td>
{% elif not current.is_valid %}
<td>{{ current.get_status_display }}</td>
{% else %}
<td>{{ submission.pretty_score }}</td>
<td>{{ submission.ranking|floatformat:3 }}</td>
{% endif %}
</tr>
{% endwith %}
{% empty %}
<tr>
<td colspan="3"><p class="text-center">There are presently no submissions</p></td>
......
......@@ -165,7 +165,8 @@ class TrackScoreView(TrackTab, TemplateView):
context = super(TrackScoreView, self).get_context_data(**kwargs)
# track
context['track'] = get_object_or_404(Track, id=self.kwargs.get('track'))
context['track'] = get_object_or_404(Track.objects.select_related('competition'), id=self.kwargs.get('track'))
context['submissions'] = context['track'].submission_set.all().select_related('owner')
return context
......
......@@ -2,14 +2,12 @@ from django.shortcuts import get_object_or_404
from django.views.generic import TemplateView
from .models import Competition, Track, Submission, SubmissionUpload
from fg_portal.models import Update
class Homepage(TemplateView):
template_name = "fg_competitions/index.html"
def get_context_data(self, **kwargs):
context = super(TemplateView, self).get_context_data(**kwargs)
context['tracks'] = Track.objects.order_by('?').filter(allow_submit=True)[:3]
context['news'] = Update.objects.all()[:5]
context['tracks'] = Track.objects.order_by('?').filter(allow_submit=True).select_related('competition')[:3]
return context
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment