diff --git a/geogame/main/admin.py b/geogame/main/admin.py index fcdbd37..f2b3914 100644 --- a/geogame/main/admin.py +++ b/geogame/main/admin.py @@ -3,13 +3,28 @@ from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from .forms import CustomUserCreationForm, CustomUserChangeForm -from .models import User, Coord +from .models import User, Coord, Challenge, Game, GameRound @admin.register(Coord) class CoordAdmin(admin.ModelAdmin): list_display = 'id', +class CoordInline(admin.TabularInline): + model = Coord + fields = 'lat', 'lng', 'user', + extra = 0 + + +@admin.register(Challenge) +class ChallengeAdmin(admin.ModelAdmin): + list_display = 'id', 'name', 'user', + raw_id_fields = 'user', + search_fields = 'name', 'user__display_name', + ordering = 'id', + inlines = [CoordInline] + + class CustomUserAdmin(UserAdmin): add_form = CustomUserCreationForm form = CustomUserChangeForm diff --git a/geogame/main/models.py b/geogame/main/models.py index c25a264..8b4b4f3 100644 --- a/geogame/main/models.py +++ b/geogame/main/models.py @@ -126,16 +126,11 @@ class GameRound(models.Model): guess_lng = models.CharField(_('longitude'), max_length=50, blank=True, null=True) result = models.PositiveIntegerField(default=0) - def get_distance(self): + def save(self, *args, **kwargs): if not self.guess_lat or not self.guess_lng: - self.distance = 0 - self.save() - return 0 - - actual_coord = (self.coord.lat, self.coord.lng,) - guess_coord = (self.guess_lat, self.guess_lng,) - - result = distance.distance(actual_coord, guess_coord).km - self.result = result - self.save() - return result \ No newline at end of file + self.result = 0 + else: + actual_coord = (self.coord.lat, self.coord.lng,) + guess_coord = (self.guess_lat, self.guess_lng,) + self.result = distance.distance(actual_coord, guess_coord).km * 1000 + super().save(*args, **kwargs) \ No newline at end of file diff --git a/geogame/main/views.py b/geogame/main/views.py index b05b6ec..c145b60 100644 --- a/geogame/main/views.py +++ b/geogame/main/views.py @@ -194,13 +194,12 @@ class RoundView(views.LoginRequiredMixin, UpdateView): self.object.guess_lat = 0 self.object.guess_lng = 0 self.object.save() - round = self.get_object() return redirect( reverse_lazy( 'game:round-recap-view', kwargs={ - 'game_pk': round.game.pk, - 'round_pk': round.pk, + 'game_pk': self.object.game.pk, + 'round_pk': self.object.pk, } ) ) @@ -281,7 +280,7 @@ class RoundRecapView(views.UserPassesTestMixin, TemplateView): context['guess_lat'] = round.guess_lat context['guess_lng'] = round.guess_lng context['game_id'] = round.game.id - context['distance'] = "{0:.3f}".format(round.get_distance()) + context['distance'] = int(round.result) next_round = GameRound.objects.filter( game=round.game, @@ -313,7 +312,6 @@ class GameRecapView(views.UserPassesTestMixin, TemplateView): self.request.user.deactive_games() coord_results = [] - distance_total = 0 for round in GameRound.objects.filter(game=game).select_related('coord'): coord_results.append( [ @@ -321,12 +319,13 @@ class GameRecapView(views.UserPassesTestMixin, TemplateView): [round.guess_lat, round.guess_lng] ] ) - distance_total += round.get_distance() context['results'] = coord_results - context['average_distance'] = GameRound.objects.filter(game=game)\ + context['average_distance'] = int( + GameRound.objects.filter(game=game)\ .aggregate(Avg('result'))\ .get('result__avg', 0) + ) # not every game is part of a challenge, so keep this in a try try: context['all_average'] = game.challenge.average diff --git a/geogame/settings.py b/geogame/settings.py index bdba9c7..b433889 100644 --- a/geogame/settings.py +++ b/geogame/settings.py @@ -40,6 +40,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', + 'django.contrib.humanize', 'allauth', 'allauth.account', diff --git a/geogame/templates/main/challenge_list.html b/geogame/templates/main/challenge_list.html index b2f7112..03340a0 100644 --- a/geogame/templates/main/challenge_list.html +++ b/geogame/templates/main/challenge_list.html @@ -1,5 +1,5 @@ {% extends 'game_base.html' %} - +{% load humanize %} {% block content %}