diff --git a/geogame/main/admin.py b/geogame/main/admin.py
index bce62cd..fcdbd37 100644
--- a/geogame/main/admin.py
+++ b/geogame/main/admin.py
@@ -7,8 +7,7 @@ from .models import User, Coord
@admin.register(Coord)
class CoordAdmin(admin.ModelAdmin):
- list_display = 'id', 'country',
- search_fields = 'country',
+ list_display = 'id',
class CustomUserAdmin(UserAdmin):
diff --git a/geogame/main/forms.py b/geogame/main/forms.py
index 629c9bb..0651e62 100644
--- a/geogame/main/forms.py
+++ b/geogame/main/forms.py
@@ -5,26 +5,17 @@ from django.conf import settings
from django.forms import widgets
from django.forms.utils import to_current_timezone
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
+from django.forms import modelformset_factory
from geogame.main.models import (
- Coord, User, GameRound
+ Coord, User, GameRound, Challenge
)
-from dal import autocomplete
-#from django_starfield import Stars
-
-
-class CoordForm(forms.ModelForm):
+class ChallengeForm(forms.ModelForm):
class Meta:
- model = Coord
- fields = ('lat', 'lng', 'country')
- widgets = {'country': autocomplete.ModelSelect2(url='game:country-autocomplete')}
-
- def clean(self):
- cleaned_data = super(CoordForm, self).clean()
- lat = cleaned_data.get('lat')
- lng = cleaned_data.get('lng')
+ model = Challenge
+ fields = ('name',)
class GuessForm(forms.ModelForm):
@@ -47,7 +38,7 @@ class APIForm(forms.ModelForm):
class Meta:
model = User
- fields = ('api_key',)
+ fields = ('api_key', 'display_name',)
class CustomUserCreationForm(UserCreationForm):
@@ -62,3 +53,12 @@ class CustomUserChangeForm(UserChangeForm):
class Meta:
model = User
fields = ('username', 'email')
+
+
+
+
+ChallengeCoordFormSet = modelformset_factory(
+ Coord,
+ fields=('lat', 'lng',),
+ extra=1,
+)
\ No newline at end of file
diff --git a/geogame/main/management/commands/seed_coords.py b/geogame/main/management/commands/seed_coords.py
index cd3df13..eb7e7dc 100644
--- a/geogame/main/management/commands/seed_coords.py
+++ b/geogame/main/management/commands/seed_coords.py
@@ -20,13 +20,18 @@ class Command(BaseCommand):
with transaction.atomic():
- country = Country.objects.get(country='United Kingdom')
+ country = Country.objects.get(country="United States of America")
user = User.objects.first()
with open('/home/ubuntu/lat_lng.csv', 'r') as csvfile:
datareader = csv.reader(csvfile)
for row in datareader:
if row:
- Coord.objects.create(lat=row[0], lng=row[1], country=country, user=user)
+ _,c = Coord.objects.get_or_create(
+ lat=row[0],
+ lng=row[1],
+ country=country,
+ user=user
+ )
if options['dry_run']:
transaction.set_rollback(True)
diff --git a/geogame/main/migrations/0004_auto_20191125_2344.py b/geogame/main/migrations/0004_auto_20191125_2344.py
new file mode 100644
index 0000000..80c5baa
--- /dev/null
+++ b/geogame/main/migrations/0004_auto_20191125_2344.py
@@ -0,0 +1,64 @@
+# Generated by Django 2.2.4 on 2019-11-25 23:44
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('main', '0003_auto_20190822_2204'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='Challenge',
+ fields=[
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('name', models.CharField(blank=True, db_index=True, max_length=255, verbose_name='Challenge Name')),
+ ('average', models.PositiveIntegerField()),
+ ],
+ ),
+ migrations.RemoveField(
+ model_name='coord',
+ name='country',
+ ),
+ migrations.RemoveField(
+ model_name='game',
+ name='country',
+ ),
+ migrations.AddField(
+ model_name='coord',
+ name='reports',
+ field=models.PositiveIntegerField(default=0),
+ ),
+ migrations.AddField(
+ model_name='gameround',
+ name='result',
+ field=models.PositiveIntegerField(default=0),
+ ),
+ migrations.AddField(
+ model_name='user',
+ name='display_name',
+ field=models.CharField(blank=True, db_index=True, max_length=25, verbose_name='display name'),
+ ),
+ migrations.DeleteModel(
+ name='Country',
+ ),
+ migrations.AddField(
+ model_name='challenge',
+ name='user',
+ field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='challenge_user', to=settings.AUTH_USER_MODEL),
+ ),
+ migrations.AddField(
+ model_name='coord',
+ name='challenge',
+ field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='coord_challenge', to='main.Challenge'),
+ ),
+ migrations.AddField(
+ model_name='game',
+ name='challenge',
+ field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='game_challenge', to='main.Challenge'),
+ ),
+ ]
diff --git a/geogame/main/models.py b/geogame/main/models.py
index 73d8938..c25a264 100644
--- a/geogame/main/models.py
+++ b/geogame/main/models.py
@@ -5,6 +5,7 @@ import uuid
from django.contrib.postgres.search import SearchVectorField, SearchQuery, SearchVector
from django.conf import settings
from django.db import models, transaction, IntegrityError
+from django.db.models import Avg
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
@@ -13,8 +14,6 @@ from django.shortcuts import reverse
from django.contrib.auth.models import AbstractUser
from django.utils import timezone
-
-from django_countries.fields import CountryField
from geopy import distance
@@ -23,17 +22,16 @@ class User(AbstractUser):
last_name = models.CharField(_('last name'), max_length=50, blank=True, db_index=True)
email = models.EmailField(_('email address'), blank=False, null=False, db_index=True)
api_key = models.CharField(_('google maps api key'), max_length=255, blank=True, db_index=True)
+ display_name = models.CharField(_('display name'), max_length=25, blank=True, db_index=True)
- def generate_new_game(self, country=None):
+ def generate_new_game(self):
game = Game.objects.create(
start=timezone.now(),
user=self,
score=0,
active=True,
)
- qs = Coord.objects.all()
- if country:
- qs = qs.filter(country=country)
+ qs = Coord.objects.filter(reports=0)
coords = qs.order_by('?')[:5]
for i, coord in enumerate(coords):
round = GameRound.objects.create(
@@ -52,34 +50,65 @@ class User(AbstractUser):
return Game.objects.filter(user=self).update(active=False)
-class Country(models.Model):
- country = models.CharField(_('Country'), max_length=255, null=False, blank=False)
+class Challenge(models.Model):
+ name = models.CharField(_('Challenge Name'), max_length=255, blank=True, db_index=True)
+ average = models.PositiveIntegerField(default=0)
+ user = models.ForeignKey(User, models.PROTECT,
+ related_name='challenge_user',
+ null=False, blank=False)
- def __str__(self):
- return self.country
+ def update_average_score(self):
+ average = GameRound.objects.filter(game__challenge=self).aggregate(Avg('result')).get('result__avg', 0)
+ self.average = average
+ self.save()
+ return average
+
+ def setup_challenge(self, user):
+ rounds = Coord.objects.filter(challenge=self)
+ game = Game.objects.create(
+ challenge=self,
+ start=timezone.now(),
+ user=user,
+ score=0,
+ active=True,
+ )
+ for order, coord in enumerate(rounds):
+ round = GameRound.objects.create(
+ game=game,
+ coord=coord,
+ order=order
+ )
+ if order == 0:
+ round_id = round.id
+ return game.id, round_id
class Coord(models.Model):
lng = models.CharField(_('longitude'), max_length=50, null=False, blank=False)
lat = models.CharField(_('latitude'), max_length=50, null=False, blank=False)
- country = models.ForeignKey(Country, models.PROTECT,
- related_name='coord_country',
- null=False, blank=False)
user = models.ForeignKey(User, models.PROTECT,
related_name='coord_user',
null=False, blank=False)
+ reports = models.PositiveIntegerField(default=0)
+ challenge = models.ForeignKey(Challenge, models.PROTECT,
+ related_name='coord_challenge',
+ null=True, blank=True)
+
+ def report(self):
+ self.reports = self.reports + 1
+ self.save()
class Game(models.Model):
+ challenge = models.ForeignKey(Challenge, models.PROTECT,
+ related_name='game_challenge',
+ null=True, blank=True)
start = models.DateTimeField()
user = models.ForeignKey(User, models.PROTECT,
related_name='game_user',
null=False, blank=False)
score = models.PositiveIntegerField()
active = models.BooleanField()
- country = models.ForeignKey(Country, models.PROTECT,
- related_name='game_country',
- null=True, blank=True)
def get_rounds(self):
return GameRound.objects.filter(game=self)
@@ -95,10 +124,18 @@ class GameRound(models.Model):
order = models.IntegerField(_('round order'))
guess_lat = models.CharField(_('latitude'), max_length=50, blank=True, null=True)
guess_lng = models.CharField(_('longitude'), max_length=50, blank=True, null=True)
+ result = models.PositiveIntegerField(default=0)
def get_distance(self):
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,)
- return distance.distance(actual_coord, guess_coord).km
+
+ result = distance.distance(actual_coord, guess_coord).km
+ self.result = result
+ self.save()
+ return result
\ No newline at end of file
diff --git a/geogame/main/urls.py b/geogame/main/urls.py
index 68c1f03..dc38f10 100644
--- a/geogame/main/urls.py
+++ b/geogame/main/urls.py
@@ -2,8 +2,8 @@ from django.conf.urls import url
from django.contrib.auth.decorators import permission_required
from geogame.main.views import (
- RoundView, RoundRecapView, GameRecapView, NewGameView,
- ContributeView, CountryAutocomplete, RemoveCoordView
+ RoundView, RoundRecapView, GameRecapView, NewGameView, ChallengeListView,
+ EditChallengeView, RemoveCoordView, CoordDeleteView, ChallengeCreateView
)
@@ -13,7 +13,8 @@ urlpatterns = [
url(r'^round-recap/(?P
- API key + Edit Details
@@ -25,6 +25,7 @@Coming soon
Coming soon
+Challenge Name | +Average Score | ++ |
---|---|---|
{{challenge.name}} | +{{challenge.average}} | +Edit | +