Fixing average latlon function

This commit is contained in:
Leonhard Strohmidel 2022-09-20 11:58:20 +02:00
parent 8597e53599
commit 86c9de3213

View File

@ -1,6 +1,6 @@
import os import os
import datetime import datetime
from math import round, ceil from math import ceil
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
@ -78,20 +78,20 @@ class Place(Submittable, Taggable, Mapable):
# Get center position of LP-geocoordinates. # Get center position of LP-geocoordinates.
def average_latlon(cls, place_list): def average_latlon(cls, place_list):
amount = len(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: if amount > 0:
latitude = 0
longitude = 0
for place in place_list: for place in place_list:
longitude += place.longitude longitude += place.longitude
latitude += place.latitude latitude += place.latitude
return {'latitude':latitude / amount, 'longitude': longitude / amount} return {'latitude': latitude / amount, 'longitude': longitude / amount}
else:
return {'latitude': latitude, 'longitude': longitude} # 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): def calculate_place_level(self):
if self.placevotings.count() == 0: if self.placevotings.count() == 0:
@ -112,11 +112,14 @@ class Place(Submittable, Taggable, Mapable):
accuaries = []; accuaries = [];
for vote in self.placevotings.all(): 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)) accuracy = 100 - (100 / (place_age / vote_age))
accuaries.append(accuracy) 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): def __str__(self):
return self.name return self.name
@ -216,7 +219,7 @@ def auto_delete_file_on_change(sender, instance, **kwargs):
old_file.delete(save=False) old_file.delete(save=False)
class PlaceVoting(PlaceAsset, Expireable): class PlaceVoting(PlaceAsset):
vote = models.IntegerField(choices=PLACE_LEVELS) vote = models.IntegerField(choices=PLACE_LEVELS)
def get_human_readable_level(self): def get_human_readable_level(self):