implement position timeout
This commit is contained in:
parent
f7b830ca6a
commit
92be350b8c
3 changed files with 30 additions and 1 deletions
18
src/c3nav/mapdata/migrations/0084_position_timeout.py
Normal file
18
src/c3nav/mapdata/migrations/0084_position_timeout.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 2.2.8 on 2019-12-27 17:25
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('mapdata', '0083_auto_20191227_1642'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='position',
|
||||||
|
name='timeout',
|
||||||
|
field=models.PositiveSmallIntegerField(default=0, help_text='0 for no timeout', verbose_name='timeout (in seconds)'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,5 +1,6 @@
|
||||||
import string
|
import string
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
|
from datetime import timedelta
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
|
|
||||||
|
@ -9,6 +10,7 @@ from django.core.validators import MaxValueValidator, MinValueValidator, RegexVa
|
||||||
from django.db import models, transaction
|
from django.db import models, transaction
|
||||||
from django.db.models import FieldDoesNotExist, Prefetch
|
from django.db.models import FieldDoesNotExist, Prefetch
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from django.utils import timezone
|
||||||
from django.utils.crypto import get_random_string
|
from django.utils.crypto import get_random_string
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
from django.utils.text import format_lazy
|
from django.utils.text import format_lazy
|
||||||
|
@ -497,6 +499,7 @@ class Position(models.Model):
|
||||||
name = models.CharField(_('name'), max_length=32)
|
name = models.CharField(_('name'), max_length=32)
|
||||||
secret = models.CharField(_('secret'), unique=True, max_length=32, default=get_position_secret)
|
secret = models.CharField(_('secret'), unique=True, max_length=32, default=get_position_secret)
|
||||||
last_coordinates_update = models.DateTimeField(_('last coordinates update'), null=True)
|
last_coordinates_update = models.DateTimeField(_('last coordinates update'), null=True)
|
||||||
|
timeout = models.PositiveSmallIntegerField(_('timeout (in seconds)'), default=0, help_text=_('0 for no timeout'))
|
||||||
coordinates_id = models.CharField(_('coordinates'), null=True, max_length=48)
|
coordinates_id = models.CharField(_('coordinates'), null=True, max_length=48)
|
||||||
api_secret = models.CharField(_('api secret'), max_length=64, default=get_position_api_secret)
|
api_secret = models.CharField(_('api secret'), max_length=64, default=get_position_api_secret)
|
||||||
|
|
||||||
|
@ -507,6 +510,14 @@ class Position(models.Model):
|
||||||
verbose_name_plural = _('Dynamic position')
|
verbose_name_plural = _('Dynamic position')
|
||||||
default_related_name = 'dynamic_positions'
|
default_related_name = 'dynamic_positions'
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
if self.timeout and self.last_coordinates_update:
|
||||||
|
end_time = self.last_coordinates_update + timedelta(seconds=self.timeout)
|
||||||
|
if timezone.now() >= end_time:
|
||||||
|
self.cordinates = None
|
||||||
|
self.last_coordinates_update = end_time
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def user_has_positions(cls, user):
|
def user_has_positions(cls, user):
|
||||||
if not user.is_authenticated:
|
if not user.is_authenticated:
|
||||||
|
|
|
@ -54,7 +54,7 @@ class ReportUpdateForm(ModelForm):
|
||||||
class PositionForm(ModelForm):
|
class PositionForm(ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Position
|
model = Position
|
||||||
fields = ['name']
|
fields = ['name', 'timeout']
|
||||||
|
|
||||||
|
|
||||||
class PositionSetForm(Form):
|
class PositionSetForm(Form):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue