From abc842092a3e9e2d80c217e87fcf2ddc575a55ee Mon Sep 17 00:00:00 2001 From: root Date: Fri, 17 Jan 2020 01:32:28 +0100 Subject: [PATCH] made challenge name a required field --- .../migrations/0005_auto_20200117_0023.py | 23 +++++++++++++++++++ geogame/main/models.py | 4 ++-- geogame/main/views.py | 15 ++++++++---- 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 geogame/main/migrations/0005_auto_20200117_0023.py diff --git a/geogame/main/migrations/0005_auto_20200117_0023.py b/geogame/main/migrations/0005_auto_20200117_0023.py new file mode 100644 index 0000000..4aaf998 --- /dev/null +++ b/geogame/main/migrations/0005_auto_20200117_0023.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.4 on 2020-01-17 00:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('main', '0004_auto_20191125_2344'), + ] + + operations = [ + migrations.AlterField( + model_name='challenge', + name='average', + field=models.PositiveIntegerField(default=0), + ), + migrations.AlterField( + model_name='challenge', + name='name', + field=models.CharField(db_index=True, max_length=255, verbose_name='Challenge Name'), + ), + ] diff --git a/geogame/main/models.py b/geogame/main/models.py index 8b4b4f3..ff55876 100644 --- a/geogame/main/models.py +++ b/geogame/main/models.py @@ -51,7 +51,7 @@ class User(AbstractUser): class Challenge(models.Model): - name = models.CharField(_('Challenge Name'), max_length=255, blank=True, db_index=True) + name = models.CharField(_('Challenge Name'), max_length=255, blank=False, db_index=True) average = models.PositiveIntegerField(default=0) user = models.ForeignKey(User, models.PROTECT, related_name='challenge_user', @@ -133,4 +133,4 @@ class GameRound(models.Model): 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 + super().save(*args, **kwargs) diff --git a/geogame/main/views.py b/geogame/main/views.py index 37ab5a7..41be95c 100644 --- a/geogame/main/views.py +++ b/geogame/main/views.py @@ -198,15 +198,22 @@ class RoundView(views.UserPassesTestMixin, UpdateView): round_id = self.kwargs.get('round_pk', 0) return get_object_or_404(GameRound, pk=round_id) + def get(self, *args, **kwargs): + round_id = self.kwargs.get('round_pk', 0) + round = get_object_or_404(GameRound, pk=round_id) + + if round.guess_lat: + # user has already played this round, so something has gone wrong + messages.warning(self.request, 'You have already played this round, something went wrong. Hit "Continue Last Game" to try again.') + return redirect(reverse_lazy('home')) + + return super().get(*args, **kwargs) + def get_context_data(self, **kwargs): context = super(RoundView, self).get_context_data(**kwargs) user = self.request.user round_id = self.kwargs.get('round_pk', 0) round = get_object_or_404(GameRound, pk=round_id) - if round.guess_lat: - #user has already played this round, so something has gone wrong - messages.warning(self.request, 'You have already played this round, something went wrong. Hit "Continue Last Game" to try again.') - return redirect(reverse_lazy('home')) context['api_key'] = user.api_key context['lat'] = round.coord.lat