From 86c9de321308ec3f009aba77149ff34a039fa634 Mon Sep 17 00:00:00 2001 From: Leonhard Strohmidel Date: Tue, 20 Sep 2022 11:58:20 +0200 Subject: [PATCH] Fixing average latlon function --- django_lostplaces/lostplaces/models/place.py | 29 +++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/django_lostplaces/lostplaces/models/place.py b/django_lostplaces/lostplaces/models/place.py index 191d849..879185f 100644 --- a/django_lostplaces/lostplaces/models/place.py +++ b/django_lostplaces/lostplaces/models/place.py @@ -1,6 +1,6 @@ import os import datetime -from math import round, ceil +from math import ceil from django.db import models from django.urls import reverse @@ -78,20 +78,20 @@ class Place(Submittable, Taggable, Mapable): # Get center position of LP-geocoordinates. def average_latlon(cls, place_list): amount = len(place_list) - # Init fill values to prevent None - # China Corner in Münster - # Where I almost always eat lunch - # (Does'nt help losing wheight, tho) - longitude = 7.6295628132604385 - latitude = 51.961922091398904 if amount > 0: + latitude = 0 + longitude = 0 + for place in place_list: longitude += place.longitude latitude += place.latitude - return {'latitude':latitude / amount, 'longitude': longitude / amount} - - return {'latitude': latitude, 'longitude': longitude} + return {'latitude': latitude / amount, 'longitude': longitude / amount} + else: + # Location of China Corner in Münster + # Where I almost always eat lunch + # (Does'nt help losing wheight, tho) + return {'latitude': 51.961922091398904, 'longitude': 7.6295628132604385} def calculate_place_level(self): if self.placevotings.count() == 0: @@ -112,11 +112,14 @@ class Place(Submittable, Taggable, Mapable): accuaries = []; for vote in self.placevotings.all(): - vote_age = timezone.now() - vote.created_when; + vote_age = timezone.now() - vote.submitted_when; accuracy = 100 - (100 / (place_age / vote_age)) accuaries.append(accuracy) - return ceil(sum(accuaries) / len(accuaries)) + if len(accuaries) > 0: + return ceil(sum(accuaries) / len(accuaries)) + else: + return 0 def __str__(self): return self.name @@ -216,7 +219,7 @@ def auto_delete_file_on_change(sender, instance, **kwargs): old_file.delete(save=False) -class PlaceVoting(PlaceAsset, Expireable): +class PlaceVoting(PlaceAsset): vote = models.IntegerField(choices=PLACE_LEVELS) def get_human_readable_level(self):