Compare commits

..

No commits in common. "49301afe513581cb0b9d13ea7333f00e4d30ab20" and "8597e53599dc666f5dce6a7bac6b9421ab19de09" have entirely different histories.

4 changed files with 32 additions and 173 deletions

View File

@ -1,6 +1,6 @@
# lostplaces-backend
lostplaces-backend is a django (4.x) based webproject. It once wants to become a software which allows a group of urban explorers to manage, document and share the locations of lost places while not exposing too much / any information to the public.
lostplaces-backend is a django (3.x) based webproject. It once wants to become a software which allows a group of urban explorers to manage, document and share the locations of lost places while not exposing too much / any information to the public.
The software is currently in early development status, neither scope, datamodel(s) nor features are finalized yet. Therefore we would not recommend to download or install this piece of software anywhere - except your local django dev server.

View File

@ -1,6 +1,6 @@
import os
import datetime
from math import ceil
from math import round, 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}
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}
return {'latitude': latitude, 'longitude': longitude}
def calculate_place_level(self):
if self.placevotings.count() == 0:
@ -112,14 +112,11 @@ class Place(Submittable, Taggable, Mapable):
accuaries = [];
for vote in self.placevotings.all():
vote_age = timezone.now() - vote.submitted_when;
vote_age = timezone.now() - vote.created_when;
accuracy = 100 - (100 / (place_age / vote_age))
accuaries.append(accuracy)
if len(accuaries) > 0:
return ceil(sum(accuaries) / len(accuaries))
else:
return 0
def __str__(self):
return self.name
@ -219,7 +216,7 @@ def auto_delete_file_on_change(sender, instance, **kwargs):
old_file.delete(save=False)
class PlaceVoting(PlaceAsset):
class PlaceVoting(PlaceAsset, Expireable):
vote = models.IntegerField(choices=PLACE_LEVELS)
def get_human_readable_level(self):

View File

@ -1,14 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
from django.test import TestCase
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from lostplaces.models import Place, PlaceVoting
from lostplaces.models import Place
from lostplaces.tests.models import ModelTestCase
class PlaceTestCase(ModelTestCase):
@ -107,12 +106,12 @@ class PlaceTestCase(ModelTestCase):
an empty list
'''
avg_latlon = Place.average_latlon([])
self.assertEqual(avg_latlon['latitude'], 51.961922091398904,
self.assertEqual(avg_latlon['latitude'], 0,
msg='%s: (no places) average latitude missmatch' % (
self.model.__name__
)
)
self.assertEqual(avg_latlon['longitude'], 7.6295628132604385,
self.assertEqual(avg_latlon['longitude'], 0,
msg='%s: (no places) average longitude missmatch' % (
self.model.__name__
)
@ -127,167 +126,29 @@ class PlaceTestCase(ModelTestCase):
)
def test_level_calculation(self):
explorer = self.place.submitted_by
place = self.place
explorer = place.submitted_by
PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote=5
place=place,
vote=5,
expires_when=timezone.now()+self.delta
)
PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote=2
place=place,
vote=2,
expires_when=timezone.now()+self.delta
)
PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote=4
place=place,
vote=4,
expires_when=timezone.now()+self.delta
)
self.place.calculate_place_level()
self.assertEqual(
4,
self.place.level,
msg='Expecting the place level to be 4'
place.calculate_place_level()
)
def test_level_calculation_no_votes(self):
self.place.calculate_place_level()
self.assertEqual(
5,
self.place.level,
msg='Expecting the default place level to be 5'
)
def test_level_mid_accuracy(self):
explorer = self.place.submitted_by
six_month_ago = datetime.timedelta(days=180)
self.place.submitted_when = timezone.now() - six_month_ago
votings = [
{
'date': timezone.now() - datetime.timedelta(days=170),
'vote': 5
},
{
'date': timezone.now() - datetime.timedelta(days=23),
'vote': 2
},
{
'date': timezone.now() - datetime.timedelta(days=1),
'vote': 4
}
]
for vote in votings:
voting = PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote= vote['vote']
)
voting.submitted_when = vote['date']
voting.save()
self.assertEqual(
65,
self.place.calculate_voting_accuracy()
)
def test_level_high_accuracy(self):
explorer = self.place.submitted_by
six_month_ago = datetime.timedelta(days=180)
self.place.submitted_when = timezone.now() - six_month_ago
votings = [
{
'date': timezone.now() - datetime.timedelta(days=9),
'vote': 5
},
{
'date': timezone.now() - datetime.timedelta(days=14),
'vote': 2
},
{
'date': timezone.now() - datetime.timedelta(days=0),
'vote': 4
}
]
for vote in votings:
voting = PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote= vote['vote']
)
voting.submitted_when = vote['date']
voting.save()
self.assertEqual(
96,
self.place.calculate_voting_accuracy()
)
def test_level_low_accuracy(self):
explorer = self.place.submitted_by
six_month_ago = datetime.timedelta(days=180)
self.place.submitted_when = timezone.now() - six_month_ago
votings = [
{
'date': timezone.now() - datetime.timedelta(days=177),
'vote': 5
},
{
'date': timezone.now() - datetime.timedelta(days=150),
'vote': 2
},
{
'date': timezone.now() - datetime.timedelta(days=100),
'vote': 4
}
]
for vote in votings:
voting = PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote= vote['vote']
)
voting.submitted_when = vote['date']
voting.save()
self.assertEqual(
21,
self.place.calculate_voting_accuracy()
)
def test_level_accuracy_zero_timedelta(self):
explorer = self.place.submitted_by
six_month_ago = datetime.timedelta(days=180)
self.place.submitted_when = timezone.now() - six_month_ago
votings = [
{
'date': timezone.now() - datetime.timedelta(days=0),
'vote': 4
}
]
for vote in votings:
voting = PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote= vote['vote']
)
voting.submitted_when = vote['date']
voting.save()
self.assertEqual(
100,
self.place.calculate_voting_accuracy(),
msg='Expecting the accurcy to be 100% when the vote is 0 time units old'
)

View File

@ -221,11 +221,12 @@ class PlaceVoteView(IsEligibleToSeePlaceMixin, View):
voting = PlaceVoting.objects.create(
submitted_by=explorer,
place=place,
vote=vote
vote=vote,
expires_when=timezone.now()+self.delta
)
messages.success(self.request, _('Vote submitted'))
else:
voting.submitted_when = timezone.now()
voting.expires_when=timezone.now()+self.delta
voting.vote = vote
messages.success(self.request, _('Your vote has been update'))